diff options
Diffstat (limited to 'plugins/check_swap.c')
-rw-r--r-- | plugins/check_swap.c | 325 |
1 files changed, 325 insertions, 0 deletions
diff --git a/plugins/check_swap.c b/plugins/check_swap.c new file mode 100644 index 00000000..d225e1dc --- /dev/null +++ b/plugins/check_swap.c | |||
@@ -0,0 +1,325 @@ | |||
1 | /****************************************************************************** | ||
2 | * | ||
3 | * CHECK_SWAP.C | ||
4 | * | ||
5 | * Program: Process plugin for Nagios | ||
6 | * License: GPL | ||
7 | * Copyright (c) 2000 Karl DeBisschop (kdebisschop@users.sourceforge.net) | ||
8 | * | ||
9 | * $Id$ | ||
10 | * | ||
11 | ******************************************************************************/ | ||
12 | |||
13 | #include "common.h" | ||
14 | #include "popen.h" | ||
15 | #include "utils.h" | ||
16 | |||
17 | #define PROGNAME "check_swap" | ||
18 | |||
19 | int process_arguments (int argc, char **argv); | ||
20 | int call_getopt (int argc, char **argv); | ||
21 | int validate_arguments (void); | ||
22 | void print_usage (void); | ||
23 | void print_help (void); | ||
24 | |||
25 | int warn_percent = 200, crit_percent = 200, warn_size = -1, crit_size = -1; | ||
26 | |||
27 | int | ||
28 | main (int argc, char **argv) | ||
29 | { | ||
30 | int total_swap, used_swap, free_swap, percent_used; | ||
31 | int result = STATE_OK; | ||
32 | char input_buffer[MAX_INPUT_BUFFER]; | ||
33 | #ifdef HAVE_SWAP | ||
34 | char *temp_buffer; | ||
35 | #endif | ||
36 | #ifdef HAVE_PROC_MEMINFO | ||
37 | FILE *fp; | ||
38 | #endif | ||
39 | char str[32]; | ||
40 | char *status = NULL; | ||
41 | |||
42 | if (process_arguments (argc, argv) != OK) | ||
43 | usage ("Invalid command arguments supplied\n"); | ||
44 | |||
45 | #ifdef HAVE_PROC_MEMINFO | ||
46 | fp = fopen (PROC_MEMINFO, "r"); | ||
47 | status = ssprintf (status, "%s", "Swap used:"); | ||
48 | while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) { | ||
49 | sscanf (input_buffer, " %s %d %d %d", str, &total_swap, &used_swap, | ||
50 | &free_swap); | ||
51 | if (strstr (str, "Swap")) { | ||
52 | percent_used = 100 * (((float) used_swap) / ((float) total_swap)); | ||
53 | status = ssprintf | ||
54 | (status, | ||
55 | "%s %2d%% (%d bytes out of %d)", | ||
56 | status, percent_used, used_swap, total_swap); | ||
57 | if (percent_used >= crit_percent || free_swap <= crit_size) | ||
58 | result = STATE_CRITICAL; | ||
59 | else if (percent_used >= warn_percent || free_swap <= warn_size) | ||
60 | result = STATE_WARNING; | ||
61 | break; | ||
62 | } | ||
63 | } | ||
64 | fclose (fp); | ||
65 | #else | ||
66 | #ifdef HAVE_SWAP | ||
67 | child_process = spopen (SWAP_COMMAND); | ||
68 | if (child_process == NULL) { | ||
69 | printf ("Could not open pipe: %s\n", SWAP_COMMAND); | ||
70 | return STATE_UNKNOWN; | ||
71 | } | ||
72 | |||
73 | child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r"); | ||
74 | if (child_stderr == NULL) | ||
75 | printf ("Could not open stderr for %s\n", SWAP_COMMAND); | ||
76 | |||
77 | sprintf (str, "%s", ""); | ||
78 | /* read 1st line */ | ||
79 | fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process); | ||
80 | if (strcmp (SWAP_FORMAT, "") == 0) { | ||
81 | temp_buffer = strtok (input_buffer, " \n"); | ||
82 | while (temp_buffer) { | ||
83 | if (strstr (temp_buffer, "blocks")) | ||
84 | sprintf (str, "%s %s", str, "%f"); | ||
85 | else if (strstr (temp_buffer, "free")) | ||
86 | sprintf (str, "%s %s", str, "%f"); | ||
87 | else | ||
88 | sprintf (str, "%s %s", str, "%*s"); | ||
89 | temp_buffer = strtok (NULL, " \n"); | ||
90 | } | ||
91 | } | ||
92 | |||
93 | status = ssprintf (status, "%s", "Swap used:"); | ||
94 | while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { | ||
95 | sscanf (input_buffer, SWAP_FORMAT, &total_swap, &free_swap); | ||
96 | used_swap = total_swap - free_swap; | ||
97 | percent_used = 100 * ((float) used_swap) / ((float) total_swap); | ||
98 | status = ssprintf | ||
99 | (status, | ||
100 | "%s %2d%% (%d bytes out of %d)", | ||
101 | status, percent_used, used_swap, total_swap); | ||
102 | if (percent_used >= crit_percent || free_swap <= crit_size) | ||
103 | result = STATE_CRITICAL; | ||
104 | else if (percent_used >= warn_percent || free_swap <= warn_size) | ||
105 | result = STATE_WARNING; | ||
106 | } | ||
107 | |||
108 | /* If we get anything on STDERR, at least set warning */ | ||
109 | while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) | ||
110 | result = max (result, STATE_WARNING); | ||
111 | |||
112 | /* close stderr */ | ||
113 | (void) fclose (child_stderr); | ||
114 | |||
115 | /* close the pipe */ | ||
116 | if (spclose (child_process)) | ||
117 | result = max (result, STATE_WARNING); | ||
118 | #endif | ||
119 | #endif | ||
120 | |||
121 | #ifndef SWAP_COMMAND | ||
122 | #ifndef SWAP_FILE | ||
123 | #ifndef HAVE_PROC_MEMINFO | ||
124 | return STATE_UNKNOWN; | ||
125 | #endif | ||
126 | #endif | ||
127 | #endif | ||
128 | |||
129 | if (result == STATE_OK) | ||
130 | printf ("Swap ok - %s\n", status); | ||
131 | else if (result == STATE_CRITICAL) | ||
132 | printf ("CRITICAL - %s\n", status); | ||
133 | else if (result == STATE_WARNING) | ||
134 | printf ("WARNING - %s\n", status); | ||
135 | else if (result == STATE_UNKNOWN) | ||
136 | printf ("Unable to read output\n"); | ||
137 | else { | ||
138 | result = STATE_UNKNOWN; | ||
139 | printf ("UNKNOWN - %s\n", status); | ||
140 | } | ||
141 | |||
142 | return result; | ||
143 | } | ||
144 | |||
145 | |||
146 | |||
147 | |||
148 | |||
149 | /* process command-line arguments */ | ||
150 | int | ||
151 | process_arguments (int argc, char **argv) | ||
152 | { | ||
153 | int c; | ||
154 | |||
155 | if (argc < 2) | ||
156 | return ERROR; | ||
157 | |||
158 | c = 0; | ||
159 | while (c += (call_getopt (argc - c, &argv[c]))) { | ||
160 | if (argc <= c) | ||
161 | break; | ||
162 | |||
163 | if (warn_percent > 100 && is_intnonneg (argv[c])) | ||
164 | warn_percent = atoi (argv[c]); | ||
165 | else if (crit_percent > 100 && is_intnonneg (argv[c])) | ||
166 | crit_percent = atoi (argv[c]); | ||
167 | else if (warn_size < 0 && is_intnonneg (argv[c])) | ||
168 | warn_size = atoi (argv[c]); | ||
169 | else if (crit_size < 0 && is_intnonneg (argv[c])) | ||
170 | crit_size = atoi (argv[c]); | ||
171 | } | ||
172 | |||
173 | return validate_arguments (); | ||
174 | } | ||
175 | |||
176 | |||
177 | |||
178 | |||
179 | |||
180 | int | ||
181 | call_getopt (int argc, char **argv) | ||
182 | { | ||
183 | int c, i = 0; | ||
184 | |||
185 | #ifdef HAVE_GETOPT_H | ||
186 | int option_index = 0; | ||
187 | static struct option long_options[] = { | ||
188 | {"warning", required_argument, 0, 'w'}, | ||
189 | {"critical", required_argument, 0, 'c'}, | ||
190 | {"verbose", no_argument, 0, 'v'}, | ||
191 | {"version", no_argument, 0, 'V'}, | ||
192 | {"help", no_argument, 0, 'h'}, | ||
193 | {0, 0, 0, 0} | ||
194 | }; | ||
195 | #endif | ||
196 | |||
197 | while (1) { | ||
198 | #ifdef HAVE_GETOPT_H | ||
199 | c = getopt_long (argc, argv, "+?Vhc:w:", long_options, &option_index); | ||
200 | #else | ||
201 | c = getopt (argc, argv, "+?Vhc:w:"); | ||
202 | #endif | ||
203 | |||
204 | i++; | ||
205 | |||
206 | if (c == -1 || c == EOF) | ||
207 | break; | ||
208 | |||
209 | switch (c) { | ||
210 | case 'c': | ||
211 | case 'w': | ||
212 | i++; | ||
213 | } | ||
214 | |||
215 | switch (c) { | ||
216 | case 'w': /* warning time threshold */ | ||
217 | if (is_intnonneg (optarg)) { | ||
218 | warn_size = atoi (optarg); | ||
219 | break; | ||
220 | } | ||
221 | else if (strstr (optarg, ",") && | ||
222 | strstr (optarg, "%") && | ||
223 | sscanf (optarg, "%d,%d%%", &warn_size, &warn_percent) == 2) { | ||
224 | break; | ||
225 | } | ||
226 | else if (strstr (optarg, "%") && | ||
227 | sscanf (optarg, "%d%%", &warn_percent) == 1) { | ||
228 | break; | ||
229 | } | ||
230 | else { | ||
231 | usage ("Warning threshold must be integer or percentage!\n"); | ||
232 | } | ||
233 | case 'c': /* critical time threshold */ | ||
234 | if (is_intnonneg (optarg)) { | ||
235 | crit_size = atoi (optarg); | ||
236 | break; | ||
237 | } | ||
238 | else if (strstr (optarg, ",") && | ||
239 | strstr (optarg, "%") && | ||
240 | sscanf (optarg, "%d,%d%%", &crit_size, &crit_percent) == 2) { | ||
241 | break; | ||
242 | } | ||
243 | else if (strstr (optarg, "%") && | ||
244 | sscanf (optarg, "%d%%", &crit_percent) == 1) { | ||
245 | break; | ||
246 | } | ||
247 | else { | ||
248 | usage ("Critical threshold must be integer or percentage!\n"); | ||
249 | } | ||
250 | case 'V': /* version */ | ||
251 | print_revision (my_basename (argv[0]), "$Revision$"); | ||
252 | exit (STATE_OK); | ||
253 | case 'h': /* help */ | ||
254 | print_help (); | ||
255 | exit (STATE_OK); | ||
256 | case '?': /* help */ | ||
257 | usage ("Invalid argument\n"); | ||
258 | } | ||
259 | } | ||
260 | return i; | ||
261 | } | ||
262 | |||
263 | |||
264 | |||
265 | |||
266 | |||
267 | int | ||
268 | validate_arguments (void) | ||
269 | { | ||
270 | if (warn_percent > 100 && crit_percent > 100 && warn_size < 0 | ||
271 | && crit_size < 0) { | ||
272 | return ERROR; | ||
273 | } | ||
274 | else if (warn_percent > crit_percent) { | ||
275 | usage | ||
276 | ("Warning percentage should not be less than critical percentage\n"); | ||
277 | } | ||
278 | else if (warn_size < crit_size) { | ||
279 | usage | ||
280 | ("Warning free space should not be more than critical free space\n"); | ||
281 | } | ||
282 | return OK; | ||
283 | } | ||
284 | |||
285 | |||
286 | |||
287 | |||
288 | |||
289 | void | ||
290 | print_usage (void) | ||
291 | { | ||
292 | printf | ||
293 | ("Usage: check_swap -w <used_percentage>%% -c <used_percentage>%%\n" | ||
294 | " check_swap -w <bytes_free> -c <bytes_free>\n" | ||
295 | " check_swap (-V|--version)\n" " check_swap (-h|--help)\n"); | ||
296 | } | ||
297 | |||
298 | |||
299 | |||
300 | |||
301 | |||
302 | void | ||
303 | print_help (void) | ||
304 | { | ||
305 | print_revision (PROGNAME, "$Revision$"); | ||
306 | printf | ||
307 | ("Copyright (c) 2000 Karl DeBisschop\n\n" | ||
308 | "This plugin will check all of the swap partitions and return an\n" | ||
309 | "error if the the avalable swap space is less than specified.\n\n"); | ||
310 | print_usage (); | ||
311 | printf | ||
312 | ("\nOptions:\n" | ||
313 | " -w, --warning=INTEGER\n" | ||
314 | " Exit with WARNING status if less than INTEGER bytes of swap space are free\n" | ||
315 | " -w, --warning=PERCENT%%\n" | ||
316 | " Exit with WARNING status if more than PERCENT of swap space has been used\n" | ||
317 | " -c, --critical=INTEGER\n" | ||
318 | " Exit with CRITICAL status if less than INTEGER bytes of swap space are free\n" | ||
319 | " -c, --critical=PERCENT%%\n" | ||
320 | " Exit with CRITCAL status if more than PERCENT of swap space has been used\n" | ||
321 | " -h, --help\n" | ||
322 | " Print detailed help screen\n" | ||
323 | " -V, --version\n" " Print version information\n\n"); | ||
324 | support (); | ||
325 | } | ||