Add -x/--exclude parameter
This commit is contained in:
parent
9380c63913
commit
a8fff06d00
2 changed files with 32 additions and 4 deletions
|
@ -19,12 +19,18 @@ service nagios-nrpe-server reload
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
```
|
```
|
||||||
usage: check_esphome_devices [-h] [-d] [-H HOST]
|
usage: check_esphome_devices [-h] [-d] [-H HOST] [-r RETRY_COUNT] [-D RETRY_DELAY] [-x EXCLUDE]
|
||||||
|
|
||||||
optional arguments:
|
optional arguments:
|
||||||
-h, --help show this help message and exit
|
-h, --help show this help message and exit
|
||||||
-d, --debug
|
-d, --debug
|
||||||
-H HOST, --host HOST ESPHome dashboard URL (default: http://127.0.0.1:6052)
|
-H HOST, --host HOST ESPHome dashboard URL (default: http://127.0.0.1:6052)
|
||||||
|
-r RETRY_COUNT, --retry RETRY_COUNT
|
||||||
|
Number of retry to retreive device status (default: 4)
|
||||||
|
-D RETRY_DELAY, --delay RETRY_DELAY
|
||||||
|
Delay in second between two retry to retreive device status (default: 1s)
|
||||||
|
-x EXCLUDE, --exclude EXCLUDE
|
||||||
|
Regex exclude pattern(
|
||||||
```
|
```
|
||||||
|
|
||||||
## Copyright
|
## Copyright
|
||||||
|
|
|
@ -23,6 +23,7 @@ import argparse
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
import logging
|
import logging
|
||||||
|
import re
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
|
@ -74,6 +75,17 @@ parser.add_argument(
|
||||||
default=DEFAULT_RETRY_DELAY
|
default=DEFAULT_RETRY_DELAY
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def exclude_pattern(value):
|
||||||
|
return re.compile(value)
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
'-x', '--exclude',
|
||||||
|
action="append",
|
||||||
|
dest="exclude",
|
||||||
|
help='Regex exclude pattern(s)',
|
||||||
|
type=exclude_pattern,
|
||||||
|
default=[]
|
||||||
|
)
|
||||||
options = parser.parse_args()
|
options = parser.parse_args()
|
||||||
|
|
||||||
logging.basicConfig(
|
logging.basicConfig(
|
||||||
|
@ -83,6 +95,14 @@ logging.basicConfig(
|
||||||
if options.host[-1] == '/':
|
if options.host[-1] == '/':
|
||||||
options.host = options.host[-1]
|
options.host = options.host[-1]
|
||||||
|
|
||||||
|
def is_excluded(dev):
|
||||||
|
for pattern in options.exclude:
|
||||||
|
if pattern.search(dev):
|
||||||
|
logging.debug('Device %s is excluded', dev)
|
||||||
|
return True
|
||||||
|
logging.debug('Device %s is not excluded', dev)
|
||||||
|
return False
|
||||||
|
|
||||||
r = requests.get(f'{options.host}/devices')
|
r = requests.get(f'{options.host}/devices')
|
||||||
devices_data = r.json()
|
devices_data = r.json()
|
||||||
logging.debug('Devices data: %s (%s)', devices_data, type(devices_data))
|
logging.debug('Devices data: %s (%s)', devices_data, type(devices_data))
|
||||||
|
@ -101,7 +121,7 @@ while count < options.retry_count:
|
||||||
if ping_data:
|
if ping_data:
|
||||||
unreachable = False
|
unreachable = False
|
||||||
for dev in ping_data:
|
for dev in ping_data:
|
||||||
if not ping_data[dev]:
|
if not ping_data[dev] and not is_excluded(dev.replace('.yaml', '')):
|
||||||
unreachable = True
|
unreachable = True
|
||||||
break
|
break
|
||||||
|
|
||||||
|
@ -125,6 +145,8 @@ devices = {}
|
||||||
for dev in devices_data['configured']:
|
for dev in devices_data['configured']:
|
||||||
devices[dev['name']] = dev
|
devices[dev['name']] = dev
|
||||||
logging.debug('Device %s: %s', dev['name'], dev)
|
logging.debug('Device %s: %s', dev['name'], dev)
|
||||||
|
if is_excluded(dev['name']):
|
||||||
|
continue
|
||||||
if dev['deployed_version'] != dev['current_version']:
|
if dev['deployed_version'] != dev['current_version']:
|
||||||
UPDATE_AVAILABLE += 1
|
UPDATE_AVAILABLE += 1
|
||||||
errors.append(
|
errors.append(
|
||||||
|
@ -157,8 +179,8 @@ else:
|
||||||
|
|
||||||
print("\nDevices:\n" + "\n".join([
|
print("\nDevices:\n" + "\n".join([
|
||||||
(
|
(
|
||||||
f'- {name} (version = {dev["deployed_version"]}, '
|
f'- {name} (version = {dev["deployed_version"] if dev["deployed_version"] else "unknown"}, '
|
||||||
f'address = {dev["address"]})'
|
f'address = {dev["address"] if dev["address"] else "unknown"})'
|
||||||
)
|
)
|
||||||
for name, dev in devices.items()
|
for name, dev in devices.items()
|
||||||
]))
|
]))
|
||||||
|
|
Loading…
Reference in a new issue