--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/.hgignore Tue May 17 15:55:18 2011 +0200
@@ -0,0 +1,8 @@
+syntax: glob
+*.swp
+debian/files
+check_uptime
+
+syntax: regexp
+(build|configure)-stamp$
+debian/nagios-plugin-uptime[./]
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/.perltitdy Tue May 17 15:55:18 2011 +0200
@@ -0,0 +1,2 @@
+--paren-tightness=2
+--square-bracket-tightness=2
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Makefile Tue May 17 15:55:18 2011 +0200
@@ -0,0 +1,22 @@
+SCRIPTS = check_uptime
+CLEANFILES = ${SCRIPTS}
+DESTDIR =
+prefix = /usr
+
+plugindir = ${prefix}/lib/nagios/plugins/ius
+
+.PHONY: all clean install
+
+all: ${SCRIPTS}
+
+clean:
+ -rm -f ${CLEANFILES}
+
+install: all
+ install -d -m 0755 ${DESTDIR}/${plugindir}
+ install -m 0755 $(SCRIPTS) ${DESTDIR}/${plugindir}/
+
+%: %.pl
+ @perl -c $<
+ @cp -f $< $@
+ @chmod +x $@
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/check_uptime.pl Tue May 17 15:55:18 2011 +0200
@@ -0,0 +1,210 @@
+#! /usr/bin/perl
+
+# Copyright (C) 2011 Christian Arnold
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+# Christian Arnold <arnold@schlittermann.de>
+
+use 5.010;
+use strict;
+use warnings;
+use File::Basename qw(basename);
+use Pod::Usage;
+use Getopt::Long;
+
+delete @ENV{ grep /^LC_/ => keys %ENV };
+$ENV{LANG} = "C";
+$ENV{LC_ALL} = "C";
+
+sub check_uptime($$);
+sub report($$);
+sub version($$);
+
+my %ERRORS = (
+ OK => 0,
+ WARNING => 1,
+ CRITICAL => 2,
+ UNKNOWN => 3,
+ DEPENDENT => 4
+);
+
+my $ME = basename $0;
+my $NAME = 'UPTIME';
+my $STAMP_DIR = '/var/tmp/nagios';
+my $STAMP_FILE = 'check_uptime.stamp';
+my $VERSION = '0.1';
+
+my $PROC_UPTIME = '/proc/uptime';
+
+my %opt = ( time => 86400 );
+
+MAIN: {
+ Getopt::Long::Configure('no_ignore_case_always');
+ GetOptions(
+ "t|time=i" => \$opt{time},
+ "h|help" => sub { pod2usage( -verbose => 1, -exitval => $ERRORS{OK} ) },
+ "m|man" => sub { pod2usage( -verbose => 2, -exitval => $ERRORS{OK} ) },
+ "V|version" => sub { version( $ME, $VERSION ); exit $ERRORS{OK}; }
+ ) or pod2usage( -verbose => 1, -exitval => $ERRORS{CRITICAL} );
+
+ # prepare the nest
+ unless ( -d $STAMP_DIR ) {
+ unless ( mkdir $STAMP_DIR ) {
+ say "$NAME CRITICAL: cannot create directory $STAMP_DIR $!";
+ exit $ERRORS{CRITICAL};
+ }
+ my $mode = 0777;
+ chmod $mode, $STAMP_DIR;
+ }
+
+ unless ( -r $PROC_UPTIME ) {
+ say "$NAME CRITICAL: $PROC_UPTIME not found or not readable.";
+ exit $ERRORS{CRITICAL};
+ }
+
+ my ( $rc, $uptime ) = check_uptime( $PROC_UPTIME, $opt{time} );
+ report( $rc, $uptime );
+
+}
+
+sub check_uptime($$) {
+ my ( $file, $time ) = @_;
+ my $rc = "OK";
+ my $mesg = "";
+ my $uptime = "";
+
+ # get current uptime
+ unless ( open UPTIME, '<', $file ) {
+ $rc = 'CRITICAL';
+ $mesg = $!;
+ return ( $rc, $mesg );
+ }
+ my $line = <UPTIME>;
+ close UPTIME;
+
+ $line =~ /^(\d+)/ and $uptime = $1;
+
+ # prepare the output
+ my $c_days = sprintf( "%.2f", $uptime / 60 / 60 / 24 );
+ my $s_days = sprintf( "%.2f", $time / 60 / 60 / 24 );
+
+ if ( $uptime < $time ) {
+ $rc = "WARNING";
+ $mesg = "current uptime $c_days days is less than $s_days days";
+ }
+ else {
+ $rc = "OK";
+ $mesg = "current uptime $c_days days is greater than $s_days days";
+ }
+
+ # store last check time
+ unless ( open STAMP, '>', "$STAMP_DIR/$STAMP_FILE" ) {
+ say "$NAME CRITICAL: connot open $STAMP_DIR/$STAMP_FILE for writing $!";
+ exit $ERRORS{CRITICAL};
+ }
+
+ my $mode = 0666;
+ chmod $mode, "$STAMP_DIR/$STAMP_FILE";
+ say STAMP time();
+ close STAMP;
+
+ return ( $rc, $mesg );
+}
+
+sub report($$) {
+ my ( $rc, $mesg ) = @_;
+ say "$NAME $rc: $mesg"
+ and exit $ERRORS{WARNING}
+ if ( $rc eq "WARNING" );
+ say "$NAME $rc: $mesg";
+ exit $ERRORS{OK};
+}
+
+sub version($$) {
+ my ( $progname, $version ) = @_;
+ say <<_VERSION;
+$progname version $version
+Copyright (C) 2011 by Christian Arnold and Schlittermann internet & unix support.
+
+$ME comes with ABSOLUTELY NO WARRANTY. This is free software,
+and you are welcome to redistribute it under certain conditions.
+See the GNU General Public Licence for details.
+_VERSION
+}
+
+__END__
+
+=head1 NAME
+
+check_uptime - nagios plugin to check server uptime
+
+=head1 SYNOPSIS
+
+check_uptime [B<-t>|B<--time> integer]
+
+check_uptime [B<-h>|B<--help>]
+
+check_uptime [B<-m>|B<--man>]
+
+check_uptime [B<-V>|B<--version>]
+
+=head1 OPTIONS
+
+=over
+
+=item B<-t>|B<--time> I<integer>
+
+Uptime should be greater than B<time> in I<seconds> before change to B<WARNING> status. (default: 86400)
+
+=item B<-h>|B<--help>
+
+Print detailed help screen.
+
+=item B<-m>|B<--man>
+
+Print manual page.
+
+=item B<-V>|B<--version>
+
+Print version information.
+
+=back
+
+=head1 DESCRIPTION
+
+This nagios plugin checks the server uptime. The current server uptime is get from I</proc/uptime> file.
+The last check time will hold in the file I</var/tmp/nagios/check_uptime.stamp> (unix timestamp)
+
+=head1 FILES
+
+I</proc/uptime>
+
+I</var/tmp/nagios/check_uptime.stamp>
+
+=head1 VERSION
+
+This man page is current for version 0.1 of B<check_uptime>.
+
+=head1 AUTHOR
+
+Written by Christian Arnold L<arnold@schlittermann.de>
+
+=head1 COPYRIGHT
+
+Copyright (C) 2011 by Christian Arnold and Schlittermann internet & unix support. This is free software, and you are welcome to
+redistribute it under certain conditions.
+See the GNU General Public Licence for details.
+
+=cut
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/debian/changelog Tue May 17 15:55:18 2011 +0200
@@ -0,0 +1,5 @@
+nagios-plugin-uptime (0.1) lenny squeeze; urgency=low
+
+ * Initial Release.
+
+ -- Christian Arnold <arnold@schlittermann.de> Tue, 17 May 2011 15:49:38 +0200
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/debian/compat Tue May 17 15:55:18 2011 +0200
@@ -0,0 +1,1 @@
+7
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/debian/control Tue May 17 15:55:18 2011 +0200
@@ -0,0 +1,13 @@
+Source: nagios-plugin-uptime
+Section: net
+Priority: extra
+Maintainer: Christian Arnold <arnold@schlittermann.de>
+Build-Depends: debhelper (>= 7.0.50~)
+Standards-Version: 3.8.4
+Homepage: https://keller.schlittermann.de/hg/ius/nagios/nagios-plugin-uptime
+
+Package: nagios-plugin-uptime
+Architecture: all
+Depends: perl-base, perl-doc
+Description: nagios plugin to check server uptime
+ This plugin checks the server uptime.
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/debian/copyright Tue May 17 15:55:18 2011 +0200
@@ -0,0 +1,39 @@
+This work was packaged for Debian by:
+
+ Christian Arnold <arnold@schlittermann.de> on Tue, 17 May 2011 15:49:38 +0200
+
+It was downloaded from:
+
+ https://keller.schlittermann.de/hg/ius/nagios/nagios-plugin-uptime/
+
+Upstream Author(s):
+
+ Christian Arnold <arnold@schlittermann.de>
+
+Copyright:
+
+ Copyright (C) 2011 Schlittermann internet & unix support
+
+License:
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This package is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+On Debian systems, the complete text of the GNU General
+Public License version 3 can be found in "/usr/share/common-licenses/GPL-3".
+
+The Debian packaging is:
+
+ Copyright (C) 2011 Christian Arnold <arnold@schlittermann.de>
+
+and is licensed under the GPL version 3, see above.
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/debian/rules Tue May 17 15:55:18 2011 +0200
@@ -0,0 +1,13 @@
+#!/usr/bin/make -f
+# -*- makefile -*-
+# Sample debian/rules that uses debhelper.
+# This file was originally written by Joey Hess and Craig Small.
+# As a special exception, when this file is copied by dh-make into a
+# dh-make output file, you may use that output file without restriction.
+# This special exception was added by Craig Small in version 0.37 of dh-make.
+
+# Uncomment this to turn on verbose mode.
+#export DH_VERBOSE=1
+
+%:
+ dh $@
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/debian/source/format Tue May 17 15:55:18 2011 +0200
@@ -0,0 +1,1 @@
+3.0 (native)