Ad Widget

Collapse

Script to update snmp port descriptions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • limo
    Senior Member
    • Dec 2004
    • 192

    #1

    Script to update snmp port descriptions

    Hi all,

    I just made small script which takes interface descriptions from ifDescr and updates descriptions in ifInputOctets and ifOutputOctets. We have many cisco switches and it is very hard to find which port belongs to snmp inteface id. We have interfaces descriptions set in cisco switches. This script takes latest values from ifDescrx and change descriptions inn ifInputOctetsx and ifOutputOctetsx.

    This works for me, but I know that it needs many enhancements
    Feel free to use!

    Put tis script into zabbix www directory.

    <?


    include "include/config.inc.php";
    include "include/forms.inc.php";

    // Get all items which are snmpv2 and are not template and have key_ like ifDesc%
    $query="select i.itemid as itemid,i.key_ as key_,i.hostid as hostid,i.description as description from items i,hosts h where h.hostid=i.hostid and h.status<>3 and i.type=".ITEM_TYPE_SNMPV2C." and i.key_ like 'ifDesc%'
    order by i.hostid,i.key_";
    $items=DBexecute($query);
    $queryor="";
    $numitems=0;
    while ($row=DBFetch($items)) {
    $key=$row["key_"];
    $hostid=$row["hostid"];
    $itemid=$row["itemid"];
    $queryor.=" or itemid=$itemid";
    $numitems++;
    }

    // Get needed data from history in one turn
    $query="select itemid,value,clock from history_str where clock>(unix_timestamp(NOW())-3600*24)";
    $values=DBexecute($query);
    while ($row=DBFetch($values)) {
    $itemid=$row["itemid"];
    $value=$row["value"];
    $clock=$row["clock"];
    $history[$itemid]=$value;
    }

    $query="select i.itemid as itemid,i.key_ as key_,i.hostid as hostid,i.description as description from items i,hosts h where h.hostid=i.hostid and h.status<>3 and i.type=".ITEM_TYPE_SNMPV2C." and i.key_ like 'ifDesc%'
    order by i.hostid,i.key_";
    $items=DBexecute($query);
    while ($row=DBFetch($items)) {
    $hostid=$row["hostid"];
    $itemid=$row["itemid"];
    $key=$row["key_"];
    sscanf($key,"ifDescr%d",&$portnum);
    if (is_int($portnum)) {
    echo "updating (hostid=$hostid,itemid=$itemid,key=$key)\n";
    // Update IfInOctets
    $updateid=DBexecute("select i.itemid as itemid from items i,hosts h where h.hostid=$hostid and h.hostid=i.hostid and i.key_='ifInOctets$portnum'");
    $row2=DBfetch($updateid);
    $updateid=$row2["itemid"];
    $description=$history[$itemid];
    $sql="update items set description='IfInOctets$portnum [$description]' where itemid=$updateid";
    $update=DBexecute($sql);
    // Update IfOutOctets
    $updateid=DBexecute("select i.itemid as itemid from items i,hosts h where h.hostid=$hostid and h.hostid=i.hostid and i.key_='ifOutOctets$portnum'");
    $row2=DBfetch($updateid);
    $updateid=$row2["itemid"];
    $description=$history[$itemid];
    $sql="update items set description='IfOutOctets$portnum [$description]' where itemid=$updateid";
    $update=DBexecute($sql);
    }
    }

    ?>
Working...