demo: simplify demoreload.sh
[racktables-contribs] / snmpgeneric.php
index 5dee0db..9fa9979 100644 (file)
@@ -14,6 +14,7 @@
  *             - ifxTable
  *             - ipAddrTable (ipv4 only)
  *             - ipAddressTable (ipv4 + ipv6)
+ *             - ipv6AddrAddress (ipv6)
  *
  *     Features:
  *             - update object attributes
  * TESTED on FreeBSD 9.0, nginx/1.0.12, php 5.3.10, NET-SNMP 5.7.1
  *     and RackTables <= 0.20.3
  *
- * (c)2015 Maik Ehinger <m.ehinger@ltur.de>
+ * (c)2015 Maik Ehinger <github138@t-online.de>
  */
 
 /****
  * INSTALL
  *     just place file in plugins directory
  *
+ *     Increase max_input_vars in php.ini if not all ports were added on one run.
+ *
  */
 
 /**
@@ -79,7 +82,7 @@
 /* RackTables Debug Mode */
 //$debug_mode=1;
 
-require_once('inc/snmp.php');
+require_once(realpath (dirname (__FILE__).'/../wwwroot').'/inc/snmp.php');
 
 $tab['object']['snmpgeneric'] = 'SNMP Generic sync';
 $tabhandler['object']['snmpgeneric'] = 'snmpgeneric_tabhandler';
