Commit version 24.12.13800

This commit is contained in:
2025-01-06 17:35:06 -05:00
parent b7f6a79c2c
commit 55d9218816
6133 changed files with 4239740 additions and 1374287 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,323 @@
<?php
/**
* Observium
*
* This file is part of Observium.
*
* @package observium
* @subpackage entities
* @copyright (C) Adam Armstrong
*
*/
function discover_processor_definition($device, $mib, $entry) {
// Just append mib name to definition entry, for simple pass to external functions
if (empty($entry['mib'])) {
$entry['mib'] = $mib;
}
// Check that types listed in skip_if_valid_exist have already been found
if (discovery_check_if_type_exist($entry, 'processor')) {
echo '!]';
return FALSE;
}
// Check array requirements list
if (discovery_check_requires_pre($device, $entry, 'processor')) {
echo '!]';
return FALSE;
}
// Units, see: LANCOM-GS2310PPLUS-MIB
$unit = $entry['unit'] ?? NULL;
// CLEANME, Compat, remove when converted
if ($entry['type'] === 'table' && !isset($entry['table'])) {
$entry['table'] = $entry['object'];
}
// elseif (!isset($entry['rename_rrd']) && OBSERVIUM_REV < 13500) {
// // Add rename by default
// $entry['rename_rrd'] = $entry['object'] . '-%index%';
// }
// Fetch table or Oids
$table_oids = [
'oid', 'oid_descr', 'oid_scale', 'oid_precision', 'oid_count',
//'oid_limit_low', 'oid_limit_low_warn', 'oid_limit_high_warn', 'oid_limit_high', 'oid_limit_warn',
//'oid_limit_nominal', 'oid_limit_delta_warn', 'oid_limit_delta', 'oid_limit_scale',
'oid_extra', //'oid_unit', 'oid_entPhysicalIndex'
];
$processors_array = discover_fetch_oids($device, $mib, $entry, $table_oids);
if (empty($entry['oid_num'])) {
// Use snmptranslate if oid_num not set
$entry['oid_num'] = snmp_translate($entry['oid'], $mib);
}
$found = FALSE;
$i = 1; // Used in descr as $i++
$processors_count = count($processors_array);
foreach ($processors_array as $index => $processor) {
$dot_index = '.' . $index;
$oid_num = $entry['oid_num'] . $dot_index;
// Check valid exist with entity tags
if (discovery_check_if_type_exist($entry, 'processor', $processor)) {
continue;
}
// Rewrite specific keys
$index_tags = entity_index_tags($index, $i);
$options = [ 'i' => $i, 'count' => $processors_count, 'oid' => $entry['oid'] ];
if ($processors_count === 1 && isset($entry['indexes'])) {
// Compat with old static definitions
$options['indexes'] = count($entry['indexes']);
}
$descr = entity_descr_definition('processor', $entry, array_merge($index_tags, $processor), $processors_count);
if (isset($entry['oid_count'])) {
// See F5-PLATFORM-STATS-MIB
if (str_contains($entry['oid_count'], '.')) {
$oid = array_tag_replace($index_tags, $entry['oid_count']);
// Get processors count if exist for MIB
$processor_count = snmp_get_oid($device, $oid, $mib);
} else {
$processor_count = $processor[$entry['oid_count']];
}
if (is_numeric($processor_count) && $processor_count > 1) {
$descr .= ' x' . $processor_count;
}
$options['count'] = $processor_count;
}
// Options
if (!empty($unit)) {
$options['unit'] = $unit;
}
if (isset($entry['idle'])) {
$options['idle'] = $entry['idle'] ? 1 : 0;
}
// FIXME. entPhysicalIndex and hrDeviceIndex
// Rename old (converted) RRDs to definition format
if (isset($entry['rename_rrd'])) {
$options['rename_rrd'] = $entry['rename_rrd'];
}
// Precision (scale)
// FIXME, currently we support only int precision, need convert all to float scale!
$scale = entity_scale_definition($device, $entry, $processor, 'processor');
$precision = $scale !== 1 ? round(float_div(1, $scale)) : 1;
$usage = snmp_fix_numeric($processor[$entry['oid']], $unit);
if (discovery_check_value_valid($device, $usage, $entry, 'processor')) {
discover_processor_ng($device, $mib, $entry['object'], $oid_num, $index, $descr, $precision, $usage, $options);
$found = TRUE;
}
$i++;
}
return $found;
}
// Compatibility wrapper!
function discover_processor(&$valid, $device, $processor_oid, $processor_index, $processor_type, $processor_descr, $processor_precision = 1, $value = NULL, $entPhysicalIndex = NULL, $hrDeviceIndex = NULL, $processor_returns_idle = 0) {
$options = [ 'idle' => $processor_returns_idle ? 1 : 0 ];
if (!safe_empty($processor_type)) {
$options['processor_type'] = $processor_type;
}
if (!safe_empty($entPhysicalIndex)) {
$options['entPhysicalIndex'] = $entPhysicalIndex;
}
if (!safe_empty($hrDeviceIndex)) {
$options['hrDeviceIndex'] = $hrDeviceIndex;
}
return discover_processor_ng($device, '', '', $processor_oid, $processor_index, $processor_descr, $processor_precision, $value, $options);
}
function discover_processor_ng($device, $processor_mib, $processor_object, $processor_oid, $processor_index,
$processor_descr, $processor_precision = 1, $value = NULL, $options = []) {
// Old: processor_type
// New: mib-object
if ($discovery_ng = empty($options['processor_type'])) {
if (!empty($processor_object)) {
$processor_type = $processor_object;
}
if (!empty($processor_mib)) {
$processor_type = $processor_mib . '-' . $processor_type;
}
} else {
$processor_type = $options['processor_type'];
}
// Split unit need extend type for multiple entities (see NEWTEC-DEVICE-MIB definition)
if (!empty($options['unit']) && str_starts_with($options['unit'], 'split_cpu')) {
$processor_type .= '-' . $options['unit'];
}
// Idle (100-proc)?
$processor_returns_idle = isset($options['idle']) && $options['idle'];
print_debug($device['device_id'] . " -> $processor_oid, $processor_index, $processor_type, $processor_descr, $processor_precision, $value");
// Check processor ignore filters
if (entity_descr_check($processor_descr, 'processor')) {
return FALSE;
}
// Skip discovery processor if value not numeric or null(default)
if ($value !== NULL) {
$value = snmp_fix_numeric($value);
}
if (!(is_numeric($value) || $value === NULL)) {
print_debug("Skipped by not numeric value: $value, $processor_descr ");
return FALSE;
}
// Main params
$params = [ 'processor_index', 'processor_mib', 'processor_object', 'processor_oid', 'processor_type', 'processor_descr', 'processor_precision' ];
$params_opt = [ 'entPhysicalIndex' => 'entPhysicalIndex', 'hrDeviceIndex' => 'hrDeviceIndex', 'idle' => 'processor_returns_idle' ];
$processor_db = dbFetchRow("SELECT * FROM `processors` WHERE `device_id` = ? AND `processor_index` = ? AND `processor_type` = ?", [ $device['device_id'], $processor_index, $processor_type ]);
// Compat with old discovery (update instead delete/add)
if ($discovery_ng && !$processor_db) {
if (isset($options['indexes'])) {
// Old static converted to indexes
$processor_db = dbFetchRow("SELECT * FROM `processors` WHERE `device_id` = ? AND `processor_index` = ? AND `processor_oid` = ? AND `processor_mib` IS NULL",
[ $device['device_id'], $processor_index, $processor_oid ]);
} else {
$old_index1 = $processor_object . '.' . $processor_index;
$old_index2 = $options['oid'] . '.' . $processor_index;
$processor_db = dbFetchRow("SELECT * FROM `processors` WHERE `device_id` = ? AND `processor_index` IN (?, ?, ?) AND `processor_type` = ? AND `processor_mib` IS NULL",
[ $device['device_id'], $processor_index, $old_index1, $old_index2, $processor_object ]);
}
if (!isset($options['rename_rrd']) && isset($processor_db['processor_index'])) {
// Derp old table indexes..
if ($processor_db['processor_index'] === $old_index1) {
$options['rename_rrd'] = $processor_object . '-' . $old_index1;
} elseif ($processor_db['processor_index'] === $old_index2) {
$options['rename_rrd'] = $processor_object . '-' . $old_index2;
}
}
}
if (!isset($processor_db['processor_id'])) {
$insert = [ 'device_id' => $device['device_id'] ];
if (!$processor_precision) {
$processor_precision = 1;
}
foreach ($params as $param) {
$insert[$param] = $$param ?? [ 'NULL' ];
}
foreach ($params_opt as $opt => $param) {
if (isset($options[$opt])) {
$insert[$param] = $options[$opt];
}
}
if ($processor_precision != 1) {
$value = round(float_div($value, $processor_precision), 2);
}
// The OID returns idle value, so we subtract it from 100.
if ($processor_returns_idle) {
$value = 100 - $value;
}
$insert['processor_usage'] = $value;
$id = dbInsert($insert, 'processors');
$GLOBALS['module_stats']['processors']['added']++;
log_event("Processor added: index $processor_index, type $processor_type, descr $processor_descr", $device, 'processor', $id);
} else {
$update = [];
foreach ($params as $param) {
if ($$param != $processor_db[$param]) {
$update[$param] = $$param ?? [ 'NULL' ];
}
}
foreach ($params_opt as $opt => $param) {
if (isset($options[$opt]) && $options[$opt] != $processor_db[$param]) {
print_debug_vars($options);
print_debug_vars($processor_db);
$update[$param] = $options[$opt];
}
}
// Skip WMI processor description update, this is done in poller
if (isset($update['processor_descr']) && $processor_type === 'hr' &&
($update['processor_descr'] === 'Unknown Processor Type' || $update['processor_descr'] === 'Intel') &&
is_module_enabled($device, 'wmi', 'poller')) {
unset($update['processor_descr']);
}
if (count($update)) {
dbUpdate($update, 'processors', '`processor_id` = ?', [ $processor_db['processor_id'] ]);
$GLOBALS['module_stats']['processors']['updated']++;
log_event("Processor updated: index $processor_index, type $processor_type, descr $processor_descr", $device, 'processor', $processor_db['processor_id']);
} else {
$GLOBALS['module_stats']['processors']['unchanged']++;
}
$id = $processor_db['processor_id'];
}
// Rename old (converted) RRDs to definition format
if (isset($options['rename_rrd'])) {
$rrd_tags = [
'index' => $processor_index,
'type' => $processor_type,
'mib' => $processor_mib,
'object' => $processor_object,
'oid' => $processor_object,
'count' => $options['count'],
'i' => $options['i'],
// for rrd
'processor_index' => $processor_index,
'processor_type' => $processor_type,
'processor_mib' => $processor_mib,
'processor_object' => $processor_object,
];
$options['rename_rrd'] = array_tag_replace($rrd_tags, $options['rename_rrd']);
$old_rrd = 'processor-' . $options['rename_rrd'];
$new_rrd = get_processor_rrd($device, $rrd_tags);
rename_rrd($device, $old_rrd, $new_rrd);
}
$GLOBALS['valid']['processor'][$processor_type][$processor_index] = 1;
return $id;
}
function get_processor_rrd($device, $processor, $full = TRUE) {
$index = $processor['processor_index'];
if (!empty($processor['processor_mib']) && !empty($processor['processor_object'])) {
// for discover_processor_ng(), note here is just status index
if ($processor['processor_type'] === $processor['processor_mib'] . '-' . $processor['processor_object']) {
// FIXME. Use old style of rrd naming, because rename rrd impossible for remote rrd
$rrd_file = $processor['processor_object'] . "-" . $index;
} else {
$rrd_file = $processor['processor_mib'] . "-" . $processor['processor_object'] . "-" . $index;
}
} else {
// for discover_processor(), note index == "%object%.%index%"
$rrd_file = $processor['processor_type'] . "-" . $index;
}
if ($full) {
// Prepend processor
return 'processor-' . $rrd_file . '.rrd';
}
return $rrd_file;
}
// EOF

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,39 @@
<?php
/**
* Observium
*
* This file is part of Observium.
*
* @package observium
* @subpackage entities
* @copyright (C) Adam Armstrong
*
*/
/**
* Get named SLA index.
*
* @param array $sla
* @return string
*/
function get_sla_index($sla) {
$index = $sla['sla_index'];
if (!in_array($sla['sla_mib'], [ 'CISCO-RTTMON-MIB', 'HPICF-IPSLA-MIB', 'TWAMP-MIB' ])) {
// Use 'owner.index' as index for all except Cisco and HPE
$index = $sla['sla_owner'] . '.' . $index;
}
return $index;
}
function get_sla_rrd_index($sla) {
$rrd_index = strtolower($sla['sla_mib']) . '-' . $sla['sla_index'];
if ($sla['sla_owner']) {
// Add owner name to rrd file if not empty
$rrd_index .= '-' . $sla['sla_owner'];
}
return $rrd_index;
}
// EOF

