Uptime in Cacti Monitoring
Cacti does get SNMP-Uptime from devices, but it only shows it on the device’s own page, and once you have more than 100 such devices, opening each page becomes a pain. Need to pull them into one table.
You can get a server’s uptime with snmpwalk like this:
snmpget -v2c -c public localhost 1.3.6.1.2.1.1.3.0
or like this:
snmpget -v2c -c public **localhost** sysUpTime.0
Change localhost to the ip address of the server you’re interested in.
For Cacti we’ll need the monitor_uptime plugin.
The plugin’s code is written in PHP, so editing it isn’t a big deal. To get data from the equipment it uses the standard PHP functions snmpget and snmp2_get, which take the host parameters from the Cacti DB, the uptime OID and a wait timeout as arguments. The resulting uptime code ended up like this:
$snmp_uptime = " N\A";
include("../../include/config.php");
mysql_connect($database_hostname,$database_username,$database_password) OR DIE(mysql_error());
mysql_select_db($database_default) OR DIE(mysql_error());
$sql="SELECT * FROM `host` where `id`=$id";
$sql_res=mysql_query($sql) OR DIE(mysql_error());
while ($host=mysql_fetch_array($sql_res)){
if ($host["snmp_version"] == '1'){
$snmp_uptime = snmpget($host["hostname"],$host["snmp_community"],".1.3.6.1.2.1.1.3.0",55000);
}
else if ($host["snmp_version"] == '2'){
$snmp_uptime =snmp2_get($host["hostname"],$host["snmp_community"],".1.3.6.1.2.1.1.3.0",55000);
}
$str=strpos($snmp_uptime, ")");
$snmp_uptime = substr($snmp_uptime,$str+1);}
Let’s also add a row to the table itself
<tr bgcolor='#eeeeee'>
<td><strong>Uptime:</strong></td>
<td><strong>$snmp_uptime</strong></td>
</tr><
After that save it, open Cacti and behold real-time uptime in a convenient view
