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)
115 {
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);
169 }
170 \f
174 void
175 print_help (void)
176 {
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 ();
186 }
188 void
189 print_usage (void)
190 {
191 printf ("Usage:\n" " %s %s\n"
192 " %s (-h | --help) for detailed help\n"
193 " %s (-V | --version) for version information\n",
194 progname, OPTIONS, progname, progname);
195 }
196 \f
199 /******************************************************************************
200 @@-
201 <sect3>
202 <title>process_arguments</title>
204 <para>This function parses the command line into the needed
205 variables.</para>
207 <para>Aside from the standard 'help' and 'version' options, there
208 is a only a 'timeout' option.No validation is currently done.</para>
210 </sect3>
211 -@@
212 ******************************************************************************/
214 /* process command-line arguments */
215 int
216 process_arguments (int argc, char **argv)
217 {
218 int c;
220 int option_index = 0;
221 static struct option long_options[] = {
222 {"help", no_argument, 0, 'h'},
223 {"version", no_argument, 0, 'V'},
224 {"timeout", required_argument, 0, 't'},
225 {0, 0, 0, 0}
226 };
228 while (1) {
229 c = getopt_long (argc, argv, "hVt:",
230 long_options, &option_index);
232 if (c == -1 || c == EOF)
233 break;
235 switch (c) {
236 case '?': /* help */
237 usage3 ("Unknown argument", optopt);
238 case 'h': /* help */
239 print_help ();
240 exit (EXIT_SUCCESS);
241 case 'V': /* version */
242 print_revision (progname, REVISION);
243 exit (EXIT_SUCCESS);
244 case 't': /* timeout period */
245 if (!is_integer (optarg))
246 usage2 ("Timeout Interval must be an integer", optarg);
247 timeout_interval = atoi (optarg);
248 break;
249 }
250 }
252 asprintf (&command_line, "%s", argv[optind]);
253 for (c = optind+1; c < argc; c++) {
254 asprintf (&command_line, "%s %s", command_line, argv[c]);
255 }
257 return validate_arguments ();
258 }
261 /******************************************************************************
262 @@-
263 <sect3>
264 <title>validate_arguments</title>
266 <para>No validation is currently done.</para>
268 </sect3>
269 -@@
270 ******************************************************************************/
272 int
273 validate_arguments ()
274 {
275 if (command_line == NULL)
276 return ERROR;
277 return STATE_OK;
278 }
279 \f
281 /******************************************************************************
282 @@-
283 </sect2>
284 </sect1>
285 </article>
286 -@@
287 ******************************************************************************/