Skip to content

Commit

Permalink
add search, notification, exclude notification, more history records
Browse files Browse the repository at this point in the history
  • Loading branch information
xmacan committed Aug 17, 2022
1 parent 916c76a commit 231853b
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 39 deletions.
2 changes: 1 addition & 1 deletion INFO
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[info]
name = snver
version = 0.5
version = 0.6
longname = Serial number and version
author = Petr Macek
email = [email protected]
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ If you find a problem, let me know via github or https://forums.cacti.net


## Changelog
--- 0.6
Add search in history
Add notification email
Add more history records
Add exclude for email notification
Poller function speedup
--- 0.5
Fix lenght of stored history
Fix php warning about $config variable
Expand Down
13 changes: 9 additions & 4 deletions TODO
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
more than one history record?
- add to configuration

send mail when hw is changed
- add to configuration
u aruby mam uptime apcek, ale kazi mi history data. Resit to nejak dalsi tabulkou? - ANO
- mam to zapoznamkovane v setup.php

presunout funkci poller_bottom do functions?

kvuli posilani musi byt navaznost na thold - jak to zajistit?





lepsi zpusob, jak vlozit jquery, ted to mam pres hook dole pod device
Expand Down
1 change: 1 addition & 0 deletions data/ent.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
DELETE FROM plugin_snver_organizations;
INSERT INTO plugin_snver_organizations (id, organization) VALUES ('0','Reserved');
INSERT INTO plugin_snver_organizations (id, organization) VALUES ('1','NxNetworks');
INSERT INTO plugin_snver_organizations (id, organization) VALUES ('2','IBM (https://w3.ibm.com/standards )');
Expand Down
43 changes: 36 additions & 7 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ function plugin_snver_get_info($host_id) {
'.1.3.6.1.2.1.1.2.0', $host['snmp_version'],
$host['snmp_username'], $host['snmp_password'], $host['snmp_auth_protocol'],
$host['snmp_priv_passphrase'], $host['snmp_priv_protocol'],
$host['snmp_context'], $host['snmp_port'], $host['snmp_timeout']);
$host['snmp_context'], $host['snmp_port'], $host['snmp_timeout'],1);

