Skip to content

Commit

Permalink
improve display speed - move ntp check and analyse db to poller (run …
Browse files Browse the repository at this point in the history
…periodically and display only result)
  • Loading branch information
xmacan committed Nov 15, 2018
1 parent 6bc91ff commit 337a1b9
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 63 deletions.
2 changes: 1 addition & 1 deletion INFO
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[info]
name = intropage
version = 1.7
version = 1.8.1
longname = Intropage/Dashboard
author = Petr Macek
email = petr.macek@kostax.cz
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ If you find a problem, let me know via github or https://forums.cacti.net/viewto
Tomas Macek, Peter Michael Calum, Trevor Leadley, Earendil

## Changelog

1.8.1 ---
Fix javascript/jquery error (MSIE11 fix)
Move Analyse DB and NTP to poller and run periodically
1.8 ---
Add check for cacti and spine version
Expand Down
106 changes: 47 additions & 59 deletions include/data.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,47 +13,23 @@ function intropage_analyse_db() {
'detail' => '',
);

$damaged = 0;
$memtables = 0;
$db_check_level = read_config_option('intropage_analyse_db_level');

$tables = db_fetch_assoc('SHOW TABLES');
foreach ($tables as $key => $val) {
$row = db_fetch_row('check table ' . current($val) . ' ' . $db_check_level);

if (preg_match('/^note$/i', $row['Msg_type']) && preg_match('/doesn\'t support/i', $row['Msg_text'])) {
$memtables++;
} elseif (!preg_match('/OK/i', $row['Msg_text']) && !preg_match('/Table is already up to date/i', $row['Msg_text'])) {
$damaged++;
$result['detail'] .= 'Table ' . $row['Table'] . ' status ' . $row['Msg_text'] . '<br/>';
}
$result['alarm'] = db_fetch_cell("SELECT value from plugin_intropage_trends where name='db_check_alarm'");
$result['data'] = db_fetch_cell("SELECT value from plugin_intropage_trends where name='db_check_result'");
$result['detail'] = db_fetch_cell("SELECT value from plugin_intropage_trends where name='db_check_detail'");

$result['data'] .= '<br/><br/>Last check: ' . db_fetch_cell("SELECT date from plugin_intropage_trends where name='db_check_result'") . '<br/>';
$often = read_config_option('intropage_analyse_db_interval');
if ($often == 900) {
$result['data'] .= 'Checked every 15 minutes';
}

if ($damaged > 0) {
$result['alarm'] = 'red';
$result['data'] = '<span class="txt_big">DB problem</span><br/><br/>';
} else {
$result['data'] = '<span class="txt_big">DB OK</span><br/><br/>';
}

// connection errors
$cerrors = 0;
$con_err = db_fetch_assoc("SHOW GLOBAL STATUS LIKE '%Connection_errors%'");

foreach ($con_err as $key => $val) {
$cerrors = $cerrors + $val['Value'];
}

if ($cerrors > 0) { // only yellow
$result['detail'] .= 'Connection errors - try to restart database. <br/>';

if ($result['alarm'] == 'green') {
$result['alarm'] = 'yellow';
}
elseif ($often == 3600) {
$result['data'] .= 'Checked hourly';
}
else {
$result['data'] .= 'Checked daily';
}

$result['data'] .= 'Connection errors: ' . $cerrors . '<br/>';
$result['data'] .= 'Damaged tables: ' . $damaged . '<br/>Memory tables: ' . $memtables . '<br/>All tables: ' . count($tables);

$result['data'] .= '<br/><br/>';

return $result;
}
Expand Down Expand Up @@ -1059,27 +1035,39 @@ function intropage_ntp() {
);

$ntp_server = read_config_option('intropage_ntp_server');

if (filter_var($ntp_server, FILTER_VALIDATE_IP) || preg_match('/^(([a-z0-9]|[a-z0-9][a-z0-9\-]*[a-z0-9])\.)*([a-z0-9]|[a-z0-9][a-z0-9\-]*[a-z])$/i', $ntp_server)) {
$ntp_time = ntp_time($ntp_server);
if ($ntp_time > 0) {
$diff_time = date('U') - $ntp_time;
if ($diff_time < -600 || $diff_time > 600) {
$result['alarm'] = 'red';
$result['data'] = '<span class="txt_big">' . date('Y-m-d') . '<br/>'. date('H:i:s') . "</span><br/><br/>Please check time.<br/>It is different (more than $diff_time seconds) from NTP server $ntp_server";
} elseif ($diff_time < -120 || $diff_time > 120) {
$values['time']['alarm'] = 'yellow';
$values['time']['data'] = '<span class="txt_big">' . date('Y-m-d') . '<br/>' . date('H:i:s') . "</span><br/><br/>Please check time.<br/>It is different (more than $diff_time seconds) from NTP server $ntp_server";
} else {
$result['data'] = '<span class="txt_big">' . date('Y-m-d') . '<br/>' . date('H:i:s') . "</span><br/><br/>Localtime is equal to NTP server<br/>$ntp_server";
}
} else {
$result['alarm'] = 'red';
$result['data'] = 'Unable to contact the NTP server indicated.<br/>Please check your configuration';
}
} else {

$diff_time = db_fetch_cell("SELECT value from plugin_intropage_trends where name='ntp_diff_time'");

if ($diff_time === false) {
$result['alarm'] = 'yellow';
$result['data'] = 'Waiting for data<br/>';
}
elseif ($diff_time != "error") {
if ($diff_time < -600 || $diff_time > 600) {
$result['alarm'] = 'red';
$result['data'] = 'Incorrect ntp server address, please insert IP or DNS name';
$result['data'] = '<span class="txt_big">' . date('Y-m-d') . '<br/>'. date('H:i:s') . "</span><br/><br/>Please check time.<br/>It is different (more than $diff_time seconds) from NTP server $ntp_server<br/>";
} elseif ($diff_time < -120 || $diff_time > 120) {
$result['alarm'] = 'yellow';
$result['data'] = '<span class="txt_big">' . date('Y-m-d') . '<br/>' . date('H:i:s') . "</span><br/><br/>Please check time.<br/>It is different (more than $diff_time seconds) from NTP server $ntp_server<br/>";
} else {
$result['data'] = '<span class="txt_big">' . date('Y-m-d') . '<br/>' . date('H:i:s') . "</span><br/><br/>Localtime is equal to NTP server<br/>$ntp_server<br/>";
}
}
else {
$result['alarm'] = 'red';
$result['data'] = 'Unable to contact the NTP server indicated.<br/>Please check your configuration.<br/>';
}

$result['data'] .= '<br/>Last check: ' . db_fetch_cell("SELECT date from plugin_intropage_trends where name='ntp_diff_time'") . '<br/>';
$often = read_config_option('intropage_ntp_interval');
if ($often == 900) {
$result['data'] .= 'Checked every 15 minutes';
}
elseif ($often == 3600) {
$result['data'] .= 'Checked hourly';
}
else {
$result['data'] .= 'Checked daily';
}

return $result;
Expand Down
10 changes: 9 additions & 1 deletion include/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ function ntp_time($host) {
$timestamp = -1;
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);

$timeout = array('sec' => 1, 'usec' => 500000);
$timeout = array('sec' => 1, 'usec' => 400000);
socket_set_option($sock, SOL_SOCKET, SO_RCVTIMEO, $timeout);
socket_clear_error();

Expand All @@ -341,7 +341,14 @@ function ntp_time($host) {
// Unix time is seconds since 0000 UT on 1 January 1970
$timestamp -= 2208988800;
}
else {
$timestamp = "error";
}
}
else {
$timestamp = "error";
}

return $timestamp;
}

