|
1 #! /usr/bin/perl |
|
2 |
|
3 # Copyright (C) 2011 Christian Arnold |
|
4 # |
|
5 # This program is free software: you can redistribute it and/or modify |
|
6 # it under the terms of the GNU General Public License as published by |
|
7 # the Free Software Foundation, either version 3 of the License, or |
|
8 # (at your option) any later version. |
|
9 # |
|
10 # This program is distributed in the hope that it will be useful, |
|
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 # GNU General Public License for more details. |
|
14 # |
|
15 # You should have received a copy of the GNU General Public License |
|
16 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17 # |
|
18 # Christian Arnold <arnold@schlittermann.de> |
|
19 |
|
20 use 5.010; |
|
21 use strict; |
|
22 use warnings; |
|
23 use File::Basename qw(basename); |
|
24 use Pod::Usage; |
|
25 use Getopt::Long; |
|
26 |
|
27 delete @ENV{ grep /^LC_/ => keys %ENV }; |
|
28 $ENV{LANG} = "C"; |
|
29 $ENV{LC_ALL} = "C"; |
|
30 |
|
31 sub check_uptime($$); |
|
32 sub report($$); |
|
33 sub version($$); |
|
34 |
|
35 my %ERRORS = ( |
|
36 OK => 0, |
|
37 WARNING => 1, |
|
38 CRITICAL => 2, |
|
39 UNKNOWN => 3, |
|
40 DEPENDENT => 4 |
|
41 ); |
|
42 |
|
43 my $ME = basename $0; |
|
44 my $NAME = 'UPTIME'; |
|
45 my $STAMP_DIR = '/var/tmp/nagios'; |
|
46 my $STAMP_FILE = 'check_uptime.stamp'; |
|
47 my $VERSION = '0.1'; |
|
48 |
|
49 my $PROC_UPTIME = '/proc/uptime'; |
|
50 |
|
51 my %opt = ( time => 86400 ); |
|
52 |
|
53 MAIN: { |
|
54 Getopt::Long::Configure('no_ignore_case_always'); |
|
55 GetOptions( |
|
56 "t|time=i" => \$opt{time}, |
|
57 "h|help" => sub { pod2usage( -verbose => 1, -exitval => $ERRORS{OK} ) }, |
|
58 "m|man" => sub { pod2usage( -verbose => 2, -exitval => $ERRORS{OK} ) }, |
|
59 "V|version" => sub { version( $ME, $VERSION ); exit $ERRORS{OK}; } |
|
60 ) or pod2usage( -verbose => 1, -exitval => $ERRORS{CRITICAL} ); |
|
61 |
|
62 # prepare the nest |
|
63 unless ( -d $STAMP_DIR ) { |
|
64 unless ( mkdir $STAMP_DIR ) { |
|
65 say "$NAME CRITICAL: cannot create directory $STAMP_DIR $!"; |
|
66 exit $ERRORS{CRITICAL}; |
|
67 } |
|
68 my $mode = 0777; |
|
69 chmod $mode, $STAMP_DIR; |
|
70 } |
|
71 |
|
72 unless ( -r $PROC_UPTIME ) { |
|
73 say "$NAME CRITICAL: $PROC_UPTIME not found or not readable."; |
|
74 exit $ERRORS{CRITICAL}; |
|
75 } |
|
76 |
|
77 my ( $rc, $uptime ) = check_uptime( $PROC_UPTIME, $opt{time} ); |
|
78 report( $rc, $uptime ); |
|
79 |
|
80 } |
|
81 |
|
82 sub check_uptime($$) { |
|
83 my ( $file, $time ) = @_; |
|
84 my $rc = "OK"; |
|
85 my $mesg = ""; |
|
86 my $uptime = ""; |
|
87 |
|
88 # get current uptime |
|
89 unless ( open UPTIME, '<', $file ) { |
|
90 $rc = 'CRITICAL'; |
|
91 $mesg = $!; |
|
92 return ( $rc, $mesg ); |
|
93 } |
|
94 my $line = <UPTIME>; |
|
95 close UPTIME; |
|
96 |
|
97 $line =~ /^(\d+)/ and $uptime = $1; |
|
98 |
|
99 # prepare the output |
|
100 my $c_days = sprintf( "%.2f", $uptime / 60 / 60 / 24 ); |
|
101 my $s_days = sprintf( "%.2f", $time / 60 / 60 / 24 ); |
|
102 |
|
103 if ( $uptime < $time ) { |
|
104 $rc = "WARNING"; |
|
105 $mesg = "current uptime $c_days days is less than $s_days days"; |
|
106 } |
|
107 else { |
|
108 $rc = "OK"; |
|
109 $mesg = "current uptime $c_days days is greater than $s_days days"; |
|
110 } |
|
111 |
|
112 # store last check time |
|
113 unless ( open STAMP, '>', "$STAMP_DIR/$STAMP_FILE" ) { |
|
114 say "$NAME CRITICAL: connot open $STAMP_DIR/$STAMP_FILE for writing $!"; |
|
115 exit $ERRORS{CRITICAL}; |
|
116 } |
|
117 |
|
118 my $mode = 0666; |
|
119 chmod $mode, "$STAMP_DIR/$STAMP_FILE"; |
|
120 say STAMP time(); |
|
121 close STAMP; |
|
122 |
|
123 return ( $rc, $mesg ); |
|
124 } |
|
125 |
|
126 sub report($$) { |
|
127 my ( $rc, $mesg ) = @_; |
|
128 say "$NAME $rc: $mesg" |
|
129 and exit $ERRORS{WARNING} |
|
130 if ( $rc eq "WARNING" ); |
|
131 say "$NAME $rc: $mesg"; |
|
132 exit $ERRORS{OK}; |
|
133 } |
|
134 |
|
135 sub version($$) { |
|
136 my ( $progname, $version ) = @_; |
|
137 say <<_VERSION; |
|
138 $progname version $version |
|
139 Copyright (C) 2011 by Christian Arnold and Schlittermann internet & unix support. |
|
140 |
|
141 $ME comes with ABSOLUTELY NO WARRANTY. This is free software, |
|
142 and you are welcome to redistribute it under certain conditions. |
|
143 See the GNU General Public Licence for details. |
|
144 _VERSION |
|
145 } |
|
146 |
|
147 __END__ |
|
148 |
|
149 =head1 NAME |
|
150 |
|
151 check_uptime - nagios plugin to check server uptime |
|
152 |
|
153 =head1 SYNOPSIS |
|
154 |
|
155 check_uptime [B<-t>|B<--time> integer] |
|
156 |
|
157 check_uptime [B<-h>|B<--help>] |
|
158 |
|
159 check_uptime [B<-m>|B<--man>] |
|
160 |
|
161 check_uptime [B<-V>|B<--version>] |
|
162 |
|
163 =head1 OPTIONS |
|
164 |
|
165 =over |
|
166 |
|
167 =item B<-t>|B<--time> I<integer> |
|
168 |
|
169 Uptime should be greater than B<time> in I<seconds> before change to B<WARNING> status. (default: 86400) |
|
170 |
|
171 =item B<-h>|B<--help> |
|
172 |
|
173 Print detailed help screen. |
|
174 |
|
175 =item B<-m>|B<--man> |
|
176 |
|
177 Print manual page. |
|
178 |
|
179 =item B<-V>|B<--version> |
|
180 |
|
181 Print version information. |
|
182 |
|
183 =back |
|
184 |
|
185 =head1 DESCRIPTION |
|
186 |
|
187 This nagios plugin checks the server uptime. The current server uptime is get from I</proc/uptime> file. |
|
188 The last check time will hold in the file I</var/tmp/nagios/check_uptime.stamp> (unix timestamp) |
|
189 |
|
190 =head1 FILES |
|
191 |
|
192 I</proc/uptime> |
|
193 |
|
194 I</var/tmp/nagios/check_uptime.stamp> |
|
195 |
|
196 =head1 VERSION |
|
197 |
|
198 This man page is current for version 0.1 of B<check_uptime>. |
|
199 |
|
200 =head1 AUTHOR |
|
201 |
|
202 Written by Christian Arnold L<arnold@schlittermann.de> |
|
203 |
|
204 =head1 COPYRIGHT |
|
205 |
|
206 Copyright (C) 2011 by Christian Arnold and Schlittermann internet & unix support. This is free software, and you are welcome to |
|
207 redistribute it under certain conditions. |
|
208 See the GNU General Public Licence for details. |
|
209 |
|
210 =cut |