summaryrefslogtreecommitdiffstats
path: root/plugins/check_apt.c
blob: 867fe41e251938a8abed79d20289583c0ee0a2b9 (plain)
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/******************************************************************************
 * check_apt.c: check for available updates in apt package management systems
 * original author: sean finney <seanius@seanius.net> 
 *                  (with some common bits stolen from check_nagios.c)
 ******************************************************************************

 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., 675 Mass Ave, Cambridge, MA 02139, USA.

 $Id$
 
******************************************************************************/

const char *progname = "check_apt";
const char *revision = "$Revision$";
const char *copyright = "2006";
const char *email = "nagiosplug-devel@lists.sourceforge.net";

#include "common.h"
#include "runcmd.h"
#include "utils.h"

#define APTGET_UPGRADE "/usr/bin/apt-get -o 'Debug::NoLocking=true' -s -qq upgrade"
#define APTGET_DISTUPGRADE "/usr/bin/apt-get -o 'Debug::NoLocking=true' -s -qq dist-upgrade"
#define APTGET_UPDATE "/usr/bin/apt-get -q update"

int process_arguments(int, char **);
void print_help(void);
void print_usage(void);

int run_update(void);
int run_upgrade(int *pkgcount);

static int verbose = 0;
static int do_update = 0;
static int dist_upgrade = 0;
static int stderr_warning = 0;
static int exec_warning = 0;

int main (int argc, char **argv) {
	int result=STATE_UNKNOWN, packages_available=0;

	if (process_arguments(argc, argv) == ERROR)
		usage_va(_("Could not parse arguments"));

	/* Set signal handling and alarm timeout */
	if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR) {
		usage_va(_("Cannot catch SIGALRM"));
	}

	/* handle timeouts gracefully... */
	alarm (timeout_interval);

	/* if they want to run apt-get update first... */
	if(do_update) result = run_update();

	/* apt-get upgrade */
	result = max_state(result, run_upgrade(&packages_available));

	if(stderr_warning){
		fprintf(stderr, "warning, output detected on stderr. ");
		fprintf(stderr, "re-run with -v for more information.\n");
	}

	if(packages_available > 0){
		result = max_state(result, STATE_WARNING);
	} else {
		result = max_state(result, STATE_OK);
	}

	printf("APT %s: %d packages available for %s.%s%s%s\n", 
	       state_text(result),
	       packages_available,
	       (dist_upgrade)?"dist-upgrade":"upgrade",
	       (stderr_warning)?" (warnings detected)":"",
	       (stderr_warning && exec_warning)?",":"",
	       (exec_warning)?" (errors detected)":""
	       );

	return result;
}

/* process command-line arguments */
int process_arguments (int argc, char **argv) {
	int c;

	static struct option longopts[] = {
		{"version", no_argument, 0, 'V'},
		{"help", no_argument, 0, 'h'},
		{"verbose", no_argument, 0, 'v'},
		{"timeout", required_argument, 0, 't'},
		{"update", no_argument, 0, 'u'},
		{"dist-upgrade", no_argument, 0, 'd'},
		{0, 0, 0, 0}
	};

	while(1) {
		c = getopt_long(argc, argv, "hVvt:ud", longopts, NULL);

		if(c == -1 || c == EOF || c == 1) break;

		switch(c) {
		case 'h':									/* help */
			print_help();
			exit(STATE_OK);
		case 'V':									/* version */
			print_revision(progname, revision);
			exit(STATE_OK);
		case 'v':
			verbose++;
			break;
		case 't':
			timeout_interval=atoi(optarg);
			break;
		case 'd':
			dist_upgrade=1;
			break;
		case 'u':
			do_update=1;
			break;
		default:
			/* print short usage statement if args not parsable */
			usage_va(_("Unknown argument - %s"), optarg);
		}
	}

	return OK;
}


/* informative help message */
void print_help(void){
	print_revision(progname, revision);
	printf(_(COPYRIGHT), copyright, email);
	printf(_("\
This plugin checks for software updates on systems that use\n\
package management systems based on the apt-get(8) command\n\
found in Debian GNU/Linux\n\
\n\n"));
	print_usage();
	printf(_(UT_HELP_VRSN));
	printf(_(UT_TIMEOUT), timeout_interval);
   	printf(_("\n\
 -d, --dist-upgrade\n\
   Perform a dist-upgrade instead of normal upgrade.\n\n\
The following options require root privileges and should be used with care: \
\n\n"));
   	printf(_("\
 -u, --update\n\
   First perform an 'apt-get update' (note: you may also need to use -t)\
\n\n"));
}

/* simple usage heading */
void print_usage(void){
	printf ("Usage: %s [-du] [-t timeout]\n", progname);
}

/* run an apt-get upgrade */
int run_upgrade(int *pkgcount){
	int i=0, result=STATE_UNKNOWN, pc=0;
	struct output chld_out, chld_err;

	/* run the upgrade */
	if(dist_upgrade==0){
		result = np_runcmd(APTGET_UPGRADE, &chld_out, &chld_err, 0);
	} else {
		result = np_runcmd(APTGET_DISTUPGRADE, &chld_out, &chld_err, 0);
	}
	/* apt-get only changes exit status if there is an internal error */
	if(result != 0){
		exec_warning=1;
		result = STATE_UNKNOWN;
		fprintf(stderr, "'%s' exited with non-zero status.\n%s\n",
		    APTGET_UPGRADE,
		    "Run again with -v for more info.");
	}

	/* parse the output, which should only consist of lines like
	 *
	 * Inst package ....
	 * Conf package ....
	 *
	 * so we'll filter based on "Inst".  If we ever want to do
	 */
	for(i = 0; i < chld_out.lines; i++) {
		if(strncmp(chld_out.line[i], "Inst", 4)==0){
			if(verbose){
				printf("%s\n", chld_out.line[i]);
			}
			pc++;
		}
	}
	*pkgcount=pc;

	/* If we get anything on stderr, at least set warning */
	if(chld_err.buflen){
		stderr_warning=1;
		result = max_state(result, STATE_WARNING);
		if(verbose){
			for(i = 0; i < chld_err.lines; i++) {
				printf("%s\n", chld_err.line[i]);
			}
		}
	}
	return result;
}

/* run an apt-get update (needs root) */
int run_update(void){
	int i=0, result=STATE_UNKNOWN;
	struct output chld_out, chld_err;

	/* run the upgrade */
	result = np_runcmd(APTGET_UPDATE, &chld_out, &chld_err, 0);
	/* apt-get only changes exit status if there is an internal error */
	if(result != 0){
		exec_warning=1;
		result = STATE_UNKNOWN;
		fprintf(stderr, "'%s' exited with non-zero status.\n",
		        APTGET_UPDATE);
	}

	if(verbose){
		for(i = 0; i < chld_out.lines; i++) {
			printf("%s\n", chld_out.line[i]);
		}
	}

	/* If we get anything on stderr, at least set warning */
	if(chld_err.buflen){
		stderr_warning=1;
		result = max_state(result, STATE_WARNING);
		if(verbose){
			for(i = 0; i < chld_err.lines; i++) {
				printf("%s\n", chld_err.line[i]);
			}
		}
	}
	return result;
}