Code

Added getaddrinfo.[ch] & gethostbyname.[ch] to provide RFC2553 functions
[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 #ifdef HAVE_GETOPT_H
144         int option_index = 0;
145         static struct option long_options[] = {
146                 {"help", no_argument, 0, 'h'},
147                 {"version", no_argument, 0, 'V'},
148                 {"critical", required_argument, 0, 'c'},
149                 {"warning", required_argument, 0, 'w'},
150                 {"command", required_argument, 0, 'C'},
151                 {0, 0, 0, 0}
152         };
153 #endif
155         if (argc < 2)
156                 return ERROR;
158         while (1) {
159 #ifdef HAVE_GETOPT_H
160                 c = getopt_long (argc, argv, "+hVc:w:C:", long_options, &option_index);
161 #else
162                 c = getopt (argc, argv, "+hVc:w:C:");
163 #endif
164                 if (c == EOF)
165                         break;
167                 switch (c) {
168                 case '?':                                                                       /* help */
169                         print_usage (progname);
170                         exit (STATE_UNKNOWN);
171                 case 'h':                                                                       /* help */
172                         print_help (progname);
173                         exit (STATE_OK);
174                 case 'V':                                                                       /* version */
175                         print_revision (progname, "$Revision$");
176                         exit (STATE_OK);
177                 case 'c':                                                                       /* critical threshold */
178                         if (!is_intnonneg (optarg)) {
179                                 printf ("%s: critical threshold must be an integer: %s\n",
180                                                                 progname, optarg);
181                                 print_usage (progname);
182                                 exit (STATE_UNKNOWN);
183                         }
184                         crit = atoi (optarg);
185                         break;
186                 case 'w':                                                                       /* warning threshold */
187                         if (!is_intnonneg (optarg)) {
188                                 printf ("%s: warning threshold must be an integer: %s\n",
189                                                                 progname, optarg);
190                                 print_usage (progname);
191                                 exit (STATE_UNKNOWN);
192                         }
193                         warn = atoi (optarg);
194                         break;
195                 case 'C':                                                                       /* command name */
196                         proc = optarg;
197                         break;
198                 }
199         }
201         c = optind;
202         if (warn == -1) {
203                 if (!is_intnonneg (argv[c])) {
204                         printf ("%s: critical threshold must be an integer: %s\n",
205                                                         progname, argv[c]);
206                         print_usage (progname);
207                         exit (STATE_UNKNOWN);
208                 }
209                 warn = atoi (argv[c++]);
210         }
212         if (crit == -1) {
213                 if (!is_intnonneg (argv[c])) {
214                         printf ("%s: critical threshold must be an integer: %s\n",
215                                                         progname, argv[c]);
216                         print_usage (progname);
217                         exit (STATE_UNKNOWN);
218                 }
219                 crit = atoi (argv[c++]);
220         }
222         if (proc == NULL)
223                 proc = argv[c];
225         return c;
228 void
229 print_usage (const char *cmd)
231         printf ("Usage: %s -w <wsize> -c <csize> [-C command]\n"
232                                         "       %s --help\n" "       %s --version\n", cmd, cmd, cmd);
235 void
236 print_help (const char *cmd)
238         print_revision ("check_vsz", "$Revision$");
239         printf
240                 ("Copyright (c) 2000 Karl DeBisschop <kdebiss@alum.mit.edu>\n\n"
241                  "This plugin checks the image size of a running program and returns an\n"
242                  "error if the number is above either of the thresholds given.\n\n");
243         print_usage (cmd);
244         printf
245                 ("\nOptions:\n"
246                  " -h, --help\n"
247                  "    Print detailed help\n"
248                  " -V, --version\n"
249                  "    Print version numbers and license information\n"
250                  " -w, --warning=INTEGER\n"
251                  "    Program image size necessary to cause a WARNING state\n"
252                  " -c, --critical=INTEGER\n"
253                  "    Program image size necessary to cause a CRITICAL state\n"
254                  " -C, --command=STRING\n" "    Program to search for [optional]\n");