summaryrefslogtreecommitdiffstats
path: root/plugins/negate.c
diff options
context:
space:
mode:
authorThomas Guyot-Sionnest <dermoth@users.sourceforge.net>2008-05-02 09:28:15 (GMT)
committerThomas Guyot-Sionnest <dermoth@users.sourceforge.net>2008-05-02 09:28:15 (GMT)
commit82f8c52fbdca00bd8118bab3cee977da14757818 (patch)
tree83c0e557cbd8e58d352c0a852baf1178f312a756 /plugins/negate.c
parentaca1e6a6cec40754c67d3cf229db2660c4685202 (diff)
downloadmonitoring-plugins-82f8c52fbdca00bd8118bab3cee977da14757818.tar.gz
negate now has the ability to replace the status text as well (-s, --substitute)
Also changed: - default timeout to 11 instead of 9 (since it's recommended to have a higher timeout, and many plugins default to 10 seconds) - If there's no STDOUT lines returned, still returns the returned result if it's NON-ok instead of forcing it to UNKNOWN git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@1989 f882894a-f735-0410-b71e-b25c423dba1c
Diffstat (limited to 'plugins/negate.c')
-rw-r--r--plugins/negate.c51
1 files changed, 35 insertions, 16 deletions
diff --git a/plugins/negate.c b/plugins/negate.c
index eccc404..40ff191 100644
--- a/plugins/negate.c
+++ b/plugins/negate.c
@@ -3,7 +3,7 @@
3* Nagios negate plugin 3* Nagios negate plugin
4* 4*
5* License: GPL 5* License: GPL
6* Copyright (c) 2002-2007 Nagios Plugins Development Team 6* Copyright (c) 2002-2008 Nagios Plugins Development Team
7* 7*
8* Last Modified: $Date$ 8* Last Modified: $Date$
9* 9*
@@ -34,10 +34,10 @@
34 34
35const char *progname = "negate"; 35const char *progname = "negate";
36const char *revision = "$Revision$"; 36const char *revision = "$Revision$";
37const char *copyright = "2002-2007"; 37const char *copyright = "2002-2008";
38const char *email = "nagiosplug-devel@lists.sourceforge.net"; 38const char *email = "nagiosplug-devel@lists.sourceforge.net";
39 39
40#define DEFAULT_TIMEOUT 9 40#define DEFAULT_TIMEOUT 11
41 41
42#include "common.h" 42#include "common.h"
43#include "utils.h" 43#include "utils.h"
@@ -49,6 +49,7 @@ static const char **process_arguments (int, char **);
49int validate_arguments (char **); 49int validate_arguments (char **);
50void print_help (void); 50void print_help (void);
51void print_usage (void); 51void print_usage (void);
52int subst_text = FALSE;
52 53
53static int state[4] = { 54static int state[4] = {
54 STATE_OK, 55 STATE_OK,
@@ -61,7 +62,7 @@ int
61main (int argc, char **argv) 62main (int argc, char **argv)
62{ 63{
63 int found = 0, result = STATE_UNKNOWN; 64 int found = 0, result = STATE_UNKNOWN;
64 char *buf; 65 char *buf, *sub;
65 char **command_line; 66 char **command_line;
66 output chld_out, chld_err; 67 output chld_out, chld_err;
67 int i; 68 int i;
@@ -92,10 +93,22 @@ main (int argc, char **argv)
92 exit (STATE_WARNING); 93 exit (STATE_WARNING);
93 } 94 }
94 95
96 /* Return UNKNOWN or worse if no output is returned */
95 if (chld_out.lines == 0) 97 if (chld_out.lines == 0)
96 die (STATE_UNKNOWN, _("No data returned from command\n")); 98 die (max_state_alt (result, STATE_UNKNOWN), _("No data returned from command\n"));
97 99
98 for (i = 0; i < chld_out.lines; i++) { 100 for (i = 0; i < chld_out.lines; i++) {
101 if (subst_text && result != state[result] &&
102 result >= 0 && result <= 4) {
103 /* Loop over each match found */
104 while ((sub = strstr (chld_out.line[i], state_text (result)))) {
105 /* Terminate the first part and skip over the string we'll substitute */
106 *sub = '\0';
107 sub += strlen (state_text (result));
108 /* then put everything back together */
109 asprintf (&chld_out.line[i], "%s%s%s", chld_out.line[i], state_text (state[result]), sub);
110 }
111 }
99 printf ("%s\n", chld_out.line[i]); 112 printf ("%s\n", chld_out.line[i]);
100 } 113 }
101 114
@@ -123,11 +136,12 @@ process_arguments (int argc, char **argv)
123 {"warning", required_argument, 0, 'w'}, 136 {"warning", required_argument, 0, 'w'},
124 {"critical", required_argument, 0, 'c'}, 137 {"critical", required_argument, 0, 'c'},
125 {"unknown", required_argument, 0, 'u'}, 138 {"unknown", required_argument, 0, 'u'},
139 {"substitute", no_argument, 0, 's'},
126 {0, 0, 0, 0} 140 {0, 0, 0, 0}
127 }; 141 };
128 142
129 while (1) { 143 while (1) {
130 c = getopt_long (argc, argv, "+hVt:o:w:c:u:", longopts, &option); 144 c = getopt_long (argc, argv, "+hVt:o:w:c:u:s", longopts, &option);
131 145
132 if (c == -1 || c == EOF) 146 if (c == -1 || c == EOF)
133 break; 147 break;
@@ -170,6 +184,9 @@ process_arguments (int argc, char **argv)
170 usage4 (_("Unknown must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3).")); 184 usage4 (_("Unknown must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3)."));
171 permute = FALSE; 185 permute = FALSE;
172 break; 186 break;
187 case 's': /* Substitute status text */
188 subst_text = TRUE;
189 break;
173 } 190 }
174 } 191 }
175 192
@@ -232,13 +249,15 @@ print_help (void)
232 printf (_(UT_TIMEOUT), DEFAULT_TIMEOUT); 249 printf (_(UT_TIMEOUT), DEFAULT_TIMEOUT);
233 printf (" %s\n", _("Keep timeout longer than the plugin timeout to retain CRITICAL status.")); 250 printf (" %s\n", _("Keep timeout longer than the plugin timeout to retain CRITICAL status."));
234 251
235 printf(" -o,--ok=STATUS\n"); 252 printf(" -o, --ok=STATUS\n");
236 printf(" -w,--warning=STATUS\n"); 253 printf(" -w, --warning=STATUS\n");
237 printf(" -c,--critical=STATUS\n"); 254 printf(" -c, --critical=STATUS\n");
238 printf(" -u,--unknown=STATUS\n"); 255 printf(" -u, --unknown=STATUS\n");
239 printf(_(" STATUS can be 'OK', 'WARNING', 'CRITICAL' or 'UNKNOWN' without single\n")); 256 printf(_(" STATUS can be 'OK', 'WARNING', 'CRITICAL' or 'UNKNOWN' without single\n"));
240 printf(_(" quotes. Numeric values are accepted. If nothing is specified, permutes\n")); 257 printf(_(" quotes. Numeric values are accepted. If nothing is specified, permutes\n"));
241 printf(_(" OK and CRITICAL.\n")); 258 printf(_(" OK and CRITICAL.\n"));
259 printf(" -s, --substitute\n");
260 printf(_(" Substitute output text as well. Will only substitute text in CAPITALS\n"));
242 261
243 printf ("\n"); 262 printf ("\n");
244 printf ("%s\n", _("Examples:")); 263 printf ("%s\n", _("Examples:"));
@@ -248,11 +267,11 @@ print_help (void)
248 printf (" %s\n", _("This will return OK instead of WARNING and UNKNOWN instead of CRITICAL")); 267 printf (" %s\n", _("This will return OK instead of WARNING and UNKNOWN instead of CRITICAL"));
249 printf ("\n"); 268 printf ("\n");
250 printf ("%s\n", _("Notes:")); 269 printf ("%s\n", _("Notes:"));
251 printf ("%s\n", _("This plugin is a wrapper to take the output of another plugin and invert it.")); 270 printf (" %s\n", _("This plugin is a wrapper to take the output of another plugin and invert it."));
252 printf ("%s\n", _("The full path of the plugin must be provided.")); 271 printf (" %s\n", _("The full path of the plugin must be provided."));
253 printf ("%s\n", _("If the wrapped plugin returns OK, the wrapper will return CRITICAL.")); 272 printf (" %s\n", _("If the wrapped plugin returns OK, the wrapper will return CRITICAL."));
254 printf ("%s\n", _("If the wrapped plugin returns CRITICAL, the wrapper will return OK.")); 273 printf (" %s\n", _("If the wrapped plugin returns CRITICAL, the wrapper will return OK."));
255 printf ("%s\n", _("Otherwise, the output state of the wrapped plugin is unchanged.")); 274 printf (" %s\n", _("Otherwise, the output state of the wrapped plugin is unchanged."));
256 275
257 printf (_(UT_SUPPORT)); 276 printf (_(UT_SUPPORT));
258} 277}
@@ -263,5 +282,5 @@ void
263print_usage (void) 282print_usage (void)
264{ 283{
265 printf (_("Usage:")); 284 printf (_("Usage:"));
266 printf ("%s [-t timeout] [-owcu STATE] <definition of wrapped plugin>\n", progname); 285 printf ("%s [-t timeout] [-owcu STATE] [-s] <definition of wrapped plugin>\n", progname);
267} 286}