Code

Fixed reading too many argv parameters and changed to asprintf
[nagiosplug.git] / plugins / negate.c
1 /******************************************************************************
2  *
3  * Program: Inverting plugin wrapper for Nagios
4  * License: GPL
5  *
6  * License Information:
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  * $Id$
23  *
24  *****************************************************************************/
26 const char *progname = "negate";
27 #define REVISION "$Revision$"
28 #define COPYRIGHT "2002"
29 #define AUTHOR "Karl DeBisschop"
30 #define EMAIL "kdebisschop@users.sourceforge.net"
31 #define SUMMARY "Negates the status of a plugin (returns OK for CRITICAL, and vice-versa).\n"
33 #define OPTIONS "\
34 [-t timeout] <definition of wrapped plugin>"
36 #define LONGOPTIONS "\
37   -t, --timeout=INTEGER\n\
38     Terminate test if timeout limit is exceeded (default: %d)\n\
39      [keep this less than the plugin timeout to retain CRITICAL status]\n"
41 #define DESCRIPTION "\
42 This plugin is a wrapper to take the output of another plugin and invert it.\n\
43 If the wrapped plugin returns STATE_OK, the wrapper will return STATE_CRITICAL.\n\
44 If the wrapped plugin returns STATE_CRITICAL, the wrapper will return STATE_OK.\n\
45 Otherwise, the output state of the wrapped plugin is unchanged.\n"
47 #define DEFAULT_TIMEOUT 9
49 #include "common.h"
50 #include "utils.h"
51 #include "popen.h"
53 char *command_line;
55 int process_arguments (int, char **);
56 int validate_arguments (void);
57 void print_usage (void);
58 void print_help (void);
59 \f
60 /******************************************************************************
62 The (psuedo?)literate programming XML is contained within \@\@\- <XML> \-\@\@
63 tags in the comments. With in the tags, the XML is assembled sequentially.
64 You can define entities in tags. You also have all the #defines available as
65 entities.
67 Please note that all tags must be lowercase to use the DocBook XML DTD.
69 @@-<article>
71 <sect1>
72 <title>Quick Reference</title>
73 <!-- The refentry forms a manpage -->
74 <refentry>
75 <refmeta>
76 <manvolnum>5<manvolnum>
77 </refmeta>
78 <refnamdiv>
79 <refname>&progname;</refname>
80 <refpurpose>&SUMMARY;</refpurpose>
81 </refnamdiv>
82 </refentry>
83 </sect1>
85 <sect1>
86 <title>FAQ</title>
87 </sect1>
89 <sect1>
90 <title>Theory, Installation, and Operation</title>
92 <sect2>
93 <title>General Description</title>
94 <para>
95 &DESCRIPTION;
96 </para>
97 </sect2>
99 <sect2>
100 <title>Future Enhancements</title>
101 <para>ToDo List</para>
102 <itemizedlist>
103 <listitem>Add option to do regex substitution in output text</listitem>
104 </itemizedlist>
105 </sect2>
108 <sect2>
109 <title>Functions</title>
110 -@@
111 ******************************************************************************/
113 int
114 main (int argc, char **argv)
116         int found = 0, result = STATE_UNKNOWN;
117         char input_buffer[MAX_INPUT_BUFFER];
119         if (process_arguments (argc, argv) == ERROR)
120                 usage ("Could not parse arguments\n");
122         /* Set signal handling and alarm */
123         if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR)
124                 terminate (STATE_UNKNOWN, "Cannot catch SIGALRM");
126         (void) alarm ((unsigned) timeout_interval);
128         child_process = spopen (command_line);
129         if (child_process == NULL)
130                 terminate (STATE_UNKNOWN, "Could not open pipe: %s\n", command_line);
132         child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
133         if (child_stderr == NULL) {
134                 printf ("Could not open stderr for %s\n", command_line);
135         }
137         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
138                 found++;
139                 if (strchr (input_buffer, '\n')) {
140                         input_buffer[strcspn (input_buffer, "\n")] = 0;
141                         printf ("%s\n", input_buffer);
142                 }
143                 else {
144                         printf ("%s\n", input_buffer);
145                 }
146         }
148         if (!found)
149                 terminate (STATE_UNKNOWN,\
150                            "%s problem - No data recieved from host\nCMD: %s\n",\
151                            argv[0],     command_line);
153         /* close the pipe */
154         result = spclose (child_process);
156         /* WARNING if output found on stderr */
157         if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
158                 result = max_state (result, STATE_WARNING);
160         /* close stderr */
161         (void) fclose (child_stderr);
163         if (result == STATE_OK)
164                 exit (STATE_CRITICAL);
165         else if (result == STATE_CRITICAL)
166                 exit (EXIT_SUCCESS);
167         else
168                 exit (result);
170 \f
174 void
175 print_help (void)
177         print_revision (progname, REVISION);
178         printf
179                 ("Copyright (c) %s %s <%s>\n\n%s\n",
180                  COPYRIGHT, AUTHOR, EMAIL, SUMMARY);
181         print_usage ();
182         printf
183                 ("\nOptions:\n" LONGOPTIONS "\n" DESCRIPTION "\n", 
184                  DEFAULT_TIMEOUT);
185         support ();
188 void
189 print_usage (void)
191         printf ("Usage:\n" " %s %s\n"
192 #ifdef HAVE_GETOPT_H
193                                         " %s (-h | --help) for detailed help\n"
194                                         " %s (-V | --version) for version information\n",
195 #else
196                                         " %s -h for detailed help\n"
197                                         " %s -V for version information\n",
198 #endif
199                                         progname, OPTIONS, progname, progname);
201 \f
204 /******************************************************************************
205 @@-
206 <sect3>
207 <title>process_arguments</title>
209 <para>This function parses the command line into the needed
210 variables.</para>
212 <para>Aside from the standard 'help' and 'version' options, there
213 is a only a 'timeout' option.No validation is currently done.</para>
215 </sect3>
216 -@@
217 ******************************************************************************/
219 /* process command-line arguments */
220 int
221 process_arguments (int argc, char **argv)
223         int c;
225 #ifdef HAVE_GETOPT_H
226         int option_index = 0;
227         static struct option long_options[] = {
228                 {"help", no_argument, 0, 'h'},
229                 {"version", no_argument, 0, 'V'},
230                 {"timeout", required_argument, 0, 't'},
231                 {0, 0, 0, 0}
232         };
233 #endif
235         while (1) {
236 #ifdef HAVE_GETOPT_H
237                 c = getopt_long (argc, argv, "hVt:",
238                                  long_options, &option_index);
239 #else
240                 c = getopt (argc, argv, "hVt:");
241 #endif
242                 if (c == -1 || c == EOF)
243                         break;
245                 switch (c) {
246                 case '?':     /* help */
247                         usage3 ("Unknown argument", optopt);
248                 case 'h':     /* help */
249                         print_help ();
250                         exit (EXIT_SUCCESS);
251                 case 'V':     /* version */
252                         print_revision (progname, REVISION);
253                         exit (EXIT_SUCCESS);
254                 case 't':     /* timeout period */
255                         if (!is_integer (optarg))
256                                 usage2 ("Timeout Interval must be an integer", optarg);
257                         timeout_interval = atoi (optarg);
258                         break;
259                 }
260         }
262         asprintf (&command_line, "%s", argv[optind]);
263         for (c = optind+1; c < argc; c++) {
264                 asprintf (&command_line, "%s %s", command_line, argv[c]);
265         }
267         return validate_arguments ();
271 /******************************************************************************
272 @@-
273 <sect3>
274 <title>validate_arguments</title>
276 <para>No validation is currently done.</para>
278 </sect3>
279 -@@
280 ******************************************************************************/
282 int
283 validate_arguments ()
285         if (command_line == NULL)
286                 return ERROR;
287         return STATE_OK;
289 \f
291 /******************************************************************************
292 @@-
293 </sect2> 
294 </sect1>
295 </article>
296 -@@
297 ******************************************************************************/