Add -x/--exclude parameter

This commit is contained in:
Benjamin Renard 2022-12-03 15:13:28 +01:00 committed by root
parent 9380c63913
commit a8fff06d00
2 changed files with 32 additions and 4 deletions

View file

@ -19,12 +19,18 @@ service nagios-nrpe-server reload
## 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:
-h, --help show this help message and exit
-d, --debug
-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

View file

@ -23,6 +23,7 @@ import argparse
import sys
import time
import logging
import re
import requests
@ -74,6 +75,17 @@ parser.add_argument(
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()
logging.basicConfig(
@ -83,6 +95,14 @@ logging.basicConfig(
if 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')
devices_data = r.json()
logging.debug('Devices data: %s (%s)', devices_data, type(devices_data))
@ -101,7 +121,7 @@ while count < options.retry_count:
if ping_data:
unreachable = False
for dev in ping_data:
if not ping_data[dev]:
if not ping_data[dev] and not is_excluded(dev.replace('.yaml', '')):
unreachable = True
break
@ -125,6 +145,8 @@ devices = {}
for dev in devices_data['configured']:
devices[dev['name']] = dev
logging.debug('Device %s: %s', dev['name'], dev)
if is_excluded(dev['name']):
continue
if dev['deployed_version'] != dev['current_version']:
UPDATE_AVAILABLE += 1
errors.append(
@ -157,8 +179,8 @@ else:
print("\nDevices:\n" + "\n".join([
(
f'- {name} (version = {dev["deployed_version"]}, '
f'address = {dev["address"]})'
f'- {name} (version = {dev["deployed_version"] if dev["deployed_version"] else "unknown"}, '
f'address = {dev["address"] if dev["address"] else "unknown"})'
)
for name, dev in devices.items()
]))