Code

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