2009-10-28 13:49:37 +00:00
< ? php
unset ( $ports );
$ports = snmp_cache_ifIndex ( $device ); // Cache Port List
// Build SNMP Cache Array
$data_oids = array ( 'ifName' , 'ifDescr' , 'ifAlias' , 'ifAdminStatus' , 'ifOperStatus' , 'ifMtu' , 'ifSpeed' , 'ifHighSpeed' , 'ifType' , 'ifPhysAddress' ,
'ifPromiscuousMode' , 'ifConnectorPresent' );
$stat_oids = array ( 'ifInErrors' , 'ifOutErrors' , 'ifInUcastPkts' , 'ifOutUcastPkts' , 'ifInNUcastPkts' , 'ifOutNUcastPkts' ,
'ifHCInMulticastPkts' , 'ifHCInBroadcastPkts' , 'ifHCOutMulticastPkts' , 'ifHCOutBroadcastPkts' ,
'ifInOctets' , 'ifOutOctets' , 'ifHCInOctets' , 'ifHCOutOctets' );
$stat_oids_db = array ( 'ifInOctets' , 'ifOutOctets' , 'ifInErrors' , 'ifOutErrors' , 'ifInUcastPkts' , 'ifOutUcastPkts' ); // From above for DB
2010-02-02 19:24:12 +00:00
$etherlike_oids = array ( 'dot3StatsAlignmentErrors' , 'dot3StatsFCSErrors' , 'dot3StatsSingleCollisionFrames' , 'dot3StatsMultipleCollisionFrames' ,
2009-11-24 23:58:56 +00:00
'dot3StatsSQETestErrors' , 'dot3StatsDeferredTransmissions' , 'dot3StatsLateCollisions' , 'dot3StatsExcessiveCollisions' ,
2009-10-28 13:49:37 +00:00
'dot3StatsInternalMacTransmitErrors' , 'dot3StatsCarrierSenseErrors' , 'dot3StatsFrameTooLongs' , 'dot3StatsInternalMacReceiveErrors' ,
2009-11-24 23:58:56 +00:00
'dot3StatsSymbolErrors' );
2009-10-28 13:49:37 +00:00
$cisco_oids = array ( 'locIfHardType' , 'locIfInRunts' , 'locIfInGiants' , 'locIfInCRC' , 'locIfInFrame' , 'locIfInOverrun' , 'locIfInIgnored' , 'locIfInAbort' ,
'locIfCollisions' , 'locIfInputQueueDrops' , 'locIfOutputQueueDrops' );
$pagp_oids = array ( 'pagpOperationMode' , 'pagpPortState' , 'pagpPartnerDeviceId' , 'pagpPartnerLearnMethod' , 'pagpPartnerIfIndex' , 'pagpPartnerGroupIfIndex' ,
'pagpPartnerDeviceName' , 'pagpEthcOperationMode' , 'pagpDeviceId' , 'pagpGroupIfIndex' );
$ifmib_oids = array_merge ( $data_oids , $stat_oids );
2009-11-24 23:58:56 +00:00
$ifmib_oids = array ( 'ifEntry' , 'ifXEntry' );
2009-11-21 15:07:09 +00:00
echo ( " Caching Oids: " );
2010-07-22 21:58:49 +00:00
foreach ( $ifmib_oids as $oid ) { echo ( " $oid " ); $array = snmpwalk_cache_oid ( $device , $oid , $array , " IF-MIB " );}
2009-11-24 23:58:56 +00:00
2010-07-22 21:58:49 +00:00
if ( $config [ 'enable_ports_etherlike' ]) { echo ( " dot3Stats " ); $array = snmpwalk_cache_oid ( $device , " dot3StatsEntry " , $array , " EtherLike-MIB " ); }
2010-07-20 13:10:18 +00:00
if ( $config [ 'enable_ports_adsl' ]) {
$device [ 'adsl_count' ] = mysql_result ( mysql_query ( " SELECT COUNT(*) FROM `ports` WHERE `device_id` = ' " . $device [ 'device_id' ] . " ' AND `ifType` = 'adsl' " ), 0 );
}
if ( $device [ 'adsl_count' ] > " 0 " ) {
2010-07-20 13:02:54 +00:00
echo ( " ADSL " );
2010-07-22 21:58:49 +00:00
$array = snmpwalk_cache_oid ( $device , " .1.3.6.1.2.1.10.94.1.1.1.1 " , $array , " ADSL-LINE-MIB " );
$array = snmpwalk_cache_oid ( $device , " .1.3.6.1.2.1.10.94.1.1.2.1 " , $array , " ADSL-LINE-MIB " );
$array = snmpwalk_cache_oid ( $device , " .1.3.6.1.2.1.10.94.1.1.3.1 " , $array , " ADSL-LINE-MIB " );
$array = snmpwalk_cache_oid ( $device , " .1.3.6.1.2.1.10.94.1.1.4.1 " , $array , " ADSL-LINE-MIB " );
$array = snmpwalk_cache_oid ( $device , " .1.3.6.1.2.1.10.94.1.1.5.1 " , $array , " ADSL-LINE-MIB " );
$array = snmpwalk_cache_oid ( $device , " .1.3.6.1.2.1.10.94.1.1.6.1.1 " , $array , " ADSL-LINE-MIB " );
$array = snmpwalk_cache_oid ( $device , " .1.3.6.1.2.1.10.94.1.1.6.1.2 " , $array , " ADSL-LINE-MIB " );
$array = snmpwalk_cache_oid ( $device , " .1.3.6.1.2.1.10.94.1.1.6.1.3 " , $array , " ADSL-LINE-MIB " );
$array = snmpwalk_cache_oid ( $device , " .1.3.6.1.2.1.10.94.1.1.6.1.4 " , $array , " ADSL-LINE-MIB " );
$array = snmpwalk_cache_oid ( $device , " .1.3.6.1.2.1.10.94.1.1.6.1.5 " , $array , " ADSL-LINE-MIB " );
$array = snmpwalk_cache_oid ( $device , " .1.3.6.1.2.1.10.94.1.1.6.1.6 " , $array , " ADSL-LINE-MIB " );
$array = snmpwalk_cache_oid ( $device , " .1.3.6.1.2.1.10.94.1.1.6.1.7 " , $array , " ADSL-LINE-MIB " );
$array = snmpwalk_cache_oid ( $device , " .1.3.6.1.2.1.10.94.1.1.6.1.8 " , $array , " ADSL-LINE-MIB " );
$array = snmpwalk_cache_oid ( $device , " .1.3.6.1.2.1.10.94.1.1.7.1.1 " , $array , " ADSL-LINE-MIB " );
$array = snmpwalk_cache_oid ( $device , " .1.3.6.1.2.1.10.94.1.1.7.1.2 " , $array , " ADSL-LINE-MIB " );
$array = snmpwalk_cache_oid ( $device , " .1.3.6.1.2.1.10.94.1.1.7.1.3 " , $array , " ADSL-LINE-MIB " );
$array = snmpwalk_cache_oid ( $device , " .1.3.6.1.2.1.10.94.1.1.7.1.4 " , $array , " ADSL-LINE-MIB " );
$array = snmpwalk_cache_oid ( $device , " .1.3.6.1.2.1.10.94.1.1.7.1.5 " , $array , " ADSL-LINE-MIB " );
$array = snmpwalk_cache_oid ( $device , " .1.3.6.1.2.1.10.94.1.1.7.1.6 " , $array , " ADSL-LINE-MIB " );
$array = snmpwalk_cache_oid ( $device , " .1.3.6.1.2.1.10.94.1.1.7.1.7 " , $array , " ADSL-LINE-MIB " );
2010-07-18 09:28:25 +00:00
}
2009-11-24 23:58:56 +00:00
2009-11-21 15:07:09 +00:00
echo ( " \n " );
2009-10-28 13:49:37 +00:00
2010-07-22 21:58:49 +00:00
#foreach ($etherlike_oids as $oid) { $array = snmpwalk_cache_oid($device, $oid, $array, "EtherLike-MIB"); }
#foreach ($cisco_oids as $oid) { $array = snmpwalk_cache_oid($device, $oid, $array, "OLD-CISCO-INTERFACES-MIB"); }
#foreach ($pagp_oids as $oid) { $array = snmpwalk_cache_oid($device, $oid, $array, "CISCO-PAGP-MIB"); }
2009-10-28 13:49:37 +00:00
2009-11-28 09:48:23 +00:00
if ( $device [ 'os_group' ] == " ios " ) {
2010-02-02 19:24:12 +00:00
$array = snmp_cache_portIfIndex ( $device , $array );
$array = snmp_cache_portName ( $device , $array );
$data_oids [] = " portName " ;
2010-07-22 21:58:49 +00:00
#$array = snmpwalk_cache_oid($device, "vmVlan", $array, "CISCO-VLAN-MEMBERSHIP-MIB");
#$array = snmpwalk_cache_oid($device, "vlanTrunkPortEncapsulationOperType", $array, "CISCO-VTP-MIB");
#$array = snmpwalk_cache_oid($device, "vlanTrunkPortNativeVlan", $array, "CISCO-VTP-MIB");
2009-10-28 13:49:37 +00:00
}
$polled = time ();
/// End Building SNMP Cache Array
if ( $debug ) { print_r ( $array ); }
/// New interface detection
///// TO DO
/// End New interface detection
2010-02-20 17:22:22 +00:00
/// Loop ports in the DB and update where necessary
$port_query = mysql_query ( " SELECT * FROM `ports` WHERE `device_id` = ' " . $device [ 'device_id' ] . " ' " );
2009-10-28 13:49:37 +00:00
while ( $port = mysql_fetch_array ( $port_query )) {
echo ( " --> " . $port [ 'ifDescr' ] . " " );
2010-07-21 22:28:50 +00:00
if ( $array [ $device [ 'device_id' ]][ $port [ 'ifIndex' ]] && $port [ 'ignore' ] == " 0 " ) { // Check to make sure Port data is cached.
2009-10-28 13:49:37 +00:00
2010-07-21 22:28:50 +00:00
$this_port = & $array [ $device [ 'device_id' ]][ $port [ 'ifIndex' ]];
2009-10-28 13:49:37 +00:00
$polled_period = $polled - $port [ 'poll_time' ];
$update .= " `poll_time` = ' " . $polled . " ' " ;
$update .= " , `poll_prev` = ' " . $port [ 'poll_time' ] . " ' " ;
$update .= " , `poll_period` = ' " . $polled_period . " ' " ;
2010-07-20 13:02:54 +00:00
#echo("\n32bit - In: ".$this_port['ifInOctets']." Out: ".$this_port['ifOutOctets']);
#echo("\n64bit - In: ".$this_port['ifHCInOctets']." Out: ".$this_port['ifHCOutOctets']."\n");
2009-10-28 13:49:37 +00:00
/// Copy ifHC[In|Out]Octets values to non-HC if they exist
2010-01-19 02:23:36 +00:00
if ( $this_port [ 'ifHCInOctets' ] > 0 && is_numeric ( $this_port [ 'ifHCInOctets' ]) && $this_port [ 'ifHCOutOctets' ] > 0 && is_numeric ( $this_port [ 'ifHCOutOctets' ])) {
2009-10-28 13:49:37 +00:00
echo ( " HC " );
$this_port [ 'ifInOctets' ] = $this_port [ 'ifHCInOctets' ];
$this_port [ 'ifOutOctets' ] = $this_port [ 'ifHCOutOctets' ];
}
/// Update IF-MIB data
foreach ( $data_oids as $oid ) {
2009-12-16 13:48:05 +00:00
if ( $port [ $oid ] != $this_port [ $oid ] && ! isset ( $this_port [ $oid ])) {
$update .= " , ` $oid ` = NULL " ;
2010-06-20 21:37:05 +00:00
log_event ( $oid . " : " . $port [ $oid ] . " -> NULL " , $device [ 'device_id' ], 'interface' , $port [ 'interface_id' ]);
2009-12-16 13:48:05 +00:00
if ( $debug ) { echo ( $oid . " : " . $port [ $oid ] . " -> NULL " ); } else { echo ( $oid . " " ); }
} elseif ( $port [ $oid ] != $this_port [ $oid ] ) {
2010-02-21 11:58:07 +00:00
$update .= " , ` $oid ` = ' " . mres ( $this_port [ $oid ]) . " ' " ;
2010-06-20 21:37:05 +00:00
log_event ( $oid . " : " . $port [ $oid ] . " -> " . $this_port [ $oid ], $device [ 'device_id' ], 'interface' , $port [ 'interface_id' ]);
2009-12-16 13:48:05 +00:00
if ( $debug ) { echo ( $oid . " : " . $port [ $oid ] . " -> " . $this_port [ $oid ] . " " ); } else { echo ( $oid . " " ); }
2009-10-28 13:49:37 +00:00
}
}
2010-05-03 16:20:32 +00:00
/// Parse description (usually ifAlias) if config option set
if ( isset ( $config [ 'port_descr_parser' ]))
{
$port_attribs = array ( 'type' , 'descr' , 'circuit' , 'speed' , 'notes' );
include ( $config [ 'port_descr_parser' ]);
foreach ( $port_attribs as $attrib ) {
$attrib_key = " port_descr_ " . $attrib ;
if ( $port_ifAlias [ $attrib ])
{
if ( $port_ifAlias [ $attrib ] != $port [ $attrib_key ])
{
$update .= " , ` " . $attrib_key . " ` = ' " . $port_ifAlias [ $attrib ] . " ' " ;
2010-06-20 21:37:05 +00:00
log_event ( $attrib . " : " . $port [ $attrib_key ] . " -> " . $port_ifAlias [ $attrib ], $device [ 'device_id' ], 'interface' , $port [ 'interface_id' ]);
2010-05-03 16:20:32 +00:00
}
}
}
}
/// Ende parse ifAlias
2009-10-28 13:49:37 +00:00
/// Update IF-MIB metrics
foreach ( $stat_oids_db as $oid ) {
$update .= " , ` $oid ` = ' " . $this_port [ $oid ] . " ' " ;
$update .= " , ` " . $oid . " _prev` = ' " . $port [ $oid ] . " ' " ;
$oid_prev = $oid . " _prev " ;
if ( $port [ $oid ]) {
$oid_diff = $this_port [ $oid ] - $port [ $oid ];
$oid_rate = $oid_diff / $polled_period ;
2010-06-12 18:14:59 +00:00
if ( $oid_rate < 0 ) { $oid_rate = " 0 " ; }
2009-10-28 13:49:37 +00:00
$update .= " , ` " . $oid . " _rate` = ' " . $oid_rate . " ' " ;
$update .= " , ` " . $oid . " _delta` = ' " . $oid_diff . " ' " ;
2010-06-12 18:14:59 +00:00
if ( $debug ) { echo ( " \n $oid ( $oid_diff B) $oid_rate Bps $polled_period secs \n " );}
2009-10-28 13:49:37 +00:00
}
}
/// Update RRDs
2010-02-05 22:10:06 +00:00
$rrdfile = $host_rrd . " / " . safename ( $port [ 'ifIndex' ] . " .rrd " );
2009-10-28 13:49:37 +00:00
if ( ! is_file ( $rrdfile )) {
$woo = shell_exec ( $config [ 'rrdtool' ] . " create $rrdfile -s 300 \
DS : INOCTETS : DERIVE : 600 : 0 : 12500000000 \
DS : OUTOCTETS : DERIVE : 600 : 0 : 12500000000 \
DS : INERRORS : DERIVE : 600 : 0 : 12500000000 \
DS : OUTERRORS : DERIVE : 600 : 0 : 12500000000 \
DS : INUCASTPKTS : DERIVE : 600 : 0 : 12500000000 \
DS : OUTUCASTPKTS : DERIVE : 600 : 0 : 12500000000 \
DS : INNUCASTPKTS : DERIVE : 600 : 0 : 12500000000 \
DS : OUTNUCASTPKTS : DERIVE : 600 : 0 : 12500000000 \
RRA : AVERAGE : 0.5 : 1 : 600 \
RRA : AVERAGE : 0.5 : 6 : 700 \
RRA : AVERAGE : 0.5 : 24 : 775 \
RRA : AVERAGE : 0.5 : 288 : 797 \
RRA : MAX : 0.5 : 1 : 600 \
RRA : MAX : 0.5 : 6 : 700 \
RRA : MAX : 0.5 : 24 : 775 \
RRA : MAX : 0.5 : 288 : 797 " );
}
2010-07-04 18:23:25 +00:00
$ifx_rrd = $host_rrd . " /ifx- " . safename ( $port [ 'ifIndex' ] . " .rrd " );
$ifx_rrd_cmd = $config [ 'rrdtool' ] . " create $ifx_rrd -s 300 \
DS : InBroadcastPkts : DERIVE : 600 : 0 : 12500000000 \
DS : OutBroadcastPkts : DERIVE : 600 : 0 : 12500000000 \
DS : InMulticastPkts : DERIVE : 600 : 0 : 12500000000 \
DS : OutMulticastPkts : DERIVE : 600 : 0 : 12500000000 \
RRA : AVERAGE : 0.5 : 1 : 600 \
RRA : AVERAGE : 0.5 : 6 : 700 \
RRA : AVERAGE : 0.5 : 24 : 775 \
RRA : AVERAGE : 0.5 : 288 : 797 \
RRA : MAX : 0.5 : 1 : 600 \
RRA : MAX : 0.5 : 6 : 700 \
RRA : MAX : 0.5 : 24 : 775 \
RRA : MAX : 0.5 : 288 : 797 " ;
2009-10-28 13:49:37 +00:00
foreach ( $stat_oids as $oid ) { /// Copy values from array to global variables and force numeric.
$$oid = $this_port [ $oid ];
2010-01-28 03:01:45 +00:00
if ( ! is_numeric ( $$oid )) { $$oid = " 0 " ; }
2009-10-28 13:49:37 +00:00
}
2010-07-04 18:23:25 +00:00
$if_rrd_update = " $polled : $ifInOctets : $ifOutOctets : $ifInErrors : $ifOutErrors : $ifInUcastPkts : $ifOutUcastPkts : $ifInNUcastPkts : $ifOutNUcastPkts " ;
$ret = rrdtool_update ( " $rrdfile " , $if_rrd_update );
2010-07-21 15:14:02 +00:00
if ( $config [ 'enable_ports_Xbcmc' ] && $config [ 'os' ][ $device [ 'os' ]][ 'ifXmcbc' ]) {
2010-07-04 18:23:25 +00:00
if ( ! is_file ( $ifx_rrd )) { shell_exec ( $ifx_rrd_cmd ); }
$ifx_rrd_update = " $polled : $ifHCInBroadcastPkts : $ifHCOutBroadcastPkts : $ifHCInMulticastPkts : $ifHCOutMulticastPkts " ;
$ret = rrdtool_update ( $ifx_rrd , $ifx_rrd_update );
}
2009-10-28 13:49:37 +00:00
/// End Update IF-MIB
/// Update PAgP
if ( $this_port [ 'pagpOperationMode' ]) {
foreach ( $pagp_oids as $oid ) { // Loop the OIDs
if ( $this_port [ $oid ] != $port [ $oid ] ) { // If data has changed, build a query
$update .= " , ` $oid ` = ' " . mres ( $this_port [ $oid ]) . " ' " ;
echo ( " PAgP " );
2010-06-20 21:37:05 +00:00
log_event ( " $oid -> " . $this_port [ $oid ], $device [ 'device_id' ], 'interface' , $port [ 'interface_id' ]);
2009-10-28 13:49:37 +00:00
}
}
}
// End Update PAgP
2009-11-24 23:58:56 +00:00
/// Do EtherLike-MIB
2009-12-29 01:03:46 +00:00
if ( $config [ 'enable_ports_etherlike' ]) { include ( " port-etherlike.inc.php " ); }
2010-07-18 08:20:30 +00:00
if ( $config [ 'enable_ports_adsl' ]) { include ( " port-adsl.inc.php " ); }
2009-11-24 23:58:56 +00:00
2009-12-16 13:48:05 +00:00
// Update MySQL
if ( $update ) {
2010-02-20 17:22:22 +00:00
$update_query = " UPDATE `ports` SET " . $update . " WHERE `interface_id` = ' " . $port [ 'interface_id' ] . " ' " ;
2009-10-28 13:49:37 +00:00
@ mysql_query ( $update_query ); $mysql ++ ;
if ( $debug ) { echo ( " \n MYSQL : [ $update_query ] " );}
2009-12-16 13:48:05 +00:00
}
// End Update MySQL
2009-10-28 13:49:37 +00:00
unset ( $update_query ); unset ( $update );
2010-02-25 20:40:35 +00:00
// Send alerts for interface flaps.
if ( $config [ 'warn' ][ 'ifdown' ] && ( $port [ 'ifOperStatus' ] != $this_port [ 'ifOperStatus' ])) {
if ( $this_port [ 'ifAlias' ]) { $falias = preg_replace ( '/^"/' , '' , $this_port [ 'ifAlias' ]); $falias = preg_replace ( '/"$/' , '' , $falias ); $full = $this_port [ 'ifDescr' ] . " ( " . $falias . " ) " ; } else { $full = $this_port [ 'ifDescr' ]; }
switch ( $this_port [ 'ifOperStatus' ]) {
case " up " :
2010-07-11 19:11:46 +00:00
notify ( $device , " Interface UP - " . $device [ 'hostname' ] . " - " . $full , " Device: " . $device [ 'hostname' ] . " \n Interface: " . $full . " \n Timestamp: " . date ( $config [ 'timestamp_format' ]));
2010-02-25 20:40:35 +00:00
break ;
case " down " :
2010-07-11 19:11:46 +00:00
notify ( $device , " Interface DOWN - " . $device [ 'hostname' ] . " - " . $full , " Device: " . $device [ 'hostname' ] . " \n Interface: " . $full . " \n Timestamp: " . date ( $config [ 'timestamp_format' ]));
2010-02-25 20:40:35 +00:00
break ;
}
}
2010-06-15 22:44:34 +00:00
} elseif ( $port [ 'ignore' ] == " 0 " ) {
2009-10-28 13:49:37 +00:00
echo ( " Port Deleted? " ); // Port missing from SNMP cache?
2010-06-15 22:44:34 +00:00
} else {
echo ( " Port Ignored. " );
}
2009-10-28 13:49:37 +00:00
echo ( " \n " );
}
?>