1 /*****************************************************************************
6 * Copyright (c) 2006 Nagios Plugins Development Team
8 * Library of useful functions for plugins
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 *****************************************************************************/
29 #include "utils_base.h"
32 die (int result
, const char *fmt
, ...)
41 void set_range_start (range
*this, double value
) {
43 this->start_infinity
= FALSE
;
46 void set_range_end (range
*this, double value
) {
48 this->end_infinity
= FALSE
;
52 *parse_range_string (char *str
) {
58 temp_range
= (range
*) malloc(sizeof(range
));
61 temp_range
->start
= 0;
62 temp_range
->start_infinity
= FALSE
;
64 temp_range
->end_infinity
= TRUE
;
65 temp_range
->alert_on
= OUTSIDE
;
68 temp_range
->alert_on
= INSIDE
;
72 end_str
= index(str
, ':');
73 if (end_str
!= NULL
) {
75 temp_range
->start_infinity
= TRUE
;
77 start
= strtod(str
, NULL
); /* Will stop at the ':' */
78 set_range_start(temp_range
, start
);
80 end_str
++; /* Move past the ':' */
84 end
= strtod(end_str
, NULL
);
85 if (strcmp(end_str
, "") != 0) {
86 set_range_end(temp_range
, end
);
89 if (temp_range
->start_infinity
== TRUE
||
90 temp_range
->end_infinity
== TRUE
||
91 temp_range
->start
<= temp_range
->end
) {
98 /* returns 0 if okay, otherwise 1 */
100 _set_thresholds(thresholds
**my_thresholds
, char *warn_string
, char *critical_string
)
102 thresholds
*temp_thresholds
= NULL
;
104 if ((temp_thresholds
= malloc(sizeof(thresholds
))) == NULL
)
105 die(STATE_UNKNOWN
, _("Cannot allocate memory: %s\n"),
108 temp_thresholds
->warning
= NULL
;
109 temp_thresholds
->critical
= NULL
;
111 if (warn_string
!= NULL
) {
112 if ((temp_thresholds
->warning
= parse_range_string(warn_string
)) == NULL
) {
113 return NP_RANGE_UNPARSEABLE
;
116 if (critical_string
!= NULL
) {
117 if ((temp_thresholds
->critical
= parse_range_string(critical_string
)) == NULL
) {
118 return NP_RANGE_UNPARSEABLE
;
122 *my_thresholds
= temp_thresholds
;
128 set_thresholds(thresholds
**my_thresholds
, char *warn_string
, char *critical_string
)
130 switch (_set_thresholds(my_thresholds
, warn_string
, critical_string
)) {
133 case NP_RANGE_UNPARSEABLE
:
134 die(STATE_UNKNOWN
, _("Range format incorrect"));
135 case NP_WARN_WITHIN_CRIT
:
136 die(STATE_UNKNOWN
, _("Warning level is a subset of critical and will not be alerted"));
141 void print_thresholds(const char *threshold_name
, thresholds
*my_threshold
) {
142 printf("%s - ", threshold_name
);
143 if (! my_threshold
) {
144 printf("Threshold not set");
146 if (my_threshold
->warning
) {
147 printf("Warning: start=%g end=%g; ", my_threshold
->warning
->start
, my_threshold
->warning
->end
);
149 printf("Warning not set; ");
151 if (my_threshold
->critical
) {
152 printf("Critical: start=%g end=%g", my_threshold
->critical
->start
, my_threshold
->critical
->end
);
154 printf("Critical not set");
160 /* Returns TRUE if alert should be raised based on the range */
162 check_range(double value
, range
*my_range
)
167 if (my_range
->alert_on
== INSIDE
) {
172 if (my_range
->end_infinity
== FALSE
&& my_range
->start_infinity
== FALSE
) {
173 if ((my_range
->start
<= value
) && (value
<= my_range
->end
)) {
178 } else if (my_range
->start_infinity
== FALSE
&& my_range
->end_infinity
== TRUE
) {
179 if (my_range
->start
<= value
) {
184 } else if (my_range
->start_infinity
== TRUE
&& my_range
->end_infinity
== FALSE
) {
185 if (value
<= my_range
->end
) {
197 get_status(double value
, thresholds
*my_thresholds
)
199 if (my_thresholds
->critical
!= NULL
) {
200 if (check_range(value
, my_thresholds
->critical
) == TRUE
) {
201 return STATE_CRITICAL
;
204 if (my_thresholds
->warning
!= NULL
) {
205 if (check_range(value
, my_thresholds
->warning
) == TRUE
) {
206 return STATE_WARNING
;
212 char *np_escaped_string (const char *string
) {
215 data
= strdup(string
);
216 for (i
=0; data
[i
]; i
++) {
217 if (data
[i
] == '\\') {
242 int np_check_if_root(void) { return (geteuid() == 0); }
244 int np_warn_if_not_root(void) {
245 int status
= np_check_if_root();
247 printf(_("Warning: "));
248 printf(_("This plugin must be either run as root or setuid root.\n"));
249 printf(_("To run as root, you can use a tool like sudo.\n"));
250 printf(_("To set the setuid permissions, use the command:\n"));
251 /* XXX could we use something like progname? */
252 printf("\tchmod u+s yourpluginfile\n");
258 * Extract the value from key/value pairs, or return NULL. The value returned
260 * This function can be used to parse NTP control packet data and performance
263 char *np_extract_value(const char *varlist
, const char *name
, char sep
) {
264 char *tmp
=NULL
, *value
=NULL
;
268 /* Strip any leading space */
269 for (varlist
; isspace(varlist
[0]); varlist
++);
271 if (strncmp(name
, varlist
, strlen(name
)) == 0) {
272 varlist
+= strlen(name
);
273 /* strip trailing spaces */
274 for (varlist
; isspace(varlist
[0]); varlist
++);
276 if (varlist
[0] == '=') {
277 /* We matched the key, go past the = sign */
279 /* strip leading spaces */
280 for (varlist
; isspace(varlist
[0]); varlist
++);
282 if (tmp
= index(varlist
, sep
)) {
283 /* Value is delimited by a comma */
284 if (tmp
-varlist
== 0) continue;
285 value
= (char *)malloc(tmp
-varlist
+1);
286 strncpy(value
, varlist
, tmp
-varlist
);
287 value
[tmp
-varlist
] = '\0';
289 /* Value is delimited by a \0 */
290 if (strlen(varlist
) == 0) continue;
291 value
= (char *)malloc(strlen(varlist
) + 1);
292 strncpy(value
, varlist
, strlen(varlist
));
293 value
[strlen(varlist
)] = '\0';
298 if (tmp
= index(varlist
, sep
)) {
299 /* More keys, keep going... */
307 /* Clean-up trailing spaces/newlines */
308 if (value
) for (i
=strlen(value
)-1; isspace(value
[i
]); i
--) value
[i
] = '\0';