| version 1.3, 2003/11/13 17:57:20 |
version 1.4, 2003/11/14 09:53:51 |
|
|
| # 16/10/2003 - added automatic database reconnecting (nepto) |
# 16/10/2003 - added automatic database reconnecting (nepto) |
| # |
# |
| |
|
| # $Platon: scripts/syswatcher/syswatcher.pl,v 1.2 2003/11/13 17:28:52 rajo Exp $ |
# $Platon: scripts/syswatcher/syswatcher.pl,v 1.3 2003/11/13 17:57:20 rajo Exp $ |
| |
|
| use strict; |
use strict; |
| |
|
| use DBI; |
use DBI; |
| use Time::HiRes qw(gettimeofday usleep); |
use Time::HiRes qw(gettimeofday sleep usleep); |
| use POSIX qw(strftime); |
use POSIX qw(strftime); |
| |
|
| # declaration: {{{ |
# declaration: {{{ |
|
|
| $sql_loadavg |
$sql_loadavg |
| $sql_meminfo |
$sql_meminfo |
| |
|
| $update_interval |
$update_interval $time_adjust_step |
| |
$max_cpu_usage $fixed_time_diff |
| ); |
); |
| |
|
| # functions |
# functions |
|
|
| $database = "DBI:mysql:database=system;host=localhost;user=syswatcher;password=;compression=0"; |
$database = "DBI:mysql:database=system;host=localhost;user=syswatcher;password=;compression=0"; |
| |
|
| # time between 2 inserts or reconnects: |
# time between 2 inserts or reconnects: |
| $update_interval = 10; #seconds |
# range of this value: (2, 1E6) |
| |
$update_interval = 10; #seconds |
| |
|
| |
# time adjust step - percentage of $update_interval |
| |
$time_adjust_step = 0.20; |
| |
|
| |
# max. 5% of CPU used by this script |
| |
# range of this value: (0.001, 0.999) |
| |
$max_cpu_usage = 0.05; |
| |
|
| |
# time needed for other resources |
| |
$fixed_time_diff = 0.0028; # seconds - for Pentium 500MHz |
| |
|
| # |
# |
| # MAIN |
# MAIN |
| # |
# |
| |
|
| print_info("PID = $$ $0 started"); |
print_info("PID = $$ $0 started"); |
| |
|
| |
# convert $time_adjust_step from percent to seconds |
| |
$time_adjust_step = int ($time_adjust_step * $update_interval); |
| |
$time_adjust_step = $time_adjust_step < 1 ? 1 : $time_adjust_step; |
| |
|
| while (1) { |
while (1) { |
| |
my ($before_sec, $after_sec, $time_diff); |
| |
|
| |
|
| while (! ($dbh = DBI->connect($database))) { |
while (! ($dbh = DBI->connect($database))) { |
| print_info("Database connection failed, reconnecting in $update_interval seconds...($DBI::errstr)"); |
print_info("Database connection failed, reconnecting in $update_interval seconds...($DBI::errstr)"); |
|
|
| |
|
| PrepareSQL(); |
PrepareSQL(); |
| |
|
| |
$before_sec = Time::HiRes::gettimeofday - $update_interval; |
| while ($dbh) { |
while ($dbh) { |
| |
$fixed_time_diff = Time::HiRes::gettimeofday - $before_sec - $update_interval; |
| |
$fixed_time_diff = $fixed_time_diff < 0 ? 0 : $fixed_time_diff; |
| |
$before_sec = Time::HiRes::gettimeofday; |
| |
|
| last if (! loadavg()); |
last if (! loadavg()); |
| last if (! meminfo()); |
last if (! meminfo()); |
| sleep $update_interval; |
|
| |
$after_sec = Time::HiRes::gettimeofday; |
| |
$time_diff = $after_sec - $before_sec; |
| |
if ($time_diff > $max_cpu_usage * $update_interval) { |
| |
my $min_interval = $time_diff / $max_cpu_usage; |
| |
$update_interval = $time_adjust_step * (1 + int ($min_interval / $time_adjust_step)); |
| |
# make $update_interval: $update_interval at least 2 |
| |
$update_interval = $update_interval < 2 ? 2 : $update_interval; |
| |
print_info("Time difference is $time_diff seconds, setting update interval to $update_interval ($min_interval needed, $fixed_time_diff fixed time diff)\n"); |
| |
} |
| |
Time::HiRes::sleep ($update_interval - $time_diff - $fixed_time_diff); |
| |
|
| my $modify_time = -86400.0 * -M $0; |
my $modify_time = -86400.0 * -M $0; |
| if (defined $modify_time && $modify_time > 0) { # Autorestart - if file exists and is modified |
if (defined $modify_time && $modify_time > 0) { # Autorestart - if file exists and is modified |