Ad Widget

Collapse

Zabbix API

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sersad
    Senior Member
    • May 2009
    • 518

    #1

    Zabbix API

    How to get values item from history via API?
    Is there such a function? The documentation did not find anything similar.
  • nelsonab
    Senior Member
    Zabbix Certified SpecialistZabbix Certified Professional
    • Sep 2006
    • 1233

    #2
    Not yet. You can get the very last value queried however if you do "item.get"
    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

    • sersad
      Senior Member
      • May 2009
      • 518

      #3
      nelsonab, i get last and previous value.
      Do you plan such function in the API and when?
      I need to get from the history of the last non-zero.
      I see only one solution - to do a direct sql query to the database.

      Comment

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

        #4
        That's up to the Zabbix devs. I've talked to them before about the need for this and they agree, however it's not known at this time when it will happen.

        Yes you are right... today there are only two solutions. One query the DB directly. Second setup a script which does the following with wget
        1) input user information in the login form
        2) save cookie
        3) using cookie request latest data for the item in question during the period in question and use the fullscreen=1 along with output format type of text.
        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

        • sersad
          Senior Member
          • May 2009
          • 518

          #5
          Originally posted by nelsonab
          One query the DB directly.
          You able to help write such a query?
          I never understood the structure of database.

          Comment

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

            #6
            Ya, not a problem. It's also somewhat straightforward to script via wget, which I've also done before.
            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

            • yaap
              Junior Member
              • Aug 2009
              • 20

              #7
              I'm using the following custom function in api/class/class.citem.php to get latest value from history_text table.

              Lastvalue parameter in item table is limited to varchar (255).
              We needed longer values.

              Input parameters:
              key : item key name (eg. vfs.custom)
              hostid : host ID


              PHP Code:
              public static function lastvalue($options=array())
                      {
                              
              $sql 'SELECT i.*, ht.value as value2, ht.clock as clock2 ';
                              
              $sql.= ' FROM history_text ht INNER JOIN items i ON ht.itemid=i.itemid ';
                              
              $sql.= ' WHERE UPPER(i.key_) = '.zbx_dbstr(strtoupper($options['key']));
                              
              $sql.= ' AND i.hostid='.$options['hostid'];
                              
              $sql.= ' ORDER BY ht.clock DESC ';
                              
              $sql.= ' LIMIT 1';

                              
              $res DBselect($sql);
                              
              $result DBfetch($res);
                              return 
              $result;
                      } 

              Comment

              • Sypher
                Junior Member
                • Jan 2007
                • 24

                #8
                I've added this function to the class.citem.php file, but when I call it with
                PHP Code:
                    $data ZabbixAPI::fetch_array('item','lastvalue',array(
                                            
                'hostid' => 12345,
                                            
                'key'    => 'system.uptime'
                                            
                ));
                print_r($data); 
                It returns nothing, just:
                Array
                (
                [0] =>
                )
                Am I calling it wrong? I'm using the latest stable version of Zabbix + frontend.

                Comment

                • yaap
                  Junior Member
                  • Aug 2009
                  • 20

                  #9
                  Verify if SQL executed directly on database returns any data.

                  Comment

                  • Sypher
                    Junior Member
                    • Jan 2007
                    • 24

                    #10
                    Apparently its giving back an empty resultset.

                    Comment

                    • limo
                      Senior Member
                      • Dec 2004
                      • 192

                      #11
                      Improved version of lastvalue

                      Here is improved version which does not look into history but items for latest value. Works for all history data types. Returns even prevvalue, error and status.

                      Code:
                      	public static function lastvalue($options=array())
                      	        {
                      			$sql = 'SELECT itemid, value_type AS type, lastclock AS clock, FROM_UNIXTIME(lastclock) AS time, lastvalue AS value, prevvalue AS prevvalue, status, error';
                      			$sql.= ' FROM items';
                      			$sql.= ' WHERE UPPER(key_) = '.zbx_dbstr(strtoupper($options['key']));
                      			$sql.= ' AND hostid='.$options['hostid'];
                      			$itq = DBselect($sql);
                      			$item = DBfetch($itq);
                      			$itemid=$item["itemid"];
                      			switch ($item["type"]) {
                      				case 0:
                      					$table="history";
                      				break;
                      				case 1:
                      					$table="history_str";
                      				case 2:
                      					$table="history_log";
                      				break;
                      				case 3:
                      					$table="history_uint";
                      				break;
                      				case 4:
                      					$table="history_text";
                      				break;
                      			}
                      			$item["table"] = $table;
                      			return $item;
                      		}

                      Comment

                      • Aly
                        ZABBIX developer
                        • May 2007
                        • 1126

                        #12
                        In 1.8.3 there will be possible to select history for items through API history.get
                        Zabbix | ex GUI developer

                        Comment

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

                          #13
                          Originally posted by Aly
                          In 1.8.3 there will be possible to select history for items through API history.get
                          COOL!!! I've been busy lately but I hope to implement this asap. Any projection on when we'll see it?
                          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

                            #14
                            It's in the pre1.8.3 and trunk branches
                            Zabbix | ex GUI developer

                            Comment

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

                              #15
                              Originally posted by Aly
                              It's in the pre1.8.3 and trunk branches
                              Are we also going to see an updates file or something similar for what's changed between API revisions?
                              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

                              Working...