Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
tips:zabbix [2021/06/28 11:00] – created scipio | tips:zabbix [2021/08/02 11:22] (current) – fmussati | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Zabbix ====== | ====== Zabbix ====== | ||
+ | ===== task ===== | ||
+ | |||
+ | * < | ||
+ | * < | ||
+ | * luce accesa da troppo tempo</ | ||
+ | * < | ||
+ | * < | ||
+ | * < | ||
+ | * gestire il mancato boot della componente zabbix di hass | ||
+ | * / | ||
+ | * come far ripartire la connessione zabbix all' | ||
+ | |||
+ | |||
+ | * esphome: | ||
+ | * costruire in power meter con allarme su alexa | ||
+ | * costruire un citofono con ESP32 CAM | ||
+ | ===== API ===== | ||
Line 139: | Line 156: | ||
} | } | ||
" $url | " $url | ||
+ | </ | ||
+ | <code bash> | ||
+ | #!/bin/bash | ||
+ | |||
+ | # 1. set connection details | ||
+ | url=http:// | ||
+ | user=username | ||
+ | password=passwd | ||
+ | |||
+ | # 2. get authorization token | ||
+ | auth=$(curl -s -X POST \ | ||
+ | -H ' | ||
+ | -d " \ | ||
+ | { | ||
+ | | ||
+ | | ||
+ | | ||
+ | \" | ||
+ | \" | ||
+ | }, | ||
+ | | ||
+ | | ||
+ | } | ||
+ | " $url | \ | ||
+ | jq -r ' | ||
+ | ) | ||
+ | |||
+ | # 3. show triggers in problem state | ||
+ | curl -s -X POST \ | ||
+ | -H ' | ||
+ | -d " \ | ||
+ | { | ||
+ | | ||
+ | \" | ||
+ | \" | ||
+ | \" | ||
+ | \" | ||
+ | \" | ||
+ | \" | ||
+ | }, | ||
+ | \" | ||
+ | \" | ||
+ | }, | ||
+ | \" | ||
+ | \" | ||
+ | } | ||
+ | " $url | \ | ||
+ | jq -r ' | ||
+ | jq -r ' | ||
+ | jq -r ' | ||
+ | rm ../ | ||
+ | cat ../ | ||
+ | |||
+ | # 4. logout user | ||
+ | curl -s -X POST \ | ||
+ | -H ' | ||
+ | -d " \ | ||
+ | { | ||
+ | \" | ||
+ | \" | ||
+ | \" | ||
+ | \" | ||
+ | \" | ||
+ | } | ||
+ | " $url | ||
+ | |||
+ | </ | ||
+ | ===== Patch infinite componente zabbix ===== | ||
+ | <code python> | ||
+ | class ZabbixThread(threading.Thread): | ||
+ | """ | ||
+ | # Rename to TRIES and set to 0 | ||
+ | MAX_TRIES = 3 | ||
+ | |||
+ | def __init__(self, | ||
+ | """ | ||
+ | threading.Thread.__init__(self, | ||
+ | self.queue = queue.Queue() | ||
+ | self.zabbix_sender = zabbix_sender | ||
+ | self.event_to_metrics = event_to_metrics | ||
+ | self.write_errors = 0 | ||
+ | self.shutdown = False | ||
+ | self.float_keys = set() | ||
+ | self.string_keys = set() | ||
+ | |||
+ | def setup(self, hass): | ||
+ | """ | ||
+ | hass.bus.listen(EVENT_STATE_CHANGED, | ||
+ | hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, | ||
+ | self.start() | ||
+ | _LOGGER.debug(" | ||
+ | |||
+ | def _shutdown(self, | ||
+ | """ | ||
+ | self.queue.put(None) | ||
+ | self.join() | ||
+ | |||
+ | @callback | ||
+ | def _event_listener(self, | ||
+ | """ | ||
+ | item = (time.monotonic(), | ||
+ | self.queue.put(item) | ||
+ | |||
+ | def get_metrics(self): | ||
+ | """ | ||
+ | # Replace MAX_TRIES to TRIES | ||
+ | queue_seconds = QUEUE_BACKLOG_SECONDS + self.MAX_TRIES * RETRY_DELAY | ||
+ | |||
+ | count = 0 | ||
+ | metrics = [] | ||
+ | |||
+ | dropped = 0 | ||
+ | |||
+ | with suppress(queue.Empty): | ||
+ | while len(metrics) < BATCH_BUFFER_SIZE and not self.shutdown: | ||
+ | timeout = None if count == 0 else BATCH_TIMEOUT | ||
+ | item = self.queue.get(timeout=timeout) | ||
+ | count += 1 | ||
+ | |||
+ | if item is None: | ||
+ | self.shutdown = True | ||
+ | else: | ||
+ | timestamp, event = item | ||
+ | age = time.monotonic() - timestamp | ||
+ | |||
+ | if age < queue_seconds: | ||
+ | event_metrics = self.event_to_metrics( | ||
+ | event, self.float_keys, | ||
+ | ) | ||
+ | if event_metrics: | ||
+ | metrics += event_metrics | ||
+ | else: | ||
+ | dropped += 1 | ||
+ | |||
+ | if dropped: | ||
+ | _LOGGER.warning(" | ||
+ | |||
+ | return count, metrics | ||
+ | |||
+ | def write_to_zabbix(self, | ||
+ | """ | ||
+ | # while True: | ||
+ | for retry in range(self.MAX_TRIES + 1): | ||
+ | try: | ||
+ | self.zabbix_sender.send(metrics) | ||
+ | if self.write_errors: | ||
+ | _LOGGER.error(" | ||
+ | self.write_errors = 0 | ||
+ | |||
+ | _LOGGER.debug(" | ||
+ | # Put [self.TRIES = 0] | ||
+ | break | ||
+ | |||
+ | except OSError as err: | ||
+ | # [time.sleep(RETRY_DELAY)] out of [if retry < self.MAX_TRIES: | ||
+ | if retry < self.MAX_TRIES: | ||
+ | # Put [self.TRIES += 1] | ||
+ | time.sleep(RETRY_DELAY) | ||
+ | else: | ||
+ | if not self.write_errors: | ||
+ | _LOGGER.error(" | ||
+ | self.write_errors += len(metrics) | ||
+ | |||
</ | </ |