diff options
author | RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> | 2021-12-02 15:42:05 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-02 15:42:05 (GMT) |
commit | 911e44045d7291f5ede22739fd176ef55dd3de4a (patch) | |
tree | cf36b95a4a964b03d6ecf75770ced2cb3a2ac3a9 /plugins-scripts/check_uptime.pl | |
parent | 8294af907bd8482a86df749f562b7ec09e3faeed (diff) | |
parent | ed7cdf82a42f16532801ea4f118870ce9a130fcf (diff) | |
download | monitoring-plugins-911e44045d7291f5ede22739fd176ef55dd3de4a.tar.gz |
Merge branch 'master' into fix/shellcheckrefs/pull/1459/head
Diffstat (limited to 'plugins-scripts/check_uptime.pl')
-rwxr-xr-x | plugins-scripts/check_uptime.pl | 315 |
1 files changed, 315 insertions, 0 deletions
diff --git a/plugins-scripts/check_uptime.pl b/plugins-scripts/check_uptime.pl new file mode 100755 index 0000000..4c9f22d --- /dev/null +++ b/plugins-scripts/check_uptime.pl | |||
@@ -0,0 +1,315 @@ | |||
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 | $lower_warn_threshold $upper_warn_threshold | ||
30 | $lower_crit_threshold $upper_crit_threshold | ||
31 | $status $state $msg); | ||
32 | use FindBin; | ||
33 | use lib "$FindBin::Bin"; | ||
34 | use utils qw(%ERRORS &print_revision &support &usage ); | ||
35 | |||
36 | sub print_help (); | ||
37 | sub print_usage (); | ||
38 | sub process_arguments (); | ||
39 | |||
40 | $ENV{'PATH'}='@TRUSTED_PATH@'; | ||
41 | $ENV{'BASH_ENV'}=''; | ||
42 | $ENV{'ENV'}=''; | ||
43 | $PROGNAME = "check_uptime"; | ||
44 | $state = $ERRORS{'UNKNOWN'}; | ||
45 | |||
46 | my $uptime_file = "/proc/uptime"; | ||
47 | |||
48 | |||
49 | # Process arguments | ||
50 | |||
51 | Getopt::Long::Configure('bundling'); | ||
52 | $status = process_arguments(); | ||
53 | if ($status){ | ||
54 | print "ERROR: processing arguments\n"; | ||
55 | exit $ERRORS{"UNKNOWN"}; | ||
56 | } | ||
57 | |||
58 | |||
59 | # Get uptime info from file | ||
60 | |||
61 | if ( ! -r $uptime_file ) { | ||
62 | print "ERROR: file '$uptime_file' is not readable\n"; | ||
63 | exit $ERRORS{"UNKNOWN"}; | ||
64 | } | ||
65 | |||
66 | if ( ! open FILE, "<", $uptime_file ) { | ||
67 | print "ERROR: cannot read from file '$uptime_file'\n"; | ||
68 | exit $ERRORS{"UNKNOWN"}; | ||
69 | } | ||
70 | |||
71 | chomp( my $file_content = <FILE> ); | ||
72 | close FILE; | ||
73 | |||
74 | print "$uptime_file: $file_content\n" if $verbose; | ||
75 | |||
76 | # Get first digit value (without fraction) | ||
77 | my ( $uptime_seconds ) = $file_content =~ /^([\d]+)/; | ||
78 | |||
79 | # Bail out if value is not numeric | ||
80 | if ( $uptime_seconds !~ /^\d+$/ ) { | ||
81 | print "ERROR: no numeric value: $uptime_seconds\n"; | ||
82 | exit $ERRORS{"UNKNOWN"}; | ||
83 | } | ||
84 | |||
85 | |||
86 | # Do calculations for a "pretty" format (2 weeks, 5 days, ...) | ||
87 | |||
88 | my ( $secs, $mins, $hours, $days, $weeks ); | ||
89 | $secs = $uptime_seconds; | ||
90 | $mins = $hours = $days = $weeks = 0; | ||
91 | if ( $secs > 100 ) { | ||
92 | $mins = int( $secs / 60 ); | ||
93 | $secs -= $mins * 60; | ||
94 | } | ||
95 | if ( $mins > 100 ) { | ||
96 | $hours = int( $mins / 60 ); | ||
97 | $mins -= $hours * 60; | ||
98 | } | ||
99 | if ( $hours > 48 ) { | ||
100 | $days = int( $hours / 24 ); | ||
101 | $hours -= $days * 24; | ||
102 | } | ||
103 | if ( $days > 14 ) { | ||
104 | $weeks = int( $days / 7 ); | ||
105 | $days -= $weeks * 7; | ||
106 | } | ||
107 | |||
108 | my $pretty_uptime = ""; | ||
109 | $pretty_uptime .= sprintf( "%d week%s, ", $weeks, $weeks == 1 ? "" : "s" ) if $weeks; | ||
110 | $pretty_uptime .= sprintf( "%d day%s, ", $days, $days == 1 ? "" : "s" ) if $days; | ||
111 | $pretty_uptime .= sprintf( "%d hour%s, ", $hours, $hours == 1 ? "" : "s" ) if $hours; | ||
112 | $pretty_uptime .= sprintf( "%d minute%s, ", $mins, $mins == 1 ? "" : "s" ) if $mins; | ||
113 | # Replace last occurence of comma with "and" | ||
114 | $pretty_uptime =~ s/, $/ and /; | ||
115 | # Always print the seconds (though it may be 0 seconds) | ||
116 | $pretty_uptime .= sprintf( "%d second%s", $secs, $secs == 1 ? "" : "s" ); | ||
117 | |||
118 | |||
119 | # Default to catch errors in program | ||
120 | my $state_str = "UNKNOWN"; | ||
121 | |||
122 | # Check values | ||
123 | my $out_of_bounds_text = ""; | ||
124 | if ( $uptime_seconds > $upper_crit_threshold ) { | ||
125 | $state_str = "CRITICAL"; | ||
126 | $out_of_bounds_text = "upper crit"; | ||
127 | } elsif ( $uptime_seconds < $lower_crit_threshold ) { | ||
128 | $state_str = "CRITICAL"; | ||
129 | $out_of_bounds_text = "lower crit"; | ||
130 | } elsif ( $uptime_seconds > $upper_warn_threshold ) { | ||
131 | $state_str = "WARNING"; | ||
132 | $out_of_bounds_text = "upper warn"; | ||
133 | } elsif ( $uptime_seconds < $lower_warn_threshold ) { | ||
134 | $state_str = "WARNING"; | ||
135 | $out_of_bounds_text = "lower warn"; | ||
136 | } else { | ||
137 | $state_str = "OK"; | ||
138 | } | ||
139 | |||
140 | $msg = "$state_str: "; | ||
141 | |||
142 | $msg .= "uptime is $uptime_seconds seconds. "; | ||
143 | $msg .= "Exceeds $out_of_bounds_text threshold. " if $out_of_bounds_text; | ||
144 | $msg .= "Running for $pretty_uptime. " if $opt_f; | ||
145 | if ( $opt_s ) { | ||
146 | my $up_since = strftime( "%Y-%m-%d %H:%M:%S", localtime( time - $uptime_seconds ) ); | ||
147 | $msg .= "Running since $up_since. "; | ||
148 | } | ||
149 | |||
150 | $state = $ERRORS{$state_str}; | ||
151 | |||
152 | # Perfdata support | ||
153 | print "$msg|uptime=${uptime_seconds}s;$upper_warn_threshold;$upper_crit_threshold;0\n"; | ||
154 | exit $state; | ||
155 | |||
156 | |||
157 | ##################################### | ||
158 | #### subs | ||
159 | |||
160 | |||
161 | sub process_arguments(){ | ||
162 | GetOptions | ||
163 | ("V" => \$opt_V, "version" => \$opt_V, | ||
164 | "v" => \$opt_v, "verbose" => \$opt_v, | ||
165 | "h" => \$opt_h, "help" => \$opt_h, | ||
166 | "w=s" => \$opt_w, "warning=s" => \$opt_w, # warning if above this number | ||
167 | "c=s" => \$opt_c, "critical=s" => \$opt_c, # critical if above this number | ||
168 | "f" => \$opt_f, "for" => \$opt_f, # show "running for ..." | ||
169 | "s" => \$opt_s, "since" => \$opt_s, # show "running since ..." | ||
170 | ); | ||
171 | |||
172 | if ($opt_V) { | ||
173 | print_revision($PROGNAME,'@NP_VERSION@'); | ||
174 | exit $ERRORS{'UNKNOWN'}; | ||
175 | } | ||
176 | |||
177 | if ($opt_h) { | ||
178 | print_help(); | ||
179 | exit $ERRORS{'UNKNOWN'}; | ||
180 | } | ||
181 | |||
182 | if (defined $opt_v) { | ||
183 | $verbose = $opt_v; | ||
184 | } | ||
185 | |||
186 | unless ( defined $opt_w && defined $opt_c ) { | ||
187 | print_usage(); | ||
188 | exit $ERRORS{'UNKNOWN'}; | ||
189 | } | ||
190 | |||
191 | # Check if a range was supplied ("lowvalue:highvalue") for warning and critical | ||
192 | # Otherwise, set 0 as the lower threshold and the parameter value as upper threshold | ||
193 | # (the uptime should always be positive, so there should be no issue) | ||
194 | if ( $opt_w =~ /^(.+):(.+)$/ ) { | ||
195 | $lower_warn_threshold = $1; | ||
196 | $upper_warn_threshold = $2; | ||
197 | } else { | ||
198 | $lower_warn_threshold = 0; | ||
199 | $upper_warn_threshold = $opt_w; | ||
200 | } | ||
201 | if ( $opt_c =~ /^(.+):(.+)$/ ) { | ||
202 | $lower_crit_threshold = $1; | ||
203 | $upper_crit_threshold = $2; | ||
204 | } else { | ||
205 | $lower_crit_threshold = 0; | ||
206 | $upper_crit_threshold = $opt_c; | ||
207 | } | ||
208 | |||
209 | # Set as seconds (calculate if suffix present) | ||
210 | $lower_warn_threshold = calc_as_seconds( $lower_warn_threshold ); | ||
211 | $lower_crit_threshold = calc_as_seconds( $lower_crit_threshold ); | ||
212 | $upper_warn_threshold = calc_as_seconds( $upper_warn_threshold ); | ||
213 | $upper_crit_threshold = calc_as_seconds( $upper_crit_threshold ); | ||
214 | |||
215 | # Check for numeric value of warning parameter | ||
216 | if ( $lower_warn_threshold !~ /^\d+$/ ) { | ||
217 | print "Lower warning (-w) is not numeric\n"; | ||
218 | exit $ERRORS{'UNKNOWN'}; | ||
219 | } | ||
220 | if ( $upper_warn_threshold !~ /^\d+$/ ) { | ||
221 | print "Upper warning (-w) is not numeric\n"; | ||
222 | exit $ERRORS{'UNKNOWN'}; | ||
223 | } | ||
224 | # Check for numeric value of critical parameter | ||
225 | if ( $lower_crit_threshold !~ /^\d+$/ ) { | ||
226 | print "Lower critical (-c) is not numeric\n"; | ||
227 | exit $ERRORS{'UNKNOWN'}; | ||
228 | } | ||
229 | if ( $upper_crit_threshold !~ /^\d+$/ ) { | ||
230 | print "Upper critical (-c) is not numeric\n"; | ||
231 | exit $ERRORS{'UNKNOWN'}; | ||
232 | } | ||
233 | |||
234 | # Check boundaries | ||
235 | if ( $upper_warn_threshold >= $upper_crit_threshold ) { | ||
236 | print "Upper Warning (-w) cannot be greater than Critical (-c)!\n"; | ||
237 | exit $ERRORS{'UNKNOWN'}; | ||
238 | } | ||
239 | # No "<=" since both values are zero if no range (only upper threshold values) is given | ||
240 | if ( $lower_warn_threshold < $lower_crit_threshold ) { | ||
241 | print "Lower Warning (-w) cannot be less than Critical (-c)!\n"; | ||
242 | exit $ERRORS{'UNKNOWN'}; | ||
243 | } | ||
244 | |||
245 | return $ERRORS{'OK'}; | ||
246 | } | ||
247 | |||
248 | sub print_usage () { | ||
249 | print "Usage: $PROGNAME -w <warn> -c <crit> [-v]\n"; | ||
250 | } | ||
251 | |||
252 | sub print_help () { | ||
253 | print_revision($PROGNAME,'@NP_VERSION@'); | ||
254 | print "Copyright (c) 2002 Subhendu Ghosh/Carlos Canau/Benjamin Schmid\n"; | ||
255 | print "Copyright (c) 2018 Bernd Arnold\n"; | ||
256 | print "\n"; | ||
257 | print_usage(); | ||
258 | print "\n"; | ||
259 | print " Checks the uptime of the system using $uptime_file\n"; | ||
260 | print "\n"; | ||
261 | print "-w (--warning) = Min. number of uptime to generate warning\n"; | ||
262 | print "-c (--critical) = Min. number of uptime to generate critical alert ( w < c )\n"; | ||
263 | print "-f (--for) = Show uptime in a pretty format (Running for x weeks, x days, ...)\n"; | ||
264 | print "-s (--since) = Show last boot in yyyy-mm-dd HH:MM:SS format (output from 'uptime -s')\n"; | ||
265 | print "-h (--help)\n"; | ||
266 | print "-V (--version)\n"; | ||
267 | print "-v (--verbose) = debugging output\n"; | ||
268 | print "\n\n"; | ||
269 | print "Note: -w and -c are required arguments.\n"; | ||
270 | print " You can suffix both values with s for seconds (default), m (minutes), h (hours), d (days) or w (weeks).\n"; | ||
271 | print "\n"; | ||
272 | print "Range support: You may specify a range for both warning and critical thresholds.\n"; | ||
273 | print " This works without additional Perl modules.\n"; | ||
274 | print "Example: ./check_uptime -w 10m:4w -c 1m:8w\n"; | ||
275 | print " Results in a critical state when uptime is below 60 seconds or higher than 8 weeks,\n"; | ||
276 | print " and in a warning state when uptime is below 10 minutes or above 4 weeks.\n"; | ||
277 | print "\n\n"; | ||
278 | support(); | ||
279 | } | ||
280 | |||
281 | sub calc_as_seconds () { | ||
282 | |||
283 | my $parameter = shift; | ||
284 | |||
285 | # Check if suffix is present | ||
286 | # Calculate parameter to seconds (to get an integer value finally) | ||
287 | # If no suffix is present, just return the value | ||
288 | |||
289 | # Possible suffixes: | ||
290 | # s = seconds | ||
291 | # m = minutes | ||
292 | # h = hours | ||
293 | # d = days | ||
294 | # w = weeks | ||
295 | my %factor = ( "s" => 1, | ||
296 | "m" => 60, | ||
297 | "h" => 60 * 60, | ||
298 | "d" => 60 * 60 * 24, | ||
299 | "w" => 60 * 60 * 24 * 7, | ||
300 | ); | ||
301 | |||
302 | if ( $parameter =~ /^(\d+)([a-z])$/ ) { | ||
303 | my $value = $1; | ||
304 | my $suffix = $2; | ||
305 | print "detected: value=$value, suffix=$suffix\n" if $verbose; | ||
306 | if ( ! defined $factor{$suffix} ) { | ||
307 | print "Error: wrong suffix ($suffix) for value '$parameter'"; | ||
308 | exit $ERRORS{'UNKNOWN'}; | ||
309 | } | ||
310 | $parameter = $value * $factor{$suffix}; | ||
311 | } | ||
312 | |||
313 | return $parameter; | ||
314 | |||
315 | } | ||