if ($string == 'U') {
return ('Cannot determine sysObjectID, is snmp configured correctly? Maybe host down');
Expand Down Expand Up @@ -276,18 +276,18 @@ function plugin_snver_get_history($host_id, $data_act) {

$out = '';

$data_his = db_fetch_assoc_prepared ('SELECT * FROM plugin_snver_history WHERE host_id = ? ', array(get_request_var('host_id')));

$data_his = db_fetch_row_prepared ('SELECT * FROM plugin_snver_history
WHERE host_id = ? ORDER BY last_check LIMIT 1', array(get_request_var('host_id')));
if ($data_his) {
$data_his[0]['data'] = stripslashes($data_his[0]['data']);
$data_his['data'] = stripslashes($data_his['data']);

$out = '<b>History from ' . $data_his[0]['last_check'] . ':</b><br/>';
$out = '<b>History from ' . $data_his['last_check'] . ':</b><br/>';

if (strcmp ($data_his[0]['data'],$data_act) === 0) {
if (strcmp ($data_his['data'],$data_act) === 0) {
$out .= 'Actual and history data equal<br/><br/>';
}
else {
$out .= $data_his[0]['data'] . '<br/><br/>';
$out .= $data_his['data'] . '<br/><br/>';
}
}
else {
Expand All @@ -297,3 +297,32 @@ function plugin_snver_get_history($host_id, $data_act) {
return ($out);
}


function snver_find() {

if (read_config_option('snver_hosts_processed') == 0) {
print 'Store history is not allowed. Nothing to do ...';
return false;
}

$find = get_filter_request_var('find', FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => '/^([a-zA-Z0-9_-]{3,})$/')));
if (strlen($find) < 3) {
print 'At least 3 chars...';
return false;
}

$data = db_fetch_assoc ('SELECT id,description,data,last_check FROM host
LEFT JOIN plugin_snver_history ON host.id = plugin_snver_history.host_id
WHERE plugin_snver_history.data LIKE "%' . $find . '%"');

if (cacti_sizeof($data)) {
foreach ($data as $row) {
print '<b>Host ' . $row['description'] . '(ID: ' . $row['id'] . ')<br/>';
print 'Date ' . $row['last_check'] . '</b><br/>';
print $row['data'] . '<br/><br/>';
}
}
else {
print 'Not found';
}
}
111 changes: 100 additions & 11 deletions setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ function plugin_snver_config_settings() {
),
'default' => '0',
),
'snver_records' => array(
'friendly_name' => 'How many changes store',
'description' => 'How many history (changed) records keep for each device',
'method' => 'drop_array',
'array' => array(
'1' => 'Only last state',
'5' => '5 records',
'10' => '10 records',
),
'default' => '5',
),
'snver_history' => array(
'friendly_name' => 'Recheck after',
'description' => 'The shortest possible interval after which new testing will occur',
Expand All @@ -70,8 +81,20 @@ function plugin_snver_config_settings() {
),
'default' => '30',
),
'snver_email_notify' => array(
'friendly_name' => 'Send email on SNVer information change',
'description' => 'If SNVer find change, send email',
'method' => 'checkbox',
'default' => 'off',
),
'snver_email_notify_exclude' => array(
'friendly_name' => 'Excluded notification Host IDs',
'description' => 'Some devices report hw changes too often. You can exclude these host from email notification. Insert Host IDs, comma separator',
'method' => 'textbox',
'max_length' => '500',
'default' => '',
),
);

}


Expand All @@ -88,19 +111,81 @@ function plugin_snver_poller_bottom () {

$number_of_hosts = read_config_option('snver_hosts_processed');
$snver_history = read_config_option('snver_history');
$snver_records = read_config_option('snver_records');

if ($number_of_hosts > 0) {

$hosts = db_fetch_assoc ('SELECT * FROM host AS h1 LEFT JOIN plugin_snver_history AS h2 ON
h1.id = h2.host_id
WHERE (h2.last_check IS NULL OR now() > date_add(h2.last_check, interval ' . $snver_history . ' day))
limit ' . $number_of_hosts);
// not tested
$hosts1 = db_fetch_assoc ("(SELECT h1.id as id,last_check as xx FROM host AS h1 LEFT JOIN plugin_snver_history AS h2
ON h1.id=h2.host_id WHERE h1.disabled != 'on' AND h1.status BETWEEN 2 AND 3 AND h2.last_check IS NULL)
LIMIT " . $number_of_hosts);

$returned = cacti_sizeof($hosts1);

// old hosts
$hosts2 = db_fetch_assoc ("select h1.host_id as id,h1.last_check as xx
from plugin_snver_history as h1 join host on host.id=h1.host_id
where host.disabled != 'on' and host.status between 2 and 3
and h1.last_check = (select max(h2.last_check)
from plugin_snver_history as h2 where h1.host_id = h2.host_id)
having now() > date_add(xx, interval " . $snver_history . " day)
limit " . ($number_of_hosts-$returned) );


$hosts = array_merge($hosts1,$hosts2);

if (cacti_sizeof($hosts) > 0) {
foreach ($hosts as $host) {

db_execute ("REPLACE INTO plugin_snver_history (host_id,data,last_check) VALUES (" .
$host['id'] . ",'" . addslashes(plugin_snver_get_info($host['id'])) . "', now())");

$data_act = plugin_snver_get_info($host['id']);

$data_his = db_fetch_row_prepared ('SELECT * FROM plugin_snver_history
WHERE host_id = ? ORDER BY last_check DESC LIMIT 1', array($host['id']));

if ($data_his) {

$data_his = stripslashes($data_his['data']);

if (strcmp ($data_his, $data_act) === 0) { // only update last check
db_execute ('UPDATE plugin_snver_history set last_check = now()
WHERE host_id = ' . $host['id'] . ' ORDER BY last_check DESC LIMIT 1');
} else {

db_execute ("INSERT INTO plugin_snver_history (host_id,data,last_check) VALUES (" .
$host['id'] . ",'" . addslashes($data_act) . "', now())");

db_execute ('DELETE FROM plugin_snver_history WHERE host_id = ' . $host['id'] . ' order by last_check LIMIT ' . $snver_records);

$excluded = explode(',', read_config_option('snver_email_notify_exclude'));

if (read_config_option('snver_email_notify')) {
if (in_array($host['id'], $excluded)) {
cacti_log('Plugin SNVer - host changed (id:' . $host['id'] . '), excluded from notification');
} else {

$emails = db_fetch_cell_prepared ('SELECT emails FROM plugin_notification_lists
LEFT JOIN host
ON plugin_notification_lists.id = host.thold_host_email
WHERE host.id = ?', array($host['id']));

send_mail($emails,
read_config_option('settings_from_email'),
'Plugin SNVer - HW changed',
'I have found any HW/serial number change on Host ' . $host['id'] . ':<br/>' . PHP_EOL .
$data_act . '<br/><br/>' . PHP_EOL . 'Older data:<br/>' . PHP_EOL . $data_his, '', '', true);

cacti_log('Plugin SNVer - host changed (id:' . $host['id'] . '), sending email notification');
}

} else { // only log
cacti_log('Plugin SNVer - host changed (id:' . $host['id'] . '), only logging');
}
}
}
else {
db_execute ("INSERT INTO plugin_snver_history (host_id,data,last_check) VALUES (" .
$host['id'] . ",'" . addslashes($data_act) . "', now())");
}

$done++;
}

Expand Down Expand Up @@ -149,7 +234,6 @@ function plugin_snver_setup_database() {
$data['columns'][] = array('name' => 'host_id', 'type' => 'int(11)', 'NULL' => false);
$data['columns'][] = array('name' => 'data', 'type' => 'text', 'NULL' => true);
$data['columns'][] = array('name' => 'last_check', 'type' => 'timestamp', 'NULL' => false, 'default' => '0000-00-00 00:00:00');
$data['primary'] = 'host_id';
$data['type'] = 'InnoDB';
$data['comment'] = 'snver history';
api_plugin_db_table_create ('snver', 'plugin_snver_history', $data);
Expand Down Expand Up @@ -188,7 +272,8 @@ function plugin_snver_setup_database() {
db_execute ("INSERT INTO plugin_snver_steps (org_id,description,oid,result,method,table_items) VALUES (24681,'hw disks','1.3.6.1.4.1.24681.1.3.11.1','.*','table','2-name,5-type,3-temp,7-smart')");

// Aruba instant AP cluster
db_execute ("INSERT INTO plugin_snver_steps (org_id,description,oid,result,method,table_items) VALUES (14823,'APs','.1.3.6.1.4.1.14823.2.3.3.1.2.1.1','.*','table','1-mac,2-name,3-ip,4-serial,6-model,9-uptime')");
// uptime is problem for history - db_execute ("INSERT INTO plugin_snver_steps (org_id,description,oid,result,method,table_items) VALUES (14823,'APs','.1.3.6.1.4.1.14823.2.3.3.1.2.1.1','.*','table','1-mac,2-name,3-ip,4-serial,6-model,9-uptime')");
db_execute ("INSERT INTO plugin_snver_steps (org_id,description,oid,result,method,table_items) VALUES (14823,'APs','.1.3.6.1.4.1.14823.2.3.3.1.2.1.1','.*','table','1-mac,2-name,3-ip,4-serial,6-model')");


}
Expand Down Expand Up @@ -244,6 +329,10 @@ function plugin_snver_upgrade_database() {
if (cacti_version_compare($oldv, '0.5', '<=')) {
db_execute('ALTER TABLE plugin_snver_history MODIFY COLUMN data text');
}
if (cacti_version_compare($oldv, '0.6', '<=')) {
db_execute('ALTER TABLE plugin_snver_history DROP primary key');
}

}
}

Expand Down
27 changes: 18 additions & 9 deletions snver.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,27 @@

$number_of_hosts = read_config_option('snver_hosts_processed');

$out = plugin_snver_get_info(get_filter_request_var('host_id', FILTER_VALIDATE_INT));
$id = get_filter_request_var('host_id', FILTER_VALIDATE_INT);

print $out;
$alive = db_fetch_row_prepared ("SELECT * FROM host
WHERE id = ? AND disabled != 'on' AND status BETWEEN 2 AND 3", array($id));

print '<br/><br/>';
if (!$alive) {
print 'Disabled/down device. Nothing to do<br/><br/>';
} else {

if ($number_of_hosts > 0) {
print plugin_snver_get_history(get_request_var('host_id'),$out);
$out = plugin_snver_get_info($id);

}
else {
print 'History data store disabled';
}
print $out;

print '<br/><br/>';

if ($number_of_hosts > 0) {
print plugin_snver_get_history($id,$out);

}
else {
print 'History data store disabled';
}
}

29 changes: 22 additions & 7 deletions snver_tab.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
include_once('./lib/snmp.php');
include_once('./plugins/snver/functions.php');

if (!isempty_request_var('find')) {
set_request_var('action','find');
unset_request_var('host_id');
}

set_default_action();

Expand All @@ -41,6 +45,14 @@
get_allowed_ajax_hosts(true, 'applyFilter', $sql_where);

break;

case 'find':
general_header();
display_snver_form();
snver_find();
bottom_footer();

break;

default:
general_header();
Expand All @@ -50,6 +62,7 @@
}



function display_snver_form() {
global $config;

Expand All @@ -65,21 +78,25 @@ function display_snver_form() {
<tr>
<td>
<form name="form_snver" action="snver_tab.php">
<table width="30%" cellpadding="0" cellspacing="0">
<table width="50%" cellpadding="0" cellspacing="0">
<tr class="navigate_form">
<td>
<?php print html_host_filter(get_filter_request_var('host_id', FILTER_VALIDATE_INT), 'applyFilter', $host_where);?>

</td>
<td>
Find in stored data <input type='text' class='ui-button ui-corner-all ui-widget' name='find' id='find' value='<?php print get_request_var('find');?>'>
<input type='submit' class='ui-button ui-corner-all ui-widget' value='<?php print __('Find');?>'>
</td>
<td>
<!-- <input type='submit' class='ui-button ui-corner-all ui-widget' id='refresh' value='<?php print __('Go');?>' title='<?php print __esc('Set/Refresh Filters');?>'> //-->
<input type='button' class='ui-button ui-corner-all ui-widget' id='clear' value='<?php print __('Clear');?>' title='<?php print __esc('Clear Filters');?>'>
</td>
</tr>
</table>
</form>
</form>
</td>
<tr><td>
<tr>
<?php
html_end_box();

if (in_array(get_filter_request_var ('host_id'),snver_get_allowed_devices($_SESSION['sess_user_id'], true))) {
$out = plugin_snver_get_info(get_request_var('host_id'));
Expand All @@ -93,8 +110,6 @@ function display_snver_form() {
print 'History data store disabled';
}

print '</td></tr>';
html_end_box();
}
}

Expand Down

0 comments on commit 231853b

Please sign in to comment.