Add retry options to retreive device status to avoid false positive unreachable device alert

This commit is contained in:
Benjamin Renard 2022-10-26 15:08:45 +02:00 committed by root
parent 9f24acca6b
commit 9380c63913

View file

@ -21,6 +21,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
import argparse
import sys
import time
import logging
import requests
@ -35,6 +36,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 +56,24 @@ parser.add_argument(
default=DEFAULT_HOST
)
parser.add_argument(
'-r', '--retry',
action="store",
dest="retry_count",
help=f'Number of retry to retreive device status (default: {DEFAULT_RETRY_COUNT})',
type=int,
default=DEFAULT_RETRY_COUNT
)
parser.add_argument(
'-D', '--delay',
action="store",
dest="retry_delay",
help=f'Delay in second between two retry to retreive device status (default: {DEFAULT_RETRY_DELAY}s)',
type=int,
default=DEFAULT_RETRY_DELAY
)
options = parser.parse_args()
logging.basicConfig(
@ -70,9 +91,25 @@ 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]:
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(