1 /******************************************************************************
2 *
3 * Nagios negate plugin
4 *
5 * License: GPL
6 * Copyright (c) 2002-2006 nagios-plugins team
7 *
8 * Last Modified: $Date$
9 *
10 * Description:
11 *
12 * This file contains the negate plugin
13 *
14 * License Information:
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 *
30 * $Id$
32 @@-<article>
34 <sect1>
35 <title>Quick Reference</title>
36 <refentry>
37 <refmeta><manvolnum>5<manvolnum></refmeta>
38 <refnamdiv>
39 <refname>&progname;</refname>
40 <refpurpose>&SUMMARY;</refpurpose>
41 </refnamdiv>
42 </refentry>
43 </sect1>
45 <sect1>
46 <title>FAQ</title>
47 </sect1>
49 <sect1>
50 <title>Theory, Installation, and Operation</title>
52 <sect2>
53 <title>General Description</title>
54 <para>
55 &DESCRIPTION;
56 </para>
57 </sect2>
59 <sect2>
60 <title>Future Enhancements</title>
61 <para>ToDo List</para>
62 <itemizedlist>
63 <listitem>Add option to do regex substitution in output text</listitem>
64 </itemizedlist>
65 </sect2>-@@
67 ******************************************************************************/
69 const char *progname = "negate";
70 const char *revision = "$Revision$";
71 const char *copyright = "2002-2006";
72 const char *email = "nagiosplug-devel@lists.sourceforge.net";
74 #define DEFAULT_TIMEOUT 9
76 #include "common.h"
77 #include "utils.h"
78 #include "popen.h"
80 char *command_line;
82 int process_arguments (int, char **);
83 int validate_arguments (void);
84 void print_help (void);
85 void print_usage (void);
89 int
90 main (int argc, char **argv)
91 {
92 int found = 0, result = STATE_UNKNOWN;
93 char *buf;
95 setlocale (LC_ALL, "");
96 bindtextdomain (PACKAGE, LOCALEDIR);
97 textdomain (PACKAGE);
99 if (process_arguments (argc, argv) == ERROR)
100 usage4 (_("Could not parse arguments"));
102 /* Set signal handling and alarm */
103 if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR)
104 die (STATE_UNKNOWN, _("Cannot catch SIGALRM"));
106 (void) alarm ((unsigned) timeout_interval);
108 child_process = spopen (command_line);
109 if (child_process == NULL)
110 die (STATE_UNKNOWN, _("Could not open pipe: %s\n"), command_line);
112 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
114 if (child_stderr == NULL) {
115 printf (_("Could not open stderr for %s\n"), command_line);
116 }
118 buf = malloc(MAX_INPUT_BUFFER);
119 while (fgets (buf, MAX_INPUT_BUFFER - 1, child_process)) {
120 found++;
121 printf ("%s", buf);
122 }
124 if (!found)
125 die (STATE_UNKNOWN,
126 _("%s problem - No data received from host\nCMD: %s\n"),\
127 argv[0], command_line);
129 /* close the pipe */
130 result = spclose (child_process);
132 /* WARNING if output found on stderr */
133 if (fgets (buf, MAX_INPUT_BUFFER - 1, child_stderr))
134 result = max_state (result, STATE_WARNING);
136 /* close stderr */
137 (void) fclose (child_stderr);
139 if (result == STATE_OK)
140 exit (STATE_CRITICAL);
141 else if (result == STATE_CRITICAL)
142 exit (EXIT_SUCCESS);
143 else
144 exit (result);
145 }
147 /******************************************************************************
148 @@-
149 <sect2>
150 <title>Functions</title>
152 <sect3>
153 <title>process_arguments</title>
155 <para>This function parses the command line into the needed
156 variables.</para>
158 <para>Aside from the standard 'help' and 'version' options, there
159 is a only a 'timeout' option.</para>
161 </sect3>
162 -@@
163 ******************************************************************************/
167 /* process command-line arguments */
168 int
169 process_arguments (int argc, char **argv)
170 {
171 int c;
173 int option = 0;
174 static struct option longopts[] = {
175 {"help", no_argument, 0, 'h'},
176 {"version", no_argument, 0, 'V'},
177 {"timeout", required_argument, 0, 't'},
178 {0, 0, 0, 0}
179 };
181 while (1) {
182 c = getopt_long (argc, argv, "+hVt:",
183 longopts, &option);
185 if (c == -1 || c == EOF)
186 break;
188 switch (c) {
189 case '?': /* help */
190 usage2 (_("Unknown argument"), optarg);
191 break;
192 case 'h': /* help */
193 print_help ();
194 exit (EXIT_SUCCESS);
195 break;
196 case 'V': /* version */
197 print_revision (progname, revision);
198 exit (EXIT_SUCCESS);
199 case 't': /* timeout period */
200 if (!is_integer (optarg))
201 usage2 (_("Timeout interval must be a positive integer"), optarg);
202 else
203 timeout_interval = atoi (optarg);
204 break;
205 }
206 }
208 asprintf (&command_line, "%s", argv[optind]);
209 for (c = optind+1; c < argc; c++) {
210 asprintf (&command_line, "%s %s", command_line, argv[c]);
211 }
213 return validate_arguments ();
214 }
217 /******************************************************************************
218 @@-
219 <sect3>
220 <title>validate_arguments</title>
222 <para>No validation is currently done.</para>
224 </sect3>
225 -@@
226 ******************************************************************************/
230 int
231 validate_arguments ()
232 {
233 if (command_line == NULL)
234 return ERROR;
235 return STATE_OK;
236 }
238 /******************************************************************************
239 @@-
240 </sect2>
241 </sect1>
242 </article>
243 -@@
244 ******************************************************************************/
248 void
249 print_help (void)
250 {
251 print_revision (progname, revision);
253 printf (COPYRIGHT, copyright, email);
255 printf ("%s\n", _("Negates the status of a plugin (returns OK for CRITICAL, and vice-versa)."));
257 printf ("\n\n");
259 print_usage ();
261 printf (_(UT_HELP_VRSN));
263 printf (_(UT_TIMEOUT), DEFAULT_TIMEOUT);
265 printf (" %s\n", _("[keep timeout than the plugin timeout to retain CRITICAL status]"));
266 printf ("\n");
267 printf ("%s\n", _("Examples:"));
268 printf (" %s\n", "negate \"/usr/local/nagios/libexec/check_ping -H host\"");
269 printf (" %s\n", _("Run check_ping and invert result. Must use full path to plugin"));
270 printf (" %s\n", "negate \"/usr/local/nagios/libexec/check_procs -a 'vi negate.c'\"");
271 printf (" %s\n", _("Use single quotes if you need to retain spaces"));
272 printf (_(UT_VERBOSE));
273 printf ("\n");
274 printf ("%s\n", _("Notes:"));
275 printf ("%s\n", _("This plugin is a wrapper to take the output of another plugin and invert it."));
276 printf ("%s\n", _("If the wrapped plugin returns STATE_OK, the wrapper will return STATE_CRITICAL."));
277 printf ("%s\n", _("If the wrapped plugin returns STATE_CRITICAL, the wrapper will return STATE_OK."));
278 printf ("%s\n", _("Otherwise, the output state of the wrapped plugin is unchanged."));
280 printf (_(UT_SUPPORT));
281 }
285 void
286 print_usage (void)
287 {
288 printf (_("Usage:"));
289 printf ("%s [-t timeout] <definition of wrapped plugin>\n",progname);
290 }