Add device OID storage and display

This commit is contained in:
Paul Gear
2015-08-16 22:09:17 +10:00
parent 5d53761727
commit 953ea3fa15
4 changed files with 168 additions and 6 deletions

View File

@@ -0,0 +1,94 @@
<?php
/*
* LibreNMS device MIB OID browser
*
* Copyright (c) 2015 Gear Consulting Pty Ltd <github@libertysys.com.au>
*
* by Paul Gear
* based on code by Søren Friis Rosiak <sorenrosiak@gmail.com>
* 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);

View File

@@ -32,6 +32,23 @@ if (!isset($vars['section'])) {
</table>
</div>
<h4><i class="fa fa-file-text-o"></i> Device MIB values</h4>
<div class="table-responsive">
<table id="oids" class="table table-hover table-condensed mibs">
<thead>
<tr>
<th data-column-id="module">Module</th>
<th data-column-id="mib">MIB</th>
<th data-column-id="object_type">Object type</th>
<th data-column-id="oid">OID</th>
<th data-column-id="value">Value</th>
<th data-column-id="numvalue">Numeric Value</th>
<th data-column-id="last_modified">Last Modified</th>
</tr>
</thead>
</table>
</div>
<script>
var grid = $("#mibs").bootgrid({
ajax: true,
@@ -50,3 +67,22 @@ if (!isset($vars['section'])) {
}
});
</script>
<script>
var grid2 = $("#oids").bootgrid({
ajax: true,
rowCount: [50,100,250,-1],
post: function ()
{
return {
id: "device_oids",
device_id: '<?php echo htmlspecialchars($device['device_id']); ?>',
};
},
url: "/ajax_table.php",
formatters: {
},
templates: {
}
});
</script>

View File

@@ -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']) {

View File

@@ -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()
/*