Guest User

sensor-fix-for-arista

a guest
Aug 22nd, 2017
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.06 KB | None | 0 0
  1. <?php
  2. echo ' ENTITY-SENSOR: ';
  3. echo 'Caching OIDs:';
  4. if (!is_array($entity_array)) {
  5.     $entity_array = array();
  6.     echo ' entPhysicalDescr';
  7.     $entity_array = snmpwalk_cache_multi_oid($device, 'entPhysicalDescr', $entity_array, 'CISCO-ENTITY-SENSOR-MIB');
  8.     echo ' entPhysicalName';
  9.     $entity_array = snmpwalk_cache_multi_oid($device, 'entPhysicalName', $entity_array, 'CISCO-ENTITY-SENSOR-MIB');
  10. }
  11. $oids = array();
  12. echo ' entPhySensorType';
  13. $oids = snmpwalk_cache_multi_oid($device, 'entPhySensorType', $oids, 'ENTITY-SENSOR-MIB');
  14. echo ' entPhySensorScale';
  15. $oids = snmpwalk_cache_multi_oid($device, 'entPhySensorScale', $oids, 'ENTITY-SENSOR-MIB');
  16. echo ' entPhySensorPrecision';
  17. $oids = snmpwalk_cache_multi_oid($device, 'entPhySensorPrecision', $oids, 'ENTITY-SENSOR-MIB');
  18. echo ' entPhySensorValue';
  19. $oids = snmpwalk_cache_multi_oid($device, 'entPhySensorValue', $oids, 'ENTITY-SENSOR-MIB');
  20. if ($device['os'] === 'arista_eos') {
  21.     require 'includes/discovery/sensors/misc/arista-eos-limits.inc.php';
  22. }
  23. $entitysensor['voltsDC']   = 'voltage';
  24. $entitysensor['voltsAC']   = 'voltage';
  25. $entitysensor['amperes']   = 'current';
  26. $entitysensor['watts']     = 'power';
  27. $entitysensor['hertz']     = 'freq';
  28. $entitysensor['percentRH'] = 'humidity';
  29. $entitysensor['rpm']       = 'fanspeed';
  30. $entitysensor['celsius']   = 'temperature';
  31. $entitysensor['dBm']       = 'dbm';
  32. if (is_array($oids)) {
  33.     foreach ($oids as $index => $entry) {
  34.         // Fix for Cisco ASR920, 15.5(2)S
  35.         if ($entry['entPhySensorType'] == 'other' && str_contains($entity_array[$index]['entPhysicalName'], array('Rx Power Sensor', 'Tx Power Sensor'))) {
  36.             $entitysensor['other'] = 'dbm';
  37.         }
  38.         if ($entitysensor[$entry['entPhySensorType']] && is_numeric($entry['entPhySensorValue']) && is_numeric($index)) {
  39.             $entPhysicalIndex = $index;
  40.             $oid              = '.1.3.6.1.2.1.99.1.1.1.4.'.$index;
  41.             $current          = $entry['entPhySensorValue'];
  42.             // ENTITY-SENSOR-MIB::entPhySensorUnitsDisplay.11 = STRING: "C"
  43.             $descr = ucwords($entity_array[$index]['entPhysicalName']);
  44.             // if ($descr || $device['os'] == "iosxr")
  45.             if ($descr) {
  46.                 $descr = rewrite_entity_descr($descr);
  47.             } else {
  48.                 $descr = $entity_array[$index]['entPhysicalDescr'];
  49.                 $descr = rewrite_entity_descr($descr);
  50.             }
  51.             $valid_sensor = check_entity_sensor($descr, $device);
  52.             $type = $entitysensor[$entry['entPhySensorType']];
  53.             // FIXME this stuff is foul
  54.             if ($entry['entPhySensorScale'] == 'nano') {
  55.                 $divisor    = '1000000000';
  56.                 $multiplier = '1';
  57.             }
  58.             if ($entry['entPhySensorScale'] == 'micro') {
  59.                 $divisor    = '1000000';
  60.                 $multiplier = '1';
  61.             }
  62.             if ($entry['entPhySensorScale'] == 'milli') {
  63.                 $divisor    = '1000';
  64.                 $multiplier = '1';
  65.             }
  66.             if ($entry['entPhySensorScale'] == 'units') {
  67.                 $divisor    = '1';
  68.                 $multiplier = '1';
  69.             }
  70.             if ($entry['entPhySensorScale'] == 'kilo') {
  71.                 $divisor    = '1';
  72.                 $multiplier = '1000';
  73.             }
  74.             if ($entry['entPhySensorScale'] == 'mega') {
  75.                 $divisor    = '1';
  76.                 $multiplier = '1000000';
  77.             }
  78.             if ($entry['entPhySensorScale'] == 'giga') {
  79.                 $divisor    = '1';
  80.                 $multiplier = '1000000000';
  81.             }
  82.             if ($entry['entPhySensorScale'] == 'yocto') {
  83.                 $divisor    = '1';
  84.                 $multiplier = '1';
  85.             }
  86.             if (is_numeric($entry['entPhySensorPrecision']) && $entry['entPhySensorPrecision'] > '0') {
  87.                 $divisor = $divisor.str_pad('', $entry['entPhySensorPrecision'], '0');
  88.             }
  89.             $current = ($current * $multiplier / $divisor);
  90.             if ($type == 'temperature') {
  91.                 if ($current > '200') {
  92.                     $valid_sensor = false;
  93.                 }
  94.                 $descr = preg_replace('/[T|t]emperature[|s]/', '', $descr);
  95.             }
  96.             if ($device['os'] == 'rittal-lcp') {
  97.                 if ($type == 'voltage') {
  98.                     $divisor = 1000;
  99.                 }
  100.                 if ($descr == 'Temperature.Value') {
  101.                     $divisor = 1000;
  102.                 }
  103.                 if ($descr == 'System.Temperature.Value') {
  104.                     $divisor = 1000;
  105.                 }
  106.                 if ($type == 'humidity' && $current == '0') {
  107.                     $valid_sensor = false;
  108.                 }
  109.             }
  110.             // echo($descr . "|" . $index . "|" .$current . "|" . $multiplier . "|" . $divisor ."|" . $entry['entPhySensorScale'] . "|" . $entry['entPhySensorPrecision'] . "\n");
  111.             if ($current == '-127' || ($device['os'] == 'asa' && str_contains($device['hardware'], 'sc'))) {
  112.                 $valid_sensor = false;
  113.             }
  114.             if ($valid_sensor && dbFetchCell("SELECT COUNT(*) FROM `sensors` WHERE device_id = ? AND `sensor_class` = ? AND `sensor_type` = 'cisco-entity-sensor' AND `sensor_index` = ?", array($device['device_id'], $type, $index)) == '0') {
  115.                 // Check to make sure we've not already seen this sensor via cisco's entity sensor mib
  116.                 if ($type == "power" && $device['os'] == "arista_eos" && preg_match("/DOM (R|T)x Power/i", $descr)) {
  117.                     $type = "dbm";
  118.                     $current = round(10 * log10($entry['entPhySensorValue'] / 10000), 3);
  119.                     $multiplier = 1;
  120.                     $divisor = 1;
  121.                 }
  122.                 $low_limit      = null;
  123.                 $low_warn_limit = null;
  124.                 $warn_limit     = null;
  125.                 $high_limit     = null;
  126.                 if ($device['os'] === 'arista_eos') {
  127.                     if ($entry['entPhySensorScale'] == 'milli' &&  $entry['entPhySensorType'] == 'amperes') {
  128.                         $divisor    = '100';
  129.                         $multiplier = '1';
  130.                     }
  131.                     if ($entry['aristaEntSensorThresholdLowWarning'] != '-1000000000' && $entry['aristaEntSensorThresholdLowWarning'] != '0') {
  132.                         if ($entry['entPhySensorScale'] == 'milli' &&  $entry['entPhySensorType'] == 'watts') {
  133.                             $temp_low_warn_limit = $entry['aristaEntSensorThresholdLowWarning'] / 10000;
  134.                             $low_warn_limit = round(10 * log10($temp_low_warn_limit),2);
  135.                         }
  136.                         else {
  137.                             $low_warn_limit = $entry['aristaEntSensorThresholdLowWarning'] / $divisor;
  138.                         }
  139.                     }
  140.                     if ($entry['aristaEntSensorThresholdLowCritical'] != '-1000000000' && $entry['aristaEntSensorThresholdLowCritical'] != '0') {
  141.                         if ($entry['entPhySensorScale'] == 'milli' &&  $entry['entPhySensorType'] == 'watts') {
  142.                             $temp_low_limit = $entry['aristaEntSensorThresholdLowCritical'] / 10000;
  143.                             $low_limit = round(10 * log10($temp_low_limit),2);
  144.                         }
  145.                         else {
  146.                             $low_limit = $entry['aristaEntSensorThresholdLowCritical'] / $divisor;
  147.                         }
  148.                     }
  149.                     if ($entry['aristaEntSensorThresholdHighWarning'] != '1000000000' && $entry['aristaEntSensorThresholdHighWarning'] != '0') {
  150.                         if ($entry['entPhySensorScale'] == 'milli' &&  $entry['entPhySensorType'] == 'watts') {
  151.                             $temp_warn_limit = $entry['aristaEntSensorThresholdHighWarning'] / 10000;
  152.                             $warn_limit = round(10 * log10($temp_warn_limit),2);
  153.                         }
  154.                         else {
  155.                             $warn_limit = $entry['aristaEntSensorThresholdHighWarning'] / $divisor;
  156.                         }
  157.                     }
  158.                     if ($entry['aristaEntSensorThresholdHighCritical'] != '1000000000' && $entry['aristaEntSensorThresholdHighCritical'] != '0') {
  159.                         if ($entry['entPhySensorScale'] == 'milli' &&  $entry['entPhySensorType'] == 'watts') {
  160.                             $temp_high_limit = $entry['aristaEntSensorThresholdHighCritical'] / 10000;
  161.                             $high_limit = round(10 * log10($temp_high_limit),2);
  162.                         }
  163.                         else {
  164.                             $high_limit = $entry['aristaEntSensorThresholdHighCritical'] / $divisor;
  165.                         }
  166.                     }
  167.                 }
  168.                 discover_sensor($valid['sensor'], $type, $device, $oid, $index, 'entity-sensor', $descr, $divisor, $multiplier, $low_limit, $low_warn_limit, $warn_limit, $high_limit, $current, 'snmp', $entPhysicalIndex, $entry['entSensorMeasuredEntity']);
  169.             }
  170.         }//end if
  171.     }//end foreach
  172.     unset(
  173.         $entity_array
  174.     );
  175. }//end if
  176. echo "\n";
Add Comment
Please, Sign In to add comment