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 ******************************************************************************/
19 const char *progname = "negate";
20 const char *revision = "$Revision$";
21 const char *copyright = "2002-2003";
22 const char *email = "nagiosplug-devel@lists.sourceforge.net";
24 #define DEFAULT_TIMEOUT 9
26 #include "common.h"
27 #include "utils.h"
28 #include "popen.h"
30 void
31 print_usage (void)
32 {
33 printf (_("Usage: %s [-t timeout] <definition of wrapped plugin>\n"),
34 progname);
35 printf (_(UT_HLP_VRS), progname, progname);
36 }
38 void
39 print_help (void)
40 {
41 print_revision (progname, revision);
43 printf (_(COPYRIGHT), copyright, email);
45 printf (_("\
46 Negates the status of a plugin (returns OK for CRITICAL, and vice-versa).\n\
47 \n"));
49 print_usage ();
51 printf (_(UT_HELP_VRSN));
53 printf (_(UT_TIMEOUT), DEFAULT_TIMEOUT);
55 printf (_("\
56 [keep timeout than the plugin timeout to retain CRITICAL status]\n"));
58 printf (_("\
59 negate \"/usr/local/nagios/libexec/check_ping -H host\"\n\
60 Run check_ping and invert result. Must use full path to plugin\n\
61 negate \"/usr/local/nagios/libexec/check_procs -a 'vi negate.c'\"\n\
62 Use single quotes if you need to retain spaces\n"));
64 printf (_("\
65 This plugin is a wrapper to take the output of another plugin and invert it.\n\
66 If the wrapped plugin returns STATE_OK, the wrapper will return STATE_CRITICAL.\n\
67 If the wrapped plugin returns STATE_CRITICAL, the wrapper will return STATE_OK.\n\
68 Otherwise, the output state of the wrapped plugin is unchanged.\n"));
70 printf (_(UT_SUPPORT));
71 }
72 \f
73 char *command_line;
75 int process_arguments (int, char **);
76 int validate_arguments (void);
77 /******************************************************************************
79 The (psuedo?)literate programming XML is contained within \@\@\- <XML> \-\@\@
80 tags in the comments. With in the tags, the XML is assembled sequentially.
81 You can define entities in tags. You also have all the #defines available as
82 entities.
84 Please note that all tags must be lowercase to use the DocBook XML DTD.
86 @@-<article>
88 <sect1>
89 <title>Quick Reference</title>
90 <!-- The refentry forms a manpage -->
91 <refentry>
92 <refmeta>
93 <manvolnum>5<manvolnum>
94 </refmeta>
95 <refnamdiv>
96 <refname>&progname;</refname>
97 <refpurpose>&SUMMARY;</refpurpose>
98 </refnamdiv>
99 </refentry>
100 </sect1>
102 <sect1>
103 <title>FAQ</title>
104 </sect1>
106 <sect1>
107 <title>Theory, Installation, and Operation</title>
109 <sect2>
110 <title>General Description</title>
111 <para>
112 &DESCRIPTION;
113 </para>
114 </sect2>
116 <sect2>
117 <title>Future Enhancements</title>
118 <para>ToDo List</para>
119 <itemizedlist>
120 <listitem>Add option to do regex substitution in output text</listitem>
121 </itemizedlist>
122 </sect2>
125 <sect2>
126 <title>Functions</title>
127 -@@
128 ******************************************************************************/
130 int
131 main (int argc, char **argv)
132 {
133 int found = 0, result = STATE_UNKNOWN;
134 char input_buffer[MAX_INPUT_BUFFER];
136 if (process_arguments (argc, argv) == ERROR)
137 usage (_("Could not parse arguments\n"));
139 /* Set signal handling and alarm */
140 if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR)
141 die (STATE_UNKNOWN, _("Cannot catch SIGALRM"));
143 (void) alarm ((unsigned) timeout_interval);
145 child_process = spopen (command_line);
146 if (child_process == NULL)
147 die (STATE_UNKNOWN, _("Could not open pipe: %s\n"), command_line);
149 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
150 if (child_stderr == NULL) {
151 printf (_("Could not open stderr for %s\n"), command_line);
152 }
154 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
155 found++;
156 if (strchr (input_buffer, '\n')) {
157 input_buffer[strcspn (input_buffer, "\n")] = 0;
158 printf ("%s\n", input_buffer);
159 }
160 else {
161 printf ("%s\n", input_buffer);
162 }
163 }
165 if (!found)
166 die (STATE_UNKNOWN,\
167 _("%s problem - No data recieved from host\nCMD: %s\n"),\
168 argv[0], command_line);
170 /* close the pipe */
171 result = spclose (child_process);
173 /* WARNING if output found on stderr */
174 if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
175 result = max_state (result, STATE_WARNING);
177 /* close stderr */
178 (void) fclose (child_stderr);
180 if (result == STATE_OK)
181 exit (STATE_CRITICAL);
182 else if (result == STATE_CRITICAL)
183 exit (EXIT_SUCCESS);
184 else
185 exit (result);
186 }
187 \f
190 /******************************************************************************
191 @@-
192 <sect3>
193 <title>process_arguments</title>
195 <para>This function parses the command line into the needed
196 variables.</para>
198 <para>Aside from the standard 'help' and 'version' options, there
199 is a only a 'timeout' option.No validation is currently done.</para>
201 </sect3>
202 -@@
203 ******************************************************************************/
205 /* process command-line arguments */
206 int
207 process_arguments (int argc, char **argv)
208 {
209 int c;
211 int option_index = 0;
212 static struct option long_options[] = {
213 {"help", no_argument, 0, 'h'},
214 {"version", no_argument, 0, 'V'},
215 {"timeout", required_argument, 0, 't'},
216 {0, 0, 0, 0}
217 };
219 while (1) {
220 c = getopt_long (argc, argv, "+hVt:",
221 long_options, &option_index);
223 if (c == -1 || c == EOF)
224 break;
226 switch (c) {
227 case '?': /* help */
228 usage3 (_("Unknown argument"), optopt);
229 case 'h': /* help */
230 print_help ();
231 exit (EXIT_SUCCESS);
232 case 'V': /* version */
233 print_revision (progname, revision);
234 exit (EXIT_SUCCESS);
235 case 't': /* timeout period */
236 if (!is_integer (optarg))
237 usage2 (_("Timeout Interval must be an integer"), optarg);
238 timeout_interval = atoi (optarg);
239 break;
240 }
241 }
243 asprintf (&command_line, "%s", argv[optind]);
244 for (c = optind+1; c < argc; c++) {
245 asprintf (&command_line, "%s %s", command_line, argv[c]);
246 }
248 return validate_arguments ();
249 }
252 /******************************************************************************
253 @@-
254 <sect3>
255 <title>validate_arguments</title>
257 <para>No validation is currently done.</para>
259 </sect3>
260 -@@
261 ******************************************************************************/
263 int
264 validate_arguments ()
265 {
266 if (command_line == NULL)
267 return ERROR;
268 return STATE_OK;
269 }
270 \f
272 /******************************************************************************
273 @@-
274 </sect2>
275 </sect1>
276 </article>
277 -@@
278 ******************************************************************************/