0

I wanted to add some debug logging to a Perl script I created as a Nagios plugin. It works perfectly when started from the command line, but when it is being called by Nagios, 'localtime' returns the time the nagios3 daemon was last restarted (yesterday afternoon, in this case).

Running Perl v5.14.2 and Nagios 3.2.3 on Ubuntu Server 12.04 LTS.

script (partial):

#!/usr/bin/perl
use POSIX;

my $result = 0;
my $IP=$ARGV[0];

# actual processing here...

open (LOGFILE, ">>/var/log/nagios3/check_pisystem.log");
print LOGFILE strftime "%F %T%z (%Z)", localtime $^T;
print LOGFILE "," . $IP . "," . $result . "\n";
close (LOGFILE);

Logfile contents after running for a while: the lines at 10:xx were added by manually starting the script from the shell prompt. The other lines are the result of it being called by nagios, 4 different IP's at 5 minute intervals (each).

2013-07-29 15:18:24+0200 (CEST),10.3.4.83,0
2013-07-29 15:18:24+0200 (CEST),10.3.3.83,0
2013-07-30 10:24:51+0200 (CEST),10.3.0.83,0
2013-07-29 15:18:24+0200 (CEST),10.3.1.83,1
...
2013-07-29 15:18:24+0200 (CEST),10.3.3.83,1
2013-07-29 15:18:24+0200 (CEST),10.3.1.83,0
2013-07-29 15:18:24+0200 (CEST),10.3.0.83,0
2013-07-29 15:18:24+0200 (CEST),10.3.4.83,0
2013-07-30 10:46:54+0200 (CEST),10.3.0.83,0
Luc VdV
  • 1,088
  • 9
  • 14
  • 1
    Sorry, found it myself. The localtime formatting line was copied from another SO question, number 1814196 "quickly getting to yyyy mm dd hhmmss in perl" The sample code in the top answer was using $^T, which stands for "gimme the time this program was started" :( Replaced it by 'localtime time' and it works. – Luc VdV Jul 30 '13 at 09:08
  • Link to the article in question: http://stackoverflow.com/questions/1814196/quickly-getting-to-yyyy-mm-dd-hhmmss-in-perl – Luc VdV Jul 30 '13 at 09:10

1 Answers1

1

The problem was, as you addressed in a comment, that you were passing $^T (which holds the time the program was started) to localtime().

As you noted, you can say localtime(time) - or even easier, just pass nothing to localtime - perldoc -f localtime explains:

If EXPR is omitted, "localtime()" uses the current time (as returned by time(3)).

So, just say localtime(), and your problems go away :)

David Precious
  • 6,544
  • 1
  • 24
  • 31