Code

changed Error: by CRITICAL -
[nagiosplug.git] / plugins / negate.c
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.
16  
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-2003";
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) != OK)
87                 usage (_("negate: could not parse arguments\n"));
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");
100         if (child_stderr == NULL) {
101                 printf (_("Could not open stderr for %s\n"), command_line);
102         }
104         buf = malloc(MAX_INPUT_BUFFER);
105         while (fgets (buf, MAX_INPUT_BUFFER - 1, child_process)) {
106                 found++;
107                 printf ("%s", buf);
108         }
110         if (!found)
111                 die (STATE_UNKNOWN,
112                      _("%s problem - No data received from host\nCMD: %s\n"),\
113                      argv[0], command_line);
115         /* close the pipe */
116         result = spclose (child_process);
118         /* WARNING if output found on stderr */
119         if (fgets (buf, MAX_INPUT_BUFFER - 1, child_stderr))
120                 result = max_state (result, STATE_WARNING);
122         /* close stderr */
123         (void) fclose (child_stderr);
125         if (result == STATE_OK)
126                 exit (STATE_CRITICAL);
127         else if (result == STATE_CRITICAL)
128                 exit (EXIT_SUCCESS);
129         else
130                 exit (result);
133 /******************************************************************************
134 @@-
135 <sect2>
136 <title>Functions</title>
138 <sect3>
139 <title>process_arguments</title>
141 <para>This function parses the command line into the needed
142 variables.</para>
144 <para>Aside from the standard 'help' and 'version' options, there
145 is a only a 'timeout' option.</para>
147 </sect3>
148 -@@
149 ******************************************************************************/
153 /* process command-line arguments */
154 int
155 process_arguments (int argc, char **argv)
157         int c;
159         int option = 0;
160         static struct option longopts[] = {
161                 {"help", no_argument, 0, 'h'},
162                 {"version", no_argument, 0, 'V'},
163                 {"timeout", required_argument, 0, 't'},
164                 {0, 0, 0, 0}
165         };
167         while (1) {
168                 c = getopt_long (argc, argv, "+hVt:",
169                                  longopts, &option);
171                 if (c == -1 || c == EOF)
172                         break;
174                 switch (c) {
175                 case '?':     /* help */
176                         printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
177                         print_usage ();
178                         exit (STATE_UNKNOWN);
179                         break;
180                 case 'h':     /* help */
181                         print_help ();
182                         exit (EXIT_SUCCESS);
183                         break;
184                 case 'V':     /* version */
185                         print_revision (progname, revision);
186                         exit (EXIT_SUCCESS);
187                 case 't':     /* timeout period */
188                         if (!is_integer (optarg))
189                                 usage2 (_("Timeout interval must be a positive integer"), optarg);
190                         else
191                                 timeout_interval = atoi (optarg);
192                         break;
193                 }
194         }
196         asprintf (&command_line, "%s", argv[optind]);
197         for (c = optind+1; c < argc; c++) {
198                 asprintf (&command_line, "%s %s", command_line, argv[c]);
199         }
201         return validate_arguments ();
205 /******************************************************************************
206 @@-
207 <sect3>
208 <title>validate_arguments</title>
210 <para>No validation is currently done.</para>
212 </sect3>
213 -@@
214 ******************************************************************************/
218 int
219 validate_arguments ()
221         if (command_line == NULL)
222                 return ERROR;
223         return STATE_OK;
226 /******************************************************************************
227 @@-
228 </sect2> 
229 </sect1>
230 </article>
231 -@@
232 ******************************************************************************/
236 void
237 print_help (void)
239         print_revision (progname, revision);
241         printf (_(COPYRIGHT), copyright, email);
243         printf (_("\
244 Negates the status of a plugin (returns OK for CRITICAL, and vice-versa).\n\
245 \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));
273 void
274 print_usage (void)
276         printf (_("Usage: %s [-t timeout] <definition of wrapped plugin>\n"),
277                 progname);
278         printf (_(UT_HLP_VRS), progname, progname);