Ad Widget

Collapse

[FIX] discovery set-hostname_get-sysName

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • eric_at_2037
    Junior Member
    • May 2008
    • 12

    #1

    [FIX] discovery set-hostname_get-sysName

    Hi,

    This subject is a translation of this original article in French : http://it.2037.biz/viewtopic.php?t=8907

    When you use the discovery functionality by default host discovered is named by their IP address. This php script list all hosts and try to get sysName by SNMP.

    Before executing, make sure that you have understood what the script does! druleid must be correctly set.

    Regards,

    Eric

    PHP Code:
    <?php

    #
    # Ce script distribué sous la licence GNU FDL a été développé a l'origine par Eric B
    #
    # Site officiel du script : http://2037.org/ || http://it.2037.biz/viewtopic.php?t=8907
    #
    # Permission vous est donnée de distribuer, modifier ce script tant que cette note apparaît clairement
    #
    # Version 1.0.0 du 19 mai 2008
    #

    // the name of snmp community
    $C_COMMUNITY "public";

    // connexion to MySQL database server
    $db mysql_connect('mysql_server''mysql_user''mysql_password')  or die('Erreur de connexion '.mysql_error());

    // select  MySQL base
    mysql_select_db('zabbix',$db)  or die('Erreur de selection '.mysql_error());

    // select dhost
    // druleid is the id of the discovery rule, see table drules
    $sql "SELECT * FROM `dhosts` WHERE `druleid` = '3'";

    $result mysql_query($sql) or die('Erreur SQL !'.$sql.'<br>'.mysql_error());

    // for each result we get the device name by snmp and update the record in the database
    while ($row mysql_fetch_assoc($result))
    {
      
    $C_IP $row['ip'];
      
    $sysName snmpgetclear(".iso.org.dod.internet.mgmt.mib-2.system.sysName.0");

      
    // if result is too long or too short nothing is done
      
    if(strlen($sysName) < '64' AND strlen($sysName) > '3')
       {
        
    $sql2 "UPDATE `hosts` SET `host` = '{$sysName}' WHERE `ip` = '{$row['ip']}' LIMIT 1;";
        
    $result2 mysql_query($sql2) or die('Erreur SQL !'.$sql.'<br>'.mysql_error());
       }
    }

    // the end

    echo 'script done !';

    // On format la response SNMP
    function snmpgetclear($oid)
    {
      
    $invar snmpget($GLOBALS['C_IP'],$GLOBALS['C_COMMUNITY'],"$oid");
      
    $var eregi_replace("STRING: ","",$invar);
      return 
    eregi_replace("\"","",$var);
    }


    ?>
    Last edited by eric_at_2037; 19-01-2017, 15:06.
  • eric_at_2037
    Junior Member
    • May 2008
    • 12

    #2
    Add SLA

    Before executing, make sure that you have understood what the script does! druleid and function parameter add_service must be correctly set.

    PHP Code:
    <?php

    #
    # Ce script distribué sous la licence GNU FDL a été développé a l'origine par Eric B
    #
    # Site officiel du script : http://2037.org/ || http://it.2037.biz/viewtopic.php?t=8907
    #
    # Permission vous est donnée de distribuer, modifier ce script tant que cette note apparaît clairement
    #
    # Version 1.0.0 du 21 mai 2008
    #


    // connexion to MySQL database server
    $db mysql_connect('mysql_server''mysql_user''mysql_password')  or die('Erreur de connexion '.mysql_error());

    // select  MySQL base
    mysql_select_db('zabbix',$db)  or die('Erreur de selection '.mysql_error());

    // select dhost
    // druleid is the id of the discovery rule, see table drules
    $sql "SELECT * FROM `dhosts` WHERE `druleid` = '3'";

    $result mysql_query($sql) or die('Erreur SQL !'.$sql.'<br>'.mysql_error());

    // for each result we get the device name by snmp and update the record in the database
    while ($row mysql_fetch_assoc($result))
    {
      
    //echo $row['ip'] . "\n";
     
      // get hostid itemid triggerid
      
    $sql2 "SELECT * FROM `items` AS i, `hosts` AS h, functions AS f WHERE h.ip = '{$row['ip']}' AND h.hostid = i.hostid AND i.key_ = 'icmpping' AND i.itemid = f.itemid";
      
    $result2 mysql_query($sql2) or die('Erreur SQL !'.$sql.'<br>'.mysql_error()); 
      
    $row2 mysql_fetch_assoc($result2);
      
    // echo $row2['triggerid'] . "\n";

    $result3 mysql_query("SELECT COUNT(*) FROM services WHERE triggerid = '{$row2['triggerid']}'");
    $row3 mysql_fetch_assoc($result3);

    // service is not already set for this dhost
    if($row3['COUNT(*)'] == '0')
      {
        
    //echo "on s'occupe de {$row2['host']} - {$row2['triggerid']}\n";
            
    include_once "include/config.inc.php";
            include_once 
    "include/services.inc.php";

        
    /* add_service(
           $_REQUEST["name"],$triggerid,$_REQUEST["algorithm"],
           $_REQUEST["showsla"],$_REQUEST["goodsla"],$_REQUEST["sortorder"],
           $service_times,$_REQUEST['parentid'],$childs);
        */
         
    echo add_service($row2['host'],$row2['triggerid'],"0","1","99.05","0","","14","");
       
      }
     
    }

    // the end

    echo 'script done !';

    ?>
    Last edited by eric_at_2037; 19-01-2017, 15:07.

    Comment

    Working...