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 @@-<article>
19 <sect1>
20 <title>Quick Reference</title>
21 <refentry>
22 <refmeta><manvolnum>5<manvolnum></refmeta>
23 <refnamdiv>
24 <refname>&progname;</refname>
25 <refpurpose>&SUMMARY;</refpurpose>
26 </refnamdiv>
27 </refentry>
28 </sect1>
30 <sect1>
31 <title>FAQ</title>
32 </sect1>
34 <sect1>
35 <title>Theory, Installation, and Operation</title>
37 <sect2>
38 <title>General Description</title>
39 <para>
40 &DESCRIPTION;
41 </para>
42 </sect2>
44 <sect2>
45 <title>Future Enhancements</title>
46 <para>ToDo List</para>
47 <itemizedlist>
48 <listitem>Add option to do regex substitution in output text</listitem>
49 </itemizedlist>
50 </sect2>-@@
52 ******************************************************************************/
54 const char *progname = "negate";
55 const char *revision = "$Revision$";
56 const char *copyright = "2002-2003";
57 const char *email = "nagiosplug-devel@lists.sourceforge.net";
59 #define DEFAULT_TIMEOUT 9
61 #include "common.h"
62 #include "utils.h"
63 #include "popen.h"
65 char *command_line;
67 int process_arguments (int, char **);
68 int validate_arguments (void);
69 void print_help (void);
70 void print_usage (void);
72 int
73 main (int argc, char **argv)
74 {
75 int found = 0, result = STATE_UNKNOWN;
76 char *buf;
78 if (process_arguments (argc, argv) == ERROR)
79 usage (_("Could not parse arguments\n"));
81 /* Set signal handling and alarm */
82 if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR)
83 die (STATE_UNKNOWN, _("Cannot catch SIGALRM"));
85 (void) alarm ((unsigned) timeout_interval);
87 child_process = spopen (command_line);
88 if (child_process == NULL)
89 die (STATE_UNKNOWN, _("Could not open pipe: %s\n"), command_line);
91 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
92 if (child_stderr == NULL) {
93 printf (_("Could not open stderr for %s\n"), command_line);
94 }
96 buf = malloc(MAX_INPUT_BUFFER);
97 while (fgets (buf, MAX_INPUT_BUFFER - 1, child_process)) {
98 found++;
99 printf ("%s", buf);
100 }
102 if (!found)
103 die (STATE_UNKNOWN,
104 _("%s problem - No data recieved from host\nCMD: %s\n"),\
105 argv[0], command_line);
107 /* close the pipe */
108 result = spclose (child_process);
110 /* WARNING if output found on stderr */
111 if (fgets (buf, MAX_INPUT_BUFFER - 1, child_stderr))
112 result = max_state (result, STATE_WARNING);
114 /* close stderr */
115 (void) fclose (child_stderr);
117 if (result == STATE_OK)
118 exit (STATE_CRITICAL);
119 else if (result == STATE_CRITICAL)
120 exit (EXIT_SUCCESS);
121 else
122 exit (result);
123 }
124 \f
127 /******************************************************************************
128 @@-
129 <sect2>
130 <title>Functions</title>
132 <sect3>
133 <title>process_arguments</title>
135 <para>This function parses the command line into the needed
136 variables.</para>
138 <para>Aside from the standard 'help' and 'version' options, there
139 is a only a 'timeout' option.</para>
141 </sect3>
142 -@@
143 ******************************************************************************/
145 /* process command-line arguments */
146 int
147 process_arguments (int argc, char **argv)
148 {
149 int c;
151 int option = 0;
152 static struct option longopts[] = {
153 {"help", no_argument, 0, 'h'},
154 {"version", no_argument, 0, 'V'},
155 {"timeout", required_argument, 0, 't'},
156 {0, 0, 0, 0}
157 };
159 while (1) {
160 c = getopt_long (argc, argv, "+hVt:",
161 longopts, &option);
163 if (c == -1 || c == EOF)
164 break;
166 switch (c) {
167 case '?': /* help */
168 usage3 (_("Unknown argument"), optopt);
169 break;
170 case 'h': /* help */
171 print_help ();
172 exit (EXIT_SUCCESS);
173 break;
174 case 'V': /* version */
175 print_revision (progname, revision);
176 exit (EXIT_SUCCESS);
177 case 't': /* timeout period */
178 if (!is_integer (optarg))
179 usage2 (_("Timeout Interval must be an integer"), optarg);
180 else
181 timeout_interval = atoi (optarg);
182 break;
183 }
184 }
186 asprintf (&command_line, "%s", argv[optind]);
187 for (c = optind+1; c < argc; c++) {
188 asprintf (&command_line, "%s %s", command_line, argv[c]);
189 }
191 return validate_arguments ();
192 }
195 /******************************************************************************
196 @@-
197 <sect3>
198 <title>validate_arguments</title>
200 <para>No validation is currently done.</para>
202 </sect3>
203 -@@
204 ******************************************************************************/
206 int
207 validate_arguments ()
208 {
209 if (command_line == NULL)
210 return ERROR;
211 return STATE_OK;
212 }
214 /******************************************************************************
215 @@-
216 </sect2>
217 </sect1>
218 </article>
219 -@@
220 ******************************************************************************/
226 \f
227 void
228 print_help (void)
229 {
230 print_revision (progname, revision);
232 printf (_(COPYRIGHT), copyright, email);
234 printf (_("\
235 Negates the status of a plugin (returns OK for CRITICAL, and vice-versa).\n\
236 \n"));
238 print_usage ();
240 printf (_(UT_HELP_VRSN));
242 printf (_(UT_TIMEOUT), DEFAULT_TIMEOUT);
244 printf (_("\
245 [keep timeout than the plugin timeout to retain CRITICAL status]\n"));
247 printf (_("\
248 negate \"/usr/local/nagios/libexec/check_ping -H host\"\n\
249 Run check_ping and invert result. Must use full path to plugin\n\
250 negate \"/usr/local/nagios/libexec/check_procs -a 'vi negate.c'\"\n\
251 Use single quotes if you need to retain spaces\n"));
253 printf (_("\
254 This plugin is a wrapper to take the output of another plugin and invert it.\n\
255 If the wrapped plugin returns STATE_OK, the wrapper will return STATE_CRITICAL.\n\
256 If the wrapped plugin returns STATE_CRITICAL, the wrapper will return STATE_OK.\n\
257 Otherwise, the output state of the wrapped plugin is unchanged.\n"));
259 printf (_(UT_SUPPORT));
260 }
266 void
267 print_usage (void)
268 {
269 printf (_("Usage: %s [-t timeout] <definition of wrapped plugin>\n"),
270 progname);
271 printf (_(UT_HLP_VRS), progname, progname);
272 }