From 953ea3fa152044b09c7a07fee33c138af0243b17 Mon Sep 17 00:00:00 2001 From: Paul Gear Date: Sun, 16 Aug 2015 22:09:17 +1000 Subject: [PATCH] Add device OID storage and display --- html/includes/table/device_oids.inc.php | 94 +++++++++++++++++++++++++ html/pages/device/mib.inc.php | 36 ++++++++++ includes/discovery/functions.inc.php | 5 ++ includes/snmp.inc.php | 39 ++++++++-- 4 files changed, 168 insertions(+), 6 deletions(-) create mode 100644 html/includes/table/device_oids.inc.php diff --git a/html/includes/table/device_oids.inc.php b/html/includes/table/device_oids.inc.php new file mode 100644 index 000000000..f016a8583 --- /dev/null +++ b/html/includes/table/device_oids.inc.php @@ -0,0 +1,94 @@ + + * + * by Paul Gear + * based on code by Søren Friis Rosiak + * in commit 054bf3ae209f34a2c3bc8968300722004903df1b + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. Please see LICENSE.txt at the top level of + * the source code distribution for details. + */ + +$columns = array( + 'module', + 'mib', + 'object_type', + 'oid', + 'value', + 'numvalue', + 'last_modified', +); + +function search_phrase_column($c) +{ + global $searchPhrase; + return "$c LIKE '%$searchPhrase%'"; +} + +function quote_sql_word($c) +{ + return "`$c`"; +} + + +$params = array( + $_POST['device_id'], +); + +// start of sql definition +$sql = 'SELECT * FROM `device_oids`'; + +$wheresql = ' WHERE `device_id` = ?'; + +// all columns are searchable - search across them +if (isset($searchPhrase) && !empty($searchPhrase)) { + $searchsql = implode(' OR ', array_map("search_phrase_column", array_map("quote_sql_word", $columns))); + $wheresql .= " AND ( $searchsql )"; +} +$sql .= $wheresql; + +// get total +$count_sql = "SELECT COUNT(*) FROM `device_oids`".$wheresql; +$total = dbFetchCell($count_sql, $params); +if (empty($total)) { + $total = 0; +} + +// sort by first three columns by default +if (!isset($sort) || empty($sort)) { + $sort = implode(', ', array_map("quote_sql_word", array_slice($columns, 0, 3))); +} +$sql .= " ORDER BY $sort"; + +// select only the required rows +if (isset($current)) { + $limit_low = (($current * $rowCount) - ($rowCount)); + $limit_high = $rowCount; +} +if ($rowCount != -1) { + $sql .= " LIMIT $limit_low,$limit_high"; +} + +// load data from database into response array +$response = array(); +foreach (dbFetchRows($sql, $params) as $mib) { + $mibrow = array(); + foreach ($columns as $col) { + $mibrow[$col] = $mib[$col]; + } + $response[] = $mibrow; +} + +$output = array( + 'current' => $current, + 'rowCount' => $rowCount, + 'rows' => $response, + 'total' => $total, +); +echo _json_encode($output); diff --git a/html/pages/device/mib.inc.php b/html/pages/device/mib.inc.php index fc7f32b69..7c56d57c3 100644 --- a/html/pages/device/mib.inc.php +++ b/html/pages/device/mib.inc.php @@ -32,6 +32,23 @@ if (!isset($vars['section'])) { +

Device MIB values

+
+ + + + + + + + + + + + +
ModuleMIBObject typeOIDValueNumeric ValueLast Modified
+
+ + + diff --git a/includes/discovery/functions.inc.php b/includes/discovery/functions.inc.php index af421323b..98a91e853 100644 --- a/includes/discovery/functions.inc.php +++ b/includes/discovery/functions.inc.php @@ -131,6 +131,11 @@ function discover_device($device, $options=null) { } } + if (is_mib_poller_enabled($device)) { + $devicemib = array($device['sysObjectID'] => 'all'); + register_mibs($device, $devicemib, "includes/discovery/functions.inc.php"); + } + // Set type to a predefined type for the OS if it's not already set if ($device['type'] == 'unknown' || $device['type'] == '') { if ($config['os'][$device['os']]['type']) { diff --git a/includes/snmp.inc.php b/includes/snmp.inc.php index d70f4e097..d3c83337c 100644 --- a/includes/snmp.inc.php +++ b/includes/snmp.inc.php @@ -926,11 +926,18 @@ function snmp_mib_walk($mib, $module, $mibdir=null) }//end snmp_mib_walk() +function quote_column($a) +{ + return '`'.$a.'`'; +} + + function join_array($a, $b) { - return "$a=$b"; + return quote_column($a).'='.$b; } + /* * Update the given table in the database with the given row & column data. * @param tablename The table to update @@ -940,6 +947,7 @@ function join_array($a, $b) */ function update_db_table($tablename, $columns, $numkeys, $rows) { + dbBeginTransaction(); foreach ($rows as $nothing => $obj) { // create a parameter list based on the columns $params = array(); @@ -954,12 +962,14 @@ function update_db_table($tablename, $columns, $numkeys, $rows) $update_definitions = array_map("join_array", $non_key_columns, $non_key_placeholders); $non_key_params = array_slice($params, $numkeys); - $sql = 'INSERT INTO `' . $tablename . '` (' . implode(',', $columns) . + $sql = 'INSERT INTO `' . $tablename . '` (' . + implode(',', array_map("quote_column", $columns)) . ') VALUES (' . implode(',', $column_placeholders) . ') ON DUPLICATE KEY UPDATE ' . implode(',', $update_definitions); $result = dbQuery($sql, array_merge($params, $non_key_params)); d_echo("Result: $result\n"); } + dbCommitTransaction(); } /* @@ -1118,23 +1128,37 @@ function update_mib_graph_types($mibname, $oids, $mibdef, $graphs) /* * Save all of the measurable oids for the device in their own RRDs. + * Save the current value of all the oids in the database. */ function save_mibs($device, $mibname, $oids, $mibdef, &$graphs) { $usedoids = array(); + $deviceoids = array(); foreach ($oids as $index => $array) { - foreach ($array as $oid => $val) { - $type = oid_rrd_type($oid, $mibdef); + foreach ($array as $obj => $val) { + // build up the device_oid row for saving into the database + $numvalue = preg_match('/^\d+$/', $val) ? $val : null; + $deviceoids[] = array( + 'device_id' => $device['device_id'], + 'oid' => $mibdef[$obj]['oid'].".".$index, + 'module' => $mibdef[$obj]['module'], + 'mib' => $mibdef[$obj]['mib'], + 'object_type' => $obj, + 'value' => $val, + 'numvalue' => $numvalue, + ); + + $type = oid_rrd_type($obj, $mibdef); if ($type === false) { continue; } - $usedoids[$index][$oid] = $val; + $usedoids[$index][$obj] = $val; rrd_create_update( $device, array( $mibname, - $mibdef[$oid]['shortname'], + $mibdef[$obj]['shortname'], $index, ), array("DS:mibval:$type"), @@ -1146,6 +1170,9 @@ function save_mibs($device, $mibname, $oids, $mibdef, &$graphs) tag_graphs($mibname, $usedoids, $mibdef, $graphs); update_mib_graph_types($mibname, $usedoids, $mibdef, $graphs); + // update database + $columns = array('device_id', 'oid', 'module', 'mib', 'object_type', 'value', 'numvalue'); + update_db_table('device_oids', $columns, 2, $deviceoids); }//end save_mibs() /*