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/06/28 12:52] – [task] scipiotips:zabbix [2021/08/02 11:22] (current) fmussati
Line 3: Line 3:
 ===== task ===== ===== task =====
  
-  * dashboard con conteggi aggregati (e altri grafici interessanti) +  * <del>dashboard con conteggi aggregati (e altri grafici interessanti)</del> 
-  * trigger di allarme: +  * <del>trigger di allarme: 
-    * luce accesa da troppo tempo +    * luce accesa da troppo tempo</del> 
-  * export dati csv con API +  * <del>export dati csv con API</del> 
-  * gestire utente non privilegiato su zabbix +  * <del>gestire utente non privilegiato su zabbix</del> 
-  * VPN robusta che riparte quando il cloud non è raggiungibile+  * <del>VPN robusta che riparte quando il cloud non è raggiungibile</del>
   * gestire il mancato boot della componente zabbix di hass   * gestire il mancato boot della componente zabbix di hass
     * /etc/openvpn/iotaiuto.conf ?     * /etc/openvpn/iotaiuto.conf ?
Line 16: Line 16:
   * esphome:   * esphome:
     * costruire in power meter con allarme su alexa     * costruire in power meter con allarme su alexa
-    * cotruire un citofono con ESP32 CAM+    * costruire un citofono con ESP32 CAM
 ===== API ===== ===== API =====
  
Line 156: Line 156:
     }     }
     " $url     " $url
 +</code>
 +<code bash>
 +#!/bin/bash
 +
 +# 1. set connection details
 +url=http://monitor.iotaiuto.it/api_jsonrpc.php
 +user=username
 +password=passwd
 + 
 +# 2. get authorization token
 +auth=$(curl -s -X POST \
 +-H 'Content-Type: application/json-rpc' \
 +-d " \
 +{
 + \"jsonrpc\": \"2.0\",
 + \"method\": \"user.login\",
 + \"params\": {
 +  \"user\": \"$user\",
 +  \"password\": \"$password\"
 + },
 + \"id\": 1,
 + \"auth\": null
 +}
 +" $url | \
 +jq -r '.result'
 +)
 + 
 +# 3. show triggers in problem state
 +curl -s -X POST \
 +-H 'Content-Type: application/json-rpc' \
 +-d " \
 +{
 + \"jsonrpc\": \"2.0\",
 +    \"method\": \"trigger.get\",
 +    \"params\": {
 +        \"output\": \"extend\",
 +        \"selectHosts\": \"extend\",
 +        \"filter\": {
 +            \"value\": 1
 +        },
 +        \"sortfield\": \"priority\",
 +        \"sortorder\": \"DESC\"
 +    },
 +    \"auth\": \"$auth\",
 +    \"id\": 1
 +}
 +" $url | \
 +jq -r '.result' > ../flussi/dati.json
 +jq -r 'del(.[].hosts) | (map(keys) | add | unique) as $cols | map(. as $row | $cols | map($row[.])) as $rows | $cols, $rows[] | @csv' ../flussi/dati.json > ../flussi/dati_trigger.csv
 +jq -r '(map(.hosts[] + {"triggerid": .triggerid} | keys) | add | unique) as $cols | map(. as $row | $cols | map($row[.])) as $rows | $cols, $rows[] | @csv' ../flussi/dati.json > ../flussi/dati_host.csv
 +rm ../flussi/dati.json
 +cat ../flussi/dati_trigger.csv & ../flussi/dati_host.csv
 +
 +# 4. logout user
 +curl -s -X POST \
 +-H 'Content-Type: application/json-rpc' \
 +-d " \
 +{
 +    \"jsonrpc\": \"2.0\",
 +    \"method\": \"user.logout\",
 +    \"params\": [],
 +    \"id\": 1,
 +    \"auth\": \"$auth\"
 +}
 +" $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.1624877575.txt.gz
  • Last modified: 2021/06/28 12:52
  • by scipio