Ad Widget

Collapse

Zabbix sender PHP version

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • limo
    Senior Member
    • Dec 2004
    • 192

    #1

    Zabbix sender PHP version

    For those who do not want external command to send zabbix event from php scripts:
    Code:
    function zabbix_send($server,$port,$hostname,$key,$msg) {
                    $host64=base64_encode($hostname);
                    $key64=base64_encode($key);
                    $msg64=base64_encode($msg); 
    $req="<req><host>$host64</host><key>$key64</key><data>$msg64</data></req>\n";
                    $s=fsockopen($server,$port,$errnum,$errstr,15);
                    if (is_resource($s)) {
                            fputs($s,$req);
                            while (!feof($s)) {
                                    $ret.= fgets($s, 1024);
                            }
                            return($ret);
                    } else {
                            return(false);
                    }
    }
  • dreamlomax
    Junior Member
    • Aug 2011
    • 2

    #2
    PHP Native zabbix_sender compatible with 1.8 using Json

    Can anybody try to do compatible with 1.8 version? I tried but is impossible for me. Thanks for advance.


    I found for 1.6.x this:



    But in currently is needed add some headers and calculate sizes. Can any body help and give an example working with current 1.8 version?

    I supose that with this examples should be possible, but for me is so hard try to port code related with pack from python to php:


    Source code: Lib/struct.py This module converts between Python values and C structs represented as Python bytes objects. Compact format strings describe the intended conversions to/from Python valu...

    Source code: Lib/struct.py This module converts between Python values and C structs represented as Python bytes objects. Compact format strings describe the intended conversions to/from Python valu...



    Possible from here somebody that understand better java can try to help to port:

    http://www.zabbix.com/wiki/doc/tech/...senderprotocol

    Comment

    • eht16
      Junior Member
      • Sep 2011
      • 2

      #3
      I'm not sure whether you want an implementation in PHP or Python, for the latter, see http://www.zabbix.com/forum/showthread.php?p=90132

      Comment

      • dreamlomax
        Junior Member
        • Aug 2011
        • 2

        #4
        I'm interested in php version

        Thanks for info but i'm interested in a php port as a i commented.

        ;-D

        Comment

        • Pazaan
          Junior Member
          • Aug 2011
          • 1

          #5
          Pure PHP zabbix_sender compatible with 1.8 using Json

          A zabbix 1.8 version of the zabbix_sender in PHP, with complete error handling.

          Code:
          function zabbix_sender( $server,$port,$hostname,$data ) {
             $body = (object)array(
                "request" => "sender data",
                "data" => array(),
             );
          
             foreach( $data as $key => $value ) {
                array_push( $body->data, (object)array(
                      "host" => $hostname,
                      "key" => $key,
                      "value" => $value,
                   )
                );
             }
          
             $json_body = json_encode( $body );
          
             $size = strlen( $json_body );
             $request = pack( "a4CV2a*", "ZBXD", 1, $size, ( $size >> 32 ), $json_body );
          
             // Suppress warning, since we check below for success
             @$s=fsockopen($server,$port,$errnum,$errstr,15);
             if( is_resource( $s ) ) {
                fputs($s,$request);
                // 13 byte header
                for( $bytesRead = 0; $bytesRead < 13 && !feof( $s ); $bytesRead++ ) {
                   $header .= fread( $s, 1 );
                }
          
                list( $ZBX, , $sizeL, $sizeH ) =
                   array_values( unpack( 'a4zbx/Csep/Vh/Vl', $header ) );
                $msgSize = ( $sizeL + ( $sizeH >> 32 ) );
                while( !feof( $s ) ) {
                   $ret .= fread( $s, $msgSize );
                }
          
                if( $retStatus = json_decode($ret,true) ) {
                   $status = preg_split( "/([A-Z][a-z ]*)\s+(\S+)\s*/",
                      $retStatus["info"], -1,
                      PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY );
                   for( $i = 0; $i < count( $status ); $i += 2 ) {
                      $key = $status[$i];
                      $val = $status[$i+1];
                      $retStatus["status"][$key] = $val;
                   }
                }
                else {
                   $retStatus = array(
                      "response" => "failed",
                      "info" => "Invalid response",
                   );
                }
                return( $retStatus );
             } else {
                $retStatus = array(
                   "response" => "failed",
                   "info" => "$errstr ($errno)",
                );
                return( $retStatus );
             }
          }
          To use:
          Code:
          $data = array(
             "statusthing1" => "175",
             "statusthing2" => "1231"
          );
          
          $rv = zabbix_sender( "127.0.0.1", 10051, "myhostname", $data );
          var_dump( $rv );
          $rv contains an array that looks like this:
          Code:
          array(3) {
            ["response"]=>
            string(7) "success"
            ["info"]=>
            string(51) "Processed 2 Failed 0 Total 2 Seconds spent 0.000041"
            ["status"]=>
            array(4) {
              ["Processed"]=>
              string(1) "2"
              ["Failed"]=>
              string(1) "0"
              ["Total"]=>
              string(1) "2"
              ["Seconds spent"]=>
              string(8) "0.000041"
            }
          }
          If there's a socket or message handling error, $rv["response"] will be "failed", with $rv["info"] giving the reason why.

          Comment

          • Ralf Hollmann
            Junior Member
            • Mar 2013
            • 2

            #6
            Float values are cuted?

            If I send a float value with this script the zabbix system recieves the cuted value before the dot (5.51 --> 5)

            If I use zabbi_sender all is working well...

            Any idea?

            Comment

            Working...