Ad Widget

Collapse

How to do parse hosts IP to external script ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fableman
    Member
    • Oct 2007
    • 78

    #1

    How to do parse hosts IP to external script ?

    Item/

    Key: icmp_ping[]

    when i run this command it dont work.

    But If I goto Hosts tab and rename ex. backup storage to an IP address like 192.168.123.123

    then my icmp_ping[] start to work (output will be 192.168.123.123)

    Soo... how can I parse the IP number and not name: "Backup storage" to my script ?

    is ther some kind of key i can use to get the ip address parsed?
    ex. icmp_ping[$ip] ??



    I cant have a zabbix list with over 200 IP addresses instead of real names.

    please help.



    This is my script icmp_ping
    #!/bin/sh
    ping -c10 -i0.20 $1 | tail -n 1 | awk '{print $4}'| cut -d/ -f2
  • Tenzer
    Senior Member
    • Nov 2007
    • 316

    #2
    Why would you create a script like that, when there is already icmppingsec in the simple checks section. Description: "Return ICMP ping response time Number of seconds Example: 0.02".

    Is it meant as a test script or something?

    Comment

    • fableman
      Member
      • Oct 2007
      • 78

      #3
      Originally posted by Tenzer
      Why would you create a script like that, when there is already icmppingsec in the simple checks section. Description: "Return ICMP ping response time Number of seconds Example: 0.02".

      Is it meant as a test script or something?
      The built in function of zabbix only send 1 ping using fping its useless and dont tell the reality of the latency of the network.

      why ?? Lots of routers, layer3 switches don't priority icmp traffic and if you only send 1 ping you can get answers thats are very far from reality.

      Soo thats why I use my own script that will send 10 icmp pings every 20ms, soo in 200ms I get 10 answers and I get out the average ms time. and that give me a correct latency that I have use for.

      The network Iam administrating is a huge one with lots of vpn tunnels,routerslayer 3 switches, gbit speed all the way to the clients, and lots of fiber single mode thats connect the different networks. Even some wireless WDS connections aswell.

      Soo its important for me soo see trends if the latency is getting to high its indicating something is not as it should and soo on.

      In small networks you might never see the problem with just sending 1 ping, and different brands of network equipment behave different from 1 ping to another aswell.

      Hope that explains why I need external scripts to ping my network

      Comment

      • Tenzer
        Senior Member
        • Nov 2007
        • 316

        #4
        Okay, I get the point.

        Unfortunately the "{IPADDRESS}" macro isn't available when making items, as when making triggers and actions, that would have been an easy solution, or if the item simply used the IP address or hostname, depending on what the setting was for the specific host.
        I would make more sense if it sent the IP address to the script, in case the host is being checked by it's IP address.

        The only solution I can think of right now, is to make the script in PHP/Perl/... which then queries the database for the IP address to the host given, and then starts pinging, but that would just slow down the script and increase database load.

        Comment

        • fableman
          Member
          • Oct 2007
          • 78

          #5
          The only solution now I have found is to rename all hosts to:
          192.168.222.267 Oracle databse server 4
          192.168.20.44 Layer3 switch facility 44

          and soo

          then my scrit gets feeded by ip number as first input to it.
          But It don't look soon good in zabbix doing this work around.

          Soo any other solution or fix for this problem ?

          Comment

          • just2blue4u
            Senior Member
            • Apr 2006
            • 347

            #6
            Maybe i still didn't get it right, then just forget my idea.
            Anyways- Why don't you use DNS?

            Best regards,
            Heiner
            Big ZABBIX is watching you!
            (... and my 48 hosts, 4513 items, 1280 triggers via zabbix v1.6 on CentOS 5.0)

            Comment

            • Tenzer
              Senior Member
              • Nov 2007
              • 316

              #7
              I've made a quick PHP script which covers the suggestion I made earlier on, by querying a MySQL database from an external script, in order to get the IP address we want to ping. It is not pretty, but it gets the job done

              NB: Remember to correct the MySQL database settings!

              PHP Code:
              #!/usr/bin/php
              <?
                  // Check for required parameters
                  if(!$_SERVER[argv][1])
                  {
                      echo "You need to supply a hostname to ping. Quitting.\n";
                      exit;
                  }

                  // Dirty way of removing the script name and avoid a whitespace in front of the hostname
                  array_shift($_SERVER[argv]);
                  $hostname = array_shift($_SERVER[argv]);

                  // Assemble the hostname, if it contains whitespaces
                  foreach($_SERVER[argv] as $number)
                  {
                      $hostname = $hostname." ".$number;
                  }

                  // MySQL database informations
                  $db_username = "root";
                  $db_hostname = "localhost";
                  $db_password = "";
                  $db_database = "zabbix";

                  // Connect to the MySQL server and select the database
                  $db_connection = mysql_connect($db_hostname, $db_username, $db_password);
                  $db_connection = mysql_select_db($db_database, $db_connection);

                  // Get the host's IP address from the database
                  $data = mysql_query("SELECT ip FROM hosts WHERE host = '$hostname'");
                  $ip_addr = mysql_fetch_assoc($data);

                  // Check if a host was found with the IP address
                  if(!isset($ip_addr[ip]))
                  {
                      echo "The host, $hostname, was not found in the database, script aborting.\n";
                      exit();
                  } else {
                      $ip_addr = $ip_addr[ip];
                  }

                  // Ping the host
                  $command = exec("ping -c10 -i0.20 $ip_addr | tail -n 1 | awk '{print $4}'| cut -d/ -f2", $output, $return);

                  // Print the output for Zabbix to read
                  echo "$output[0]\n";
              ?>
              just2blue4u, you might not have a DNS record for every device in your network, and it would be neat if the external script system was a bit more flexible.

              Comment

              • cristhiano
                Member
                • Nov 2007
                • 48

                #8
                I need a similar feature and change file /src/zabbix_server/poller/checks_external.c line 89 from:

                zbx_snprintf(cmd, MAX_STRING_LEN-1, "%s/%s %s %s", CONFIG_EXTERNALSCRIPTS, scriptname, item->host_name, params);

                To:

                zbx_snprintf(cmd, MAX_STRING_LEN-1, "%s/%s %s %s", CONFIG_EXTERNALSCRIPTS, scriptname, item->useip==1 ? item->host_ip : item->host_dns, params);

                And recompile zabbix_server.

                External plugins receive IP address or DNS instead of hostame.

                Comment

                • Tenzer
                  Senior Member
                  • Nov 2007
                  • 316

                  #9
                  Perfect, that was one of the solutions I mentioned earlier, but since I don't have any experience in C programming, I wasn't able to figure out what to correct, but I was at least looking in the right file

                  That small change should really be included in the official release, since it makes much more sense than using the host's name.

                  Comment

                  • fableman
                    Member
                    • Oct 2007
                    • 78

                    #10
                    Originally posted by Tenzer
                    I've made a quick PHP script which covers the suggestion I made earlier on, by querying a MySQL database from an external script, in order to get the IP address we want to ping. It is not pretty, but it gets the job done

                    NB: Remember to correct the MySQL database settings!

                    PHP Code:
                    #!/usr/bin/php
                    <?
                        // Check for required parameters
                        if(!$_SERVER[argv][1])
                        {
                            echo "You need to supply a hostname to ping. Quitting.\n";
                            exit;
                        }

                        // Dirty way of removing the script name and avoid a whitespace in front of the hostname
                        array_shift($_SERVER[argv]);
                        $hostname = array_shift($_SERVER[argv]);

                        // Assemble the hostname, if it contains whitespaces
                        foreach($_SERVER[argv] as $number)
                        {
                            $hostname = $hostname." ".$number;
                        }

                        // MySQL database informations
                        $db_username = "root";
                        $db_hostname = "localhost";
                        $db_password = "";
                        $db_database = "zabbix";

                        // Connect to the MySQL server and select the database
                        $db_connection = mysql_connect($db_hostname, $db_username, $db_password);
                        $db_connection = mysql_select_db($db_database, $db_connection);

                        // Get the host's IP address from the database
                        $data = mysql_query("SELECT ip FROM hosts WHERE host = '$hostname'");
                        $ip_addr = mysql_fetch_assoc($data);

                        // Check if a host was found with the IP address
                        if(!isset($ip_addr[ip]))
                        {
                            echo "The host, $hostname, was not found in the database, script aborting.\n";
                            exit();
                        } else {
                            $ip_addr = $ip_addr[ip];
                        }

                        // Ping the host
                        $command = exec("ping -c10 -i0.20 $ip_addr | tail -n 1 | awk '{print $4}'| cut -d/ -f2", $output, $return);

                        // Print the output for Zabbix to read
                        echo "$output[0]\n";
                    ?>
                    just2blue4u, you might not have a DNS record for every device in your network, and it would be neat if the external script system was a bit more flexible.
                    Was a neat solution ya made.. Thanks alot for that.

                    The only negative part is the load on mysql wil get bigger and its pritty huge as it is now with all my hosts.

                    And yes I dont have dns records for all network devices. I even monitor the card readers that grant access into buildings, and other IO servers monitoring heat system in buildings and stuff soo list is getting huge hehe

                    Comment

                    • Tenzer
                      Senior Member
                      • Nov 2007
                      • 316

                      #11
                      Just use the solution cristhiano posted, it works great and uses fewer resources overall.

                      Comment

                      • RobertS
                        Member
                        • Aug 2006
                        • 57

                        #12
                        Request

                        On the host-configuration screen you can select to use DNS or IP. I would expect zabbix to use this setting for the first parameter to an external script and nothing else.

                        Comment

                        • fableman
                          Member
                          • Oct 2007
                          • 78

                          #13
                          Originally posted by RobertS
                          On the host-configuration screen you can select to use DNS or IP. I would expect zabbix to use this setting for the first parameter to an external script and nothing else.
                          Hope it will be added in next version.

                          Comment

                          • fableman
                            Member
                            • Oct 2007
                            • 78

                            #14
                            Originally posted by cristhiano
                            I need a similar feature and change file /src/zabbix_server/poller/checks_external.c line 89 from:

                            zbx_snprintf(cmd, MAX_STRING_LEN-1, "%s/%s %s %s", CONFIG_EXTERNALSCRIPTS, scriptname, item->host_name, params);

                            To:

                            zbx_snprintf(cmd, MAX_STRING_LEN-1, "%s/%s %s %s", CONFIG_EXTERNALSCRIPTS, scriptname, item->useip==1 ? item->host_ip : item->host_dns, params);

                            And recompile zabbix_server.

                            External plugins receive IP address or DNS instead of hostame.
                            Iam using an rpm soo its allready compiled.

                            Comment

                            Working...