-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_apt
executable file
·147 lines (130 loc) · 3.64 KB
/
check_apt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/perl -w
#
# check_debian_packages - nagios plugin
#
#
# Copyright (C) 2005 Francesc Guasch (main effort, see http://exchange.nagios.org/directory/Plugins/Operating-Systems/Linux/check_apt/details)
# Copyright (C) 2014 Thomas Dalichow (small changes, mainly for convenience)
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# Report bugs to: frankie@etsetb.upc.edu
#
use strict;
use lib '/usr/lib/nagios/plugins';
use utils qw(%ERRORS &print_revision &support &usage);
use Getopt::Long;
my $VERSION = '0.06';
my $RET = 'OK';
my $LOCK_FILE = "/var/lib/dpkg/lock";
my $CMD_APT = "/usr/bin/apt-get -s dist-upgrade";
my $TIMEOUT = 60;
my $DEBUG = 0;
#####################################################################
#
# Command line arguments
#
sub print_usage ();
my ($help,$version);
GetOptions( help => \$help,
debug => \$DEBUG,
version => \$version,
'timeout=s' => \$TIMEOUT
);
my ($PROGNAME) = $0 =~ m#.*/(.*)#;
if ($help) {
print_revision($PROGNAME,"\$Revision: $VERSION \$");
print "Copyright (c) 2005 Francesc Guasch - Ortiz
Perl Check debian packages plugin for Nagios
";
print_usage();
exit($ERRORS{OK});
}
if ($version) {
print_revision($PROGNAME,"\$Revision: $VERSION \$");
exit($ERRORS{OK});
}
#
# unlikely but compliant
#
$SIG{'ALRM'} = sub {
print ("ERROR: Timeout\n");
exit $ERRORS{"UNKNOWN"};
};
alarm($TIMEOUT);
######################################################################
#
# subs
#
sub print_usage () {
print "Usage: $PROGNAME [--debug] [--version] [--help]"
." [--timeout=$TIMEOUT]\n";
}
sub add_info {
my ($info,$type,$pkg) = @_;
$$info .= scalar(keys %$pkg)." new pkgs in $type: ";
if ($type eq "other") {
$$info .= join " ",keys %$pkg;
}
else {
$$info .= join " ",keys %$pkg,"- ";
}
}
sub exit_unknown {
my ($info) = @_;
chomp $info;
$RET='UNKNOWN';
print "$RET: $info\n";
exit $ERRORS{$RET};
};
sub run_apt {
my ($pkg,$ver,$type,$release);
open APT,"$CMD_APT 2>&1|" or exit_unknown($!);
my (%stable,%security,%other);
while (<APT>) {
print "APT: $_" if $DEBUG;
exit_unknown($_) if /(Could not open lock file)|(Could not get lock)/;
next unless /^Inst/;
($pkg,$ver,$release) = /Inst (.*?) .*\((.*?) (.*?)\)/;
print "$_\npkg=$pkg ver=$ver release=$release\n" if $DEBUG;
die "$_\n" unless defined $release;
$release = 'stable'
if $release =~ /stable$/ && $release !~/security/i;
$release = 'security'
if $release =~ /security/i;
if ($release eq 'stable') {
$stable{$pkg} = $ver;
} elsif ($release eq 'security') {
$security{$pkg} = $ver;
} else {
$other{$pkg}=$ver;
}
}
close APT;
my $info = '';
if (keys (%security)) {
$RET = 'CRITICAL';
add_info(\$info,'security',\%security);
add_info(\$info,'stable',\%stable) if keys %stable;
add_info(\$info,'other',\%other) if keys %other;
} elsif (keys (%other) or keys(%stable)) {
$RET = 'WARNING';
add_info(\$info,'stable',\%stable);
add_info(\$info,'other',\%other) if keys %other;
}
print "$RET: $info\n";
}
run_apt();
exit $ERRORS{$RET};