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
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);
}
?>
Comment