diff options
author | Bernd Arnold <wopfel@gmail.com> | 2018-06-03 14:05:54 (GMT) |
---|---|---|
committer | Bernd Arnold <wopfel@gmail.com> | 2018-06-03 14:05:54 (GMT) |
commit | 44816b4979cdad1698179e749726b8f45e5587d2 (patch) | |
tree | 5b391c23e79d6e023fd8f1a53e2dedb2ec4ca546 /plugins-scripts/check_uptime.pl | |
parent | de0872e2d71caec807c659c7f518adcbefe7e665 (diff) | |
download | monitoring-plugins-44816b4979cdad1698179e749726b8f45e5587d2.tar.gz |
Rename to .pl
All other check script also have the .pl ending
Diffstat (limited to 'plugins-scripts/check_uptime.pl')
-rwxr-xr-x | plugins-scripts/check_uptime.pl | 255 |
1 files changed, 255 insertions, 0 deletions
diff --git a/plugins-scripts/check_uptime.pl b/plugins-scripts/check_uptime.pl new file mode 100755 index 0000000..09600df --- /dev/null +++ b/plugins-scripts/check_uptime.pl | |||
@@ -0,0 +1,255 @@ | |||
1 | #!@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 | |||
24 | use POSIX; | ||
25 | use strict; | ||
26 | use Getopt::Long; | ||
27 | use vars qw($opt_V $opt_h $opt_v $verbose $PROGNAME $opt_w $opt_c | ||
28 | $opt_f $opt_s | ||
29 | $status $state $msg); | ||
30 | use FindBin; | ||
31 | use lib "$FindBin::Bin"; | ||
32 | use utils qw(%ERRORS &print_revision &support &usage ); | ||
33 | |||
34 | sub print_help (); | ||
35 | sub print_usage (); | ||
36 | sub process_arguments (); | ||
37 | |||
38 | $ENV{'PATH'}='@TRUSTED_PATH@'; | ||
39 | $ENV{'BASH_ENV'}=''; | ||
40 | $ENV{'ENV'}=''; | ||
41 | $PROGNAME = "check_uptime"; | ||
42 | $state = $ERRORS{'UNKNOWN'}; | ||
43 | |||
44 | |||
45 | # Process arguments | ||
46 | |||
47 | Getopt::Long::Configure('bundling'); | ||
48 | $status = process_arguments(); | ||
49 | if ($status){ | ||
50 | print "ERROR: processing arguments\n"; | ||
51 | exit $ERRORS{"UNKNOWN"}; | ||
52 | } | ||
53 | |||
54 | |||
55 | # Get uptime info from file | ||
56 | |||
57 | my $uptime_file = "/proc/uptime"; | ||
58 | |||
59 | if ( ! -r $uptime_file ) { | ||
60 | print "ERROR: file '$uptime_file' is not readable\n"; | ||
61 | exit $ERRORS{"UNKNOWN"}; | ||
62 | } | ||
63 | |||
64 | if ( ! open FILE, "<", $uptime_file ) { | ||
65 | print "ERROR: cannot read from file '$uptime_file'\n"; | ||
66 | exit $ERRORS{"UNKNOWN"}; | ||
67 | } | ||
68 | |||
69 | chomp( my $file_content = <FILE> ); | ||
70 | close FILE; | ||
71 | |||
72 | print "$uptime_file: $file_content\n" if $verbose; | ||
73 | |||
74 | # Get first digit value (without fraction) | ||
75 | my ( $uptime_seconds ) = $file_content =~ /^([\d]+)/; | ||
76 | |||
77 | # Bail out if value is not numeric | ||
78 | if ( $uptime_seconds !~ /^\d+$/ ) { | ||
79 | print "ERROR: no numeric value: $uptime_seconds\n"; | ||
80 | exit $ERRORS{"UNKNOWN"}; | ||
81 | } | ||
82 | |||
83 | |||
84 | # Do calculations for a "pretty" format (2 weeks, 5 days, ...) | ||
85 | |||
86 | my ( $secs, $mins, $hours, $days, $weeks ); | ||
87 | $secs = $uptime_seconds; | ||
88 | if ( $secs > 100 ) { | ||
89 | $mins = int( $secs / 60 ); | ||
90 | $secs -= $mins * 60; | ||
91 | } | ||
92 | if ( $mins > 100 ) { | ||
93 | $hours = int( $mins / 60 ); | ||
94 | $mins -= $hours * 60; | ||
95 | } | ||
96 | if ( $hours > 48 ) { | ||
97 | $days = int( $hours / 24 ); | ||
98 | $hours -= $days * 24; | ||
99 | } | ||
100 | if ( $days > 14 ) { | ||
101 | $weeks = int( $days / 7 ); | ||
102 | $days -= $weeks * 7; | ||
103 | } | ||
104 | |||
105 | my $pretty_uptime = ""; | ||
106 | $pretty_uptime .= sprintf( "%d week%s, ", $weeks, $weeks == 1 ? "" : "s" ) if $weeks; | ||
107 | $pretty_uptime .= sprintf( "%d day%s, ", $days, $days == 1 ? "" : "s" ) if $days; | ||
108 | $pretty_uptime .= sprintf( "%d hour%s, ", $hours, $hours == 1 ? "" : "s" ) if $hours; | ||
109 | $pretty_uptime .= sprintf( "%d minute%s, ", $mins, $mins == 1 ? "" : "s" ) if $mins; | ||
110 | # Replace last occurence of comma with "and" | ||
111 | $pretty_uptime =~ s/, $/ and /; | ||
112 | # Always print the seconds (though it may be 0 seconds) | ||
113 | $pretty_uptime .= sprintf( "%d second%s", $secs, $secs == 1 ? "" : "s" ); | ||
114 | |||
115 | |||
116 | # Default to catch errors in program | ||
117 | my $state_str = "UNKNOWN"; | ||
118 | |||
119 | # Check values | ||
120 | if ( $uptime_seconds > $opt_c ) { | ||
121 | $state_str = "CRITICAL"; | ||
122 | } elsif ( $uptime_seconds > $opt_w ) { | ||
123 | $state_str = "WARNING"; | ||
124 | } else { | ||
125 | $state_str = "OK"; | ||
126 | } | ||
127 | |||
128 | $msg = "$state_str: "; | ||
129 | |||
130 | $msg .= "uptime is $uptime_seconds seconds. "; | ||
131 | $msg .= "Running for $pretty_uptime. " if $opt_f; | ||
132 | if ( $opt_s ) { | ||
133 | chomp( my $up_since = `uptime -s` ); | ||
134 | $msg .= "Running since $up_since. "; | ||
135 | } | ||
136 | |||
137 | $state = $ERRORS{$state_str}; | ||
138 | |||
139 | # Perfdata support | ||
140 | print "$msg|uptime=$uptime_seconds;$opt_w;$opt_c;0\n"; | ||
141 | exit $state; | ||
142 | |||
143 | |||
144 | ##################################### | ||
145 | #### subs | ||
146 | |||
147 | |||
148 | sub process_arguments(){ | ||
149 | GetOptions | ||
150 | ("V" => \$opt_V, "version" => \$opt_V, | ||
151 | "v" => \$opt_v, "verbose" => \$opt_v, | ||
152 | "h" => \$opt_h, "help" => \$opt_h, | ||
153 | "w=s" => \$opt_w, "warning=s" => \$opt_w, # warning if above this number | ||
154 | "c=s" => \$opt_c, "critical=s" => \$opt_c, # critical if above this number | ||
155 | "f" => \$opt_f, "for" => \$opt_f, # show "running for ..." | ||
156 | "s" => \$opt_s, "since" => \$opt_s, # show "running since ..." | ||
157 | ); | ||
158 | |||
159 | if ($opt_V) { | ||
160 | print_revision($PROGNAME,'@NP_VERSION@'); | ||
161 | exit $ERRORS{'UNKNOWN'}; | ||
162 | } | ||
163 | |||
164 | if ($opt_h) { | ||
165 | print_help(); | ||
166 | exit $ERRORS{'UNKNOWN'}; | ||
167 | } | ||
168 | |||
169 | if (defined $opt_v) { | ||
170 | $verbose = $opt_v; | ||
171 | } | ||
172 | |||
173 | unless ( defined $opt_w && defined $opt_c ) { | ||
174 | print_usage(); | ||
175 | exit $ERRORS{'UNKNOWN'}; | ||
176 | } | ||
177 | |||
178 | # Check if suffix is present | ||
179 | # Calculate parameter to seconds (to get an integer value finally) | ||
180 | # s = seconds | ||
181 | # m = minutes | ||
182 | # h = hours | ||
183 | # d = days | ||
184 | # w = weeks | ||
185 | my %factor = ( "s" => 1, | ||
186 | "m" => 60, | ||
187 | "h" => 60 * 60, | ||
188 | "d" => 60 * 60 * 24, | ||
189 | "w" => 60 * 60 * 24 * 7, | ||
190 | ); | ||
191 | if ( $opt_w =~ /^(\d+)([a-z])$/ ) { | ||
192 | my $value = $1; | ||
193 | my $suffix = $2; | ||
194 | print "warning: value=$value, suffix=$suffix\n" if $verbose; | ||
195 | if ( ! defined $factor{$suffix} ) { | ||
196 | print "Error: wrong suffix ($suffix) for warning"; | ||
197 | exit $ERRORS{'UNKNOWN'}; | ||
198 | } | ||
199 | $opt_w = $value * $factor{$suffix}; | ||
200 | } | ||
201 | if ( $opt_c =~ /^(\d+)([a-z])$/ ) { | ||
202 | my $value = $1; | ||
203 | my $suffix = $2; | ||
204 | print "critical: value=$value, suffix=$suffix\n" if $verbose; | ||
205 | if ( ! defined $factor{$suffix} ) { | ||
206 | print "Error: wrong suffix ($suffix) for critical"; | ||
207 | exit $ERRORS{'UNKNOWN'}; | ||
208 | } | ||
209 | $opt_c = $value * $factor{$suffix}; | ||
210 | } | ||
211 | |||
212 | if ( $opt_w !~ /^\d+$/ ) { | ||
213 | print "Warning (-w) is not numeric\n"; | ||
214 | exit $ERRORS{'UNKNOWN'}; | ||
215 | } | ||
216 | if ( $opt_c !~ /^\d+$/ ) { | ||
217 | print "Critical (-c) is not numeric\n"; | ||
218 | exit $ERRORS{'UNKNOWN'}; | ||
219 | } | ||
220 | |||
221 | if ( $opt_w >= $opt_c) { | ||
222 | print "Warning (-w) cannot be greater than Critical (-c)!\n"; | ||
223 | exit $ERRORS{'UNKNOWN'}; | ||
224 | } | ||
225 | |||
226 | return $ERRORS{'OK'}; | ||
227 | } | ||
228 | |||
229 | sub print_usage () { | ||
230 | print "Usage: $PROGNAME -w <warn> -c <crit> [-v]\n"; | ||
231 | } | ||
232 | |||
233 | sub print_help () { | ||
234 | print_revision($PROGNAME,'@NP_VERSION@'); | ||
235 | print "Copyright (c) 2002 Subhendu Ghosh/Carlos Canau/Benjamin Schmid\n"; | ||
236 | print "Copyright (c) 2018 Bernd Arnold\n"; | ||
237 | print "\n"; | ||
238 | print_usage(); | ||
239 | print "\n"; | ||
240 | print " Checks the uptime of the system using $uptime_file\n"; | ||
241 | print "\n"; | ||
242 | print "-w (--warning) = Min. number of uptime to generate warning\n"; | ||
243 | print "-c (--critical) = Min. number of uptime to generate critical alert ( w < c )\n"; | ||
244 | print "-f (--for) = Show uptime in a pretty format (Running for x weeks, x days, ...)\n"; | ||
245 | print "-s (--since) = Show last boot in yyyy-mm-dd HH:MM:SS format (output from 'uptime -s')\n"; | ||
246 | print "-h (--help)\n"; | ||
247 | print "-V (--version)\n"; | ||
248 | print "-v (--verbose) = debugging output\n"; | ||
249 | print "\n\n"; | ||
250 | print "Note: -w and -c are required arguments.\n"; | ||
251 | print " You can suffix both values with s for seconds (default), m (minutes), h (hours), d (days) or w (weeks).\n"; | ||
252 | print ""; | ||
253 | print "\n\n"; | ||
254 | support(); | ||
255 | } | ||