From fe7bd3cad4ecf991dae899e5051028cf00f1dc44 Mon Sep 17 00:00:00 2001 From: mdtrooper Date: Tue, 11 Sep 2012 16:30:55 +0000 Subject: [PATCH] 2012-09-11 Miguel de Dios * lib/PandoraFMS/Core.pm: changed to use own code instead the external module Time::Piece. * lib/PandoraFMS/Tools.pm: added function "month_have_days" for to get the count of days for any month in any year. git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@6955 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f --- pandora_server/ChangeLog | 8 ++ pandora_server/lib/PandoraFMS/Core.pm | 152 ++++++++++++++++--------- pandora_server/lib/PandoraFMS/Tools.pm | 126 ++++++++++++-------- 3 files changed, 186 insertions(+), 100 deletions(-) diff --git a/pandora_server/ChangeLog b/pandora_server/ChangeLog index 8c5792372d..b6a5d90c2b 100644 --- a/pandora_server/ChangeLog +++ b/pandora_server/ChangeLog @@ -1,3 +1,11 @@ +2012-09-11 Miguel de Dios + + * lib/PandoraFMS/Core.pm: changed to use own code instead the + external module Time::Piece. + + * lib/PandoraFMS/Tools.pm: added function "month_have_days" for + to get the count of days for any month in any year. + 2012-09-10 Vanessa Gil * lib/PandoraFMS/Core.pm: Added several fields diff --git a/pandora_server/lib/PandoraFMS/Core.pm b/pandora_server/lib/PandoraFMS/Core.pm index 34ebd84d03..dfa5f74e72 100644 --- a/pandora_server/lib/PandoraFMS/Core.pm +++ b/pandora_server/lib/PandoraFMS/Core.pm @@ -111,12 +111,6 @@ use HTML::Entities; use Time::Local; use POSIX qw(strftime); -#This library need for planned downtimes for periodical weekly and monthly -use Time::Piece; -#And the with $var = localtime(); -#print "Day in the week is ", $var1->_wday -#if it is sunday return 0 instead to 7 - # Force XML::Simple to use XML::Parser instead SAX to manage XML # due a bug processing some XML with blank spaces. # See http://www.perlmonks.org/?node_id=706838 @@ -1360,10 +1354,19 @@ Start the planned downtime, the monthly type. ######################################################################## sub pandora_planned_downtime_monthly_start($$) { my ($pa_config, $dbh) = @_; - my $local_time = localtime(); - my $number_day_month = $local_time->mday; - my $number_last_day_month = $local_time->month_last_day; - my $time = $local_time->hms; + #my $local_time = localtime(); + my @var_localtime = localtime(time); + my $year = $var_localtime[5] + 1900; + my $month = $var_localtime[4]; + + #my $number_day_month = $local_time->mday; + my $number_day_month = $var_localtime[3]; + + #my $number_last_day_month = $local_time->month_last_day; + my $number_last_day_month = month_have_days($month, $year); + + #my $time = $local_time->hms; + my $time = $var_localtime[2] . ":" . $var_localtime[1] . ":" . $var_localtime[0]; # Start pending downtimes my @downtimes = get_db_rows($dbh, 'SELECT * @@ -1374,14 +1377,17 @@ sub pandora_planned_downtime_monthly_start($$) { foreach my $downtime (@downtimes) { #Convert to identical type. - my $date_downtime = Time::Piece->strptime( - $downtime->{'periodically_time_from'}, - "%H:%M:%S"); - my $date_now_time = Time::Piece->strptime( - $time, - "%H:%M:%S"); - if ($date_now_time >= $date_downtime) { + #my $date_downtime = Time::Piece->strptime( + # $downtime->{'periodically_time_from'}, + # "%H:%M:%S"); + #my $date_now_time = Time::Piece->strptime( + # $time, + # "%H:%M:%S"); + # + #if ($date_now_time >= $date_downtime) { + if (($time gt $downtime->{'periodically_time_from'}) + || ($time eq $downtime->{'periodically_time_from'})) { if (!defined($downtime->{'description'})) { $downtime->{'description'} = "N/A"; @@ -1391,7 +1397,7 @@ sub pandora_planned_downtime_monthly_start($$) { $downtime->{'name'} = "N/A"; } - logger($pa_config, "Starting planned downtime '" . $downtime->{'name'} . "'.", 1); + logger($pa_config, "Starting planned monthly downtime '" . $downtime->{'name'} . "'.", 1); db_do($dbh, 'UPDATE tplanned_downtime SET executed = 1 @@ -1422,10 +1428,19 @@ Start the planned downtime, the montly type. ######################################################################## sub pandora_planned_downtime_monthly_stop($$) { my ($pa_config, $dbh) = @_; - my $local_time = localtime(); - my $number_day_month = $local_time->mday; - my $number_last_day_month = $local_time->month_last_day; - my $time = $local_time->hms; + #my $local_time = localtime(); + my @var_localtime = localtime(time); + my $year = $var_localtime[5] + 1900; + my $month = $var_localtime[4]; + + #my $number_day_month = $local_time->mday; + my $number_day_month = $var_localtime[3]; + + #my $number_last_day_month = $local_time->month_last_day; + my $number_last_day_month = month_have_days($month, $year); + + #my $time = $local_time->hms; + my $time = $var_localtime[2] . ":" . $var_localtime[1] . ":" . $var_localtime[0]; #With this stop the planned downtime for 31 (or 30) day in months # with less days. @@ -1451,14 +1466,16 @@ sub pandora_planned_downtime_monthly_stop($$) { foreach my $downtime (@downtimes) { #Convert to identical type. - my $date_downtime = Time::Piece->strptime( - $downtime->{'periodically_time_to'}, - "%H:%M:%S"); - my $date_now_time = Time::Piece->strptime( - $time, - "%H:%M:%S"); - - if ($date_now_time <= $date_downtime) { + #my $date_downtime = Time::Piece->strptime( + # $downtime->{'periodically_time_to'}, + # "%H:%M:%S"); + #my $date_now_time = Time::Piece->strptime( + # $time, + # "%H:%M:%S"); + # + #if ($date_now_time <= $date_downtime) { + if (($time lt $downtime->{'periodically_time_to'}) + || ($time eq $downtime->{'periodically_time_to'})) { if (!defined($downtime->{'description'})) { $downtime->{'description'} = "N/A"; @@ -1468,7 +1485,7 @@ sub pandora_planned_downtime_monthly_stop($$) { $downtime->{'name'} = "N/A"; } - logger($pa_config, "Stopping planned downtime '" . $downtime->{'name'} . "'.", 1); + logger($pa_config, "Stopping planned monthly downtime '" . $downtime->{'name'} . "'.", 1); db_do($dbh, 'UPDATE tplanned_downtime SET executed = 0 @@ -1498,9 +1515,15 @@ Start the planned downtime, the montly type. ######################################################################## sub pandora_planned_downtime_weekly_start($$) { my ($pa_config, $dbh) = @_; - my $local_time = localtime(); - my $number_day_week = $local_time->_wday; - my $time = $local_time->hms; + #my $local_time = localtime(); + my @var_localtime = localtime(time); + + #my $number_day_week = $local_time->_wday; + my $number_day_week = $var_localtime[6]; + + #my $time = $local_time->hms; + my $time = $var_localtime[2] . ":" . $var_localtime[1] . ":" . $var_localtime[0]; + my $found = 0; # Start pending downtimes @@ -1540,16 +1563,22 @@ sub pandora_planned_downtime_weekly_start($$) { $found = 1; } + logger($pa_config, "Debug time = " . $time, 5); + logger($pa_config, "Debug periodically_time_to = " . + $downtime->{'periodically_time_to'}, 5); + if ($found) { #Convert to identical type. - my $date_downtime = Time::Piece->strptime( - $downtime->{'periodically_time_from'}, - "%H:%M:%S"); - my $date_now_time = Time::Piece->strptime( - $time, - "%H:%M:%S"); - - if ($date_now_time >= $date_downtime) { + #my $date_downtime = Time::Piece->strptime( + # $downtime->{'periodically_time_from'}, + # "%H:%M:%S"); + #my $date_now_time = Time::Piece->strptime( + # $time, + # "%H:%M:%S"); + # + #if ($date_now_time >= $date_downtime) { + if (($time gt $downtime->{'periodically_time_from'}) + || ($time eq $downtime->{'periodically_time_from'})) { if (!defined($downtime->{'description'})) { $downtime->{'description'} = "N/A"; } @@ -1558,7 +1587,7 @@ sub pandora_planned_downtime_weekly_start($$) { $downtime->{'name'} = "N/A"; } - logger($pa_config, "Starting planned downtime '" . $downtime->{'name'} . "'.", 1); + logger($pa_config, "Starting planned weekly downtime '" . $downtime->{'name'} . "'.", 1); db_do($dbh, 'UPDATE tplanned_downtime SET executed = 1 @@ -1589,9 +1618,15 @@ Stop the planned downtime, the montly type. ######################################################################## sub pandora_planned_downtime_weekly_stop($$) { my ($pa_config, $dbh) = @_; - my $local_time = localtime(); - my $number_day_week = $local_time->_wday; - my $time = $local_time->hms; + #my $local_time = localtime(); + my @var_localtime = localtime(time); + + #my $number_day_week = $local_time->_wday; + my $number_day_week = $var_localtime[6]; + + #my $time = $local_time->hms; + my $time = $var_localtime[2] . ":" . $var_localtime[1] . ":" . $var_localtime[0]; + my $found = 0; # Start pending downtimes @@ -1602,14 +1637,21 @@ sub pandora_planned_downtime_weekly_stop($$) { foreach my $downtime (@downtimes) { #Convert to identical type. - my $date_downtime = Time::Piece->strptime( - $downtime->{'periodically_time_to'}, - "%H:%M:%S"); - my $date_now_time = Time::Piece->strptime( - $time, - "%H:%M:%S"); - - if ($date_now_time <= $date_downtime) { + #my $date_downtime = Time::Piece->strptime( + # $downtime->{'periodically_time_to'}, + # "%H:%M:%S"); + #my $date_now_time = Time::Piece->strptime( + # $time, + # "%H:%M:%S"); + # + #if ($date_now_time <= $date_downtime) { + + logger($pa_config, "Debug time = " . $time, 5); + logger($pa_config, "Debug periodically_time_to = " . + $downtime->{'periodically_time_to'}, 5); + + if (($time lt $downtime->{'periodically_time_to'}) + || ($time eq $downtime->{'periodically_time_to'})) { if (!defined($downtime->{'description'})) { $downtime->{'description'} = "N/A"; @@ -1619,7 +1661,7 @@ sub pandora_planned_downtime_weekly_stop($$) { $downtime->{'name'} = "N/A"; } - logger($pa_config, "Starting planned downtime '" . $downtime->{'name'} . "'.", 1); + logger($pa_config, "Stopping planned weekly downtime '" . $downtime->{'name'} . "'.", 1); db_do($dbh, 'UPDATE tplanned_downtime SET executed = 0 diff --git a/pandora_server/lib/PandoraFMS/Tools.pm b/pandora_server/lib/PandoraFMS/Tools.pm index f6bfb2cb85..6475a668cf 100644 --- a/pandora_server/lib/PandoraFMS/Tools.pm +++ b/pandora_server/lib/PandoraFMS/Tools.pm @@ -66,6 +66,7 @@ our @EXPORT = qw( ticks_totime safe_input safe_output + month_have_days ); ######################################################################## @@ -798,13 +799,13 @@ Returns: ############################################################################## sub pandora_ping ($$) { my ($pa_config, $host) = @_; - + my $output = 0; my $i; - + # See codes on http://perldoc.perl.org/perlport.html#PLATFORMS my $OSNAME = $^O; - + # Windows XP .. Windows 7 if (($OSNAME eq "MSWin32") || ($OSNAME eq "MSWin32-x64") || ($OSNAME eq "cygwin")){ my $ms_timeout = $pa_config->{'networktimeout'} * 1000; @@ -817,17 +818,17 @@ sub pandora_ping ($$) { } return 0; } - + elsif ($OSNAME eq "solaris"){ my $ping_command = "ping"; - + if ($host =~ /\d+:|:\d+/ ) { $ping_command = "ping -A inet6" } - + # Note: timeout option is not implemented in ping. # 'networktimeout' is not used by ping on Solaris. - + # Ping the host for ($i=0; $i < $pa_config->{'icmp_checks'}; $i++) { `$ping_command -s -n $host 56 1 >/dev/null 2>&1`; @@ -838,17 +839,17 @@ sub pandora_ping ($$) { } return 0; } - + elsif ($OSNAME eq "freebsd"){ my $ping_command = "ping -t $pa_config->{'networktimeout'}"; - + if ($host =~ /\d+:|:\d+/ ) { $ping_command = "ping6"; } - + # Note: timeout(-t) option is not implemented in ping6. # 'networktimeout' is not used by ping6 on FreeBSD. - + # Ping the host for ($i=0; $i < $pa_config->{'icmp_checks'}; $i++) { `$ping_command -q -n -c 1 $host >/dev/null 2>&1`; @@ -859,16 +860,16 @@ sub pandora_ping ($$) { } return 0; } - + # by default LINUX calls else { - + my $ping_command = "ping"; - + if ($host =~ /\d+:|:\d+/ ) { $ping_command = "ping6"; } - + # Ping the host for ($i=0; $i < $pa_config->{'icmp_checks'}; $i++) { `$ping_command -q -W $pa_config->{'networktimeout'} -n -c 1 $host >/dev/null 2>&1`; @@ -879,117 +880,152 @@ sub pandora_ping ($$) { } return 0; } - + return $output; } -############################################################################## +######################################################################## =head2 C<< pandora_ping_latency (I<$pa_config>, I<$host>) >> Ping the given host. Returns the average round-trip time. =cut -############################################################################## +######################################################################## sub pandora_ping_latency ($$) { my ($pa_config, $host) = @_; - + my $output = 0; - + # See codes on http://perldoc.perl.org/perlport.html#PLATFORMS my $OSNAME = $^O; - + # Windows XP .. Windows 2008, I assume Win7 is the same if (($OSNAME eq "MSWin32") || ($OSNAME eq "MSWin32-x64") || ($OSNAME eq "cygwin")){ - + # System ping reports in different languages, but with the same format: # Mínimo = xxms, Máximo = xxms, Media = XXms # Minimun = xxms, Mamimun = xxms, Average = XXms - + # If this fails, ping can be replaced by fping which also have the same format # but always in english - + my $ms_timeout = $pa_config->{'networktimeout'} * 1000; $output = `ping -n $pa_config->{'icmp_checks'} -w $ms_timeout $host`; - + if ($output =~ m/\=\s([0-9]*)[a-z][a-z]\r/){ return $1; } else { return 0; } - + } - + elsif ($OSNAME eq "solaris"){ my $ping_command = "ping"; - + if ($host =~ /\d+:|:\d+/ ) { $ping_command = "ping -A inet6"; } - + # Note: timeout option is not implemented in ping. # 'networktimeout' is not used by ping on Solaris. - + # Ping the host my @output = `$ping_command -s -n $host 56 $pa_config->{'icmp_checks'} 2>/dev/null`; - + # Something went wrong return 0 if ($? != 0); - + # Parse the output my $stats = pop (@output); return 0 unless ($stats =~ m/([\d\.]+)\/([\d\.]+)\/([\d\.]+)\/([\d\.]+) +ms/); return $2; } - + elsif ($OSNAME eq "freebsd"){ my $ping_command = "ping"; - + if ($host =~ /\d+:|:\d+/ ) { $ping_command = "ping6"; } - + # Note: timeout(-t) option is not implemented in ping6. # timeout(-t) and waittime(-W) options in ping are not the same as # Linux. On latency, there are no way to set timeout. # 'networktimeout' is not used on FreeBSD. - + # Ping the host my @output = `$ping_command -q -n -c $pa_config->{'icmp_checks'} $host 2>/dev/null`; - + # Something went wrong return 0 if ($? != 0); - + # Parse the output my $stats = pop (@output); return 0 unless ($stats =~ m/([\d\.]+)\/([\d\.]+)\/([\d\.]+)\/([\d\.]+) +ms/); return $2; } - + # by default LINUX calls else { my $ping_command = "ping"; - + if ($host =~ /\d+:|:\d+/ ) { $ping_command = "ping6"; } - - + + # Ping the host my @output = `$ping_command -q -W $pa_config->{'networktimeout'} -n -c $pa_config->{'icmp_checks'} $host 2>/dev/null`; - + # Something went wrong return 0 if ($? != 0); - + # Parse the output my $stats = pop (@output); return 0 unless ($stats =~ m/([\d\.]+)\/([\d\.]+)\/([\d\.]+)\/([\d\.]+) +ms/); return $2; } - + # If no valid get values until now, just return with empty value (not valid) return $output; } +######################################################################## +=head2 C<< month_have_days (I<$month>, I<$year>) >> + +Pass a $month (as january 0 number and each month with numbers) and the year +as number (for example 1981). And return the days of this month. + +=cut +######################################################################## +sub month_have_days($$) { + my $month= shift(@_); + my $year= @_ ? shift(@_) : (1900 + (localtime())[5]); + + my @monthDays= qw( 31 28 31 30 31 30 31 31 30 31 30 31 ); + + if ( $year <= 1752 ) { + # Note: Although September 1752 only had 19 days, + # they were numbered 1,2,14..30! + if (1752 == $year && 9 == $month) { + return 19; + } + if (2 == $month && 0 == $year % 4) { + return 29; + } + } + else { + #Check if Leap year + if (2 == $month && 0 == $year % 4 && 0 == $year%100 + || 0 == $year%400) { + return 29; + } + } + + return $monthDays[$month]; +} + # End of function declaration # End of defined Code