1<?php
2
3/**
4 * Observium
5 *
6 *   This file is part of Observium.
7 *
8 * @package    observium
9 * @subpackage discovery
10 * @author     Adam Armstrong <adama@observium.org>
11 * @copyright  (C) 2006-2013 Adam Armstrong, (C) 2013-2019 Observium Limited
12 *
13 */
14
15// Include all discovery modules by supported MIB
16
17$include_dir = "includes/discovery/processors";
18include("includes/include-dir-mib.inc.php");
19
20// Detect processors by simple MIB-based discovery :
21// FIXME - this should also be extended to understand multiple entries in a table, and take descr from an OID but this is all I need right now :)
22foreach (get_device_mibs_permitted($device) as $mib)
23{
24  if (is_array($config['mibs'][$mib]['processor']))
25  {
26    print_cli_data_field("$mib ");
27    foreach ($config['mibs'][$mib]['processor'] as $entry_name => $entry)
28    {
29      $entry['found'] = FALSE;
30
31      // Check duplicate processors by $valid['processor'] array
32      if (discovery_check_if_type_exist($GLOBALS['valid'], $entry, 'processor')) { continue 2; }
33
34      // Precision (scale)
35      $precision = 1;
36      if (isset($entry['scale']) && is_numeric($entry['scale']) && $entry['scale'] != 1)
37      {
38        // FIXME, currently we support only int precision, need convert all to float scale!
39        $precision = round(1 / $entry['scale'], 0);
40      }
41
42      if ($entry['type'] == 'table')
43      {
44
45        // Use the type as the table if no table is set.
46        if(!isset($entry['table'])) { $entry['table'] = $entry_name; }
47
48        $processors_array = snmpwalk_cache_oid($device, $entry['table'], array(), $mib, NULL, OBS_SNMP_ALL_NUMERIC_INDEX);
49        if ($entry['table_descr'])
50        {
51          // If descr in separate table with same indexes
52          $processors_array = snmpwalk_cache_oid($device, $entry['table_descr'], $processors_array, $mib, NULL, OBS_SNMP_ALL_NUMERIC_INDEX);
53        }
54        if (empty($entry['oid_num']))
55        {
56          // Use snmptranslate if oid_num not set
57          $entry['oid_num'] = snmp_translate($entry['oid'], $mib);
58        }
59
60        $i = 1; // Used in descr as $i++
61        $processors_count = count($processors_array);
62        foreach ($processors_array as $index => $processor)
63        {
64          $dot_index = '.' . $index;
65          $oid_num   = $entry['oid_num'] . $dot_index;
66
67          // Rewrite specific keys
68          $replace_array = array(
69            'index' => $index,    // Index in descr
70            'i'     => $i,        // i++ counter in descr
71          );
72
73          $descr = entity_descr_definition('processor', $entry, array_merge($replace_array, $processor), $processors_count);
74
75          $idle  = (isset($entry['idle']) && $entry['idle'] ? 1 : 0);
76
77          $usage = snmp_fix_numeric($processor[$entry['oid']]);
78          if (is_numeric($usage))
79          {
80            if (isset($entry['rename_rrd']))
81            {
82              $old_rrd = 'processor-'.$entry['rename_rrd'].'-'.$index;
83              $new_rrd = 'processor-'.$entry_name.'-'.$entry['table'] . $dot_index;
84              rename_rrd($device, $old_rrd, $new_rrd);
85              unset($old_rrd, $new_rrd);
86            }
87            discover_processor($valid['processor'], $device, $oid_num, $entry['oid'] . $dot_index, $entry_name, $descr, $precision, $usage, NULL, NULL, $idle);
88            $entry['found'] = TRUE;
89          }
90          $i++;
91        }
92      } else {
93        // Static processor
94        $index = 0; // FIXME. Need use same indexes style as in sensors
95
96        // Fetch description from oid if specified
97        $replace_array = array('index' => $index);
98        if (isset($entry['oid_descr']))
99        {
100          $replace_array[$entry['oid_descr']] = snmp_get_oid($device, $entry['oid_descr'], $mib);
101        }
102        $descr = entity_descr_definition('processor', $entry, $replace_array);
103
104        if (isset($entry['oid_count']) && $entry['oid_count'])
105        {
106          // Get processors count if exist for MIB
107          $processor_count = snmp_get_oid($device, $entry['oid_count'], $mib);
108          if ($processor_count > 1)
109          {
110            $descr .= ' x'.$processor_count;
111          }
112        }
113
114        if (empty($entry['oid_num']))
115        {
116          // Use snmptranslate if oid_num not set
117          $entry['oid_num'] = snmp_translate($entry['oid'], $mib);
118        }
119
120        // Idle
121        $idle  = (isset($entry['idle']) && $entry['idle'] ? 1 : 0);
122
123        $usage = snmp_fix_numeric(snmp_get_oid($device, $entry['oid'], $mib));
124
125        // If we have valid usage, discover the processor
126        if (is_numeric($usage) && $usage != '4294967295')
127        {
128          // Rename RRD if requested
129          if (isset($entry['rename_rrd']))
130          {
131            $old_rrd = 'processor-'.$entry['rename_rrd'];
132            $new_rrd = 'processor-'.$entry_name.'-'.$index;
133            rename_rrd($device, $old_rrd, $new_rrd);
134            unset($old_rrd, $new_rrd);
135          }
136          discover_processor($valid['processor'], $device, $entry['oid_num'], $index, $entry_name, $descr, $precision, $usage, NULL, NULL, $idle);
137          $entry['found'] = TRUE;
138        }
139      }
140      unset($processors_array, $processor, $dot_index, $descr, $i); // Clean up
141      if (isset($entry['stop_if_found']) && $entry['stop_if_found'] && $entry['found']) { break; } // Stop loop if processor found
142    }
143  }
144}
145
146// Remove processors which weren't redetected here
147foreach (dbFetchRows('SELECT * FROM `processors` WHERE `device_id` = ?', array($device['device_id'])) as $test_processor)
148{
149  $processor_index = $test_processor['processor_index'];
150  $processor_type  = $test_processor['processor_type'];
151  $processor_descr = $test_processor['processor_descr'];
152  print_debug($processor_index . " -> " . $processor_type);
153
154  if (!$valid['processor'][$processor_type][$processor_index])
155  {
156    $GLOBALS['module_stats'][$module]['deleted']++; //echo('-');
157    dbDelete('processors', '`processor_id` = ?', array($test_processor['processor_id']));
158    log_event("Processor removed: type ".$processor_type." index ".$processor_index." descr ". $processor_descr, $device, 'processor', $test_processor['processor_id']);
159  }
160  unset($processor_oid); unset($processor_type);
161}
162
163$GLOBALS['module_stats'][$module]['status'] = count($valid['processor']);
164if (OBS_DEBUG && $GLOBALS['module_stats'][$module]['status'])
165{
166  print_vars($valid['processor']);
167}
168
169// EOF
170