Code

Move check_ntp's extract_value to utils_base.c.
[nagiosplug.git] / lib / utils_base.c
1 /*****************************************************************************
2 *
3 * utils_base.c
4 *
5 * License: GPL
6 * Copyright (c) 2006 Nagios Plugins Development Team
7 *
8 * Library of useful functions for plugins
9
10 *
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.
15
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.
20
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/>.
23
24 *
25 *****************************************************************************/
27 #include "common.h"
28 #include <stdarg.h>
29 #include "utils_base.h"
31 void
32 die (int result, const char *fmt, ...)
33 {
34         va_list ap;
35         va_start (ap, fmt);
36         vprintf (fmt, ap);
37         va_end (ap);
38         exit (result);
39 }
41 void set_range_start (range *this, double value) {
42         this->start = value;
43         this->start_infinity = FALSE;
44 }
46 void set_range_end (range *this, double value) {
47         this->end = value;
48         this->end_infinity = FALSE;
49 }
51 range
52 *parse_range_string (char *str) {
53         range *temp_range;
54         double start;
55         double end;
56         char *end_str;
58         temp_range = (range *) malloc(sizeof(range));
60         /* Set defaults */
61         temp_range->start = 0;
62         temp_range->start_infinity = FALSE;
63         temp_range->end = 0;
64         temp_range->end_infinity = TRUE;
65         temp_range->alert_on = OUTSIDE;
67         if (str[0] == '@') {
68                 temp_range->alert_on = INSIDE;
69                 str++;
70         }
72         end_str = index(str, ':');
73         if (end_str != NULL) {
74                 if (str[0] == '~') {
75                         temp_range->start_infinity = TRUE;
76                 } else {
77                         start = strtod(str, NULL);      /* Will stop at the ':' */
78                         set_range_start(temp_range, start);
79                 }
80                 end_str++;              /* Move past the ':' */
81         } else {
82                 end_str = str;
83         }
84         end = strtod(end_str, NULL);
85         if (strcmp(end_str, "") != 0) {
86                 set_range_end(temp_range, end);
87         }
89         if (temp_range->start_infinity == TRUE ||
90                 temp_range->end_infinity == TRUE ||
91                 temp_range->start <= temp_range->end) {
92                 return temp_range;
93         }
94         free(temp_range);
95         return NULL;
96 }
98 /* returns 0 if okay, otherwise 1 */
99 int
100 _set_thresholds(thresholds **my_thresholds, char *warn_string, char *critical_string)
102         thresholds *temp_thresholds = NULL;
104         temp_thresholds = malloc(sizeof(temp_thresholds));
106         temp_thresholds->warning = NULL;
107         temp_thresholds->critical = NULL;
109         if (warn_string != NULL) {
110                 if ((temp_thresholds->warning = parse_range_string(warn_string)) == NULL) {
111                         return NP_RANGE_UNPARSEABLE;
112                 }
113         }
114         if (critical_string != NULL) {
115                 if ((temp_thresholds->critical = parse_range_string(critical_string)) == NULL) {
116                         return NP_RANGE_UNPARSEABLE;
117                 }
118         }
120         *my_thresholds = temp_thresholds;
122         return 0;
125 void
126 set_thresholds(thresholds **my_thresholds, char *warn_string, char *critical_string)
128         switch (_set_thresholds(my_thresholds, warn_string, critical_string)) {
129         case 0:
130                 return;
131         case NP_RANGE_UNPARSEABLE:
132                 die(STATE_UNKNOWN, _("Range format incorrect"));
133         case NP_WARN_WITHIN_CRIT:
134                 die(STATE_UNKNOWN, _("Warning level is a subset of critical and will not be alerted"));
135                 break;
136         }
139 void print_thresholds(const char *threshold_name, thresholds *my_threshold) {
140         printf("%s - ", threshold_name);
141         if (! my_threshold) {
142                 printf("Threshold not set");
143         } else {
144                 if (my_threshold->warning) {
145                         printf("Warning: start=%g end=%g; ", my_threshold->warning->start, my_threshold->warning->end);
146                 } else {
147                         printf("Warning not set; ");
148                 }
149                 if (my_threshold->critical) {
150                         printf("Critical: start=%g end=%g", my_threshold->critical->start, my_threshold->critical->end);
151                 } else {
152                         printf("Critical not set");
153                 }
154         }
155         printf("\n");
158 /* Returns TRUE if alert should be raised based on the range */
159 int
160 check_range(double value, range *my_range)
162         int no = FALSE;
163         int yes = TRUE;
165         if (my_range->alert_on == INSIDE) {
166                 no = TRUE;
167                 yes = FALSE;
168         }
170         if (my_range->end_infinity == FALSE && my_range->start_infinity == FALSE) {
171                 if ((my_range->start <= value) && (value <= my_range->end)) {
172                         return no;
173                 } else {
174                         return yes;
175                 }
176         } else if (my_range->start_infinity == FALSE && my_range->end_infinity == TRUE) {
177                 if (my_range->start <= value) {
178                         return no;
179                 } else {
180                         return yes;
181                 }
182         } else if (my_range->start_infinity == TRUE && my_range->end_infinity == FALSE) {
183                 if (value <= my_range->end) {
184                         return no;
185                 } else {
186                         return yes;
187                 }
188         } else {
189                 return no;
190         }
193 /* Returns status */
194 int
195 get_status(double value, thresholds *my_thresholds)
197         if (my_thresholds->critical != NULL) {
198                 if (check_range(value, my_thresholds->critical) == TRUE) {
199                         return STATE_CRITICAL;
200                 }
201         }
202         if (my_thresholds->warning != NULL) {
203                 if (check_range(value, my_thresholds->warning) == TRUE) {
204                         return STATE_WARNING;
205                 }
206         }
207         return STATE_OK;
210 char *np_escaped_string (const char *string) {
211         char *data;
212         int i, j=0;
213         data = strdup(string);
214         for (i=0; data[i]; i++) {
215                 if (data[i] == '\\') {
216                         switch(data[++i]) {
217                                 case 'n':
218                                         data[j++] = '\n';
219                                         break;
220                                 case 'r':
221                                         data[j++] = '\r';
222                                         break;
223                                 case 't':
224                                         data[j++] = '\t';
225                                         break;
226                                 case '\\':
227                                         data[j++] = '\\';
228                                         break;
229                                 default:
230                                         data[j++] = data[i];
231                         }
232                 } else {
233                         data[j++] = data[i];
234                 }
235         }
236         data[j] = '\0';
237         return data;
240 int np_check_if_root(void) { return (geteuid() == 0); }
242 int np_warn_if_not_root(void) {
243         int status = np_check_if_root();
244         if(!status) {
245                 printf(_("Warning: "));
246                 printf(_("This plugin must be either run as root or setuid root.\n"));
247                 printf(_("To run as root, you can use a tool like sudo.\n"));
248                 printf(_("To set the setuid permissions, use the command:\n"));
249                 /* XXX could we use something like progname? */
250                 printf("\tchmod u+s yourpluginfile\n");
251         }
252         return status;
255 /*
256  * Extract the value from key/value pairs, or return NULL. The value returned
257  * can be free()ed.
258  * This function can be used to parse NTP control packet data and performance
259  * data strings.
260  */
261 char *np_extract_value(const char *varlist, const char *name) {
262         char *tmp=NULL, *value=NULL;
263         int i;
265         while (1) {
266                 /* Strip any leading space */
267                 for (varlist; isspace(varlist[0]); varlist++);
269                 if (strncmp(name, varlist, strlen(name)) == 0) {
270                         varlist += strlen(name);
271                         /* strip trailing spaces */
272                         for (varlist; isspace(varlist[0]); varlist++);
274                         if (varlist[0] == '=') {
275                                 /* We matched the key, go past the = sign */
276                                 varlist++;
277                                 /* strip leading spaces */
278                                 for (varlist; isspace(varlist[0]); varlist++);
280                                 if (tmp = index(varlist, ',')) {
281                                         /* Value is delimited by a comma */
282                                         if (tmp-varlist == 0) continue;
283                                         value = (char *)malloc(tmp-varlist+1);
284                                         strncpy(value, varlist, tmp-varlist);
285                                         value[tmp-varlist] = '\0';
286                                 } else {
287                                         /* Value is delimited by a \0 */
288                                         if (strlen(varlist) == 0) continue;
289                                         value = (char *)malloc(strlen(varlist) + 1);
290                                         strncpy(value, varlist, strlen(varlist));
291                                         value[strlen(varlist)] = '\0';
292                                 }
293                                 break;
294                         }
295                 }
296                 if (tmp = index(varlist, ',')) {
297                         /* More keys, keep going... */
298                         varlist = tmp + 1;
299                 } else {
300                         /* We're done */
301                         break;
302                 }
303         }
305         /* Clean-up trailing spaces/newlines */
306         if (value) for (i=strlen(value)-1; isspace(value[i]); i--) value[i] = '\0';
308         return value;