diff options
-rw-r--r-- | plugins/check_apt.c | 173 |
1 files changed, 173 insertions, 0 deletions
diff --git a/plugins/check_apt.c b/plugins/check_apt.c new file mode 100644 index 00000000..8d020f50 --- /dev/null +++ b/plugins/check_apt.c | |||
@@ -0,0 +1,173 @@ | |||
1 | /****************************************************************************** | ||
2 | * check_apt.c: check for available updates in apt package management systems | ||
3 | * original author: sean finney <seanius@seanius.net> | ||
4 | * (with some common bits stolen from check_nagios.c) | ||
5 | ****************************************************************************** | ||
6 | |||
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, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
20 | |||
21 | $Id$ | ||
22 | |||
23 | ******************************************************************************/ | ||
24 | |||
25 | const char *progname = "check_apt"; | ||
26 | const char *revision = "$Revision$"; | ||
27 | const char *copyright = "2006"; | ||
28 | const char *email = "nagiosplug-devel@lists.sourceforge.net"; | ||
29 | |||
30 | #include "common.h" | ||
31 | #include "runcmd.h" | ||
32 | #include "utils.h" | ||
33 | |||
34 | #define APTGET_UPGRADE "/usr/bin/apt-get -o 'Debug::NoLocking=true' -s -qq upgrade" | ||
35 | #define APTGET_DISTUPGRADE "/usr/bin/apt-get -o 'Debug::NoLocking=true' -s -qq dist-upgrade" | ||
36 | #define APTGET_UPDATE "/usr/bin/apt-get update" | ||
37 | |||
38 | int process_arguments(int, char **); | ||
39 | void print_help(void); | ||
40 | void print_usage(void); | ||
41 | |||
42 | int run_upgrade(int *pkgcount); | ||
43 | |||
44 | static int verbose = 0; | ||
45 | |||
46 | int main (int argc, char **argv) { | ||
47 | int result=STATE_UNKNOWN, packages_available=0; | ||
48 | |||
49 | if (process_arguments(argc, argv) == ERROR) | ||
50 | usage_va(_("Could not parse arguments")); | ||
51 | |||
52 | /* Set signal handling and alarm timeout */ | ||
53 | if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR) { | ||
54 | usage_va(_("Cannot catch SIGALRM")); | ||
55 | } | ||
56 | |||
57 | /* handle timeouts gracefully... */ | ||
58 | alarm (timeout_interval); | ||
59 | |||
60 | /* apt-get upgrade */ | ||
61 | result = run_upgrade(&packages_available); | ||
62 | |||
63 | if(packages_available > 0){ | ||
64 | result = STATE_WARNING; | ||
65 | printf("APT WARNING: "); | ||
66 | } else { | ||
67 | result = STATE_OK; | ||
68 | printf("APT OK: "); | ||
69 | } | ||
70 | printf("%d packages available for upgrade\n", packages_available); | ||
71 | |||
72 | return result; | ||
73 | } | ||
74 | |||
75 | /* process command-line arguments */ | ||
76 | int process_arguments (int argc, char **argv) { | ||
77 | int c; | ||
78 | |||
79 | static struct option longopts[] = { | ||
80 | {"version", no_argument, 0, 'V'}, | ||
81 | {"help", no_argument, 0, 'h'}, | ||
82 | {"verbose", no_argument, 0, 'v'}, | ||
83 | {"timeout", required_argument, 0, 't'}, | ||
84 | {0, 0, 0, 0} | ||
85 | }; | ||
86 | |||
87 | while(1) { | ||
88 | c = getopt_long(argc, argv, "hVvt", longopts, NULL); | ||
89 | |||
90 | if(c == -1 || c == EOF || c == 1) break; | ||
91 | |||
92 | switch(c) { | ||
93 | case 'h': /* help */ | ||
94 | print_help(); | ||
95 | exit(STATE_OK); | ||
96 | case 'V': /* version */ | ||
97 | print_revision(progname, revision); | ||
98 | exit(STATE_OK); | ||
99 | case 'v': | ||
100 | verbose++; | ||
101 | break; | ||
102 | case 't': | ||
103 | timeout_interval=atoi(optarg); | ||
104 | break; | ||
105 | default: | ||
106 | /* print short usage statement if args not parsable */ | ||
107 | usage_va(_("Unknown argument - %s"), optarg); | ||
108 | } | ||
109 | } | ||
110 | |||
111 | return OK; | ||
112 | } | ||
113 | |||
114 | |||
115 | /* informative help message */ | ||
116 | void print_help(void){ | ||
117 | print_revision(progname, revision); | ||
118 | printf(_(COPYRIGHT), copyright, email); | ||
119 | printf(_("\ | ||
120 | This plugin checks for software updates on systems that use\n\ | ||
121 | package management systems based on the apt-get(8) command\n\ | ||
122 | found in Debian GNU/Linux\n\ | ||
123 | \n\n")); | ||
124 | print_usage(); | ||
125 | printf(_(UT_HELP_VRSN)); | ||
126 | printf(_("\ | ||
127 | -t, --timeout=INTEGER\n\ | ||
128 | Seconds to wait for plugin execution to complete\n\ | ||
129 | ")); | ||
130 | } | ||
131 | |||
132 | /* simple usage heading */ | ||
133 | void print_usage(void){ | ||
134 | printf ("Usage: %s [-u] [-t timeout]\n", progname); | ||
135 | } | ||
136 | |||
137 | /* run an apt-get upgrade */ | ||
138 | int run_upgrade(int *pkgcount){ | ||
139 | int i=0, result=STATE_UNKNOWN, pc=0; | ||
140 | struct output chld_out, chld_err; | ||
141 | |||
142 | /* run the upgrade */ | ||
143 | if((result = np_runcmd(APTGET_UPGRADE, &chld_out, &chld_err, 0)) != 0) | ||
144 | result = STATE_WARNING; | ||
145 | |||
146 | /* parse the output, which should only consist of lines like | ||
147 | * | ||
148 | * Inst package .... | ||
149 | * Conf package .... | ||
150 | * | ||
151 | * so we'll filter based on "Inst" | ||
152 | */ | ||
153 | for(i = 0; i < chld_out.lines; i++) { | ||
154 | if(strncmp(chld_out.line[i], "Inst", 4)==0){ | ||
155 | if(verbose){ | ||
156 | printf("%s\n", chld_out.line[i]); | ||
157 | } | ||
158 | pc++; | ||
159 | } | ||
160 | } | ||
161 | *pkgcount=pc; | ||
162 | |||
163 | /* If we get anything on stderr, at least set warning */ | ||
164 | if(chld_err.buflen){ | ||
165 | fprintf(stderr, "warning, output detected on stderr\n"); | ||
166 | for(i = 0; i < chld_err.lines; i++) { | ||
167 | printf("got this: %s\n", chld_err.line[i]); | ||
168 | result = max_state (result, STATE_WARNING); | ||
169 | } | ||
170 | } | ||
171 | |||
172 | return result; | ||
173 | } | ||