diff options
-rw-r--r-- | lib/utils_base.c | 17 | ||||
-rw-r--r-- | lib/utils_base.h | 13 | ||||
-rw-r--r-- | plugins/negate.c | 29 |
3 files changed, 31 insertions, 28 deletions
diff --git a/lib/utils_base.c b/lib/utils_base.c index f5f7506..04c4b4f 100644 --- a/lib/utils_base.c +++ b/lib/utils_base.c | |||
@@ -369,6 +369,23 @@ char *np_extract_value(const char *varlist, const char *name, char sep) { | |||
369 | return value; | 369 | return value; |
370 | } | 370 | } |
371 | 371 | ||
372 | |||
373 | /* | ||
374 | * Read a string representing a state (ok, warning... or numeric: 0, 1) and | ||
375 | * return the corresponding STATE_ value or ERROR) | ||
376 | */ | ||
377 | int mp_translate_state (char *state_text) { | ||
378 | if (!strcasecmp(state_text,"OK") || !strcmp(state_text,"0")) | ||
379 | return STATE_OK; | ||
380 | if (!strcasecmp(state_text,"WARNING") || !strcmp(state_text,"1")) | ||
381 | return STATE_WARNING; | ||
382 | if (!strcasecmp(state_text,"CRITICAL") || !strcmp(state_text,"2")) | ||
383 | return STATE_CRITICAL; | ||
384 | if (!strcasecmp(state_text,"UNKNOWN") || !strcmp(state_text,"3")) | ||
385 | return STATE_UNKNOWN; | ||
386 | return ERROR; | ||
387 | } | ||
388 | |||
372 | /* | 389 | /* |
373 | * Returns a string to use as a keyname, based on an md5 hash of argv, thus | 390 | * Returns a string to use as a keyname, based on an md5 hash of argv, thus |
374 | * hopefully a unique key per service/plugin invocation. Use the extra-opts | 391 | * hopefully a unique key per service/plugin invocation. Use the extra-opts |
diff --git a/lib/utils_base.h b/lib/utils_base.h index b4bd92e..04f6370 100644 --- a/lib/utils_base.h +++ b/lib/utils_base.h | |||
@@ -79,6 +79,10 @@ int np_check_if_root(void); | |||
79 | * code from the above function, in case it's helpful for testing */ | 79 | * code from the above function, in case it's helpful for testing */ |
80 | int np_warn_if_not_root(void); | 80 | int np_warn_if_not_root(void); |
81 | 81 | ||
82 | /* mp_suid() returns true if the real and effective uids differs, such as when | ||
83 | * running a suid plugin */ | ||
84 | #define mp_suid() (getuid() != geteuid()) | ||
85 | |||
82 | /* | 86 | /* |
83 | * Extract the value from key/value pairs, or return NULL. The value returned | 87 | * Extract the value from key/value pairs, or return NULL. The value returned |
84 | * can be free()ed. | 88 | * can be free()ed. |
@@ -93,6 +97,11 @@ char *np_extract_value(const char*, const char*, char); | |||
93 | */ | 97 | */ |
94 | #define np_extract_ntpvar(l, n) np_extract_value(l, n, ',') | 98 | #define np_extract_ntpvar(l, n) np_extract_value(l, n, ',') |
95 | 99 | ||
100 | /* | ||
101 | * Read a string representing a state (ok, warning... or numeric: 0, 1) and | ||
102 | * return the corresponding NP_STATE or ERROR) | ||
103 | */ | ||
104 | int mp_translate_state (char *); | ||
96 | 105 | ||
97 | void np_enable_state(char *, int); | 106 | void np_enable_state(char *, int); |
98 | state_data *np_state_read(); | 107 | state_data *np_state_read(); |
@@ -102,8 +111,4 @@ void np_init(char *, int argc, char **argv); | |||
102 | void np_set_args(int argc, char **argv); | 111 | void np_set_args(int argc, char **argv); |
103 | void np_cleanup(); | 112 | void np_cleanup(); |
104 | 113 | ||
105 | /* mp_suid() returns true if the real and effective uids differs, such as when | ||
106 | * running a suid plugin */ | ||
107 | #define mp_suid() (getuid() != geteuid()) | ||
108 | |||
109 | #endif /* _UTILS_BASE_ */ | 114 | #endif /* _UTILS_BASE_ */ |
diff --git a/plugins/negate.c b/plugins/negate.c index 84ad7eb..4bd09de 100644 --- a/plugins/negate.c +++ b/plugins/negate.c | |||
@@ -45,7 +45,6 @@ const char *email = "devel@monitoring-plugins.org"; | |||
45 | 45 | ||
46 | static const char **process_arguments (int, char **); | 46 | static const char **process_arguments (int, char **); |
47 | int validate_arguments (char **); | 47 | int validate_arguments (char **); |
48 | int translate_state (char *); | ||
49 | void print_help (void); | 48 | void print_help (void); |
50 | void print_usage (void); | 49 | void print_usage (void); |
51 | int subst_text = FALSE; | 50 | int subst_text = FALSE; |
@@ -166,27 +165,27 @@ process_arguments (int argc, char **argv) | |||
166 | timeout_interval = atoi (optarg); | 165 | timeout_interval = atoi (optarg); |
167 | break; | 166 | break; |
168 | case 'T': /* Result to return on timeouts */ | 167 | case 'T': /* Result to return on timeouts */ |
169 | if ((timeout_state = translate_state(optarg)) == ERROR) | 168 | if ((timeout_state = mp_translate_state(optarg)) == ERROR) |
170 | usage4 (_("Timeout result must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3).")); | 169 | usage4 (_("Timeout result must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3).")); |
171 | break; | 170 | break; |
172 | case 'o': /* replacement for OK */ | 171 | case 'o': /* replacement for OK */ |
173 | if ((state[STATE_OK] = translate_state(optarg)) == ERROR) | 172 | if ((state[STATE_OK] = mp_translate_state(optarg)) == ERROR) |
174 | usage4 (_("Ok must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3).")); | 173 | usage4 (_("Ok must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3).")); |
175 | permute = FALSE; | 174 | permute = FALSE; |
176 | break; | 175 | break; |
177 | 176 | ||
178 | case 'w': /* replacement for WARNING */ | 177 | case 'w': /* replacement for WARNING */ |
179 | if ((state[STATE_WARNING] = translate_state(optarg)) == ERROR) | 178 | if ((state[STATE_WARNING] = mp_translate_state(optarg)) == ERROR) |
180 | usage4 (_("Warning must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3).")); | 179 | usage4 (_("Warning must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3).")); |
181 | permute = FALSE; | 180 | permute = FALSE; |
182 | break; | 181 | break; |
183 | case 'c': /* replacement for CRITICAL */ | 182 | case 'c': /* replacement for CRITICAL */ |
184 | if ((state[STATE_CRITICAL] = translate_state(optarg)) == ERROR) | 183 | if ((state[STATE_CRITICAL] = mp_translate_state(optarg)) == ERROR) |
185 | usage4 (_("Critical must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3).")); | 184 | usage4 (_("Critical must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3).")); |
186 | permute = FALSE; | 185 | permute = FALSE; |
187 | break; | 186 | break; |
188 | case 'u': /* replacement for UNKNOWN */ | 187 | case 'u': /* replacement for UNKNOWN */ |
189 | if ((state[STATE_UNKNOWN] = translate_state(optarg)) == ERROR) | 188 | if ((state[STATE_UNKNOWN] = mp_translate_state(optarg)) == ERROR) |
190 | usage4 (_("Unknown must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3).")); | 189 | usage4 (_("Unknown must be a valid state name (OK, WARNING, CRITICAL, UNKNOWN) or integer (0-3).")); |
191 | permute = FALSE; | 190 | permute = FALSE; |
192 | break; | 191 | break; |
@@ -218,24 +217,6 @@ validate_arguments (char **command_line) | |||
218 | } | 217 | } |
219 | 218 | ||
220 | 219 | ||
221 | int | ||
222 | translate_state (char *state_text) | ||
223 | { | ||
224 | char *temp_ptr; | ||
225 | for (temp_ptr = state_text; *temp_ptr; temp_ptr++) { | ||
226 | *temp_ptr = toupper(*temp_ptr); | ||
227 | } | ||
228 | if (!strcmp(state_text,"OK") || !strcmp(state_text,"0")) | ||
229 | return STATE_OK; | ||
230 | if (!strcmp(state_text,"WARNING") || !strcmp(state_text,"1")) | ||
231 | return STATE_WARNING; | ||
232 | if (!strcmp(state_text,"CRITICAL") || !strcmp(state_text,"2")) | ||
233 | return STATE_CRITICAL; | ||
234 | if (!strcmp(state_text,"UNKNOWN") || !strcmp(state_text,"3")) | ||
235 | return STATE_UNKNOWN; | ||
236 | return ERROR; | ||
237 | } | ||
238 | |||
239 | void | 220 | void |
240 | print_help (void) | 221 | print_help (void) |
241 | { | 222 | { |