Ad Widget

Collapse

Zabbix API to get latest 20 issues

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mvrk
    Member
    • Oct 2008
    • 71

    #1

    Zabbix API to get latest 20 issues

    Hi,

    Is it possible to retrieve the latest 20 issues just like they appear in the dashboard with Zabbix API?
  • Aly
    ZABBIX developer
    • May 2007
    • 1126

    #2
    Use event.get();
    Zabbix | ex GUI developer

    Comment

    • mvrk
      Member
      • Oct 2008
      • 71

      #3
      with event.get(), i retrieve records like this :

      [0] => Array
      (
      [eventid] => 3171523
      [source] => 0
      [object] => 0
      [objectid] => 18916
      [clock] => 1264501791
      [value] => 0
      [acknowledged] => 0
      )

      Whow do i transform this to the kind of info that shows in the dashboard latest 20 issues?

      What does the objectid correspond to? triggerid?

      I need something like:
      HOSTNAME | ISSUE
      server1 | CPU ocupation high (95%)

      Comment

      • nelsonab
        Senior Member
        Zabbix Certified SpecialistZabbix Certified Professional
        • Sep 2006
        • 1233

        #4
        Originally posted by mvrk
        I need something like:
        HOSTNAME | ISSUE
        server1 | CPU ocupation high (95%)
        To do that you'll have to dig a little deeper with the values returned. That's pretty much the way it's done in the Zabbix GUI code. You get an id, then look up the id, which gives you the name, and component parts and so forth. Trying to unravel trigger expressions in the DB... that can be painful... :-D
        RHCE, author of zbxapi
        Ansible, the missing piece (Zabconf 2017): https://www.youtube.com/watch?v=R5T9NidjjDE
        Zabbix and SNMP on Linux (Zabconf 2015): https://www.youtube.com/watch?v=98PEHpLFVHM

        Comment

        • Aly
          ZABBIX developer
          • May 2007
          • 1126

          #5
          Originally posted by mvrk
          with event.get(), i retrieve records like this :

          [0] => Array
          (
          [eventid] => 3171523
          [source] => 0
          [object] => 0
          [objectid] => 18916
          [clock] => 1264501791
          [value] => 0
          [acknowledged] => 0
          )

          Whow do i transform this to the kind of info that shows in the dashboard latest 20 issues?

          What does the objectid correspond to? triggerid?

          I need something like:
          HOSTNAME | ISSUE
          server1 | CPU ocupation high (95%)
          1. objectid = triggerid if object = 0
          2. set sortfield with limit.. because there could be > 100,000 events per trigger, I bet U don't need them all.
          3. try adding params like:
          select_hosts =>1, select_triggers=>1,extendoutput=>1
          Zabbix | ex GUI developer

          Comment

          • jmusbach
            Member
            • Sep 2013
            • 37

            #6
            Hello, I'm trying to do this as well but am finding it difficult and am wondering if anyone might be able to offer some tips. I currently have:

            Code:
            #!/usr/bin/perl
            
            use ZabbixAPI;
            use DateTime
            
            my $counter=1;
            my $zab=ZabbixAPI->new("http://zabbox.asfasf.com/zabbix/");
            $zab->login("apiguy2","sfgdgdgsdgsr");
            
            my $events=$zab->event_get(
                    {
                            output => 'extend',
                            selectHosts => 'extend',
                            selectTriggers => 'extend',
                            value => 1,
                            limit => 20
                    }
            );
            
            for my $event (@$events) {
                    my $dt=DateTime->from_epoch(epoch => $event->{clock});
                    my $timestamp=$dt->day." ".$dt->month." ".$dt->year." ".$dt->hour.":".$dt->minute.":".$dt->second;
            
                    print "Host: ".${$event->{hosts}}[0]->{name}." Issue: ".${$event->{triggers}}[0]->{description}." Last change: ".$timestamp."\n"; 
            }
            This works fine except the problem is that despite me filtering for only events that are marked as a problem (value => 1) I'm getting old events that're no longer problems. There's only 4 issues currently listed in the zabbix web ui's Last 20 Issues box but this API query's returning back events from several months ago. What am I missing? Thanks!

            Comment

            • jmusbach
              Member
              • Sep 2013
              • 37

              #7
              Bump, anyone?

              Comment

              • Speedfight
                Member
                • May 2007
                • 67

                #8
                add:

                PHP Code:
                only_true => true

                Comment

                • jmusbach
                  Member
                  • Sep 2013
                  • 37

                  #9
                  Thanks but are you sure that's supported for event.get? When I do that I get the error below.

                  Code:
                  malformed JSON string, neither array, object, number, string or atom, at character offset 0 (before "<html><head><title>O...") at (eval 4) line 163.

                  Comment

                  • Speedfight
                    Member
                    • May 2007
                    • 67

                    #10
                    Sorry my bad.

                    I did this for "trigger.get"

                    Comment

                    • kargh
                      Junior Member
                      • Feb 2014
                      • 21

                      #11
                      Basically what you need to do is grab the last 20 events using event.get. Then you need to grab the event tree for each of the events so you can figure out duration (Current Event Time - Previous Event Time = Duration). You'll also need to grab the trigger for each of the events if you want to know what triggered the event.

                      I have code that does something similar. Let me see if I can modify it to work for you.


                      -- Jess

                      Comment

                      • kargh
                        Junior Member
                        • Feb 2014
                        • 21

                        #12
                        I borrowed this code directly from the Zabbix source and modified it slightly so that it would work for what you want. Also, I use the PhpZabbixApi library. If you don't, then you'll have to change the API call to whatever method you prefer.

                        PHP Code:
                        $sortField = array('lastchange');
                        $sortOrder = array('DESC');

                        $eventsTimeFrame $api->triggerGet(array(
                             
                        'groupids'      => null,
                             
                        'hostids'       => null,
                             
                        'monitored'     => true,
                             
                        'maintenance'   => false,
                             
                        'filter'        => array('value' => 1),
                             
                        'skipDependent' => true,
                             
                        'output'        => array('triggerid''state''error''url''expression''description''priority''lastchange'),
                             
                        'selectHosts'   => array('hostid''name'),
                             
                        'selectLastEvent' => array('eventid''acknowledged''objectid''clock''ns'),
                             
                        'sortfield'     => $sortField,
                             
                        'sortorder'     => $sortOrder,
                             
                        'limit'         => '20'
                        ));


                        $eventsTimeFrame json_decode(json_encode($eventsTimeFrame), true); 

                        -- Jess

                        Comment

                        • Speedfight
                          Member
                          • May 2007
                          • 67

                          #13
                          @kargh

                          Thanks,

                          I builded somelike your code.

                          PHP Code:
                              $data = array(
                                  
                          'jsonrpc' => "2.0",
                                  
                          'method' => "trigger.get",
                                  
                          'params' => array(
                                      
                          'output' => array('triggerid''state''error''url''expression''description''priority''lastchange'),
                                      
                          'selectHosts' => array('hostid''name'),
                                      
                          'selectLastEvent' => array('eventid''acknowledged''objectid''clock''ns'),
                                      
                          'sortfield'     => "lastchange",
                                      
                          'monitored'=> "true",
                                      
                          'only_true' => "true",
                                      
                          'maintenance' => "false",
                                      
                          'limit' => "20",
                                  ),
                                  
                          'id' => "1",
                                  
                          'auth' => $authcode
                              
                          ); 

                          Comment

                          • jmusbach
                            Member
                            • Sep 2013
                            • 37

                            #14
                            Originally posted by Speedfight
                            Sorry my bad.

                            I did this for "trigger.get"
                            Oh hm ok, guess that's the method that's supposed to be used then. I'll look into changing my code to call that instead. Thanks.

                            EDIT: Thanks for the examples guys I'll check those out.
                            Last edited by jmusbach; 07-04-2014, 19:46.

                            Comment

                            • kargh
                              Junior Member
                              • Feb 2014
                              • 21

                              #15
                              You could probably do it another way but why reinvent the wheel when the work was already done by someone else?

                              Comment

                              Working...