Code

still trying to fix #1094326
[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-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");
100         
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);
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)
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                         usage2 (_("Unknown argument"), optarg);
178                         break;
179                 case 'h':     /* help */
180                         print_help ();
181                         exit (EXIT_SUCCESS);
182                         break;
183                 case 'V':     /* version */
184                         print_revision (progname, revision);
185                         exit (EXIT_SUCCESS);
186                 case 't':     /* timeout period */
187                         if (!is_integer (optarg))
188                                 usage2 (_("Timeout interval must be a positive integer"), optarg);
189                         else
190                                 timeout_interval = atoi (optarg);
191                         break;
192                 }
193         }
195         asprintf (&command_line, "%s", argv[optind]);
196         for (c = optind+1; c < argc; c++) {
197                 asprintf (&command_line, "%s %s", command_line, argv[c]);
198         }
200         return validate_arguments ();
204 /******************************************************************************
205 @@-
206 <sect3>
207 <title>validate_arguments</title>
209 <para>No validation is currently done.</para>
211 </sect3>
212 -@@
213 ******************************************************************************/
217 int
218 validate_arguments ()
220         if (command_line == NULL)
221                 return ERROR;
222         return STATE_OK;
225 /******************************************************************************
226 @@-
227 </sect2> 
228 </sect1>
229 </article>
230 -@@
231 ******************************************************************************/
235 void
236 print_help (void)
238         print_revision (progname, revision);
240         printf (COPYRIGHT, copyright, email);
242         printf (_("\
243 Negates the status of a plugin (returns OK for CRITICAL, and vice-versa).\n\n"));
245         print_usage ();
247         printf (_(UT_HELP_VRSN));
249         printf (_(UT_TIMEOUT), DEFAULT_TIMEOUT);
251         printf (_("\
252      [keep timeout than the plugin timeout to retain CRITICAL status]\n"));
254         printf (_("\
255   negate \"/usr/local/nagios/libexec/check_ping -H host\"\n\
256     Run check_ping and invert result. Must use full path to plugin\n\
257   negate \"/usr/local/nagios/libexec/check_procs -a 'vi negate.c'\"\n\
258     Use single quotes if you need to retain spaces\n"));
260         printf (_("\
261 This plugin is a wrapper to take the output of another plugin and invert it.\n\
262 If the wrapped plugin returns STATE_OK, the wrapper will return STATE_CRITICAL.\n\
263 If the wrapped plugin returns STATE_CRITICAL, the wrapper will return STATE_OK.\n\
264 Otherwise, the output state of the wrapped plugin is unchanged.\n"));
266         printf (_(UT_SUPPORT));
271 void
272 print_usage (void)
274         printf ("Usage: %s [-t timeout] <definition of wrapped plugin>\n",progname);