Expand All @@ -366,3 +373,4 @@ function intropage_graph_button($data) {
}
}
}

16 changes: 16 additions & 0 deletions include/variables.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@
'default' => '1000',
),

'intropage_analyse_db_interval' => array(
'friendly_name' => 'How often analyze DB',
'description' => '<strong>Poller runs this task. It could cause long poller run.</strong>',
'method' => 'drop_array',
'array' => array('900' => 'Every 15 minutes', '3600' => 'Every hour', '86400' => 'Every day'),
'default' => '86400',
),

'intropage_analyse_db_level' => array(
'friendly_name' => 'Analyse DB - Level of db check',
'description' => 'Quick - no check rows for inforccert links<br/>Fast - check only not properly closed tables<br/>Changed - check tables changed from last check<br/>Medium - with rows scan<br/>Extended - full rows and keys<br/><strong>Medium and extended may causes slow page load!</strong>',
Expand All @@ -78,6 +86,14 @@
'default' => 'pool.ntp.org',
),

'intropage_ntp_interval' => array(
'friendly_name' => 'How often check NTP',
'description' => '<strong>Poller runs this task. It could cause long poller run.</strong>',
'method' => 'drop_array',
'array' => array('900' => 'Every 15 minutes', '3600' => 'Every hour', '86400' => 'Every day'),
'default' => '3600',
),

