Handle case with no current work
[infodrom/icinga-plugins] / check_uucp
1 #! /usr/bin/perl -w
2
3 #   Copyright (c) 2014  Joey Schulze <joey@infodrom.org>
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 2 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, write to the Free Software
17 #   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
18
19 use strict;
20 use Getopt::Long;
21 use lib '/usr/lib/nagios/plugins';
22 use utils qw(%ERRORS);
23
24 my $config = {
25     'host' => undef,
26     'warn_jobs' => 50,
27     'crit_jobs' => 150,
28     'warn_hours' => 24,
29     'crit_hours' => 72,
30
31 };
32
33 my $host;
34 Getopt::Long::Configure('no_ignore_case');
35 GetOptions
36     'host|h=s' => \$host,
37     'warn=s' => \$config->{warn_jobs},
38     'crit=s' => \$config->{crit_jobs},
39     'WARN=s' => \$config->{warn_hours},
40     'CRIT=s' => \$config->{crit_hours},
41     'help' => \&print_help;
42
43 sub print_help
44 {
45     print<<EOT;
46 help
47 EOT
48     exit $ERRORS{'OK'};
49 }
50
51 sub uustat
52 {
53     my $host = shift;
54     my $hours = shift;
55
56     my $cmd = sprintf('/usr/bin/uustat -s %s %s', $host,
57                       $hours ? '-o ' . $hours : '');
58
59     open my $prg, '-|', $cmd || return;
60     my @list;
61     while (<$prg>) {
62         push @list, $_;
63     }
64     close $prg;
65
66     return \@list;
67 }
68
69 unless ($host) {
70     print "UUCP ERROR: -h <hostname> missing\n";
71     exit $ERRORS{CRITICAL};
72 }
73
74 my $list = uustat $host;
75
76 unless (defined $list) {
77     printf "UUCP CRITICAL - %s: Execution of uustat failed\n", $host;
78     exit $ERRORS{CRITICAL};
79 }
80
81 if (scalar @$list > $config->{warn_jobs}) {
82     printf "UUCP WARNING - %s: %d jobs\n", $host, scalar @$list;
83     exit $ERRORS{WARNING};
84 }
85
86 if (scalar @$list > $config->{crit_jobs}) {
87     printf "UUCP CRITICAL - %s: %d jobs\n", $host, scalar @$list;
88     exit $ERRORS{CRITICAL};
89 }
90
91 my $oldlist;
92 $oldlist = uustat $host, $config->{warn_hours};
93
94 unless (defined $oldlist) {
95     printf "UUCP WARNING - %s: Execution of uustat failed\n", $host;
96     exit $ERRORS{WARNING};
97 }
98
99 if (scalar @$oldlist > 0) {
100     printf "UUCP WARNING - %s: %d jobs older than %d hours\n", $host, scalar @$list, $config->{crit_hours};
101     exit $ERRORS{WARNING};
102 }
103
104 $oldlist = uustat $host, $config->{crit_hours};
105
106 unless (defined $oldlist) {
107     printf "UUCP WARNING - %s: Execution of uustat failed\n", $host;
108     exit $ERRORS{WARNING};
109 }
110
111 if (scalar @$oldlist > 0) {
112     printf "UUCP CRITICAL - %s: %d jobs older than %d hours\n", $host, scalar @$list, $config->{crit_hours};
113     exit $ERRORS{CRITICAL};
114 }
115
116 printf "UUCP OK - %s: %d jobs\n", $host, scalar @$list;
117 exit $ERRORS{'OK'};