Ad Widget

Collapse

Help needed with JSON output

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ArnaudG
    Junior Member
    • Apr 2015
    • 9

    #1

    Help needed with JSON output

    Hello there,

    I'm actually working on a way to make ITservices measurable.

    Thus, after creating some test ITservices, I worked on an agent php script which purpose is to collect all the existing services with name, status (problems/sla rate) and sla threshold.

    So far I stumbled over two problems :
    1/ When I create a disovery rule with the key pointing on my script, I can see a beautifull red cross in the infobox of the rule telling me : "Value should be a JSON object"
    Ok, very nice, but my output has the appropriate JSON format (JSONlint proof). So I'm a bit lost with this only hint.

    2/ In the output itself, I'm able to fetch the service name and threshold (through service.get) and the problems (trigger ids through service.getsla) but it won't show me any actual sla rate.

    Here is my script :

    PHP Code:
    <?php
            
    //Php API path

            
    require './PhpZabbixApiSLA-2.4.2/build/ZabbixApi.class.php';
            use 
    ZabbixApi\Zabbixapi;

            
    //Zabbix token creation

            
    $zab = new ZabbixApi("http://myIP/zabbix/api_jsonrpc.php","user","pwd");
            
    $zab->userLogin(array("user"=>"someuser","password"=>"somepwd"));

            
    //Data for service.get

            
    $zab->serviceGet();
            
    $result $zab->getResponse();
            
    $result json_decode($result,true);

            
    //Data for service.getsla

            
    $zab->serviceGetSla(array("params"=>array("serviceids","intervals"=>array("from"=>"1420070400","to"=>"1438785000"))));
            
    $result1 $zab->getResponse();
            
    $result1 json_decode($result1,true);

            
    $sla $result1["result"];

            
    //JSON formating

            
    echo "{\n";
            echo 
    "\t\"data\":[\n\n";

            
    $first=true;

            foreach (
    $result["result"] as $key=>$srvc) {

            if(!
    $first) echo "\t,\n";
            
    $first false;


            echo 
    "\t{\n";
            echo 
    "\t\t\"{#SRVCNAME}\":\"".$srvc["name"]."\",\n";
            echo 
    "\t\t\"{#GOODSLA}\": ".$srvc["goodsla"].",\n";
            echo 
    "\t\t\"{#STATUS}\": [".json_encode($sla[$srvc["serviceid"]]["problems"]).",".json_encode($sla[$srvc["serviceid"]]["sla"])."]\n";
            echo 
    " \t}\n";
            };


            echo 
    "\n\t]\n";
            echo 
    "}\n";


    ?>
    So, what have I missed? If someone has any ideas about these issues I'd be glad to hear about it.

    And thanks in advance for any usefull reply.
  • ArnaudG
    Junior Member
    • Apr 2015
    • 9

    #2
    After some tests and making sure I'm not doing anything wrong, I came to the conclusion there is something wrong with the zabbix api service.getsla method.
    While the status and service id show up, everything depending on the "intervals" parameter does not.

    Either in php or perl (I tried both languages), the result ouptut leaves me with empty brackets where it should be a float value corresponding to the actual sla lvl.

    Still, maybe I'm missing something, nonetheless I let the issue being checked by a teammate who came accross the same conclusions.
    Last edited by ArnaudG; 13-08-2015, 11:36.

    Comment

    • sersor
      Junior Member
      • Apr 2011
      • 19

      #3
      Please make sure to pass 'serviceids' as this is required by the method

      Comment

      • ArnaudG
        Junior Member
        • Apr 2015
        • 9

        #4
        Hi,

        I'm not sure what exactly you mean by passing the "itservices", but if it is about the matching between the different outputs, I made sure all the results match.

        I made a var_dump of the data

        PHP Code:
         echo "\nserviceid=".$srvc["serviceid"].": ";
                echo 
        "name=".$srvc["name"]." - ";
                echo 
        "goodsla=".$srvc["goodsla"]." - ";
                
        var_dump($sla[$srvc["serviceid"]]); 
        witch outputed something like this :

        PHP Code:
        serviceid=1name=GW_LAB_PSTN goodsla=95.0000 sla=array(3) {
          [
        "status"]=>
          
        string(1"0"
          
        ["problems"]=>
          array(
        0) {
          }
          [
        "sla"]=>
          array(
        0) {
          }

        I also verified by comparing with the database entries.

        Comment

        • ArnaudG
          Junior Member
          • Apr 2015
          • 9

          #5
          Hi,

          Here are some more details to what I've done (zabbix 2.4.5):

          The full code to check the methods, including the serviceids and intervals output :

          PHP Code:
           <?php
                  
          require './PhpZabbixApiSLA-2.4.2/build/ZabbixApi.class.php';
                  use 
          ZabbixApi\ZabbixApi;

                  
          $zab = new ZabbixApi("http://192.168.10.9/zabbix/api_jsonrpc.php","Admin","zabbix");
                  
          //$zab->userLogin(array("user"=>"Admin","password"=>"zabbix"));

                  //Data for service.get
                  
          $zab->serviceGet();
                  
          $def_sla $zab->getResponse();
                  
          $def_sla json_decode($def_sla,true);

                  
          //Data for service.getsla
                  
          $zab->serviceGetSla(array("params"=>array("serviceids"=>array("1"),"intervals"=>array("from"=>"1420070400","to"=>"1438785000"))));
                  
          //the above line has the same efect than : $zab->serviceGetSla();  !!!?
                  
          $res_sla $zab->getResponse();
                  
          $res_sla json_decode($res_sla,true);

                  
          //Display SLA definition of the first SLA monitored called "GW_LAB_PSTN"
                  
          var_dump($def_sla["result"][0]);
                  
          //Display SLA status
                  //var_dump($res_sla["result"]);  !! unexpected status of all SLA is returned. Need to be filtered !!
                  
          var_dump($res_sla["result"][$def_sla["result"][0]["serviceid"]]);
          ?>
          The output:
          PHP Code:
          # php itservice_get.php

           
          array(8) {
            [
          "serviceid"]=>
            
          string(1"1"
            
          ["name"]=>
            
          string(11"GW_LAB_PSTN"
            
          ["status"]=>
            
          string(1"0"
            
          ["algorithm"]=>
            
          string(1"1"
            
          ["triggerid"]=>
            
          string(1"0"
            
          ["showsla"]=>
            
          string(1"1"
            
          ["goodsla"]=>
            
          string(7"95.0000"
            
          ["sortorder"]=>
            
          string(1"0"
          }
          array(
          3) {
            [
          "status"]=>
            
          string(1"0"
            
          ["problems"]=>
            array(
          0) {
            }
            [
          "sla"]=>
            array(
          0) {
            }

          I also attached a screenshot of the SLA "GW_LAB_PSTN" state which values I was trying to get with that code.
          Attached Files
          Last edited by ArnaudG; 30-11-2015, 14:37.

          Comment

          • sersor
            Junior Member
            • Apr 2011
            • 19

            #6
            Sorry, I don't see attachment with screenshot

            Comment

            • ArnaudG
              Junior Member
              • Apr 2015
              • 9

              #7
              Sorry, it did not upload well the first time and I did not check either...

              Comment

              • ArnaudG
                Junior Member
                • Apr 2015
                • 9

                #8
                We made it, eventually.

                Though the error wasn’t where we expected it.
                I just removed the “params” argument which is implicit to the method. So each time I ran the script, the method was looking for a “params” argument in “params”. It just couldn’t work.

                Now the line of code looks like this and the full script runs like a charm :

                PHP Code:
                $zab->serviceGetSla("intervals"=>array(array("from"=>"1420070400","to"=>"1438785000"))); 
                Thanks again for the support.

                Comment

                Working...