Code

cbde6a1279e716e79646e667bee1f3ba223cbaaf
[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 "utils_cmd.h"
82 //char *command_line;
84 static const char **process_arguments (int, char **);
85 int validate_arguments (char **);
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;
96         char **command_line;
97         output chld_out, chld_err;
98         int i;
100         setlocale (LC_ALL, "");
101         bindtextdomain (PACKAGE, LOCALEDIR);
102         textdomain (PACKAGE);
104         command_line = (char **) process_arguments (argc, argv);
106         /* Set signal handling and alarm */
107         if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR)
108                 die (STATE_UNKNOWN, _("Cannot catch SIGALRM"));
110         (void) alarm ((unsigned) timeout_interval);
112         /* catch when the command is quoted */
113         if(command_line[1] == NULL) {
114                 result = cmd_run (command_line[0], &chld_out, &chld_err, 0);
115         } else {
116                 result = cmd_run_array (command_line, &chld_out, &chld_err, 0);
117         }
118         if (chld_err.lines > 0) {
119                 printf ("Error output from command:\n");
120                 for (i = 0; i < chld_err.lines; i++) {
121                         printf ("%s\n", chld_err.line[i]);
122                 }
123                 exit (STATE_WARNING);
124         }
126         if (chld_out.lines == 0)
127                 die (STATE_UNKNOWN, _("No data returned from command\n"));
129         for (i = 0; i < chld_out.lines; i++) {
130                 printf ("%s\n", chld_out.line[i]);
131         }
133         if (result == STATE_OK)
134                 exit (STATE_CRITICAL);
135         else if (result == STATE_CRITICAL)
136                 exit (EXIT_SUCCESS);
137         else
138                 exit (result);
141 /******************************************************************************
142 @@-
143 <sect2>
144 <title>Functions</title>
146 <sect3>
147 <title>process_arguments</title>
149 <para>This function parses the command line into the needed
150 variables.</para>
152 <para>Aside from the standard 'help' and 'version' options, there
153 is a only a 'timeout' option.</para>
155 </sect3>
156 -@@
157 ******************************************************************************/
161 /* process command-line arguments */
162 static const char **
163 process_arguments (int argc, char **argv)
165         int c;
167         int option = 0;
168         static struct option longopts[] = {
169                 {"help", no_argument, 0, 'h'},
170                 {"version", no_argument, 0, 'V'},
171                 {"timeout", required_argument, 0, 't'},
172                 {0, 0, 0, 0}
173         };
175         while (1) {
176                 c = getopt_long (argc, argv, "+hVt:", longopts, &option);
178                 if (c == -1 || c == EOF)
179                         break;
181                 switch (c) {
182                 case '?':     /* help */
183                         usage5 ();
184                         break;
185                 case 'h':     /* help */
186                         print_help ();
187                         exit (EXIT_SUCCESS);
188                         break;
189                 case 'V':     /* version */
190                         print_revision (progname, revision);
191                         exit (EXIT_SUCCESS);
192                 case 't':     /* timeout period */
193                         if (!is_integer (optarg))
194                                 usage2 (_("Timeout interval must be a positive integer"), optarg);
195                         else
196                                 timeout_interval = atoi (optarg);
197                         break;
198                 }
199         }
201         validate_arguments (&argv[optind]);
203         return (const char **) &argv[optind];
207 /******************************************************************************
208 @@-
209 <sect3>
210 <title>validate_arguments</title>
212 <para>No validation is currently done.</para>
214 </sect3>
215 -@@
216 ******************************************************************************/
220 int
221 validate_arguments (char **command_line)
223         if (command_line[0] == NULL)
224                 usage4 (_("Could not parse arguments"));
226         if (strncmp(command_line[0],"/",1) != 0 && strncmp(command_line[0],"./",2) != 0)
227                 usage4 (_("Require path to command"));
230 /******************************************************************************
231 @@-
232 </sect2> 
233 </sect1>
234 </article>
235 -@@
236 ******************************************************************************/
240 void
241 print_help (void)
243         print_revision (progname, revision);
245         printf (COPYRIGHT, copyright, email);
247         printf ("%s\n", _("Negates the status of a plugin (returns OK for CRITICAL, and vice-versa)."));
249         printf ("\n\n");
251         print_usage ();
253         printf (_(UT_HELP_VRSN));
255         printf (_(UT_TIMEOUT), DEFAULT_TIMEOUT);
257         printf ("    %s\n", _("[keep timeout than the plugin timeout to retain CRITICAL status]"));
258         printf ("\n");
259         printf ("%s\n", _("Examples:"));
260         printf (" %s\n", "negate /usr/local/nagios/libexec/check_ping -H host");
261         printf ("    %s\n", _("Run check_ping and invert result. Must use full path to plugin"));
262         printf (" %s\n", "negate /usr/local/nagios/libexec/check_procs -a 'vi negate.c'");
263         printf ("    %s\n", _("Use single quotes if you need to retain spaces"));
264         printf (_(UT_VERBOSE));
265         printf ("\n");
266         printf ("%s\n", _("Notes:"));
267         printf ("%s\n", _("This plugin is a wrapper to take the output of another plugin and invert it."));
268         printf ("%s\n", _("The full path of the plugin must be provided."));
269         printf ("%s\n", _("If the wrapped plugin returns STATE_OK, the wrapper will return STATE_CRITICAL."));
270         printf ("%s\n", _("If the wrapped plugin returns STATE_CRITICAL, the wrapper will return STATE_OK."));
271         printf ("%s\n", _("Otherwise, the output state of the wrapped plugin is unchanged."));
273         printf (_(UT_SUPPORT));
278 void
279 print_usage (void)
281         printf (_("Usage:"));
282         printf ("%s [-t timeout] <definition of wrapped plugin>\n",progname);