Code

spell fix "received"
[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.
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         setlocale (LC_ALL, "");
79         bindtextdomain (PACKAGE, LOCALEDIR);
80         textdomain (PACKAGE);
82         if (process_arguments (argc, argv) == ERROR)
83                 usage (_("Could not parse arguments\n"));
85         /* Set signal handling and alarm */
86         if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR)
87                 die (STATE_UNKNOWN, _("Cannot catch SIGALRM"));
89         (void) alarm ((unsigned) timeout_interval);
91         child_process = spopen (command_line);
92         if (child_process == NULL)
93                 die (STATE_UNKNOWN, _("Could not open pipe: %s\n"), command_line);
95         child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
96         if (child_stderr == NULL) {
97                 printf (_("Could not open stderr for %s\n"), command_line);
98         }
100         buf = malloc(MAX_INPUT_BUFFER);
101         while (fgets (buf, MAX_INPUT_BUFFER - 1, child_process)) {
102                 found++;
103                 printf ("%s", buf);
104         }
106         if (!found)
107                 die (STATE_UNKNOWN,
108                      _("%s problem - No data received from host\nCMD: %s\n"),\
109                      argv[0], command_line);
111         /* close the pipe */
112         result = spclose (child_process);
114         /* WARNING if output found on stderr */
115         if (fgets (buf, MAX_INPUT_BUFFER - 1, child_stderr))
116                 result = max_state (result, STATE_WARNING);
118         /* close stderr */
119         (void) fclose (child_stderr);
121         if (result == STATE_OK)
122                 exit (STATE_CRITICAL);
123         else if (result == STATE_CRITICAL)
124                 exit (EXIT_SUCCESS);
125         else
126                 exit (result);
128 \f
131 /******************************************************************************
132 @@-
133 <sect2>
134 <title>Functions</title>
136 <sect3>
137 <title>process_arguments</title>
139 <para>This function parses the command line into the needed
140 variables.</para>
142 <para>Aside from the standard 'help' and 'version' options, there
143 is a only a 'timeout' option.</para>
145 </sect3>
146 -@@
147 ******************************************************************************/
149 /* process command-line arguments */
150 int
151 process_arguments (int argc, char **argv)
153         int c;
155         int option = 0;
156         static struct option longopts[] = {
157                 {"help", no_argument, 0, 'h'},
158                 {"version", no_argument, 0, 'V'},
159                 {"timeout", required_argument, 0, 't'},
160                 {0, 0, 0, 0}
161         };
163         while (1) {
164                 c = getopt_long (argc, argv, "+hVt:",
165                                  longopts, &option);
167                 if (c == -1 || c == EOF)
168                         break;
170                 switch (c) {
171                 case '?':     /* help */
172                         usage3 (_("Unknown argument"), optopt);
173                         break;
174                 case 'h':     /* help */
175                         print_help ();
176                         exit (EXIT_SUCCESS);
177                         break;
178                 case 'V':     /* version */
179                         print_revision (progname, revision);
180                         exit (EXIT_SUCCESS);
181                 case 't':     /* timeout period */
182                         if (!is_integer (optarg))
183                                 usage2 (_("Timeout Interval must be an integer"), optarg);
184                         else
185                                 timeout_interval = atoi (optarg);
186                         break;
187                 }
188         }
190         asprintf (&command_line, "%s", argv[optind]);
191         for (c = optind+1; c < argc; c++) {
192                 asprintf (&command_line, "%s %s", command_line, argv[c]);
193         }
195         return validate_arguments ();
199 /******************************************************************************
200 @@-
201 <sect3>
202 <title>validate_arguments</title>
204 <para>No validation is currently done.</para>
206 </sect3>
207 -@@
208 ******************************************************************************/
210 int
211 validate_arguments ()
213         if (command_line == NULL)
214                 return ERROR;
215         return STATE_OK;
218 /******************************************************************************
219 @@-
220 </sect2> 
221 </sect1>
222 </article>
223 -@@
224 ******************************************************************************/
230 \f
231 void
232 print_help (void)
234         print_revision (progname, revision);
236         printf (_(COPYRIGHT), copyright, email);
238         printf (_("\
239 Negates the status of a plugin (returns OK for CRITICAL, and vice-versa).\n\
240 \n"));
242         print_usage ();
244         printf (_(UT_HELP_VRSN));
246         printf (_(UT_TIMEOUT), DEFAULT_TIMEOUT);
248         printf (_("\
249      [keep timeout than the plugin timeout to retain CRITICAL status]\n"));
251         printf (_("\
252   negate \"/usr/local/nagios/libexec/check_ping -H host\"\n\
253     Run check_ping and invert result. Must use full path to plugin\n\
254   negate \"/usr/local/nagios/libexec/check_procs -a 'vi negate.c'\"\n\
255     Use single quotes if you need to retain spaces\n"));
257         printf (_("\
258 This plugin is a wrapper to take the output of another plugin and invert it.\n\
259 If the wrapped plugin returns STATE_OK, the wrapper will return STATE_CRITICAL.\n\
260 If the wrapped plugin returns STATE_CRITICAL, the wrapper will return STATE_OK.\n\
261 Otherwise, the output state of the wrapped plugin is unchanged.\n"));
263         printf (_(UT_SUPPORT));
270 void
271 print_usage (void)
273         printf (_("Usage: %s [-t timeout] <definition of wrapped plugin>\n"),
274                 progname);
275         printf (_(UT_HLP_VRS), progname, progname);