File diff suppressed because it is too large Load Diff

View File

@ -6,344 +6,366 @@
*
* @package observium
* @subpackage entities
* @copyright (C) 2006-2013 Adam Armstrong, (C) 2013-2022 Observium Limited
* @copyright (C) Adam Armstrong
*
*/
function discover_storage_definition($device, $mib, $entry, $object) {
$entry['found'] = FALSE;
function discover_storage_definition($device, $mib, $entry, $object)
{
$entry['found'] = FALSE;
$entry['object'] = $object;
echo($object . ' [');
// Just append mib name to definition entry, for simple pass to external functions
if (empty($entry['mib'])) {
$entry['mib'] = $mib;
}
$entry['object'] = $object;
echo($object . ' [');
// Check that types listed in skip_if_valid_exist have already been found
if (discovery_check_if_type_exist($entry, 'storage')) { echo '!]'; return; }
// Check array requirements list
if (discovery_check_requires_pre($device, $entry, 'storage')) { echo '!]'; return; }
// oid_*_hc and oid_*_high/oid_*_low used with storage_hc flag
$table_oids = [ 'oid_total', 'oid_total_hc', 'oid_total_high', 'oid_total_low',
'oid_used', 'oid_used_hc', 'oid_used_high', 'oid_used_low',
'oid_free', 'oid_free_hc', 'oid_free_high', 'oid_free_low',
'oid_perc', 'oid_descr', 'oid_scale', 'oid_unit',
'oid_type', 'oid_online', 'oid_extra',
//'oid_limit_low', 'oid_limit_low_warn', 'oid_limit_high_warn', 'oid_limit_high',
//'oid_limit_nominal', 'oid_limit_delta_warn', 'oid_limit_delta', 'oid_limit_scale'
];
$storage_array = discover_fetch_oids($device, $mib, $entry, $table_oids);
// FIXME - generify description generation code and just pass it template and OID array.
$i = 1; // Used in descr as %i%
$storage_count = count($storage_array);
foreach ($storage_array as $index => $storage_entry) {
$options = [];
//$oid_num = $entry['oid_num'] . '.' . $index;
// Storage Type
if (isset($entry['oid_type']) && $storage_entry[$entry['oid_type']]) {
$storage_entry['type'] = $storage_entry[$entry['oid_type']];
} elseif (isset($entry['type'])) {
$storage_entry['type'] = $entry['type'];
} else {
// Compat (incorrect)
$storage_entry['type'] = $object;
// Just append mib name to definition entry, for simple pass to external functions
if (empty($entry['mib'])) {
$entry['mib'] = $mib;
}
$options['storage_type'] = $storage_entry['type'];
// Generate storage description
$storage_entry['i'] = $i;
$storage_entry['index'] = $index;
foreach (explode('.', $index) as $k => $i) {
$storage_entry['index'.$k] = $i; // Index parts
// Check that types listed in skip_if_valid_exist have already been found
if (discovery_check_if_type_exist($entry, 'storage')) {
echo '!]';
return;
}
$descr = entity_descr_definition('storage', $entry, $storage_entry, $storage_count);
// Check valid exist with entity tags
if (discovery_check_if_type_exist($entry, 'storage', $storage_entry)) { continue; }
// Check array requirements list
if (discovery_check_requires($device, $entry, $storage_entry, 'storage')) { continue; }
if (discovery_check_requires_pre($device, $entry, 'storage')) {
echo '!]';
return;
}
// Init
$used = NULL;
$total = NULL;
$free = NULL;
$perc = NULL;
$hc = isset($entry['hc']) && $entry['hc'];
// Convert strings '3.40 TB' to value
// See QNAP NAS-MIB or HIK-DEVICE-MIB
$unit = !isset($entry['unit']) ? NULL : $entry['unit'];
// oid_*_hc and oid_*_high/oid_*_low used with storage_hc flag
$table_oids = ['oid_total', 'oid_total_hc', 'oid_total_high', 'oid_total_low',
'oid_used', 'oid_used_hc', 'oid_used_high', 'oid_used_low',
'oid_free', 'oid_free_hc', 'oid_free_high', 'oid_free_low',
'oid_perc', 'oid_descr', 'oid_scale', 'oid_unit',
'oid_type', 'oid_online', 'oid_extra',
//'oid_limit_low', 'oid_limit_low_warn', 'oid_limit_high_warn', 'oid_limit_high',
//'oid_limit_nominal', 'oid_limit_delta_warn', 'oid_limit_delta', 'oid_limit_scale'
];
$storage_array = discover_fetch_oids($device, $mib, $entry, $table_oids);
// FIXME - generify description generation code and just pass it template and OID array.
$i = 1; // Used in descr as %i%
$storage_count = count($storage_array);
foreach ($storage_array as $index => $storage_entry) {
$options = [];
//$oid_num = $entry['oid_num'] . '.' . $index;
// Storage Type
if (isset($entry['oid_type']) && $storage_entry[$entry['oid_type']]) {
$storage_entry['type'] = $storage_entry[$entry['oid_type']];
} elseif (isset($entry['type'])) {
$storage_entry['type'] = $entry['type'];
} else {
// Compat (incorrect)
$storage_entry['type'] = $object;
}
$options['storage_type'] = $storage_entry['type'];
// Generate storage description
$storage_entry = array_merge($storage_entry, entity_index_tags($index, $i));
$descr = entity_descr_definition('storage', $entry, $storage_entry, $storage_count);
// Check valid exist with entity tags
if (discovery_check_if_type_exist($entry, 'storage', $storage_entry)) {
continue;
}
// Check array requirements list
if (discovery_check_requires($device, $entry, $storage_entry, 'storage')) {
continue;
}
// Init
$used = NULL;
$total = NULL;
$free = NULL;
$perc = NULL;
$hc = isset($entry['hc']) && $entry['hc'];
// Convert strings '3.40 TB' to value
// See QNAP NAS-MIB or HIK-DEVICE-MIB
$unit = $entry['unit'] ?? NULL;
// Fetch used, total, free and percentage values, if OIDs are defined for them
if (!safe_empty($entry['total'])) {
// Prefer hardcoded total over SNMP OIDs
$total = $entry['total'];
} else {
if (isset($entry['oid_total_high'], $entry['oid_total_low'])) {
$high = snmp_fix_numeric($storage_entry[$entry['oid_total_high']]);
$low = snmp_fix_numeric($storage_entry[$entry['oid_total_low']]);
if ($total = snmp_size64_high_low($high, $low)) {
$hc = TRUE; // set HC flag
}
}
if (!is_numeric($total) && isset($entry['oid_total_hc']) &&
$total = snmp_fix_numeric($storage_entry[$entry['oid_total_hc']], $unit)) {
$hc = TRUE; // set HC flag
}
if (!is_numeric($total) && isset($entry['oid_total'])) {
$total = snmp_fix_numeric($storage_entry[$entry['oid_total']], $unit);
}
}
if (isset($entry['oid_used_high'], $entry['oid_used_low'])) {
$high = snmp_fix_numeric($storage_entry[$entry['oid_used_high']]);
$low = snmp_fix_numeric($storage_entry[$entry['oid_used_low']]);
$used = snmp_size64_high_low($high, $low);
if (is_numeric($used)) {
$hc = TRUE; // set HC flag
}
}
if (!is_numeric($used) && isset($entry['oid_used_hc'])) {
$used = snmp_fix_numeric($storage_entry[$entry['oid_used_hc']], $unit);
if (is_numeric($used)) {
$hc = TRUE; // set HC flag
}
}
if (!is_numeric($used) && isset($entry['oid_used'])) {
$used = snmp_fix_numeric($storage_entry[$entry['oid_used']], $unit);
}
if (isset($entry['oid_free_high'], $entry['oid_free_low'])) {
$high = snmp_fix_numeric($storage_entry[$entry['oid_free_high']]);
$low = snmp_fix_numeric($storage_entry[$entry['oid_free_low']]);
$free = snmp_size64_high_low($high, $low);
if (is_numeric($free)) {
$hc = TRUE; // set HC flag
}
}
if (!is_numeric($free) && isset($entry['oid_free_hc'])) {
$free = snmp_fix_numeric($storage_entry[$entry['oid_free_hc']], $unit);
if (is_numeric($free)) {
$hc = TRUE; // set HC flag
}
}
if (!is_numeric($free) && isset($entry['oid_free'])) {
$free = snmp_fix_numeric($storage_entry[$entry['oid_free']], $unit);
}
if (isset($entry['oid_perc'])) {
$perc = snmp_fix_numeric($storage_entry[$entry['oid_perc']]);
}
// Scale
$scale = entity_scale_definition($device, $entry, $storage_entry);
// HC
if ($hc) {
$options['storage_hc'] = 1;
}
// Oper status / Ignore (see NIMBLE-MIB)
if (isset($entry['oid_online'], $storage_entry[$entry['oid_online']])) {
$options['storage_ignore'] = get_var_false($storage_entry[$entry['oid_online']]);
}
// Extrapolate all values from the ones we have.
$storage = calculate_mempool_properties($scale, $used, $total, $free, $perc, $entry);
print_debug_vars([$scale, $used, $total, $free, $perc, $options]);
print_debug_vars($storage_entry);
print_debug_vars($storage);
//print_debug_vars([ is_numeric($storage['used']), is_numeric($storage['total']) ]);
// If we have valid used and total, discover the storage
$entry['found'] = discover_storage_ng($device, $mib, $object, $index, $descr, $scale, $storage, $options);
$i++;
}
echo '] ';
}
function discover_storage_ng($device, $storage_mib, $storage_object, $storage_index, $storage_descr, $storage_units, $storage, $options = [])
{
global $valid;
// options && limits
$option = 'storage_hc';
$$option = (isset($options[$option]) && $options[$option]) ? 1 : 0;
$option = 'storage_ignore';
$$option = (isset($options[$option]) && $options[$option]) ? 1 : 0;
if (isset($options['limit_high'])) {
$storage_crit_limit = $options['limit_high'];
}
if (isset($options['limit_high_warn'])) {
$storage_warn_limit = $options['limit_high_warn'];
}
// FIXME. Ignore 0 storage size?
$storage_size = $storage['total'];
$storage_used = $storage['used'];
$storage_free = $storage['free'];
$storage_perc = $storage['perc'];
$storage_type = $options['storage_type'] ?? $storage_object;
print_debug($device['device_id'] . " -> $storage_index, $storage_object, $storage_mib, $storage_descr, $storage_units, $storage_size, $storage_used, $storage_hc");
if (!is_numeric($storage['total']) || !is_numeric($storage['used'])) {
print_debug("Skipped by not numeric storage values.");
return FALSE;
}
if (isset($storage['valid']) && !$storage['valid']) {
print_debug("Skipped by empty storage Size [$storage_size] or invalid Percent [$storage_perc] values.");
return FALSE;
}
// Check storage ignore filters
if (entity_descr_check($storage_descr, 'storage')) {
return FALSE;
}
// Search duplicates for same mib/descr
if (in_array($storage_descr, array_values((array)$valid['storage'][$storage_mib]))) {
print_debug("Skipped by already exist: $storage_descr ");
return FALSE;
}
$params = ['storage_index', 'storage_mib', 'storage_object', 'storage_type', 'storage_descr',
'storage_hc', 'storage_ignore', 'storage_units', 'storage_crit_limit', 'storage_warn_limit'];
// This is changeable params, not required for update
$params_state = ['storage_size', 'storage_used', 'storage_free', 'storage_perc'];
$device_id = $device['device_id'];
$storage_db = dbFetchRow("SELECT * FROM `storage` WHERE `device_id` = ? AND `storage_index` = ? AND `storage_mib` = ?", [$device_id, $storage_index, $storage_mib]);
if (!isset($storage_db['storage_id'])) {
$update = ['device_id' => $device_id];
foreach (array_merge($params, $params_state) as $param) {
$update[$param] = $$param ?? [ 'NULL' ];
}
$id = dbInsert($update, 'storage');
$GLOBALS['module_stats']['storage']['added']++;
log_event("Storage added: index $storage_index, mib $storage_mib, descr $storage_descr", $device, 'storage', $id);
} else {
$update = [];
foreach ($params as $param) {
if ($$param != $storage_db[$param]) {
$update[$param] = $$param ?? [ 'NULL' ];
}
}
if (count($update)) {
dbUpdate($update, 'storage', '`storage_id` = ?', [$storage_db['storage_id']]);
$GLOBALS['module_stats']['storage']['updated']++;
log_event("Storage updated: index $storage_index, mib $storage_mib, descr $storage_descr", $device, 'storage', $storage_db['storage_id']);
} else {
$GLOBALS['module_stats']['storage']['unchanged']++;
}
}
print_debug_vars($update);
if ($storage_ignore) {
$GLOBALS['module_stats']['storage']['ignored']++;
}
$valid['storage'][$storage_mib][$storage_index] = $storage_descr;
return TRUE;
}
function poll_storage_definition($device, $entry, &$storage, $cache_storage)
{
// Fetch used, total, free and percentage values, if OIDs are defined for them
if (!safe_empty($entry['total'])) {
// Prefer hardcoded total over SNMP OIDs
$total = $entry['total'];
// Prefer hardcoded total over SNMP OIDs
$total = $entry['total'];
} else {
if (isset($entry['oid_total_high'], $entry['oid_total_low'])) {
$high = snmp_fix_numeric($storage_entry[$entry['oid_total_high']]);
$low = snmp_fix_numeric($storage_entry[$entry['oid_total_low']]);
if ($total = snmp_size64_high_low($high, $low)) {
$hc = TRUE; // set HC flag
$total = get_storage_value($device, 'total', $entry, $storage, $cache_storage);
}
$used = get_storage_value($device, 'used', $entry, $storage, $cache_storage);
$free = get_storage_value($device, 'free', $entry, $storage, $cache_storage);
$perc = get_storage_value($device, 'perc', $entry, $storage, $cache_storage);
if (isset($entry['oid_online'])) {
$mib = $storage['storage_mib'];
$index = $storage['storage_index'];
if (isset($cache_storage[$mib][$index][$entry['oid_online']])) {
$value = $cache_storage[$mib][$index][$entry['oid_online']];
} else {
$value = snmp_get_oid($device, $entry['oid_online'] . '.' . $index, $mib);
}
// FIXME, probably need additional field for storages like OperStatus up/down
$ignore = get_var_false($value) ? 1 : 0;
if ($storage['storage_ignore'] != $ignore) {
force_discovery($device, 'storage');
}
}
if (!is_numeric($total) && isset($entry['oid_total_hc']) &&
$total = snmp_fix_numeric($storage_entry[$entry['oid_total_hc']], $unit)) {
$hc = TRUE; // set HC flag
}
if (!is_numeric($total) && isset($entry['oid_total'])) {
$total = snmp_fix_numeric($storage_entry[$entry['oid_total']], $unit);
}
}
if (isset($entry['oid_used_high'], $entry['oid_used_low'])) {
$high = snmp_fix_numeric($storage_entry[$entry['oid_used_high']]);
$low = snmp_fix_numeric($storage_entry[$entry['oid_used_low']]);
$used = snmp_size64_high_low($high, $low);
if (is_numeric($used)) {
$hc = TRUE; // set HC flag
}
}
if (!is_numeric($used) && isset($entry['oid_used_hc'])) {
$used = snmp_fix_numeric($storage_entry[$entry['oid_used_hc']], $unit);
if (is_numeric($used)) {
$hc = TRUE; // set HC flag
}
}
if (!is_numeric($used) && isset($entry['oid_used'])) {
$used = snmp_fix_numeric($storage_entry[$entry['oid_used']], $unit);
}
// Merge calculated used/total/free/perc array keys into $storage variable (with additional options)
$storage = array_merge($storage, calculate_mempool_properties($storage['storage_units'], $used, $total, $free, $perc, $entry));
$storage['size'] = $storage['total'];
if (isset($entry['oid_free_high'], $entry['oid_free_low'])) {
$high = snmp_fix_numeric($storage_entry[$entry['oid_free_high']]);
$low = snmp_fix_numeric($storage_entry[$entry['oid_free_low']]);
$free = snmp_size64_high_low($high, $low);
if (is_numeric($free)) {
$hc = TRUE; // set HC flag
}
}
if (!is_numeric($free) && isset($entry['oid_free_hc'])) {
$free = snmp_fix_numeric($storage_entry[$entry['oid_free_hc']], $unit);
if (is_numeric($free)) {
$hc = TRUE; // set HC flag
}
}
if (!is_numeric($free) && isset($entry['oid_free'])) {
$free = snmp_fix_numeric($storage_entry[$entry['oid_free']], $unit);
}
if (isset($entry['oid_perc'])) {
$perc = snmp_fix_numeric($storage_entry[$entry['oid_perc']]);
}
// Scale
$scale = entity_scale_definition($device, $entry, $storage_entry);
// HC
if ($hc) {
$options['storage_hc'] = 1;
}
// Oper status / Ignore (see NIMBLE-MIB)
if (isset($entry['oid_online'], $storage_entry[$entry['oid_online']])) {
$options['storage_ignore'] = get_var_false($storage_entry[$entry['oid_online']]);
}
// Extrapolate all values from the ones we have.
$storage = calculate_mempool_properties($scale, $used, $total, $free, $perc, $entry);
print_debug_vars([ $scale, $used, $total, $free, $perc, $options ]);
print_debug_vars($storage_entry);
print_debug_vars($storage);
//print_debug_vars([ is_numeric($storage['used']), is_numeric($storage['total']) ]);
// If we have valid used and total, discover the storage
$entry['found'] = discover_storage_ng($device, $mib, $object, $index, $descr, $scale, $storage, $options);
$i++;
}
echo '] ';
}
function discover_storage_ng($device, $storage_mib, $storage_object, $storage_index, $storage_descr, $storage_units, $storage, $options = []) {
global $valid;
// options && limits
$option = 'storage_hc';
$$option = (isset($options[$option]) && $options[$option]) ? 1 : 0;
$option = 'storage_ignore';
$$option = (isset($options[$option]) && $options[$option]) ? 1 : 0;
if (isset($options['limit_high'])) { $storage_crit_limit = $options['limit_high']; }
if (isset($options['limit_high_warn'])) { $storage_warn_limit = $options['limit_high_warn']; }
// FIXME. Ignore 0 storage size?
$storage_size = $storage['total'];
$storage_used = $storage['used'];
$storage_free = $storage['free'];
$storage_perc = $storage['perc'];
$storage_type = isset($options['storage_type']) ? $options['storage_type'] : $object;
print_debug($device['device_id']." -> $storage_index, $storage_object, $storage_mib, $storage_descr, $storage_units, $storage_size, $storage_used, $storage_hc");
if (!is_numeric($storage['total']) || !is_numeric($storage['used'])) {
print_debug("Skipped by not numeric storage values.");
return FALSE;
}
if (isset($storage['valid']) && !$storage['valid']) {
print_debug("Skipped by empty storage Size [$storage_size] or invalid Percent [$storage_perc] values.");
return FALSE;
}
// Check storage ignore filters
if (entity_descr_check($storage_descr, 'storage')) { return FALSE; }
// Search duplicates for same mib/descr
if (in_array($storage_descr, array_values((array)$valid['storage'][$storage_mib]))) {
print_debug("Skipped by already exist: $storage_descr ");
return FALSE;
}
$params = [ 'storage_index', 'storage_mib', 'storage_object', 'storage_type', 'storage_descr',
'storage_hc', 'storage_ignore', 'storage_units', 'storage_crit_limit', 'storage_warn_limit' ];
// This is changeable params, not required for update
$params_state = [ 'storage_size', 'storage_used', 'storage_free', 'storage_perc' ];
$device_id = $device['device_id'];
$storage_db = dbFetchRow("SELECT * FROM `storage` WHERE `device_id` = ? AND `storage_index` = ? AND `storage_mib` = ?", [ $device_id, $storage_index, $storage_mib ]);
if (!isset($storage_db['storage_id'])) {
$update = [ 'device_id' => $device_id ];
foreach (array_merge($params, $params_state) as $param) {
$update[$param] = ($$param === NULL ? [ 'NULL' ] : $$param);
}
$id = dbInsert($update, 'storage');
$GLOBALS['module_stats']['storage']['added']++; //echo('+');
log_event("Storage added: index $storage_index, mib $storage_mib, descr $storage_descr", $device, 'storage', $id);
} else {
$update = [];
foreach ($params as $param) {
if ($$param != $storage_db[$param] ) { $update[$param] = ($$param === NULL ? [ 'NULL' ] : $$param); }
}
if (count($update)) {
//if (isset($update['storage_descr']))
//{
// // Rename storage rrds, because its filename based on description
// $old_rrd = $config['rrd_dir'] . '/' . $device['hostname'] . '/' . safename('storage-' . $storage_db['storage_mib'] . '-' . $storage_db['storage_descr'] . '.rrd');
// $new_rrd = $config['rrd_dir'] . '/' . $device['hostname'] . '/' . safename('storage-' . $storage_db['storage_mib'] . '-' . $storage_descr . '.rrd');
// if (is_file($old_rrd) && !is_file($new_rrd)) { rename($old_rrd, $new_rrd); print_warning("Moved RRD"); }
//}
dbUpdate($update, 'storage', '`storage_id` = ?', array($storage_db['storage_id']));
$GLOBALS['module_stats']['storage']['updated']++; //echo('U');
log_event("Storage updated: index $storage_index, mib $storage_mib, descr $storage_descr", $device, 'storage', $storage_db['storage_id']);
} else {
$GLOBALS['module_stats']['storage']['unchanged']++; //echo('.');
}
}
print_debug_vars($update);
if ($storage_ignore) {
$GLOBALS['module_stats']['storage']['ignored']++;
}
$valid['storage'][$storage_mib][$storage_index] = $storage_descr;
return TRUE;
}
function poll_storage_definition($device, $entry, &$storage, $cache_storage) {
// Fetch used, total, free and percentage values, if OIDs are defined for them
if (!safe_empty($entry['total'])) {
// Prefer hardcoded total over SNMP OIDs
$total = $entry['total'];
} else {
$total = get_storage_value($device, 'total', $entry, $storage, $cache_storage);
}
$used = get_storage_value($device, 'used', $entry, $storage, $cache_storage);
$free = get_storage_value($device, 'free', $entry, $storage, $cache_storage);
$perc = get_storage_value($device, 'perc', $entry, $storage, $cache_storage);
if (isset($entry['oid_online'])) {
function get_storage_value($device, $param, $entry, $storage, $cache_storage = [])
{
$mib = $storage['storage_mib'];
$index = $storage['storage_index'];
if (isset($cache_storage[$mib][$index][$entry['oid_online']])) {
$value = $cache_storage[$mib][$index][$entry['oid_online']];
} else {
$value = snmp_get_oid($device, $entry['oid_online'].'.'.$index, $mib);
$hc = $storage['storage_hc'];
// Convert strings '3.40 TB' to value
// See QNAP NAS-MIB or HIK-DEVICE-MIB
$unit = ($param !== 'perc' && isset($entry['unit'])) ? $entry['unit'] : NULL;
$value = NULL;
if (isset($entry['oid_' . $param . '_high'], $entry['oid_' . $param . '_low'])) {
// High+Low set of values
if (isset($cache_storage[$mib][$index][$entry['oid_' . $param . '_high']])) {
// Cached
$high = $cache_storage[$mib][$index][$entry['oid_' . $param . '_high']];
$low = $cache_storage[$mib][$index][$entry['oid_' . $param . '_low']];
} elseif ($hc) {
if (isset($entry['oid_' . $param . '_high_num'])) {
$high = snmp_get_oid($device, $entry['oid_' . $param . '_high_num'] . '.' . $index);
} elseif (isset($entry['oid_' . $param . '_high'])) {
$high = snmp_get_oid($device, $entry['oid_' . $param . '_high'] . '.' . $index, $mib);
}
if (isset($entry['oid_' . $param . '_low_num'])) {
$low = snmp_get_oid($device, $entry['oid_' . $param . '_low_num'] . '.' . $index);
} elseif (isset($entry['oid_' . $param . '_low'])) {
$low = snmp_get_oid($device, $entry['oid_' . $param . '_low'] . '.' . $index, $mib);
}
}
$high = snmp_fix_numeric($high);
$low = snmp_fix_numeric($low);
$value = snmp_size64_high_low($high, $low);
}
// FIXME, probably need additional field for storages like OperStatus up/down
$ignore = get_var_false($value) ? 1 : 0;
if ($storage['storage_ignore'] != $ignore) {
force_discovery($device, 'storage');
if ($hc && isset($entry['oid_' . $param . '_hc']) && !is_numeric($value)) {
// Common HC value
if (isset($cache_storage[$mib][$index][$entry['oid_' . $param . '_hc']])) {
$value = snmp_fix_numeric($cache_storage[$mib][$index][$entry['oid_' . $param . '_hc']], $unit);
} else {
if (isset($entry['oid_' . $param . '_hc_num'])) {
$value = snmp_fix_numeric(snmp_get_oid($device, $entry['oid_' . $param . '_hc_num'] . '.' . $index), $unit);
} elseif (isset($entry['oid_' . $param . '_hc'])) {
$value = snmp_fix_numeric(snmp_get_oid($device, $entry['oid_' . $param . '_hc'] . '.' . $index, $mib), $unit);
}
}
}
if (!is_numeric($value) && isset($entry['oid_' . $param])) {
// Common value
if (isset($cache_storage[$mib][$index][$entry['oid_' . $param]])) {
$value = snmp_fix_numeric($cache_storage[$mib][$index][$entry['oid_' . $param]], $unit);
} else {
if (isset($entry['oid_' . $param . '_num'])) {
$value = snmp_fix_numeric(snmp_get_oid($device, $entry['oid_' . $param . '_num'] . '.' . $index), $unit);
} elseif (isset($entry['oid_' . $param])) {
$value = snmp_fix_numeric(snmp_get_oid($device, $entry['oid_' . $param] . '.' . $index, $mib), $unit);
}
}
}
}
// Merge calculated used/total/free/perc array keys into $storage variable (with additional options)
$storage = array_merge($storage, calculate_mempool_properties($storage['storage_units'], $used, $total, $free, $perc, $entry));
$storage['size'] = $storage['total'];
}
function get_storage_value($device, $param, $entry, $storage, $cache_storage = []) {
$mib = $storage['storage_mib'];
$index = $storage['storage_index'];
$hc = $storage['storage_hc'];
// Convert strings '3.40 TB' to value
// See QNAP NAS-MIB or HIK-DEVICE-MIB
$unit = ($param !== 'perc' && isset($entry['unit'])) ? $entry['unit'] : NULL;
$value = NULL;
if (isset($entry['oid_'.$param.'_high'], $entry['oid_'.$param.'_low'])) {
// High+Low set of values
if (isset($cache_storage[$mib][$index][$entry['oid_'.$param.'_high']])) {
// Cached
$high = $cache_storage[$mib][$index][$entry['oid_'.$param.'_high']];
$low = $cache_storage[$mib][$index][$entry['oid_'.$param.'_low']];
} elseif ($hc) {
if (isset($entry['oid_'.$param.'_high_num'])) { $high = snmp_get_oid($device, $entry['oid_'.$param.'_high_num'].'.'.$index); }
elseif (isset($entry['oid_'.$param.'_high'])) { $high = snmp_get_oid($device, $entry['oid_'.$param.'_high'].'.'.$index, $mib); }
if (isset($entry['oid_'.$param.'_low_num'])) { $low = snmp_get_oid($device, $entry['oid_'.$param.'_low_num'].'.'.$index); }
elseif (isset($entry['oid_'.$param.'_low'])) { $low = snmp_get_oid($device, $entry['oid_'.$param.'_low'].'.'.$index, $mib); }
}
$high = snmp_fix_numeric($high);
$low = snmp_fix_numeric($low);
$value = snmp_size64_high_low($high, $low);
}
if ($hc && isset($entry['oid_'.$param.'_hc']) && !is_numeric($value)) {
// Common HC value
if (isset($cache_storage[$mib][$index][$entry['oid_'.$param.'_hc']])) {
$value = snmp_fix_numeric($cache_storage[$mib][$index][$entry['oid_'.$param.'_hc']], $unit);
} else {
if (isset($entry['oid_'.$param.'_hc_num'])) { $value = snmp_fix_numeric(snmp_get_oid($device, $entry['oid_'.$param.'_hc_num'].'.'.$index), $unit); }
elseif (isset($entry['oid_'.$param.'_hc'])) { $value = snmp_fix_numeric(snmp_get_oid($device, $entry['oid_'.$param.'_hc'].'.'.$index, $mib), $unit); }
}
}
if (!is_numeric($value) && isset($entry['oid_'.$param])) {
// Common value
if (isset($cache_storage[$mib][$index][$entry['oid_'.$param]])) {
$value = snmp_fix_numeric($cache_storage[$mib][$index][$entry['oid_'.$param]], $unit);
} else {
if (isset($entry['oid_'.$param.'_num'])) { $value = snmp_fix_numeric(snmp_get_oid($device, $entry['oid_'.$param.'_num'].'.'.$index), $unit); }
elseif (isset($entry['oid_'.$param])) { $value = snmp_fix_numeric(snmp_get_oid($device, $entry['oid_'.$param].'.'.$index, $mib), $unit); }
}
}
return $value;
return $value;
}
/**
@ -355,124 +377,125 @@ function get_storage_value($device, $param, $entry, $storage, $cache_storage = [
*
* @return bool
*/
function poll_cache_storage($device, &$oid_cache) {
global $config;
function poll_cache_storage($device, &$oid_cache)
{
global $config;
$mib_walk_option = 'storage_walk'; // ie: $config['mibs'][$mib]['storage_walk']
//$snmp_flags = OBS_SNMP_ALL_NUMERIC; // Numeric Oids by default
$mib_walk_option = 'storage_walk'; // ie: $config['mibs'][$mib]['storage_walk']
//$snmp_flags = OBS_SNMP_ALL_NUMERIC; // Numeric Oids by default
// CLEANME. Compatibility with old (incorrect) field
$object_field = get_db_version() > 468 ? 'storage_object' : 'storage_type';
// CLEANME. Compatibility with old (incorrect) field
$object_field = get_db_version() > 468 ? 'storage_object' : 'storage_type';
// Walk query
$walk_query = "SELECT `storage_mib`, `$object_field`, `storage_hc`, GROUP_CONCAT(`storage_index` SEPARATOR ?) AS `indexes` FROM `storage` WHERE `device_id` = ? GROUP BY `storage_mib`, `$object_field`, `storage_hc`";
$walk_params = [ ',', $device['device_id'] ];
// Walk query
$walk_query = "SELECT `storage_mib`, `$object_field`, `storage_hc`, GROUP_CONCAT(`storage_index` SEPARATOR ?) AS `indexes` FROM `storage` WHERE `device_id` = ? GROUP BY `storage_mib`, `$object_field`, `storage_hc`";
$walk_params = [',', $device['device_id']];
$oid_to_cache = [];
foreach (dbFetchRows($walk_query, $walk_params, TRUE) as $entry) {
if (!isset($config['mibs'][$entry['storage_mib']]['storage'][$entry[$object_field]])) {
// Cache only definition based
continue;
}
$def = $config['mibs'][$entry['storage_mib']]['storage'][$entry[$object_field]];
$hc = $entry['storage_hc'];
// Explode indexes from GROUP_CONCAT()
$oid_to_cache[$entry['storage_mib']][$entry[$object_field]]['indexes'] = explode(',', $entry['indexes']);
// Storage need only this oids in poller
$total = FALSE;
$used = FALSE;
$free = FALSE;
if ($hc) {
// HC oids
if (isset($def['oid_total_high'], $def['oid_total_low'])) {
$oid_to_cache[$entry['storage_mib']][$entry[$object_field]]['oids'][] = $def['oid_total_high'];
$oid_to_cache[$entry['storage_mib']][$entry[$object_field]]['oids'][] = $def['oid_total_low'];
$total = TRUE;
} elseif (isset($def['oid_total_hc'])) {
$oid_to_cache[$entry['storage_mib']][$entry[$object_field]]['oids'][] = $def['oid_total_hc'];
$total = TRUE;
}
if (isset($def['oid_used_high'], $def['oid_used_low'])) {
$oid_to_cache[$entry['storage_mib']][$entry[$object_field]]['oids'][] = $def['oid_used_high'];
$oid_to_cache[$entry['storage_mib']][$entry[$object_field]]['oids'][] = $def['oid_used_low'];
$used = TRUE;
} elseif (isset($def['oid_used_hc'])) {
$oid_to_cache[$entry['storage_mib']][$entry[$object_field]]['oids'][] = $def['oid_used_hc'];
$used = TRUE;
}
if (isset($def['oid_free_high'], $def['oid_free_low'])) {
$oid_to_cache[$entry['storage_mib']][$entry[$object_field]]['oids'][] = $def['oid_free_high'];
$oid_to_cache[$entry['storage_mib']][$entry[$object_field]]['oids'][] = $def['oid_free_low'];
$free = TRUE;
} elseif (isset($def['oid_free_hc'])) {
$oid_to_cache[$entry['storage_mib']][$entry[$object_field]]['oids'][] = $def['oid_free_hc'];
$free = TRUE;
}
}
if (!$total && isset($def['oid_total'])) {
$oid_to_cache[$entry['storage_mib']][$entry[$object_field]]['oids'][] = $def['oid_total'];
}
if (!$used && isset($def['oid_used'])) {
$oid_to_cache[$entry['storage_mib']][$entry[$object_field]]['oids'][] = $def['oid_used'];
}
if (!$free && isset($def['oid_free'])) {
$oid_to_cache[$entry['storage_mib']][$entry[$object_field]]['oids'][] = $def['oid_free'];
}
if (isset($def['oid_perc'])) {
$oid_to_cache[$entry['storage_mib']][$entry[$object_field]]['oids'][] = $def['oid_perc'];
}
if (isset($def['oid_online'])) {
$oid_to_cache[$entry['storage_mib']][$entry[$object_field]]['oids'][] = $def['oid_online'];
}
}
foreach ($oid_to_cache as $mib => $object_array) {
foreach ($object_array as $object => $entry) {
//$def = $config['mibs'][$mib]['storage'][$type];
if (isset($config['mibs'][$mib][$mib_walk_option]) &&
!$config['mibs'][$mib][$mib_walk_option]) {
// MIB not support walk (by definition)
$use_walk = FALSE;
} else {
// Walk on multiple indexes
$use_walk = count($entry['indexes']) > 1;
}
if ($use_walk) {
// SNMP walk
if (isset($GLOBALS['cache']['snmp_object_polled'][$mib][$object])) {
print_debug("MIB/Type ($mib::$object) already polled.");
continue;
$oid_to_cache = [];
foreach (dbFetchRows($walk_query, $walk_params) as $entry) {
if (!isset($config['mibs'][$entry['storage_mib']]['storage'][$entry[$object_field]])) {
// Cache only definition based
continue;
}
print_debug("Caching storage snmpwalk by $mib");
foreach ($entry['oids'] as $oid) {
$oid_cache[$mib] = snmpwalk_multipart_oid($device, $oid, $oid_cache[$mib], $mib, NULL, OBS_SNMP_ALL_NUMERIC_INDEX);
$def = $config['mibs'][$entry['storage_mib']]['storage'][$entry[$object_field]];
$hc = $entry['storage_hc'];
// Explode indexes from GROUP_CONCAT()
$oid_to_cache[$entry['storage_mib']][$entry[$object_field]]['indexes'] = explode(',', $entry['indexes']);
// Storage need only this oids in poller
$total = FALSE;
$used = FALSE;
$free = FALSE;
if ($hc) {
// HC oids
if (isset($def['oid_total_high'], $def['oid_total_low'])) {
$oid_to_cache[$entry['storage_mib']][$entry[$object_field]]['oids'][] = $def['oid_total_high'];
$oid_to_cache[$entry['storage_mib']][$entry[$object_field]]['oids'][] = $def['oid_total_low'];
$total = TRUE;
} elseif (isset($def['oid_total_hc'])) {
$oid_to_cache[$entry['storage_mib']][$entry[$object_field]]['oids'][] = $def['oid_total_hc'];
$total = TRUE;
}
if (isset($def['oid_used_high'], $def['oid_used_low'])) {
$oid_to_cache[$entry['storage_mib']][$entry[$object_field]]['oids'][] = $def['oid_used_high'];
$oid_to_cache[$entry['storage_mib']][$entry[$object_field]]['oids'][] = $def['oid_used_low'];
$used = TRUE;
} elseif (isset($def['oid_used_hc'])) {
$oid_to_cache[$entry['storage_mib']][$entry[$object_field]]['oids'][] = $def['oid_used_hc'];
$used = TRUE;
}
if (isset($def['oid_free_high'], $def['oid_free_low'])) {
$oid_to_cache[$entry['storage_mib']][$entry[$object_field]]['oids'][] = $def['oid_free_high'];
$oid_to_cache[$entry['storage_mib']][$entry[$object_field]]['oids'][] = $def['oid_free_low'];
$free = TRUE;
} elseif (isset($def['oid_free_hc'])) {
$oid_to_cache[$entry['storage_mib']][$entry[$object_field]]['oids'][] = $def['oid_free_hc'];
$free = TRUE;
}
}
$GLOBALS['cache']['snmp_object_polled'][$mib][$object] = 1;
} else {
// SNMP multiget
print_debug("Caching storage snmpget by $mib");
$oids = [];
foreach ($entry['oids'] as $oid) {
foreach ($entry['indexes'] as $index) {
$oids[] = $oid . '.' . $index;
}
if (!$total && isset($def['oid_total'])) {
$oid_to_cache[$entry['storage_mib']][$entry[$object_field]]['oids'][] = $def['oid_total'];
}
if (!$used && isset($def['oid_used'])) {
$oid_to_cache[$entry['storage_mib']][$entry[$object_field]]['oids'][] = $def['oid_used'];
}
if (!$free && isset($def['oid_free'])) {
$oid_to_cache[$entry['storage_mib']][$entry[$object_field]]['oids'][] = $def['oid_free'];
}
if (isset($def['oid_perc'])) {
$oid_to_cache[$entry['storage_mib']][$entry[$object_field]]['oids'][] = $def['oid_perc'];
}
if (isset($def['oid_online'])) {
$oid_to_cache[$entry['storage_mib']][$entry[$object_field]]['oids'][] = $def['oid_online'];
}
$oid_cache[$mib] = snmp_get_multi_oid($device, $oids, $oid_cache[$mib], $mib);
}
}
}
print_debug_vars($oid_to_cache);
print_debug_vars($oid_cache);
return !empty($oid_to_cache);
foreach ($oid_to_cache as $mib => $object_array) {
foreach ($object_array as $object => $entry) {
//$def = $config['mibs'][$mib]['storage'][$type];
if (isset($config['mibs'][$mib][$mib_walk_option]) &&
!$config['mibs'][$mib][$mib_walk_option]) {
// MIB not support walk (by definition)
$use_walk = FALSE;
} else {
// Walk on multiple indexes
$use_walk = count($entry['indexes']) > 1;
}
if ($use_walk) {
// SNMP walk
if (isset($GLOBALS['cache']['snmp_object_polled'][$mib][$object])) {
print_debug("MIB/Type ($mib::$object) already polled.");
continue;
}
print_debug("Caching storage snmpwalk by $mib");
foreach ($entry['oids'] as $oid) {
$oid_cache[$mib] = snmpwalk_multipart_oid($device, $oid, $oid_cache[$mib], $mib, NULL, OBS_SNMP_ALL_NUMERIC_INDEX);
}
$GLOBALS['cache']['snmp_object_polled'][$mib][$object] = 1;
} else {
// SNMP multiget
print_debug("Caching storage snmpget by $mib");
$oids = [];
foreach ($entry['oids'] as $oid) {
foreach ($entry['indexes'] as $index) {
$oids[] = $oid . '.' . $index;
}
}
$oid_cache[$mib] = snmp_get_multi_oid($device, $oids, $oid_cache[$mib], $mib);
}
}
}
print_debug_vars($oid_to_cache);
print_debug_vars($oid_cache);
return !empty($oid_to_cache);
}
// EOF

View File

@ -4,9 +4,9 @@
*
* This file is part of Observium.
*
* @package observium
* @subpackage discovery
* @copyright (C) 2006-2013 Adam Armstrong, (C) 2013-2021 Observium Limited
* @package observium
* @subpackage discovery
* @copyright (C) Adam Armstrong
*
*/
@ -20,12 +20,11 @@ function discover_entity($device_id, $entity_type, $data)
if (is_array($GLOBALS['config']['entities'][$entity_type])) {
$def = $GLOBALS['config']['entities'][$entity_type];
$index = $data[$def['table_fields']['index']];
$def = $GLOBALS['config']['entities'][$entity_type];
$index = $data[$def['table_fields']['index']];
$params = $def['params'];
if (isset($params['table_fields']['index']) && is_array())
{
if (isset($params['table_fields']['index']) && is_array()) {
} elseif (isset($params['table_fields']['index'])) {
@ -34,23 +33,22 @@ function discover_entity($device_id, $entity_type, $data)
}
if (is_array($GLOBALS['cache'][$def['table']][$index])) {
echo 'Exists';
$db = $GLOBALS['cache'][$def['table']][$index];
$id = $db[$def['table_fields']['id']];
echo 'exists:'.$id.PHP_EOL;
echo 'exists:' . $id . PHP_EOL;
$update = array();
foreach ($params as $param)
{
if ($data[$param] != $db[$param]) { $update[$param] = $data[$param]; }
$update = [];
foreach ($params as $param) {
if ($data[$param] != $db[$param]) {
$update[$param] = $data[$param];
}
}
if (count($update))
{
dbUpdate($update, $def['table'], '`'.$def['table_fields']['id'].'` = ?', array($id));
if (count($update)) {
dbUpdate($update, $def['table'], '`' . $def['table_fields']['id'] . '` = ?', [$id]);
echo('U');
} else {
echo('.');
@ -60,12 +58,13 @@ function discover_entity($device_id, $entity_type, $data)
echo 'Doesnt Exist';
$insert = array();
$insert = [];
$insert['device_id'] = $device_id;
foreach ($params as $param)
{
foreach ($params as $param) {
$insert[$param] = $data[$param];
if ($data[$param] == NULL) { $insert[$param] = array('NULL'); }
if ($data[$param] == NULL) {
$insert[$param] = ['NULL'];
}
}
$id = dbInsert($insert, $def['table']);
echo("+");
@ -93,154 +92,156 @@ function discover_entity($device_id, $entity_type, $data)
* Discover WIFI Access Point. Returns ap_id.
*
* @param array|integer $device
* @param array $ap
* @param array $ap
*
* @return integer
*/
function discover_wifi_ap($device, $ap) {
function discover_wifi_ap($device, $ap)
{
$device_id = is_array($device) ? $device['device_id'] : $device;
$params = [ 'ap_mib', 'ap_index',
'ap_number', 'ap_name', 'ap_address', 'ap_serial', 'ap_model', 'ap_location', 'ap_fingerprint', // not required
'ap_status', 'ap_admin_status' ];
$device_id = is_array($device) ? $device['device_id'] : $device;
$params = ['ap_mib', 'ap_index',
'ap_number', 'ap_name', 'ap_address', 'ap_serial', 'ap_model', 'ap_location', 'ap_fingerprint', // not required
'ap_status', 'ap_admin_status'];
if (is_array($GLOBALS['cache']['wifi_aps'][$ap['ap_index']])) {
/* Only insert new APs, for polling ability
// Database entry exists. Lets update it!
if (is_array($GLOBALS['cache']['wifi_aps'][$ap['ap_index']])) {
/* Only insert new APs, for polling ability
// Database entry exists. Lets update it!
$ap_db = $GLOBALS['cache']['wifi_aps'][$ap['ap_index']];
$ap_id = $ap_db['wifi_ap_id'];
$ap_db = $GLOBALS['cache']['wifi_aps'][$ap['ap_index']];
$ap_id = $ap_db['wifi_ap_id'];
echo 'exists:'.$ap_id.PHP_EOL;
echo 'exists:'.$ap_id.PHP_EOL;
$update = [];
foreach ($params as $param) {
if ($ap[$param] != $ap_db[$param]) {
if (safe_empty($ap[$param])) {
$update[$param] = [ 'NULL' ];
} else {
$update[$param] = $ap[$param];
$update = [];
foreach ($params as $param) {
if ($ap[$param] != $ap_db[$param]) {
if (safe_empty($ap[$param])) {
$update[$param] = [ 'NULL' ];
} else {
$update[$param] = $ap[$param];
}
}
}
if (count($update)) {
dbUpdate($update, 'wifi_aps', '`wifi_ap_id` = ?', array($ap_db['wifi_ap_id']));
echo('U');
} else {
echo('.');
}
*/
$ap_id = $GLOBALS['cache']['wifi_aps'][$ap['ap_index']]['wifi_ap_id'];
if ($GLOBALS['cache']['wifi_aps'][$ap['ap_index']]['deleted']) {
dbUpdate(['deleted' => 0], 'wifi_aps', '`wifi_ap_id` = ?', [$ap_id]);
$GLOBALS['cache']['wifi_aps'][$ap['ap_index']]['deleted'] = 0;
}
}
}
if (count($update)) {
dbUpdate($update, 'wifi_aps', '`wifi_ap_id` = ?', array($ap_db['wifi_ap_id']));
echo('U');
} else {
echo('.');
// Database entry doesn't exist. Lets create it!
$insert = [];
$insert['device_id'] = $device_id;
foreach ($params as $param) {
$insert[$param] = $ap[$param];
if (safe_empty($ap[$param])) {
$insert[$param] = ['NULL'];
}
}
print_debug_vars($insert);
$ap_id = dbInsert($insert, 'wifi_aps');
echo("+");
$params['wifi_ap_id'] = $ap_id;
// Populate cache with this entry. Maybe we need it.
$GLOBALS['cache']['wifi_aps'][$ap['ap_index']] = $params;
}
*/
$ap_id = $GLOBALS['cache']['wifi_aps'][$ap['ap_index']]['wifi_ap_id'];
if ($GLOBALS['cache']['wifi_aps'][$ap['ap_index']]['deleted']) {
dbUpdate([ 'deleted' => 0 ], 'wifi_aps', '`wifi_ap_id` = ?', [ $ap_id ]);
$GLOBALS['cache']['wifi_aps'][$ap['ap_index']]['deleted'] = 0;
}
} else {
// Database entry doesn't exist. Lets create it!
$insert = [];
$insert['device_id'] = $device_id;
foreach ($params as $param) {
$insert[$param] = $ap[$param];
if (safe_empty($ap[$param])) {
$insert[$param] = array('NULL');
}
}
print_debug_vars($insert);
$ap_id = dbInsert($insert, 'wifi_aps');
echo("+");
$params['wifi_ap_id'] = $ap_id;
$GLOBALS['valid']['wifi']['aps'][$ap['ap_index']] = $ap_id;
// Populate cache with this entry. Maybe we need it.
$GLOBALS['cache']['wifi_aps'][$ap['ap_index']] = $params;
}
$GLOBALS['valid']['wifi']['aps'][$ap['ap_index']] = $ap_id;
return $ap_id;
return $ap_id;
}
function discover_wifi_wlan($device_id, $wlan)
{
$params = array('wlan_admin_status', 'wlan_beacon_period', 'wlan_bssid', 'wlan_bss_type', 'wlan_channel', 'wlan_dtim_period', 'wlan_frag_thresh',
'wlan_index', 'wlan_igmp_snoop', 'wlan_name', 'wlan_prot_mode', 'wlan_radio_mode', 'wlan_rts_thresh',
'wlan_ssid', 'wlan_ssid_bcast', 'wlan_vlan_id');
$params = ['wlan_admin_status', 'wlan_beacon_period', 'wlan_bssid', 'wlan_bss_type', 'wlan_channel', 'wlan_dtim_period', 'wlan_frag_thresh',
'wlan_index', 'wlan_igmp_snoop', 'wlan_name', 'wlan_prot_mode', 'wlan_radio_mode', 'wlan_rts_thresh',
'wlan_ssid', 'wlan_ssid_bcast', 'wlan_vlan_id'];
if (is_array($GLOBALS['cache']['wifi_wlans'][$wlan['wlan_index']]))
{
// Database entry exists. Lets update it!
$wlan_db = $GLOBALS['cache']['wifi_wlans'][$wlan['wlan_index']];
$wlan_id = $wlan_db['wlan_id'];
if (is_array($GLOBALS['cache']['wifi_wlans'][$wlan['wlan_index']])) {
// Database entry exists. Lets update it!
$wlan_db = $GLOBALS['cache']['wifi_wlans'][$wlan['wlan_index']];
$wlan_id = $wlan_db['wlan_id'];
$update = [];
foreach ($params as $param) {
if ($wlan[$param] != $wlan_db[$param]) {
$update[$param] = $wlan[$param];
}
}
if (count($update)) {
dbUpdate($update, 'wifi_wlans', '`wlan_id` = ?', [$wlan_db['wlan_id']]);
echo('U');
} else {
echo('.');
}
$update = array();
foreach ($params as $param)
{
if ($wlan[$param] != $wlan_db[$param]) { $update[$param] = $wlan[$param]; }
}
if (count($update))
{
dbUpdate($update, 'wifi_wlans', '`wlan_id` = ?', array($wlan_db['wlan_id']));
echo('U');
} else {
echo('.');
// Database entry doesn't exist. Lets create it!
$insert = [];
$insert['device_id'] = $device_id;
foreach ($params as $param) {
$insert[$param] = $wlan[$param];
if (is_null($wlan[$param])) {
$insert[$param] = ['NULL'];
}
}
$wlan_id = dbInsert($insert, 'wifi_wlans');
echo("+");
}
} else {
// Database entry doesn't exist. Lets create it!
$insert = array();
$insert['device_id'] = $device_id;
foreach ($params as $param)
{
$insert[$param] = $wlan[$param];
if (is_null($wlan[$param])) { $insert[$param] = array('NULL'); }
}
$wlan_id = dbInsert($insert, 'wifi_wlans');
echo("+");
}
return $wlan_id;
return $wlan_id;
}
function discover_wifi_radio($device_id, $radio) {
$params = [ 'radio_ap', 'radio_mib', 'radio_number', 'radio_util', 'radio_type', 'radio_status',
'radio_clients', 'radio_txpower', 'radio_channel', 'radio_mac', 'radio_protection', 'radio_bsstype' ];
function discover_wifi_radio($device_id, $radio)
{
$params = ['radio_ap', 'radio_mib', 'radio_number', 'radio_util', 'radio_type', 'radio_status',
'radio_clients', 'radio_txpower', 'radio_channel', 'radio_mac', 'radio_protection', 'radio_bsstype'];
if (is_array($GLOBALS['cache']['wifi_radios'][$radio['radio_ap']][$radio['radio_number']])) {
$radio_db = $GLOBALS['cache']['wifi_radios'][$radio['radio_ap']][$radio['radio_number']];
}
if (is_array($GLOBALS['cache']['wifi_radios'][$radio['radio_ap']][$radio['radio_number']])) {
$radio_db = $GLOBALS['cache']['wifi_radios'][$radio['radio_ap']][$radio['radio_number']];
}
if (!isset($radio_db['wifi_radio_id']))
{
$insert = array();
$insert['device_id'] = $device_id;
foreach ($params as $param)
{
$insert[$param] = $radio[$param];
if (is_null($radio[$param])) { $insert[$param] = array('NULL'); }
}
$wifi_radio_id = dbInsert($insert, 'wifi_radios');
echo("+");
} else {
$update = array();
foreach ($params as $param)
{
if ($radio[$param] != $radio_db[$param]) { $update[$param] = $radio[$param]; }
}
if (count($update))
{
dbUpdate($update, 'wifi_radios', '`wifi_radio_id` = ?', array($radio_db['wifi_radio_id']));
echo('U');
if (!isset($radio_db['wifi_radio_id'])) {
$insert = [];
$insert['device_id'] = $device_id;
foreach ($params as $param) {
$insert[$param] = $radio[$param];
if (is_null($radio[$param])) {
$insert[$param] = ['NULL'];
}
}
$wifi_radio_id = dbInsert($insert, 'wifi_radios');
echo("+");
} else {
echo('.');
$update = [];
foreach ($params as $param) {
if ($radio[$param] != $radio_db[$param]) {
$update[$param] = $radio[$param];
}
}
if (count($update)) {
dbUpdate($update, 'wifi_radios', '`wifi_radio_id` = ?', [$radio_db['wifi_radio_id']]);
echo('U');
} else {
echo('.');
}
}
}
$GLOBALS['valid']['wifi']['radio'][$radio['radio_mib']][$wifi_radio_id] = 1;
$GLOBALS['valid']['wifi']['radio'][$radio['radio_mib']][$wifi_radio_id] = 1;
}
// EOF