summaryrefslogtreecommitdiffstats
path: root/plugins-scripts/check_uptime
diff options
context:
space:
mode:
authorBernd Arnold <wopfel@gmail.com>2018-05-30 19:03:43 +0200
committerBernd Arnold <wopfel@gmail.com>2018-05-30 19:03:43 +0200
commit96c8dd89353be67d44ce7a21b90f621ad89a2984 (patch)
treeaff9e27392901d53675722ebb35d52c916757a86 /plugins-scripts/check_uptime
parent9661ee74885834f7b69ab0874c4e65bed0b871c9 (diff)
downloadmonitoring-plugins-96c8dd8.tar.gz
My version of check_uptime
Derived from check_mailq
Diffstat (limited to 'plugins-scripts/check_uptime')
-rwxr-xr-xplugins-scripts/check_uptime256
1 files changed, 256 insertions, 0 deletions
diff --git a/plugins-scripts/check_uptime b/plugins-scripts/check_uptime
new file mode 100755
index 00000000..4bc95282
--- /dev/null
+++ b/plugins-scripts/check_uptime
@@ -0,0 +1,256 @@
1#!/usr/bin/perl -w
2
3# check_uptime - check uptime to see how long the system is running.
4#
5
6# License Information:
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program; if not, write to the Free Software
19# Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
20# USA
21#
22############################################################################
23
24use POSIX;
25use strict;
26use Getopt::Long;
27use vars qw($opt_V $opt_h $opt_v $verbose $PROGNAME $opt_w $opt_c
28 $opt_f $opt_s
29 $status $state $msg);
30use FindBin;
31use lib "$FindBin::Bin";
32use lib "/usr/lib/monitoring-plugins/";
33use utils qw(%ERRORS &print_revision &support &usage );
34
35sub print_help ();
36sub print_usage ();
37sub process_arguments ();
38
39#$ENV{'PATH'}='/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin';
40$ENV{'BASH_ENV'}='';
41$ENV{'ENV'}='';
42$PROGNAME = "check_uptime";
43$state = $ERRORS{'UNKNOWN'};
44
45
46# Process arguments
47
48Getopt::Long::Configure('bundling');
49$status = process_arguments();
50if ($status){
51 print "ERROR: processing arguments\n";
52 exit $ERRORS{"UNKNOWN"};
53}
54
55
56# Get uptime info from file
57
58my $uptime_file = "/proc/uptime";
59
60if ( ! -r $uptime_file ) {
61 print "ERROR: file '$uptime_file' is not readable\n";
62 exit $ERRORS{"UNKNOWN"};
63}
64
65if ( ! open FILE, "<", $uptime_file ) {
66 print "ERROR: cannot read from file '$uptime_file'\n";
67 exit $ERRORS{"UNKNOWN"};
68}
69
70chomp( my $file_content = <FILE> );
71close FILE;
72
73print "$uptime_file: $file_content\n" if $verbose;
74
75# Get first digit value (without fraction)
76my ( $uptime_seconds ) = $file_content =~ /^([\d]+)/;
77
78# Bail out if value is not numeric
79if ( $uptime_seconds !~ /^\d+$/ ) {
80 print "ERROR: no numeric value: $uptime_seconds\n";
81 exit $ERRORS{"UNKNOWN"};
82}
83
84
85# Do calculations for a "pretty" format (2 weeks, 5 days, ...)
86
87my ( $secs, $mins, $hours, $days, $weeks );
88$secs = $uptime_seconds;
89if ( $secs > 100 ) {
90 $mins = int( $secs / 60 );
91 $secs -= $mins * 60;
92}
93if ( $mins > 100 ) {
94 $hours = int( $mins / 60 );
95 $mins -= $hours * 60;
96}
97if ( $hours > 48 ) {
98 $days = int( $hours / 24 );
99 $hours -= $days * 24;
100}
101if ( $days > 14 ) {
102 $weeks = int( $days / 7 );
103 $days -= $weeks * 7;
104}
105
106my $pretty_uptime = "";
107$pretty_uptime .= sprintf( "%d week%s, ", $weeks, $weeks == 1 ? "" : "s" ) if $weeks;
108$pretty_uptime .= sprintf( "%d day%s, ", $days, $days == 1 ? "" : "s" ) if $days;
109$pretty_uptime .= sprintf( "%d hour%s, ", $hours, $hours == 1 ? "" : "s" ) if $hours;
110$pretty_uptime .= sprintf( "%d minute%s, ", $mins, $mins == 1 ? "" : "s" ) if $mins;
111# Replace last occurence of comma with "and"
112$pretty_uptime =~ s/, $/ and /;
113# Always print the seconds (though it may be 0 seconds)
114$pretty_uptime .= sprintf( "%d second%s", $secs, $secs == 1 ? "" : "s" );
115
116
117# Default to catch errors in program
118my $state_str = "UNKNOWN";
119
120# Check values
121if ( $uptime_seconds > $opt_c ) {
122 $state_str = "CRITICAL";
123} elsif ( $uptime_seconds > $opt_w ) {
124 $state_str = "WARNING";
125} else {
126 $state_str = "OK";
127}
128
129$msg = "$state_str: ";
130
131$msg .= "uptime is $uptime_seconds seconds. ";
132$msg .= "Running for $pretty_uptime. " if $opt_f;
133if ( $opt_s ) {
134 chomp( my $up_since = `uptime -s` );
135 $msg .= "Running since $up_since. ";
136}
137
138$state = $ERRORS{$state_str};
139
140# Perfdata support
141print "$msg|uptime=$uptime_seconds;$opt_w;$opt_c;0\n";
142exit $state;
143
144
145#####################################
146#### subs
147
148
149sub process_arguments(){
150 GetOptions
151 ("V" => \$opt_V, "version" => \$opt_V,
152 "v" => \$opt_v, "verbose" => \$opt_v,
153 "h" => \$opt_h, "help" => \$opt_h,
154 "w=s" => \$opt_w, "warning=s" => \$opt_w, # warning if above this number
155 "c=s" => \$opt_c, "critical=s" => \$opt_c, # critical if above this number
156 "f" => \$opt_f, "for" => \$opt_f, # show "running for ..."
157 "s" => \$opt_s, "since" => \$opt_s, # show "running since ..."
158 );
159
160 if ($opt_V) {
161 print_revision($PROGNAME,'2.2');
162 exit $ERRORS{'UNKNOWN'};
163 }
164
165 if ($opt_h) {
166 print_help();
167 exit $ERRORS{'UNKNOWN'};
168 }
169
170 if (defined $opt_v) {
171 $verbose = $opt_v;
172 }
173
174 unless ( defined $opt_w && defined $opt_c ) {
175 print_usage();
176 exit $ERRORS{'UNKNOWN'};
177 }
178
179 # Check if suffix is present
180 # Calculate parameter to seconds (to get an integer value finally)
181 # s = seconds
182 # m = minutes
183 # h = hours
184 # d = days
185 # w = weeks
186 my %factor = ( "s" => 1,
187 "m" => 60,
188 "h" => 60 * 60,
189 "d" => 60 * 60 * 24,
190 "w" => 60 * 60 * 24 * 7,
191 );
192 if ( $opt_w =~ /^(\d+)([a-z])$/ ) {
193 my $value = $1;
194 my $suffix = $2;
195 print "warning: value=$value, suffix=$suffix\n" if $verbose;
196 if ( ! defined $factor{$suffix} ) {
197 print "Error: wrong suffix ($suffix) for warning";
198 exit $ERRORS{'UNKNOWN'};
199 }
200 $opt_w = $value * $factor{$suffix};
201 }
202 if ( $opt_c =~ /^(\d+)([a-z])$/ ) {
203 my $value = $1;
204 my $suffix = $2;
205 print "critical: value=$value, suffix=$suffix\n" if $verbose;
206 if ( ! defined $factor{$suffix} ) {
207 print "Error: wrong suffix ($suffix) for critical";
208 exit $ERRORS{'UNKNOWN'};
209 }
210 $opt_c = $value * $factor{$suffix};
211 }
212
213 if ( $opt_w !~ /^\d+$/ ) {
214 print "Warning (-w) is not numeric\n";
215 exit $ERRORS{'UNKNOWN'};
216 }
217 if ( $opt_c !~ /^\d+$/ ) {
218 print "Critical (-c) is not numeric\n";
219 exit $ERRORS{'UNKNOWN'};
220 }
221
222 if ( $opt_w >= $opt_c) {
223 print "Warning (-w) cannot be greater than Critical (-c)!\n";
224 exit $ERRORS{'UNKNOWN'};
225 }
226
227 return $ERRORS{'OK'};
228}
229
230sub print_usage () {
231 print "Usage: $PROGNAME -w <warn> -c <crit> [-v]\n";
232}
233
234sub print_help () {
235 print_revision($PROGNAME,'2.2');
236 print "Copyright (c) 2002 Subhendu Ghosh/Carlos Canau/Benjamin Schmid\n";
237 print "Copyright (c) 2018 Bernd Arnold\n";
238 print "\n";
239 print_usage();
240 print "\n";
241 print " Checks the uptime of the system using $uptime_file\n";
242 print "\n";
243 print "-w (--warning) = Min. number of uptime to generate warning\n";
244 print "-c (--critical) = Min. number of uptime to generate critical alert ( w < c )\n";
245 print "-f (--for) = Show uptime in a pretty format (Running for x weeks, x days, ...)\n";
246 print "-s (--since) = Show last boot in yyyy-mm-dd HH:MM:SS format (output from 'uptime -s')\n";
247 print "-h (--help)\n";
248 print "-V (--version)\n";
249 print "-v (--verbose) = debugging output\n";
250 print "\n\n";
251 print "Note: -w and -c are required arguments.\n";
252 print " You can suffix both values with s for seconds (default), m (minutes), h (hours), d (days) or w (weeks).\n";
253 print "";
254 print "\n\n";
255 support();
256}