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