tips:zabbix

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
tips:zabbix [2021/07/26 11:19] fmussatitips:zabbix [2021/08/02 11:22] (current) fmussati
Line 157: Line 157:
     " $url     " $url
 </code> </code>
-<code>+<code bash>
 #!/bin/bash #!/bin/bash
  
 # 1. set connection details # 1. set connection details
 url=http://monitor.iotaiuto.it/api_jsonrpc.php url=http://monitor.iotaiuto.it/api_jsonrpc.php
-user=admin +user=username 
-password=Galileo2018.+password=passwd
    
 # 2. get authorization token # 2. get authorization token
Line 222: Line 222:
 " $url " $url
        
 +</code>
 +===== Patch infinite componente zabbix =====
 +<code python>
 +class ZabbixThread(threading.Thread):
 +    """A threaded event handler class."""
 +    # Rename to TRIES and set to 0
 +    MAX_TRIES = 3
 +
 +    def __init__(self, hass, zabbix_sender, event_to_metrics):
 +        """Initialize the listener."""
 +        threading.Thread.__init__(self, name="Zabbix")
 +        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):
 +        """Set up the thread and start it."""
 +        hass.bus.listen(EVENT_STATE_CHANGED, self._event_listener)
 +        hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, self._shutdown)
 +        self.start()
 +        _LOGGER.debug("Started publishing state changes to Zabbix")
 +
 +    def _shutdown(self, event):
 +        """Shut down the thread."""
 +        self.queue.put(None)
 +        self.join()
 +
 +    @callback
 +    def _event_listener(self, event):
 +        """Listen for new messages on the bus and queue them for Zabbix."""
 +        item = (time.monotonic(), event)
 +        self.queue.put(item)
 +
 +    def get_metrics(self):
 +        """Return a batch of events formatted for writing."""
 +        # 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, self.string_keys
 +                        )
 +                        if event_metrics:
 +                            metrics += event_metrics
 +                    else:
 +                        dropped += 1
 +
 +        if dropped:
 +            _LOGGER.warning("Catching up, dropped %d old events", dropped)
 +
 +        return count, metrics
 +
 +    def write_to_zabbix(self, metrics):
 +        """Write preprocessed events to zabbix, with retry."""
 +        # while True:
 +        for retry in range(self.MAX_TRIES + 1):
 +            try:
 +                self.zabbix_sender.send(metrics)
 +                if self.write_errors:
 +                    _LOGGER.error("Resumed, lost %d events", self.write_errors)
 +                    self.write_errors = 0
 +
 +                _LOGGER.debug("Wrote %d metrics", len(metrics))
 +                # 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("Write error: %s", err)
 +                    self.write_errors += len(metrics)
 +
 </code> </code>
  • tips/zabbix.1627291145.txt.gz
  • Last modified: 2021/07/26 11:19
  • by fmussati