@@ -121,7 +124,7 @@ $sg_ifType2oif_id = array(
         '33' => 1469,  /*  rs232 => RS-232 (DB-9) 681 */
         '34' => 1469,  /* para => virtual port 1469 */
         '53' => 1469,  /* propVirtual => virtual port 1469 */
-        '62' => 1195,  /* fastEther => 100BASE-FX 1195 */
+        '62' => 19,    /* fastEther => 100BASE-TX 19 */
        '131' => 1469,  /* tunnel => virtual port 1469 */
        '136' => 1469,  /* l3ipvlan => virtual port 1469 */
        '160' => 1469,  /* usb => virtual port 1469 */
@@ -340,6 +343,7 @@ function snmpgeneric_pf_enterasys(&$snmp, &$sysObjectID, $attr_id) {
 
                /* TODO SW type */
                //$attrs[4]['value'] = 'Enterasys'; /* SW type */
+               $attrs[4]['key'] = '0'; /* SW type dict key 0 = NOT SET*/
 
                /* set SW version only if not already set by entitymib */
                if(isset($attrs[5]['value']) && !empty($attrs[5]['value'])) {
@@ -381,7 +385,10 @@ function snmpgeneric_pf_catalyst(&$snmp, &$sysObjectID, $attr_id) {
                        $attrs[5]['value'] = $exact_release;
 
                        if (array_key_exists ($major_line, $ios_codes))
+                       {
                                $attrs[4]['value'] = $ios_codes[$major_line];
+                               $attrs[4]['key'] = $ios_codes[$major_line];
+                       }
 
                } /* sw type / version */
 
@@ -414,6 +421,9 @@ function snmpgeneric_pf_ciscoflash(&$snmp, &$sysObjectID, $attr_id) {
 
        $ciscoflash = $snmp->walk('1.3.6.1.4.1.9.9.10.1.1.2'); /* ciscoFlashDeviceTable */
 
+       if(!$ciscoflash)
+               return;
+
        $flash = array_keys($ciscoflash, 'flash');
 
        foreach($flash as $oid) {
@@ -475,6 +485,7 @@ function snmpgeneric_pf_hwtype(&$snmp, &$sysObjectID, $attr_id) {
 
                /* return array of attr_id => attr_value) */
                $attr['value'] = $value;
+               $attr['key'] = $value;
 
        } else {
                showNotice("HW type dict_key not set - Unknown OID");
@@ -546,6 +557,7 @@ function snmpgeneric_pf_swtype(&$snmp, &$sysObjectID, $attr_id) {
 
                /* set attr value */
                $attr['value'] = $value;
+               $attr['key'] = $value;
        //      unset($attr['uncheck']);
 
        }
@@ -643,6 +655,10 @@ function snmpgeneric_pf_entitymib(&$snmp, &$sysObjectID, $attr_id) {
                        $index = $matches[1];
 
                        $name = $snmp->get(".1.3.6.1.2.1.47.1.1.1.1.7.$index");
+
+                       if(!$name)
+                               continue;
+
                        $hardwarerev = $snmp->get(".1.3.6.1.2.1.47.1.1.1.1.8.$index");
                        $firmwarerev = $snmp->get(".1.3.6.1.2.1.47.1.1.1.1.9.$index");
                        $softwarerev = $snmp->get(".1.3.6.1.2.1.47.1.1.1.1.10.$index");
@@ -1321,6 +1337,9 @@ function snmpgeneric_list($object_id) {
 
                if(isset($object['attr'][$attr_id])) {
 
+                       if(array_key_exists('key',$object['attr'][$attr_id]))
+                               $attr['key'] = $object['attr'][$attr_id]['key'];
+
                        switch(TRUE) {
 
                                case isset($attr['pf']):
@@ -1386,11 +1405,19 @@ function snmpgeneric_list($object_id) {
 
        foreach($sysObjectID['attr'] as $attr_id => &$attr) {
 
+               $attr['id'] = $attr_id;
+
                if(isset($object['attr'][$attr_id]) && isset($attr['value'])) {
 
                        if($attr['value'] == $object['attr'][$attr_id]['value'])
                                $attr['uncheck'] = 'Current = new value';
 
+                       if(isset($attr['key']) && isset($object['attr'][$attr_id]['key']))
+                       {
+                               if($attr['key'] == $object['attr'][$attr_id]['key'])
+                                       $attr['uncheck'] = 'Current = new key';
+                       }
+
                        $value = $attr['value'];
 
                        $val_key = (isset($object['attr'][$attr_id]['key']) ? ' ('.$object['attr'][$attr_id]['key'].')' : '' );
@@ -1424,14 +1451,19 @@ function snmpgeneric_list($object_id) {
 
        echo '</table>';
 
+       $object['breed'] = sg_detectDeviceBreedByObject($sysObjectID);
+
+       if(!empty($object['breed']))
+               echo "Found Breed: ".$object['breed']."<br>";
+
        /* ports */
 
        /* get ports */
        amplifyCell($object);
 
-       /* set array key to port name */
+       /* set array key to lowercase port name */
        foreach($object['ports'] as $key => $values) {
-               $object['ports'][$values['name']] = $values;
+               $object['ports'][strtolower(shortenIfName($values['name'], $object['breed']))] = $values;
                unset($object['ports'][$key]);
        }
 
@@ -1446,7 +1478,7 @@ function snmpgeneric_list($object_id) {
 
                foreach($sysObjectID['port'] as $name => $port) {
 
-                       if(array_key_exists($name,$object['ports']))
+                       if(array_key_exists(strtolower($name),$object['ports']))
                                $disableport = TRUE;
                        else
                                $disableport = FALSE;
@@ -1507,6 +1539,9 @@ function snmpgeneric_list($object_id) {
 
        $ifsnmp = new ifSNMP($snmpdev);
 
+       // needed for shortenIfName()
+       $ifsnmp->object_breed = $object['breed'];
+
        /* ip spaces */
 
        $ipspace = NULL;
@@ -1533,9 +1568,12 @@ function snmpgeneric_list($object_id) {
 
                                case 'ipv6':
 
-                                       /* format ipaddr for ip6_parse */
-                                       $ipaddr =  preg_replace('/((..):(..))/','\\2\\3',$ipaddr);
-                                       $ipaddr =  preg_replace('/%.*$/','',$ipaddr);
+                                       if(ip_checkparse($ipaddr) === false)
+                                       {
+                                               /* format ipaddr for ip6_parse */
+                                               $ipaddr =  preg_replace('/((..):(..))/','\\2\\3',$ipaddr);
+                                               $ipaddr =  preg_replace('/%.*$/','',$ipaddr);
+                                       }
 
                                        if(ip_checkparse($ipaddr) === false)
                                                continue(2); // 2 because of switch
@@ -1563,7 +1601,7 @@ function snmpgeneric_list($object_id) {
                        if(empty($netid) && $netaddr != '::1' && $netaddr != '127.0.0.1' && $netaddr != '127.0.0.0' && $netaddr != '0.0.0.0' && !$linklocal) {
 
                                $netaddr .= "/$maskbits";
-                               $ipspace[$netaddr] = $addrtype;
+                               $ipspace[$netaddr] = array('addrtype' => $addrtype, 'checked' => ($maskbits > 0 ? true : false));
                        }
                }
                unset($ipaddr);
@@ -1580,11 +1618,11 @@ function snmpgeneric_list($object_id) {
                echo '<th>Type</th><th>prefix</th><th>name</th><th width=150 title="reserve network and router addresses">reserve network / router addresses</th></tr>';
 
                $i = 1;
-               foreach($ipspace as $prefix => $addrtype) {
+               foreach($ipspace as $prefix => $ipspace) {
 
                        $netcreatecheckbox = '<b style="background-color:#00ff00">'
                                .'<input class="ipspace" style="background-color:#00ff00" type="checkbox" name="netcreate['
-                               .$i.']" value="'.$addrtype.'" checked=\"checked\"></b>';
+                               .$i.']" value="'.$ipspace['addrtype'].'"'.($ipspace['checked'] ? ' checked=\"checked\"' : '').'></b>';
 
                        $netprefixfield = '<input type="text" size=50 name="netprefix['.$i.']" value="'.$prefix.'">';
 
@@ -1592,7 +1630,7 @@ function snmpgeneric_list($object_id) {
 
                        $netreservecheckbox = '<input type="checkbox" name="netreserve['.$i.']" checked="checked">';
 
-                       echo "<tr><td>$netcreatecheckbox</td><td style=\"color:#888888\">$addrtype</td><td>$netprefixfield</td><td>$netnamefield</td><td>$netreservecheckbox</td></tr>";
+                       echo "<tr><td>$netcreatecheckbox</td><td style=\"color:#888888\">${ipspace['addrtype']}</td><td>$netprefixfield</td><td>$netnamefield</td><td>$netreservecheckbox</td></tr>";
 
                        $i++;
                }
@@ -1613,11 +1651,12 @@ function snmpgeneric_list($object_id) {
        $ifsnmp->printifInfoTableHeader("<th>add ip</th><th>add port</th><th>upd label</th><th title=\"update mac\">upd mac</th><td>upd port type</th><th>porttypeid</th><th>comment</th></tr>");
 
        echo '<tr><td colspan="11"></td>
-               <td><input type="checkbox" id="ipaddr" onclick="setchecked(this.id)">IPv4<br>
-               <input type="checkbox" id="ipv6addr" onclick="setchecked(this.id)">IPv6</td>
+               <td><input type="checkbox" id="ipaddr" onclick="setchecked(this.id);" checked="checked">IPv4<br>
+               <input type="checkbox" id="ipv6addr" onclick="setchecked(this.id);" checked="checked">IPv6</td>
                <td><input type="checkbox" id="ports" onclick="setchecked(this.id)"></td>
-               <td><input type="checkbox" id="label" onclick="setchecked(this.id)" checked="checked"></td>
-               <td><input type="checkbox" id="mac" onclick="setchecked(this.id)" checked="checked"></td></tr>';
+               <td><input type="checkbox" id="label" onclick="setchecked(this.id);" checked="checked"></td>
+               <td><input type="checkbox" id="mac" onclick="setchecked(this.id);" checked="checked"></td>
+               <td><input type="checkbox" id="porttype" onclick="setchecked(this.id);"></td></tr>';
 
        foreach($ifsnmp as $if) {
 
@@ -1759,9 +1798,12 @@ function snmpgeneric_list($object_id) {
                                        case 'ipv6':
                                                $inputname = 'ipv6';
 
-                                               /* format ipaddr for ip6_parse */
-                                               $ipaddr =  preg_replace('/((..):(..))/','\\2\\3',$ipaddr);
-                                               $ipaddr =  preg_replace('/%.*$/','',$ipaddr);
+                                               if(ip_checkparse($ipaddr) === false)
+                                               {
+                                                       /* format ipaddr for ip6_parse */
+                                                       $ipaddr =  preg_replace('/((..):(..))/','\\2\\3',$ipaddr);
+                                                       $ipaddr =  preg_replace('/%.*$/','',$ipaddr);
+                                               }
 
                                                if(ip_checkparse($ipaddr) === false)
                                                        continue(2); // 2 because of switch
@@ -1875,7 +1917,7 @@ function snmpgeneric_list($object_id) {
                if($updateporttype)
                        $updateporttypecheckbox = '<b style="background-color:#00ff00;">'
                                        .'<input class="porttype" style="background-color:#00ff00;" type="checkbox" name="updateporttype['.$if.']" value="'
-                                       .$port_info['id'].($updateporttype ? '" checked="checked"' : '' ).'></b>';
+                                       .$port_info['id'].'"></b>';
 
                $porttypeidselect = getNiftySelect($newporttypeoptions, $selectoptions, $porttypeid);
 
@@ -2267,18 +2309,6 @@ function sg_checkL2Address ($address)
         return $row;
 }
 
-/* returns oi_id and name */
-function getPortOIOptions()
-{
-        $result = usePreparedSelectBlade
-        (
-               'SELECT dict_key,dict_value from Dictionary where chapter_id = 2',
-                array ()
-        );
-        $row = $result->fetchAll(PDO::FETCH_GROUP|PDO::FETCH_UNIQUE|PDO::FETCH_COLUMN);
-        return $row;
-}
-
 function sg_checkObjectNameUniqueness ($name, $type_id, $object_id = 0)
 {
        // Some object types do not need unique names
@@ -2300,6 +2330,21 @@ function sg_checkObjectNameUniqueness ($name, $type_id, $object_id = 0)
                return true;
 }
 
+function sg_detectDeviceBreedByObject($object)
+{
+       global $breed_by_swcode, $breed_by_hwcode, $breed_by_mgmtcode;
+
+       foreach ($object['attr'] as $record)
+       {
+               if ($record['id'] == 4 and array_key_exists ($record['key'], $breed_by_swcode))
+                       return $breed_by_swcode[$record['key']];
+               elseif ($record['id'] == 2 and array_key_exists ($record['key'], $breed_by_hwcode))
+                       return $breed_by_hwcode[$record['key']];
+               elseif ($record['id'] == 30 and array_key_exists ($record['key'], $breed_by_mgmtcode))
+                       return $breed_by_mgmtcode[$record['key']];
+       }
+       return '';
+}
 
 /* ------------------------------------------------------- */
 class SNMPgeneric {
@@ -2354,6 +2399,7 @@ class SNMPgeneric {
                                snmp_set_quick_print($value);
                                break;
                        case 'oid_output_format':
+                               /* needs php >= 5.2.0 */
                                snmp_set_oid_output_format($value);
                                break;
                        case 'enum_print':
@@ -2515,20 +2561,14 @@ class mySNMP extends SNMPgeneric implements Iterator {
 
                parent::__construct($version, $host, $community);
 
-               //snmp_set_valueretrieval(SNMP_VALUE_LIBRARY);
-
                /* Return values without SNMP type hint */
-               //snmp_set_valueretrieval(SNMP_VALUE_PLAIN);
                $this->valueretrieval = SNMP_VALUE_PLAIN;
 
-               /* needs php >= 5.2.0 */
-       //      snmp_set_oid_output_format(SNMP_OID_OUTPUT_FULL);
-
-       //      snmp_set_quick_print(1);
-
        } /* __construct */
 
        function init() {
+
+               $this->oid_output_format = SNMP_OID_OUTPUT_FULL;
                /* .iso.org.dod.internet.mgmt.mib-2.system */
                $this->system = $this->walk(".1.3.6.1.2.1.1");
 
@@ -2623,7 +2663,7 @@ class mySNMP extends SNMPgeneric implements Iterator {
 
                        $trace = debug_backtrace();
                        trigger_error(
-                                       'Undefinierte Eigenschaft für __call(): ' . $name .
+                                       'Undefinierte Eigenschaft fuer __call(): ' . $name .
                                        ' in ' . $trace[0]['file'] .
                                        ' Zeile ' . $trace[0]['line'],
                                        E_USER_NOTICE);
@@ -2679,6 +2719,8 @@ class ifSNMP implements Iterator {
 
        private $interfaceserror = TRUE;
 
+       public $object_breed = NULL;
+
        function __construct(&$snmpdevice) {
                $this->snmpdevice = $snmpdevice;
 
@@ -2813,6 +2855,59 @@ class ifSNMP implements Iterator {
        //              sg_var_dump_html($ipAddressPrefix);
                } /* ipaddressifindex */
 
+               /* ipv6 MIB  */
+               /* overwrites ipv6 from ipaddresstable */
+               $ipv6interfaces = $this->snmpdevice->get('ipv6Interfaces.0');
+
+               if($ipv6interfaces)
+               {
+                       echo"Found $ipv6interfaces ipv6 interfaces<br>";
+
+                       $ipv6addraddress =  $this->snmpdevice->walk('ipv6AddrAddress');
+
+                       if(!empty($ipv6addraddress)) {
+                               $ipv6addrpfxlength =  $this->snmpdevice->walk('ipv6AddrPfxLength');
+                       //      $ipv6addrtype =  $this->snmpdevice->walk('ipv6AddrType'); /* 1 stateless, 2 stateful, 3 unknown */
+
+                               reset($ipv6addrpfxlength);
+                               //reset($ipv6addrtype);
+
+                               foreach($ipv6addraddress as $oid => $addr_bin) {
+
+                                       $addr = ip_format($addr_bin);
+
+                                       //$type = current($ipv6addrtype);
+                                       //next($ipv6addrtype);
+
+                                       if(!preg_match('/.*(ipv6).*\.([0-9]+)\..*$/',$oid, $matches))
+                                               continue;
+
+                                       $ifindex =  array_search($matches[2],$this->ifTable['ifIndex']);
+
+                                       if($ifindex === false)
+                                               continue;
+
+                                       $maskbits = current($ipv6addrpfxlength);
+                                       next($ipv6addrpfxlength);
+
+                                       $range = constructIPRange($addr_bin, $maskbits);
+
+                                       $net = ip_format($range['ip_bin']);
+                                       $bcast = NULL;
+
+                                       $this->ifTable['ipaddress'][$ifindex][$addr] = array(
+                                                                               'addrtype' => $matches[1],
+                                                                               'maskbits' => $maskbits,
+                                                                               'net' => $net,
+                                                                               'bcast' => $bcast,
+                                                                       //      'type' => ($type == 1 ? "stateless" : $type == 2 ? "statefull" : "unknown" )
+                                                                               );
+                               }
+                               unset($oid);
+                               unset($value);
+
+                       } /* ipv6addraddress */
+               } /* ipv6interfaces */
        }
 
        function printifInfoTableHeader($suffix = "") {
@@ -2882,7 +2977,7 @@ class ifSNMP implements Iterator {
                                        if($key == 'ifName') {
                                                /* create textfield set to ifDescr */
                                                $formfield = '<input type="text" size="8" name="'.$key.'['.$ifIndex.']" value="'
-                                                               .$this->ifDescr($ifIndex).'">';
+                                                               .strtolower(shortenIfName($this->ifDescr($ifIndex), $this->object_breed)).'">';
                                                $textfield = TRUE;
                                        }
 
@@ -2941,6 +3036,12 @@ class ifSNMP implements Iterator {
 
        }
 
+       function ifName($index) {
+               if(isset($this->ifTable['ifName'][$index-1])) {
+                       return strtolower(shortenIfName($this->ifTable['ifName'][$index-1], $this->object_breed));
+               }
+
+       }
        function &__get($name) {
 
                switch($name) {
@@ -2956,7 +3057,7 @@ class ifSNMP implements Iterator {
                $trace = debug_backtrace();
 
                trigger_error(
-                       'Undefinierte Eigenschaft für __get(): ' . $name .
+                       'Undefinierte Eigenschaft fuer __get(): ' . $name .
                        ' in ' . $trace[0]['file'] .
                        ' Zeile ' . $trace[0]['line'],
                        E_USER_NOTICE);
@@ -2980,7 +3081,7 @@ class ifSNMP implements Iterator {
                        $trace = debug_backtrace();
 
                        trigger_error(
-                               'Undefinierte Methode für __call(): ' . $name .
+                               'Undefinierte Methode für __call(): ' . $name .
                                ' in ' . $trace[0]['file'] .
                                ' Zeile ' . $trace[0]['line'],
                                E_USER_NOTICE);
@@ -3025,4 +3126,3 @@ function sg_var_dump_html(&$var, $text = '') {
        var_dump($var);
        echo "\n---------------------END Var Dump - $text -----------------------</pre>";
 }
-?>