1 /*****************************************************************************
3 * Nagios check_disk plugin
6 * Copyright (c) 2000 Karl DeBisschop (kdebisschop@users.sourceforge.net)
7 * Copyright (c) 2000-2007 Nagios Plugins Development Team
11 * This file contains the check_disk plugin
14 * This program is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 *****************************************************************************/
30 const char *progname
= "check_swap";
31 const char *copyright
= "2000-2007";
32 const char *email
= "nagiosplug-devel@lists.sourceforge.net";
38 #ifdef HAVE_DECL_SWAPCTL
39 # ifdef HAVE_SYS_PARAM_H
40 # include <sys/param.h>
42 # ifdef HAVE_SYS_SWAP_H
43 # include <sys/swap.h>
45 # ifdef HAVE_SYS_STAT_H
46 # include <sys/stat.h>
50 #ifndef SWAP_CONVERSION
51 # define SWAP_CONVERSION 1
54 int check_swap (int usp
, float free_swap_mb
);
55 int process_arguments (int argc
, char **argv
);
56 int validate_arguments (void);
57 void print_usage (void);
58 void print_help (void);
62 float warn_size_bytes
= 0;
63 float crit_size_bytes
= 0;
68 main (int argc
, char **argv
)
70 int percent_used
, percent
;
71 float total_swap_mb
= 0, used_swap_mb
= 0, free_swap_mb
= 0;
72 float dsktotal_mb
= 0, dskused_mb
= 0, dskfree_mb
= 0, tmp_mb
= 0;
73 int result
= STATE_UNKNOWN
;
74 char input_buffer
[MAX_INPUT_BUFFER
];
75 #ifdef HAVE_PROC_MEMINFO
78 int conv_factor
= SWAP_CONVERSION
;
84 # ifdef HAVE_DECL_SWAPCTL
85 int i
=0, nswaps
=0, swapctl_res
=0;
86 # ifdef CHECK_SWAP_SWAPCTL_SVR4
90 # ifdef CHECK_SWAP_SWAPCTL_BSD
92 # endif /* CHECK_SWAP_SWAPCTL_BSD */
93 # endif /* CHECK_SWAP_SWAPCTL_SVR4 */
94 # endif /* HAVE_DECL_SWAPCTL */
100 setlocale (LC_ALL
, "");
101 bindtextdomain (PACKAGE
, LOCALEDIR
);
102 textdomain (PACKAGE
);
104 status
= strdup ("");
106 /* Parse extra opts if any */
107 argv
=np_extra_opts (&argc
, argv
, progname
);
109 if (process_arguments (argc
, argv
) == ERROR
)
110 usage4 (_("Could not parse arguments"));
112 #ifdef HAVE_PROC_MEMINFO
114 printf("Reading PROC_MEMINFO at %s\n", PROC_MEMINFO
);
116 fp
= fopen (PROC_MEMINFO
, "r");
117 while (fgets (input_buffer
, MAX_INPUT_BUFFER
- 1, fp
)) {
118 if (sscanf (input_buffer
, "%*[S]%*[w]%*[a]%*[p]%*[:] %f %f %f", &dsktotal_mb
, &dskused_mb
, &dskfree_mb
) == 3) {
119 dsktotal_mb
= dsktotal_mb
/ 1048576; /* Apply conversion */
120 dskused_mb
= dskused_mb
/ 1048576;
121 dskfree_mb
= dskfree_mb
/ 1048576;
122 total_swap_mb
+= dsktotal_mb
;
123 used_swap_mb
+= dskused_mb
;
124 free_swap_mb
+= dskfree_mb
;
126 if (dsktotal_mb
== 0)
129 percent
= 100 * (((double) dskused_mb
) / ((double) dsktotal_mb
));
130 result
= max_state (result
, check_swap (percent
, dskfree_mb
));
132 asprintf (&status
, "%s [%.0f (%d%%)]", status
, dskfree_mb
, 100 - percent
);
135 else if (sscanf (input_buffer
, "%*[S]%*[w]%*[a]%*[p]%[TotalFre]%*[:] %f %*[k]%*[B]", str
, &tmp_mb
)) {
137 printf("Got %s with %f\n", str
, tmp_mb
);
139 /* I think this part is always in Kb, so convert to mb */
140 if (strcmp ("Total", str
) == 0) {
141 dsktotal_mb
= tmp_mb
/ 1024;
143 else if (strcmp ("Free", str
) == 0) {
144 dskfree_mb
= tmp_mb
/ 1024;
149 dskused_mb
= dsktotal_mb
- dskfree_mb
;
150 total_swap_mb
= dsktotal_mb
;
151 used_swap_mb
= dskused_mb
;
152 free_swap_mb
= dskfree_mb
;
155 asprintf(&swap_command
, "%s", SWAP_COMMAND
);
156 asprintf(&swap_format
, "%s", SWAP_FORMAT
);
158 /* These override the command used if a summary (and thus ! allswaps) is required */
159 /* The summary flag returns more accurate information about swap usage on these OSes */
162 asprintf(&swap_command
, "%s", "/usr/sbin/lsps -s");
163 asprintf(&swap_format
, "%s", "%f%*s %f");
169 printf (_("Command: %s\n"), swap_command
);
171 printf (_("Format: %s\n"), swap_format
);
173 child_process
= spopen (swap_command
);
174 if (child_process
== NULL
) {
175 printf (_("Could not open pipe: %s\n"), swap_command
);
176 return STATE_UNKNOWN
;
179 child_stderr
= fdopen (child_stderr_array
[fileno (child_process
)], "r");
180 if (child_stderr
== NULL
)
181 printf (_("Could not open stderr for %s\n"), swap_command
);
183 sprintf (str
, "%s", "");
185 fgets (input_buffer
, MAX_INPUT_BUFFER
- 1, child_process
);
186 if (strcmp (swap_format
, "") == 0) {
187 temp_buffer
= strtok (input_buffer
, " \n");
188 while (temp_buffer
) {
189 if (strstr (temp_buffer
, "blocks"))
190 sprintf (str
, "%s %s", str
, "%f");
191 else if (strstr (temp_buffer
, "dskfree"))
192 sprintf (str
, "%s %s", str
, "%f");
194 sprintf (str
, "%s %s", str
, "%*s");
195 temp_buffer
= strtok (NULL
, " \n");
199 /* If different swap command is used for summary switch, need to read format differently */
202 fgets(input_buffer
, MAX_INPUT_BUFFER
- 1, child_process
); /* Ignore first line */
203 sscanf (input_buffer
, swap_format
, &total_swap_mb
, &used_swap_mb
);
204 free_swap_mb
= total_swap_mb
* (100 - used_swap_mb
) /100;
205 used_swap_mb
= total_swap_mb
- free_swap_mb
;
207 printf (_("total=%.0f, used=%.0f, free=%.0f\n"), total_swap_mb
, used_swap_mb
, free_swap_mb
);
210 while (fgets (input_buffer
, MAX_INPUT_BUFFER
- 1, child_process
)) {
211 sscanf (input_buffer
, swap_format
, &dsktotal_mb
, &dskfree_mb
);
213 dsktotal_mb
= dsktotal_mb
/ conv_factor
;
214 /* AIX lists percent used, so this converts to dskfree in MBs */
216 dskfree_mb
= dsktotal_mb
* (100 - dskfree_mb
) / 100;
218 dskfree_mb
= dskfree_mb
/ conv_factor
;
221 printf (_("total=%.0f, free=%.0f\n"), dsktotal_mb
, dskfree_mb
);
223 dskused_mb
= dsktotal_mb
- dskfree_mb
;
224 total_swap_mb
+= dsktotal_mb
;
225 used_swap_mb
+= dskused_mb
;
226 free_swap_mb
+= dskfree_mb
;
228 percent
= 100 * (((double) dskused_mb
) / ((double) dsktotal_mb
));
229 result
= max_state (result
, check_swap (percent
, dskfree_mb
));
231 asprintf (&status
, "%s [%.0f (%d%%)]", status
, dskfree_mb
, 100 - percent
);
238 /* If we get anything on STDERR, at least set warning */
239 while (fgets (input_buffer
, MAX_INPUT_BUFFER
- 1, child_stderr
))
240 result
= max_state (result
, STATE_WARNING
);
243 (void) fclose (child_stderr
);
246 if (spclose (child_process
))
247 result
= max_state (result
, STATE_WARNING
);
249 # ifdef CHECK_SWAP_SWAPCTL_SVR4
251 /* get the number of active swap devices */
252 if((nswaps
=swapctl(SC_GETNSWP
, NULL
))== -1)
253 die(STATE_UNKNOWN
, _("Error getting swap devices\n") );
256 die(STATE_OK
, _("SWAP OK: No swap devices defined\n"));
259 printf("Found %d swap device(s)\n", nswaps
);
261 /* initialize swap table + entries */
262 tbl
=(swaptbl_t
*)malloc(sizeof(swaptbl_t
)+(sizeof(swapent_t
)*nswaps
));
265 die(STATE_UNKNOWN
, _("malloc() failed!\n"));
267 memset(tbl
, 0, sizeof(swaptbl_t
)+(sizeof(swapent_t
)*nswaps
));
269 for(i
=0;i
<nswaps
;i
++){
270 if((tbl
->swt_ent
[i
].ste_path
=(char*)malloc(sizeof(char)*MAXPATHLEN
)) == NULL
)
271 die(STATE_UNKNOWN
, _("malloc() failed!\n"));
274 /* and now, tally 'em up */
275 swapctl_res
=swapctl(SC_LIST
, tbl
);
277 perror(_("swapctl failed: "));
278 die(STATE_UNKNOWN
, _("Error in swapctl call\n"));
281 for(i
=0;i
<nswaps
;i
++){
282 dsktotal_mb
= (float) tbl
->swt_ent
[i
].ste_pages
/ SWAP_CONVERSION
;
283 dskfree_mb
= (float) tbl
->swt_ent
[i
].ste_free
/ SWAP_CONVERSION
;
284 dskused_mb
= ( dsktotal_mb
- dskfree_mb
);
287 printf ("dsktotal_mb=%.0f dskfree_mb=%.0f dskused_mb=%.0f\n", dsktotal_mb
, dskfree_mb
, dskused_mb
);
289 if(allswaps
&& dsktotal_mb
> 0){
290 percent
= 100 * (((double) dskused_mb
) / ((double) dsktotal_mb
));
291 result
= max_state (result
, check_swap (percent
, dskfree_mb
));
293 asprintf (&status
, "%s [%.0f (%d%%)]", status
, dskfree_mb
, 100 - percent
);
297 total_swap_mb
+= dsktotal_mb
;
298 free_swap_mb
+= dskfree_mb
;
299 used_swap_mb
+= dskused_mb
;
302 /* and clean up after ourselves */
303 for(i
=0;i
<nswaps
;i
++){
304 free(tbl
->swt_ent
[i
].ste_path
);
308 # ifdef CHECK_SWAP_SWAPCTL_BSD
310 /* get the number of active swap devices */
311 nswaps
=swapctl(SWAP_NSWAP
, NULL
, 0);
313 /* initialize swap table + entries */
314 ent
=(struct swapent
*)malloc(sizeof(struct swapent
)*nswaps
);
316 /* and now, tally 'em up */
317 swapctl_res
=swapctl(SWAP_STATS
, ent
, nswaps
);
319 perror(_("swapctl failed: "));
320 die(STATE_UNKNOWN
, _("Error in swapctl call\n"));
323 for(i
=0;i
<nswaps
;i
++){
324 dsktotal_mb
= (float) ent
[i
].se_nblks
/ conv_factor
;
325 dskused_mb
= (float) ent
[i
].se_inuse
/ conv_factor
;
326 dskfree_mb
= ( dsktotal_mb
- dskused_mb
);
328 if(allswaps
&& dsktotal_mb
> 0){
329 percent
= 100 * (((double) dskused_mb
) / ((double) dsktotal_mb
));
330 result
= max_state (result
, check_swap (percent
, dskfree_mb
));
332 asprintf (&status
, "%s [%.0f (%d%%)]", status
, dskfree_mb
, 100 - percent
);
336 total_swap_mb
+= dsktotal_mb
;
337 free_swap_mb
+= dskfree_mb
;
338 used_swap_mb
+= dskused_mb
;
341 /* and clean up after ourselves */
344 # endif /* CHECK_SWAP_SWAPCTL_BSD */
345 # endif /* CHECK_SWAP_SWAPCTL_SVR4 */
346 # endif /* HAVE_SWAP */
347 #endif /* HAVE_PROC_MEMINFO */
349 /* if total_swap_mb == 0, let's not divide by 0 */
351 percent_used
= 100 * ((double) used_swap_mb
) / ((double) total_swap_mb
);
356 result
= max_state (result
, check_swap (percent_used
, free_swap_mb
));
357 printf (_("SWAP %s - %d%% free (%d MB out of %d MB) %s|"),
359 (100 - percent_used
), (int) free_swap_mb
, (int) total_swap_mb
, status
);
361 puts (perfdata ("swap", (long) free_swap_mb
, "MB",
362 TRUE
, (long) max (warn_size_bytes
/(1024 * 1024), warn_percent
/100.0*total_swap_mb
),
363 TRUE
, (long) max (crit_size_bytes
/(1024 * 1024), crit_percent
/100.0*total_swap_mb
),
365 TRUE
, (long) total_swap_mb
));
373 check_swap (int usp
, float free_swap_mb
)
375 int result
= STATE_UNKNOWN
;
376 float free_swap
= free_swap_mb
* (1024 * 1024); /* Convert back to bytes as warn and crit specified in bytes */
377 if (usp
>= 0 && crit_percent
!= 0 && usp
>= (100.0 - crit_percent
))
378 result
= STATE_CRITICAL
;
379 else if (crit_size_bytes
> 0 && free_swap
<= crit_size_bytes
)
380 result
= STATE_CRITICAL
;
381 else if (usp
>= 0 && warn_percent
!= 0 && usp
>= (100.0 - warn_percent
))
382 result
= STATE_WARNING
;
383 else if (warn_size_bytes
> 0 && free_swap
<= warn_size_bytes
)
384 result
= STATE_WARNING
;
392 /* process command-line arguments */
394 process_arguments (int argc
, char **argv
)
396 int c
= 0; /* option character */
399 static struct option longopts
[] = {
400 {"warning", required_argument
, 0, 'w'},
401 {"critical", required_argument
, 0, 'c'},
402 {"allswaps", no_argument
, 0, 'a'},
403 {"verbose", no_argument
, 0, 'v'},
404 {"version", no_argument
, 0, 'V'},
405 {"help", no_argument
, 0, 'h'},
413 c
= getopt_long (argc
, argv
, "+?Vvhac:w:", longopts
, &option
);
415 if (c
== -1 || c
== EOF
)
419 case 'w': /* warning size threshold */
420 if (is_intnonneg (optarg
)) {
421 warn_size_bytes
= (float) atoi (optarg
);
424 else if (strstr (optarg
, ",") &&
425 strstr (optarg
, "%") &&
426 sscanf (optarg
, "%f,%d%%", &warn_size_bytes
, &warn_percent
) == 2) {
427 warn_size_bytes
= floorf(warn_size_bytes
);
430 else if (strstr (optarg
, "%") &&
431 sscanf (optarg
, "%d%%", &warn_percent
) == 1) {
435 usage4 (_("Warning threshold must be integer or percentage!"));
437 case 'c': /* critical size threshold */
438 if (is_intnonneg (optarg
)) {
439 crit_size_bytes
= (float) atoi (optarg
);
442 else if (strstr (optarg
, ",") &&
443 strstr (optarg
, "%") &&
444 sscanf (optarg
, "%f,%d%%", &crit_size_bytes
, &crit_percent
) == 2) {
445 crit_size_bytes
= floorf(crit_size_bytes
);
448 else if (strstr (optarg
, "%") &&
449 sscanf (optarg
, "%d%%", &crit_percent
) == 1) {
453 usage4 (_("Critical threshold must be integer or percentage!"));
455 case 'a': /* all swap */
458 case 'v': /* verbose */
461 case 'V': /* version */
462 print_revision (progname
, NP_VERSION
);
467 case '?': /* error */
474 return validate_arguments ();
475 if (warn_percent
== 0 && is_intnonneg (argv
[c
]))
476 warn_percent
= atoi (argv
[c
++]);
479 return validate_arguments ();
480 if (crit_percent
== 0 && is_intnonneg (argv
[c
]))
481 crit_percent
= atoi (argv
[c
++]);
484 return validate_arguments ();
485 if (warn_size_bytes
== 0 && is_intnonneg (argv
[c
]))
486 warn_size_bytes
= (float) atoi (argv
[c
++]);
489 return validate_arguments ();
490 if (crit_size_bytes
== 0 && is_intnonneg (argv
[c
]))
491 crit_size_bytes
= (float) atoi (argv
[c
++]);
493 return validate_arguments ();
499 validate_arguments (void)
501 if (warn_percent
== 0 && crit_percent
== 0 && warn_size_bytes
== 0
502 && crit_size_bytes
== 0) {
505 else if (warn_percent
< crit_percent
) {
507 (_("Warning percentage should be more than critical percentage"));
509 else if (warn_size_bytes
< crit_size_bytes
) {
511 (_("Warning free space should be more than critical free space"));
521 print_revision (progname
, NP_VERSION
);
523 printf (_(COPYRIGHT
), copyright
, email
);
525 printf ("%s\n", _("Check swap space on local machine."));
531 printf (_(UT_HELP_VRSN
));
532 printf (_(UT_EXTRA_OPTS
));
534 printf (" %s\n", "-w, --warning=INTEGER");
535 printf (" %s\n", _("Exit with WARNING status if less than INTEGER bytes of swap space are free"));
536 printf (" %s\n", "-w, --warning=PERCENT%%");
537 printf (" %s\n", _("Exit with WARNING status if less than PERCENT of swap space is free"));
538 printf (" %s\n", "-c, --critical=INTEGER");
539 printf (" %s\n", _("Exit with CRITICAL status if less than INTEGER bytes of swap space are free"));
540 printf (" %s\n", "-c, --critical=PERCENT%%");
541 printf (" %s\n", _("Exit with CRITCAL status if less than PERCENT of swap space is free"));
542 printf (" %s\n", "-a, --allswaps");
543 printf (" %s\n", _("Conduct comparisons for all swap partitions, one by one"));
544 printf (_(UT_VERBOSE
));
547 printf ("%s\n", _("Notes:"));
548 printf (" %s\n", _("On AIX, if -a is specified, uses lsps -a, otherwise uses lsps -s."));
551 printf (_(UT_EXTRA_OPTS_NOTES
));
555 printf (_(UT_SUPPORT
));
563 printf (_("Usage:"));
564 printf ("%s [-av] -w <percent_free>%% -c <percent_free>%%\n",progname
);
565 printf ("%s [-av] -w <bytes_free> -c <bytes_free>\n", progname
);