'intropage_admin_alert' => array(
'friendly_name' => 'Admin information panel about maintenance tasks, down devices, ..',
'description' => 'If isn\'t empty, panel will be displayed on the top. You can use html tags (b, i, ...).',
Expand Down
86 changes: 84 additions & 2 deletions setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ function intropage_setup_database() {
}

$data = array();
$data['columns'][] = array('name' => 'date', 'type' => 'timestamp', 'default' => '0000-00-00 00:00:00', 'NULL' => false);
$data['columns'][] = array('name' => 'date', 'type' => 'datetime');
$data['columns'][] = array('name' => 'name', 'type' => 'varchar(50)', 'NULL' => false, 'default' => '0');
$data['columns'][] = array('name' => 'value', 'type' => 'varchar(50)', 'NULL' => false, 'default' => '0');
$data['columns'][] = array('name' => 'value', 'type' => 'varchar(250)', 'NULL' => false, 'default' => '0');
$data['type'] = 'MyISAM';
$data['comment'] = 'trends';
api_plugin_db_table_create('intropage', 'plugin_intropage_trends', $data);
Expand Down Expand Up @@ -174,6 +174,8 @@ function intropage_setup_database() {
}

function intropage_poller_bottom() {
global $config;

$start = db_fetch_cell('SELECT min(start_time) from poller_time');

// poller stats
Expand All @@ -198,4 +200,84 @@ function intropage_poller_bottom() {
if (db_fetch_cell("SELECT directory FROM plugin_config where directory='thold' and status=1")) {
db_execute("insert into plugin_intropage_trends (name,date,value) select 'thold', now(), COUNT(*) FROM thold_data WHERE (thold_data.thold_alert!=0 OR thold_data.bl_fail_count >= thold_data.bl_fail_trigger)");
}

// check NTP
$last = db_fetch_cell("SELECT unix_timestamp(date) as `date` from plugin_intropage_trends where name='ntp_diff_time'");

if (!$last) {
db_execute("insert into plugin_intropage_trends (name,date,value) values ('ntp_diff_time', now(), '0')");
$last = 0;
}

if (time() > $last + read_config_option('intropage_ntp_interval')) {
include_once($config['base_path'] . '/plugins/intropage/include/helpers.php');
$ntp_server = read_config_option('intropage_ntp_server');
$ntp_time = ntp_time($ntp_server);
$diff_time = date('U') - $ntp_time;
db_execute("update plugin_intropage_trends set date=now(), value=$diff_time where name='ntp_diff_time'");
}

// check db
$last = db_fetch_cell("SELECT unix_timestamp(date) as `date` from plugin_intropage_trends where name='db_check_alarm'");

if (!$last) {
db_execute("insert into plugin_intropage_trends (name,date,value) values ('db_check_result', now(), 'Waiting for data')");
db_execute("insert into plugin_intropage_trends (name,date,value) values ('db_check_alarm', now(), 'yellow')");
db_execute("insert into plugin_intropage_trends (name,date,value) values ('db_check_detail', now(), NULL)");
$last = 0;
}

if (time() > $last + read_config_option('intropage_analyse_db_interval')) {
include_once($config['base_path'] . '/plugins/intropage/include/helpers.php');

$damaged = 0;
$memtables = 0;
$db_check_level = read_config_option('intropage_analyse_db_level');
$text_result = '';
$text_detail = '';
$alarm = 'green';

$tables = db_fetch_assoc('SHOW TABLES');
foreach ($tables as $key => $val) {
$row = db_fetch_row('check table ' . current($val) . ' ' . $db_check_level);

if (preg_match('/^note$/i', $row['Msg_type']) && preg_match('/doesn\'t support/i', $row['Msg_text'])) {
$memtables++;
} elseif (!preg_match('/OK/i', $row['Msg_text']) && !preg_match('/Table is already up to date/i', $row['Msg_text'])) {
$damaged++;
$text_detail .= 'Table ' . $row['Table'] . ' status ' . $row['Msg_text'] . '<br/>';
}
}

if ($damaged > 0) {
$alarm = 'red';
$text_result = '<span class="txt_big">DB problem</span><br/><br/>';
} else {
$text_result = '<span class="txt_big">DB OK</span><br/><br/>';
}

// connection errors
$cerrors = 0;
$con_err = db_fetch_assoc("SHOW GLOBAL STATUS LIKE '%Connection_errors%'");

foreach ($con_err as $key => $val) {
$cerrors = $cerrors + $val['Value'];
}

if ($cerrors > 0) { // only yellow
$text_detail .= 'Connection errors - try to restart database. <br/>';

if ($alarm == 'green') {
$alarm = 'yellow';
}
}
$text .= 'Connection errors: ' . $cerrors . '<br/>';
$text .= 'Damaged tables: ' . $damaged . '<br/>Memory tables: ' . $memtables . '<br/>All tables: ' . count($tables);

db_execute("update plugin_intropage_trends set date=now(), value='$text' where name='db_check_result'");
db_execute("update plugin_intropage_trends set date=now(), value='$alarm' where name='db_check_alarm'");
db_execute("update plugin_intropage_trends set date=now(), value='$text2' where name='db_check_detail'");

}

}

0 comments on commit 337a1b9

Please sign in to comment.