sub save_status {
my( $header, $message ) = @_;
my( $cmd, $host, $service, $color, $time, $sum, $path, $start, $duration );
+ my( $ttl ) = 0;
# Do some checking on the message. If it appears bogus, then just
# log a message, and close the connection.
- if( $header =~ /^(\w+) (\S+) (\w+) (\w+) (\d+) (.*)$/ ) {
+ if( $header =~ /^(\w+) (\S+) (\w+) (\w+) ([:0-9]+) (.*)$/ ) {
($cmd, $host, $service, $color, $time, $sum) = ($1, $2, $3, $4, $5, $6);
if( $host !~ m/^[a-z0-9_\-\.]+$/ ) {
&error( "save_status: invalid service [$service]" ); return; }
if( $color ne "red" && $color ne "yellow" && $color ne "green" ) {
&error( "save_status: invalid color [$color]" ); return;}
+ if ( $time =~ /(\d+):(\d+)/ ) { $time = $1; $ttl = $2; }
if( $time !~ m/^\d+$/ ) {
&error( "save_status: invalid time [$time]" ); return; }
+ if( $ttl !~ m/^\d+$/ ) {
+ &error( "save_status: invalid time to live [$ttl]" ); return; }
} else {
&error( "save_status: invalid header [$header]" ); return;
}
return;
}
-
$start = $time; # Default start time to event time
# Try to read start time from the status file
$data = "status $time $service $color $sum\n";
&save_data( ">>", "$SPONGDB/$host/history/current", $data);
+ # If status history is enabled, save status message for history event
if ( $STATUS_HISTORY ) {
# Save the status update information
$data = "timestamp $start $time\ncolor $color\n$time $sum\n$message\n";
foreach( "red", "yellow", "green", "purple" ) { unlink "$path/$service-$_"; }
$data = "timestamp $start $time\n$time $sum\n$message\n";
- &save_data( ">>", "$SPONGDB/$host/services/$service-$color", $data );
+ my($expire) = ($ttl == 0) ? 0 : $time + $ttl;
+ &save_data( ">>", "$SPONGDB/$host/services/$service-$color", $data,
+ $expire );
# Call of the data function modules in the registry
foreach my $df (keys %DATAFUNCS) {
# the way...
sub save_data {
- my( $mode, $file, $data ) = @_;
+ my( $mode, $file, $data, $expire ) = @_;
my( $dir ) = ( $file =~ /^(.*)\/[^\/]+$/ );
my $umask;
+ $expire = 0 if ! defined $expire;
$umask = umask();
umask 022;
close( DATA );
chmod 0644, $file;
umask $umask;
+ if ( $expire != 0 ) {
+ my( $s, $m, $h, $D, $M, $Y ) = (localtime($expire));
+ $Y += 1900;
+ my( $timestamp) = "$Y$M$D$h$m.$s";
+ utime( time(), $expire, $file);
+ }
}