Compare commits

...

3 commits

2 changed files with 85 additions and 6 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

@ -21,7 +21,9 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
import argparse
import sys
import time
import logging
import re
import requests
@ -35,6 +37,8 @@ STATUS = {
}
DEFAULT_HOST = 'http://127.0.0.1:6052'
DEFAULT_RETRY_COUNT = 4
DEFAULT_RETRY_DELAY = 1
parser = argparse.ArgumentParser()
parser.add_argument(
@ -53,6 +57,42 @@ parser.add_argument(
default=DEFAULT_HOST
)
parser.add_argument(
'-r', '--retry',
action="store",
dest="retry_count",
help=(
'Number of retry to retreive device status '
f'(default: {DEFAULT_RETRY_COUNT})'),
type=int,
default=DEFAULT_RETRY_COUNT
)
parser.add_argument(
'-D', '--delay',
action="store",
dest="retry_delay",
help=(
'Delay in second between two retry to retreive device status '
f'(default: {DEFAULT_RETRY_DELAY}s)'),
type=int,
default=DEFAULT_RETRY_DELAY
)
def exclude_pattern(value):
""" Check and compile exclusion pattern parameter """
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(
@ -62,6 +102,17 @@ logging.basicConfig(
if options.host[-1] == '/':
options.host = options.host[-1]
def is_excluded(name):
""" Check if device is excluded """
for pattern in options.exclude:
if pattern.search(name):
logging.debug('Device %s is excluded', name)
return True
logging.debug('Device %s is not excluded', name)
return False
r = requests.get(f'{options.host}/devices')
devices_data = r.json()
logging.debug('Devices data: %s (%s)', devices_data, type(devices_data))
@ -70,9 +121,28 @@ if not devices_data:
print('UNKNOWN - Fail to retreive devices using ESPHome Dashboard API')
sys.exit(STATUS['UNKNOWN'])
r = requests.get(f'{options.host}/ping')
ping_data = r.json()
logging.debug('Ping data: %s (%s)', ping_data, type(ping_data))
COUNT = 0
while COUNT < options.retry_count:
r = requests.get(f'{options.host}/ping')
COUNT += 1
ping_data = r.json()
logging.debug('Ping data: %s (%s)', ping_data, type(ping_data))
if ping_data:
UNREACHABLE = False
for dev in ping_data:
if (
not ping_data[dev]
and not is_excluded(dev.replace('.yaml', ''))
):
UNREACHABLE = True
break
if not UNREACHABLE:
break
logging.debug('Wait %d seconds before retry...', options.retry_delay)
time.sleep(options.retry_delay)
if not ping_data:
print(
@ -88,6 +158,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(
@ -120,8 +192,9 @@ else:
print("\nDevices:\n" + "\n".join([
(
f'- {name} (version = {dev["deployed_version"]}, '
f'address = {dev["address"]})'
f'- {name} (version = '
f'{dev["deployed_version"] if dev["deployed_version"] else "unknown"}'
f', address = {dev["address"] if dev["address"] else "unknown"})'
)
for name, dev in devices.items()
]))