Ad Widget

Collapse

Zabbix 1.8 API Host check & creation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mike13
    Member
    • Apr 2010
    • 30

    #1

    Zabbix 1.8 API Host check & creation

    Hi,

    I'm trying to use the new Zabbix API to add hosts with Zabbix php-class API. I would like my script to check whether the host exist or not before adding the host.
    if the host exists then the script should skip to the next one without sending an error. currently, my script is able to add hosts from a file. However it generates an error when the host exist in zabbix:


    <li>Unable to create host: Array
    (
    [code] => -32602
    [message] => Invalid params.
    [data] => [ CHost::create ] Host [ host1 ] already exists
    )




    I've tried to check an existing host with a simple script:

    $check_host = ZabbixAPI::fetch_array('host','get',array("host"=> "host1", "groups"=>array(array("groupid"=>6))))
    or die('Unable to get host: '.print_r(ZabbixAPI::getLastError(),true));
    echo " hosts exist: ".print_r($check_host, true)."\n<br>";



    But, it outputs all the existing hosts.



    Does anyone know how to do this?
    Many thanks,

    Mike,
  • falanga
    Junior Member
    • Jun 2010
    • 10

    #2
    Zabbix 1.8 API Host check &amp; creation

    Hi, Mike.

    The API function Host.Get must receive the hosts ids as an array. To do that, use the following:

    $check_host = ZabbixAPI::fetch_array('host','get',array("hostids "=>array("host1")));


    The Andrew Farley's API function fetch_array returns an array. So, you must check the arrays's 0th item:

    $if (!$check_host[0])
    die('Unable to get host: '.print_r(ZabbixAPI::getLastError(),true));
    echo " host exists: ".print_r($check_host, true)."<br>";


    I think it's a good idea receive all hosts, store them in an array and verify the host exists in memory. Calling the api all the time is really time consuming.


    I hope this helps.

    []s

    Falanga

    Originally posted by mike13
    Hi,

    I'm trying to use the new Zabbix API to add hosts with Zabbix php-class API. I would like my script to check whether the host exist or not before adding the host.
    if the host exists then the script should skip to the next one without sending an error. currently, my script is able to add hosts from a file. However it generates an error when the host exist in zabbix:


    <li>Unable to create host: Array
    (
    [code] => -32602
    [message] => Invalid params.
    [data] => [ CHost::create ] Host [ host1 ] already exists
    )




    I've tried to check an existing host with a simple script:

    $check_host = ZabbixAPI::fetch_array('host','get',array("host"=> "host1", "groups"=>array(array("groupid"=>6))))
    or die('Unable to get host: '.print_r(ZabbixAPI::getLastError(),true));
    echo " hosts exist: ".print_r($check_host, true)."\n<br>";



    But, it outputs all the existing hosts.



    Does anyone know how to do this?
    Many thanks,

    Mike,

    Comment

    • mike13
      Member
      • Apr 2010
      • 30

      #3
      Hi Falanga,

      First of all, thank you for the answer.


      I try to execute the code you have mentioned. But it returns nothing.
      $check_host = ZabbixAPI::fetch_array('host','get',array("hostids "=>array("host1")));
      if (!$check_host[0]){
      die('Unable to get host: '.print_r(ZabbixAPI::getLastError(),true));
      echo " host exists: ".print_r($check_host, true)."<br>";
      }


      And also, i've noticed that your code doesn't use the groupid. So how do we check a host in a particular group?

      Could you please give me some advice?

      Thanks
      Mike

      Comment

      • falanga
        Junior Member
        • Jun 2010
        • 10

        #4
        Hi, Mike!!

        there's no { after if():

        $check_host = ZabbixAPI::fetch_array('host','get',array("hostids "=>array("host1")));
        if (!$check_host[0])
        die('Unable to get host: '.print_r(ZabbixAPI::getLastError(),true));
        echo " host exists: ".print_r($check_host, true)."<br>";


        To check one host in a group, you must include the group id in parameter list. But groupid is also an array, so:

        $check_host = ZabbixAPI::fetch_array('host','get',array("hostids "=>array("host1") , "groupids"=>array(6)));


        You can use a list of groups and of hosts, like this:

        $check_host = ZabbixAPI::fetch_array('host','get',array("hostids "=>array("host1", "host2", "host3")) , "groupids"=>array(6, 4, 5)));


        And you'll receive a intersection between both groups. To get all information about host, include a "extendoutput"=>1 in parameter list.

        []s

        Falanga

        Comment

        • falanga
          Junior Member
          • Jun 2010
          • 10

          #5
          Mike,

          sorry, but I was thinking in a search by hostid. To search by hostname, use this:

          $check_host = ZabbixAPI::fetch_array('host','get', array("extendoutput"=>1, "pattern"=> "host", "groupids"=>array(4,5,6),"select_groups"=>1));


          Remember pattern is case insensitive.

          []s

          Falanga

          Comment

          • Aly
            ZABBIX developer
            • May 2007
            • 1126

            #6
            Originally posted by falanga
            Hi, Mike!!

            there's no { after if():

            $check_host = ZabbixAPI::fetch_array('host','get',array("hostids "=>array("host1")));
            if (!$check_host[0])
            die('Unable to get host: '.print_r(ZabbixAPI::getLastError(),true));
            echo " host exists: ".print_r($check_host, true)."<br>";


            To check one host in a group, you must include the group id in parameter list. But groupid is also an array, so:

            $check_host = ZabbixAPI::fetch_array('host','get',array("hostids "=>array("host1") , "groupids"=>array(6)));


            You can use a list of groups and of hosts, like this:

            $check_host = ZabbixAPI::fetch_array('host','get',array("hostids "=>array("host1", "host2", "host3")) , "groupids"=>array(6, 4, 5)));


            And you'll receive a intersection between both groups. To get all information about host, include a "extendoutput"=>1 in parameter list.

            []s

            Falanga
            0.02$

            1. 'extendoutput' => 1, is depricated (it works, for now), use 'output' => 'extend'
            2. pattern searches in all name space.. so if you would search for host1, it will return hosts with names like 'abrakadabra_host1', so it won't work as you probably expected.
            3. if he searches by host name, 'hostids' param is useless it only accepts IDS not host names.

            Good luck.
            Zabbix | ex GUI developer

            Comment

            • mike13
              Member
              • Apr 2010
              • 30

              #7
              Hi Falanga,

              Your last code is working fine. I've integrated it into my code as follows:

              foreach($dat as $host => $ip){
              $check_host = ZabbixAPI::fetch_array('host','get', array("output"=>'extend', "pattern"=> $host, "groupids"=>array(6),"select_groups"=>1));
              if ($check_host[0]){
              echo " host exists: ".$host."\n";
              $result=ZabbixAPI::query('host', 'create', array("host"=>$host,"ip"=>$ip,"port"=>10050,
              "groups"=>array(array("groupid"=>6))));

              }
              }



              However, it is not adding the hosts in either case.

              Could you please give me some advice?

              Thanks
              Mike
              Last edited by mike13; 02-06-2010, 10:55.

              Comment

              • falanga
                Junior Member
                • Jun 2010
                • 10

                #8
                Thanks, Aly!!

                []s

                Falanga

                Originally posted by Aly
                0.02$

                1. 'extendoutput' => 1, is depricated (it works, for now), use 'output' => 'extend'
                2. pattern searches in all name space.. so if you would search for host1, it will return hosts with names like 'abrakadabra_host1', so it won't work as you probably expected.
                3. if he searches by host name, 'hostids' param is useless it only accepts IDS not host names.

                Good luck.

                Comment

                • falanga
                  Junior Member
                  • Jun 2010
                  • 10

                  #9
                  Hi, Mike!

                  You're inserting the new host if it already exists. Try to change your code to somethig like this:

                  foreach($dat as $host => $ip)
                  {
                  $check_host = ZabbixAPI::fetch_array('host','get', array("output"=>'extend', "pattern"=> $host, "groupids"=>array(6),"select_groups"=>1));
                  if ($check_host[0]){
                  // Here, the host exists
                  echo " host exists: ".$host."\n";
                  }
                  else{
                  // here, the host doesn't exists
                  $result=ZabbixAPI::query('host', 'create', array("host"=>$host,"ip"=>$ip,"port"=>10050, "groups"=>array(array("groupid"=>6))));
                  // Test the result to confirm the operation
                  if ($result)
                  echo "host created<br>"
                  }
                  }





                  Originally posted by mike13
                  Hi Falanga,

                  Your last code is working fine. I've integrated it into my code as follows:

                  foreach($dat as $host => $ip){
                  $check_host = ZabbixAPI::fetch_array('host','get', array("output"=>'extend', "pattern"=> $host, "groupids"=>array(6),"select_groups"=>1));
                  if ($check_host[0]){
                  echo " host exists: ".$host."\n";
                  $result=ZabbixAPI::query('host', 'create', array("host"=>$host,"ip"=>$ip,"port"=>10050,
                  "groups"=>array(array("groupid"=>6))));

                  }
                  }



                  However, it is not adding the hosts in either case.

                  Could you please give me some advice?

                  Thanks
                  Mike

                  Comment

                  • mike13
                    Member
                    • Apr 2010
                    • 30

                    #10
                    Hi Falanga,

                    You are absolutely right, It is a careless mistake.

                    Thank you so much for all your help I really appreciate it.
                    By the way, is there a possibility to add host by giving the group name instead of groupid?

                    Thanks,
                    Mike


                    Originally posted by falanga
                    Hi, Mike!

                    You're inserting the new host if it already exists. Try to change your code to somethig like this:

                    foreach($dat as $host => $ip)
                    {
                    $check_host = ZabbixAPI::fetch_array('host','get', array("output"=>'extend', "pattern"=> $host, "groupids"=>array(6),"select_groups"=>1));
                    if ($check_host[0]){
                    // Here, the host exists
                    echo " host exists: ".$host."\n";
                    }
                    else{
                    // here, the host doesn't exists
                    $result=ZabbixAPI::query('host', 'create', array("host"=>$host,"ip"=>$ip,"port"=>10050, "groups"=>array(array("groupid"=>6))));
                    // Test the result to confirm the operation
                    if ($result)
                    echo "host created<br>"
                    }
                    }

                    Comment

                    • falanga
                      Junior Member
                      • Jun 2010
                      • 10

                      #11
                      Aly,

                      I changed my 'extendoutput' => 1 to 'output' => 'extend' as you sugested but it doesn't work. I look in API class file class.cHost.php and I couldn't find any reference to that option. I'm working with Api version 1.2. Is there any new version?

                      Can you show me an example of use?

                      Thanks

                      Falanga


                      Originally posted by Aly
                      0.02$

                      1. 'extendoutput' => 1, is depricated (it works, for now), use 'output' => 'extend'
                      2. pattern searches in all name space.. so if you would search for host1, it will return hosts with names like 'abrakadabra_host1', so it won't work as you probably expected.
                      3. if he searches by host name, 'hostids' param is useless it only accepts IDS not host names.

                      Good luck.

                      Comment

                      • falanga
                        Junior Member
                        • Jun 2010
                        • 10

                        #12
                        Hi, Mike!

                        I'm using api version 1.2 and I don't think there is a possibility of adding host by group name in that version!!

                        I'm glad I could help.

                        []s

                        Falanga

                        Originally posted by mike13
                        Hi Falanga,

                        You are absolutely right, It is a careless mistake.

                        Thank you so much for all your help I really appreciate it.
                        By the way, is there a possibility to add host by giving the group name instead of groupid?

                        Thanks,
                        Mike

                        Comment

                        • Aly
                          ZABBIX developer
                          • May 2007
                          • 1126

                          #13
                          Originally posted by falanga
                          Aly,

                          I changed my 'extendoutput' => 1 to 'output' => 'extend' as you sugested but it doesn't work. I look in API class file class.cHost.php and I couldn't find any reference to that option. I'm working with Api version 1.2. Is there any new version?

                          Can you show me an example of use?

                          Thanks

                          Falanga
                          What do you mean by "it doesn't work"?
                          Zabbix | ex GUI developer

                          Comment

                          • falanga
                            Junior Member
                            • Jun 2010
                            • 10

                            #14
                            Originally posted by Aly
                            What do you mean by "it doesn't work"?
                            Aly,

                            sorry, but I think I wrote something wrong during my test. But yes, it work ok!

                            thanks

                            Falanga

                            Comment

                            Working...