Code

Fix memory leak in check_http for large pages (Jimmy Bergman - #2957455)
[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         if ((temp_thresholds = malloc(sizeof(thresholds))) == NULL)
105                 die(STATE_UNKNOWN, _("Cannot allocate memory: %s\n"),
106                     strerror(errno));
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;
114                 }
115         }
116         if (critical_string != NULL) {
117                 if ((temp_thresholds->critical = parse_range_string(critical_string)) == NULL) {
118                         return NP_RANGE_UNPARSEABLE;
119                 }
120         }
122         *my_thresholds = temp_thresholds;
124         return 0;
127 void
128 set_thresholds(thresholds **my_thresholds, char *warn_string, char *critical_string)
130         switch (_set_thresholds(my_thresholds, warn_string, critical_string)) {
131         case 0:
132                 return;
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"));
137                 break;
138         }
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");
145         } else {
146                 if (my_threshold->warning) {
147                         printf("Warning: start=%g end=%g; ", my_threshold->warning->start, my_threshold->warning->end);
148                 } else {
149                         printf("Warning not set; ");
150                 }
151                 if (my_threshold->critical) {
152                         printf("Critical: start=%g end=%g", my_threshold->critical->start, my_threshold->critical->end);
153                 } else {
154                         printf("Critical not set");
155                 }
156         }
157         printf("\n");
160 /* Returns TRUE if alert should be raised based on the range */
161 int
162 check_range(double value, range *my_range)
164         int no = FALSE;
165         int yes = TRUE;
167         if (my_range->alert_on == INSIDE) {
168                 no = TRUE;
169                 yes = FALSE;
170         }
172         if (my_range->end_infinity == FALSE && my_range->start_infinity == FALSE) {
173                 if ((my_range->start <= value) && (value <= my_range->end)) {
174                         return no;
175                 } else {
176                         return yes;
177                 }
178         } else if (my_range->start_infinity == FALSE && my_range->end_infinity == TRUE) {
179                 if (my_range->start <= value) {
180                         return no;
181                 } else {
182                         return yes;
183                 }
184         } else if (my_range->start_infinity == TRUE && my_range->end_infinity == FALSE) {
185                 if (value <= my_range->end) {
186                         return no;
187                 } else {
188                         return yes;
189                 }
190         } else {
191                 return no;
192         }
195 /* Returns status */
196 int
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;
202                 }
203         }
204         if (my_thresholds->warning != NULL) {
205                 if (check_range(value, my_thresholds->warning) == TRUE) {
206                         return STATE_WARNING;
207                 }
208         }
209         return STATE_OK;
212 char *np_escaped_string (const char *string) {
213         char *data;
214         int i, j=0;
215         data = strdup(string);
216         for (i=0; data[i]; i++) {
217                 if (data[i] == '\\') {
218                         switch(data[++i]) {
219                                 case 'n':
220                                         data[j++] = '\n';
221                                         break;
222                                 case 'r':
223                                         data[j++] = '\r';
224                                         break;
225                                 case 't':
226                                         data[j++] = '\t';
227                                         break;
228                                 case '\\':
229                                         data[j++] = '\\';
230                                         break;
231                                 default:
232                                         data[j++] = data[i];
233                         }
234                 } else {
235                         data[j++] = data[i];
236                 }
237         }
238         data[j] = '\0';
239         return data;
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();
246         if(!status) {
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");
253         }
254         return status;
257 /*
258  * Extract the value from key/value pairs, or return NULL. The value returned
259  * can be free()ed.
260  * This function can be used to parse NTP control packet data and performance
261  * data strings.
262  */
263 char *np_extract_value(const char *varlist, const char *name, char sep) {
264         char *tmp=NULL, *value=NULL;
265         int i;
267         while (1) {
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 */
278                                 varlist++;
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';
288                                 } else {
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';
294                                 }
295                                 break;
296                         }
297                 }
298                 if (tmp = index(varlist, sep)) {
299                         /* More keys, keep going... */
300                         varlist = tmp + 1;
301                 } else {
302                         /* We're done */
303                         break;
304                 }
305         }
307         /* Clean-up trailing spaces/newlines */
308         if (value) for (i=strlen(value)-1; isspace(value[i]); i--) value[i] = '\0';
310         return value;