Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
| tips:zabbix [2021/07/26 11:19] – fmussati | tips:zabbix [2021/08/02 11:22] (current) – fmussati | ||
|---|---|---|---|
| Line 157: | Line 157: | ||
| " $url | " $url | ||
| </ | </ | ||
| - | < | + | < |
| #!/bin/bash | #!/bin/bash | ||
| # 1. set connection details | # 1. set connection details | ||
| url=http:// | url=http:// | ||
| - | user=admin | + | user=username |
| - | password=Galileo2018. | + | password=passwd |
| # 2. get authorization token | # 2. get authorization token | ||
| Line 222: | Line 222: | ||
| " $url | " $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) | ||
| + | |||
| </ | </ | ||