Code

Remove getopt_long checks
[nagiosplug.git] / plugins / check_vsz.c
1 /******************************************************************************
2  *
3  * CHECK_VSZ.C
4  *
5  * Program: Process plugin for Nagios
6  * License: GPL
7  * Copyright (c) 1999,2000 Karl DeBisschop <kdebiss@alum.mit.edu>
8  *
9  * Last Modified: $Date$
10  *
11  * Description:
12  *
13  * This plugin will check for processes whose total image size exceeds
14  * the warning or critical thresholds given on the command line.   With
15  * no command_name, everything that shows up on ps is evaluated.
16  * Otherwise, only jobs with the command_name given are examined.
17  * This program is particularly useful if you have to run a piece of
18  * commercial software that has a memory leak.  With it you can shut
19  * down and restart the processes whenever the program threatens to
20  * take over your system.
21  *
22  * Modifications:
23  *
24  * 11-18-1999 Karl DeBisschop (kdebiss@alum.mit.edu)
25  *            change to getopt, use print_help
26  * 08-18-1999 Ethan Galstad (nagios@nagios.org)
27  *            Changed code to use common include file
28  *            Changed fclose() to pclose()
29  * 09-09-1999 Ethan Galstad (nagios@nagios.org)
30  *            Changed popen()/pclose() to spopen()/spclose()
31  * 11-18-1999 Karl DeBisschop (kdebiss@alum.mit.edu)
32  *            set STATE_WARNING of stderr written or nonzero status returned
33  *
34  *****************************************************************************/
36 const char *progname = "check_vsz";
37 #define REVISION "$Revision$"
38 #define COPYRIGHT "1999-2002"
39 #define AUTHOR "Karl DeBisschop"
40 #define EMAIL "karl@debisschop.net"
41 #define SUMMARY "Check the image size of a running program.\n"
43 #include "common.h"
44 #include "popen.h"
45 #include "utils.h"
47 int process_arguments (int argc, char **argv);
48 void print_help (const char *cmd);
49 void print_usage (const char *cmd);
51 int warn = -1;
52 int crit = -1;
53 char *proc = NULL;
55 int
56 main (int argc, char **argv)
57 {
58         int len;
59         int result = STATE_OK;
60         int line = 0;
61         int proc_size = -1;
62         char input_buffer[MAX_INPUT_BUFFER];
63         char proc_name[MAX_INPUT_BUFFER];
64         char *message = "";
66         if (process_arguments (argc, argv) == ERROR) {
67                 printf ("%s: failure parsing arguments\n", progname);
68                 print_help (progname);
69                 return STATE_UNKNOWN;
70         }
72         /* run the command */
73         child_process = spopen (VSZ_COMMAND);
74         if (child_process == NULL) {
75                 printf ("Unable to open pipe: %s\n", VSZ_COMMAND);
76                 return STATE_UNKNOWN;
77         }
79         child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
80         if (child_stderr == NULL)
81                 printf ("Could not open stderr for %s\n", VSZ_COMMAND);
83         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
85                 line++;
87                 /* skip the first line */
88                 if (line == 1)
89                         continue;
91                 if (sscanf (input_buffer, VSZ_FORMAT, &proc_size, proc_name) == 2) {
92                         if (proc == NULL) {
93                                 if (proc_size > warn) {
94                                         asprintf (&message, "%s %s(%d)", message, proc_name, proc_size);
95                                         result = max_state (result, STATE_WARNING);
96                                 }
97                                 if (proc_size > crit) {
98                                         result = STATE_CRITICAL;
99                                 }
100                         }
101                         else if (strstr (proc_name, proc)) {
102                                 asprintf (&message, "%s %d", message, proc_size);
103                                 if (proc_size > warn) {
104                                         result = max_state (result, STATE_WARNING);
105                                 }
106                                 if (proc_size > crit) {
107                                         result = STATE_CRITICAL;
108                                 }
109                         }
110                 }
111         }
113         /* If we get anything on STDERR, at least set warning */
114         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
115                 result = max_state (result, STATE_WARNING);
117         (void) fclose (child_stderr);
119         /* close the pipe */
120         if (spclose (child_process))
121                 result = max_state (result, STATE_WARNING);
123         if (result == STATE_OK)
124                 printf ("ok (all VSZ<%d): %s\n", warn, message);
125         else if (result == STATE_UNKNOWN)
126                 printf ("Unable to read output\n");
127         else if (result == STATE_WARNING)
128                 printf ("WARNING (VSZ>%d):%s\n", warn, message);
129         else
130                 printf ("CRITICAL (VSZ>%d):%s\n", crit, message);
132         return result;
138 int
139 process_arguments (int argc, char **argv)
141         int c;
143         int option_index = 0;
144         static struct option long_options[] = {
145                 {"help", no_argument, 0, 'h'},
146                 {"version", no_argument, 0, 'V'},
147                 {"critical", required_argument, 0, 'c'},
148                 {"warning", required_argument, 0, 'w'},
149                 {"command", required_argument, 0, 'C'},
150                 {0, 0, 0, 0}
151         };
153         if (argc < 2)
154                 return ERROR;
156         while (1) {
157                 c = getopt_long (argc, argv, "+hVc:w:C:", long_options, &option_index);
159                 if (c == EOF)
160                         break;
162                 switch (c) {
163                 case '?':                                                                       /* help */
164                         print_usage (progname);
165                         exit (STATE_UNKNOWN);
166                 case 'h':                                                                       /* help */
167                         print_help (progname);
168                         exit (STATE_OK);
169                 case 'V':                                                                       /* version */
170                         print_revision (progname, "$Revision$");
171                         exit (STATE_OK);
172                 case 'c':                                                                       /* critical threshold */
173                         if (!is_intnonneg (optarg)) {
174                                 printf ("%s: critical threshold must be an integer: %s\n",
175                                                                 progname, optarg);
176                                 print_usage (progname);
177                                 exit (STATE_UNKNOWN);
178                         }
179                         crit = atoi (optarg);
180                         break;
181                 case 'w':                                                                       /* warning threshold */
182                         if (!is_intnonneg (optarg)) {
183                                 printf ("%s: warning threshold must be an integer: %s\n",
184                                                                 progname, optarg);
185                                 print_usage (progname);
186                                 exit (STATE_UNKNOWN);
187                         }
188                         warn = atoi (optarg);
189                         break;
190                 case 'C':                                                                       /* command name */
191                         proc = optarg;
192                         break;
193                 }
194         }
196         c = optind;
197         if (warn == -1) {
198                 if (!is_intnonneg (argv[c])) {
199                         printf ("%s: critical threshold must be an integer: %s\n",
200                                                         progname, argv[c]);
201                         print_usage (progname);
202                         exit (STATE_UNKNOWN);
203                 }
204                 warn = atoi (argv[c++]);
205         }
207         if (crit == -1) {
208                 if (!is_intnonneg (argv[c])) {
209                         printf ("%s: critical threshold must be an integer: %s\n",
210                                                         progname, argv[c]);
211                         print_usage (progname);
212                         exit (STATE_UNKNOWN);
213                 }
214                 crit = atoi (argv[c++]);
215         }
217         if (proc == NULL)
218                 proc = argv[c];
220         return c;
223 void
224 print_usage (const char *cmd)
226         printf ("Usage: %s -w <wsize> -c <csize> [-C command]\n"
227                                         "       %s --help\n" "       %s --version\n", cmd, cmd, cmd);
230 void
231 print_help (const char *cmd)
233         print_revision ("check_vsz", "$Revision$");
234         printf
235                 ("Copyright (c) 2000 Karl DeBisschop <kdebiss@alum.mit.edu>\n\n"
236                  "This plugin checks the image size of a running program and returns an\n"
237                  "error if the number is above either of the thresholds given.\n\n");
238         print_usage (cmd);
239         printf
240                 ("\nOptions:\n"
241                  " -h, --help\n"
242                  "    Print detailed help\n"
243                  " -V, --version\n"
244                  "    Print version numbers and license information\n"
245                  " -w, --warning=INTEGER\n"
246                  "    Program image size necessary to cause a WARNING state\n"
247                  " -c, --critical=INTEGER\n"
248                  "    Program image size necessary to cause a CRITICAL state\n"
249                  " -C, --command=STRING\n" "    Program to search for [optional]\n");