Ad Widget

Collapse

Trigger expression with sunrise and sunset values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gbmadmin
    Junior Member
    • Mar 2020
    • 18

    #1

    Trigger expression with sunrise and sunset values

    I am using the HTTP Agent to monitor a Fronius inverter. I created a trigger to receive an alert when the inverter is not answering:

    Code:
    nodata(/Fronius Solar/fronius.GetInverterRealtimeData_CommonInverterDataAPI,3m)=1 and time()>070000 and time()<200000
    The time interval is needed to avoid checking the inverter during the night, when it's off.

    Assuming I can get the sunrise and sunset time from a table, or from a websocket, how could I use that info to make the time interval more precise? Something like this:

    Code:
    nodata(/Fronius Solar/fronius.GetInverterRealtimeData_CommonInverterDataAPI,3m)=1 and time()>SUNRISE(today) and time()<SUNSET(today)
    Maybe somebody has already solved this, no need to reinvent the wheel. :-)

    Help is greatly appreciated!
  • gbmadmin
    Junior Member
    • Mar 2020
    • 18

    #2
    Originally posted by cyber
    You can use macros for specifying such things, it is a question of getting correct values to macros... Probably need to run external script which updates macro values over API... Something that somewhere after midnight looks up correct values for current day and updates macros....
    That's exaclty what I was looking for! Thanks for showing me the right direction.

    So I solved it this way.

    1. I created two new macros in the host:
    {$SUNRISE}
    {$SUNSET}


    2. I wrote a small C program to calculate the sunrise and sunset time based on latitude and longitude. The program can be called with two different names to return the desidered values for sunrise and sunset, i.e:
    sunrise lat long --> returns somthing like 064948
    sunset lat long --> returns somthing like 194354

    I also put default lat and long so that I can call sunrise or sunset without parameters for my house position.

    3. I created a simple Php script using the intellitrend zabbixapi-php lib (nice job folks!):


    Please note the script is lacking checks, it's just a quick and dirty solution.

    Code:
    <?PHP
      require_once "ZabbixApi.php";
      $zbx = new ZabbixApi();
      unset($output);
      unset($result_code);
      $cmd="/usr/local/bin/sunrise"; // Using default lat&long here!
      $sunrise = exec($cmd,$output,$result_code);
      if ($result_code != 0) {
        // Here we should check the error and do something!
        // For now we just exit
        exit;
      }
    
      unset($output);
      unset($result_code);
      $cmd="/usr/local/bin/sunset";// Using default lat&long here!
      $sunset = exec($cmd,$output,$result_code);
      if ($result_code != 0) {
        // Here we should check the error and do something!
        // For now we just exit
        exit;
      }
    
      try {
        $zbx->login('https://your.zabbix.domain', 'admin', 'password');
        $params = array(
          array('hostmacroid' => '1869',
            'value' => $sunrise),
          array('hostmacroid' => '1870',
            'value' => $sunset)
        );
        $result = $zbx->call('usermacro.update', $params);
        //var_dump($result);
    
      } catch (Exception $e) {
        print "==== Exception ===\n";
        print 'Errorcode: '.$e->getCode()."\n";
        print 'ErrorMessage: '.$e->getMessage()."\n";
        exit;
      }
    ?>
    4. The Php script runs by cron every night at 02:00, so that sunrise and sunset times are updated before they're used.

    5. The trigger checks sunrise and sunset time as previously, but now the two timings are correct for the day:
    Code:
    nodata(/Fronius Solar/fronius.GetInverterRealtimeData_CommonInverterData API,3m)=1 and time()>{$SUNRISE} and time()<{$SUNSET}

    Sometimes the trigger fires in the first minutes after sunrise or before sunset: it can be avoided by adding some "delay" in the values calculated by the C program.

    I set this yesterday and it's running smoothly, next step could be to add some checks for errors from the exec() function.

    Comment

    Working...