| version 1.4, 2003/11/14 09:53:51 |
version 1.5, 2003/11/24 18:35:45 |
|
|
| # 16/10/2003 - added automatic database reconnecting (nepto) |
# 16/10/2003 - added automatic database reconnecting (nepto) |
| # |
# |
| |
|
| # $Platon: scripts/syswatcher/syswatcher.pl,v 1.3 2003/11/13 17:57:20 rajo Exp $ |
# $Platon: scripts/syswatcher/syswatcher.pl,v 1.4 2003/11/14 09:53:51 rajo Exp $ |
| |
|
| use strict; |
use strict; |
| |
|
|
|
| $sql_loadavg |
$sql_loadavg |
| $sql_meminfo |
$sql_meminfo |
| |
|
| $update_interval $time_adjust_step |
$update_interval |
| $max_cpu_usage $fixed_time_diff |
|
| ); |
); |
| |
|
| # functions |
# functions |
| Line 44 $database = "DBI:mysql:database=system;h |
|
| Line 43 $database = "DBI:mysql:database=system;h |
|
| |
|
| # time between 2 inserts or reconnects: |
# time between 2 inserts or reconnects: |
| # range of this value: (2, 1E6) |
# range of this value: (2, 1E6) |
| $update_interval = 10; #seconds |
$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 |
| Line 62 $fixed_time_diff = 0.0028; # seconds - f |
|
| Line 51 $fixed_time_diff = 0.0028; # seconds - f |
|
| |
|
| 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; |
Time::HiRes::sleep ($update_interval - (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()); |
| |
|
| $after_sec = Time::HiRes::gettimeofday; |
Time::HiRes::sleep ($update_interval - (Time::HiRes::gettimeofday % $update_interval)); |
| $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 |