Code

5a45f84a8bafd8846287a48758fc6e1a8e7019d1
[nagiosplug.git] / lib / utils_base.c
1 /*****************************************************************************
2  *
3  * utils_base.c
4  *
5  * Library of useful functions for plugins
6  * These functions are tested with libtap. See tests/ directory
7  *
8  * Copyright (c) 2006 Nagios Plugin Development Team
9  * License: GPL
10  *
11  * $Revision$
12  * $Date$
13  ****************************************************************************/
15 #include "common.h"
16 #include "utils_base.h"
18 void
19 die (int result, const char *fmt, ...)
20 {
21         va_list ap;
22         va_start (ap, fmt);
23         vprintf (fmt, ap);
24         va_end (ap);
25         exit (result);
26 }
28 void set_range_start (range *this, double value) {
29         this->start = value;
30         this->start_infinity = FALSE;
31 }
33 void set_range_end (range *this, double value) {
34         this->end = value;
35         this->end_infinity = FALSE;
36 }
38 range
39 *parse_range_string (char *str) {
40         range *temp_range;
41         double start;
42         double end;
43         char *end_str;
45         temp_range = (range *) malloc(sizeof(range));
47         /* Set defaults */
48         temp_range->start = 0;
49         temp_range->start_infinity = FALSE;
50         temp_range->end = 0;
51         temp_range->end_infinity = TRUE;
52         temp_range->alert_on = OUTSIDE;
54         if (str[0] == '@') {
55                 temp_range->alert_on = INSIDE;
56                 str++;
57         }
59         end_str = index(str, ':');
60         if (end_str != NULL) {
61                 if (str[0] == '~') {
62                         temp_range->start_infinity = TRUE;
63                 } else {
64                         start = strtod(str, NULL);      /* Will stop at the ':' */
65                         set_range_start(temp_range, start);
66                 }
67                 end_str++;              /* Move past the ':' */
68         } else {
69                 end_str = str;
70         }
71         end = strtod(end_str, NULL);
72         if (strcmp(end_str, "") != 0) {
73                 set_range_end(temp_range, end);
74         }
76         if (temp_range->start_infinity == TRUE || 
77                 temp_range->end_infinity == TRUE ||
78                 temp_range->start <= temp_range->end) {
79                 return temp_range;
80         }
81         free(temp_range);
82         return NULL;
83 }
85 /* returns 0 if okay, otherwise 1 */
86 int
87 _set_thresholds(thresholds **my_thresholds, char *warn_string, char *critical_string)
88 {
89         thresholds *temp_thresholds = NULL;
91         temp_thresholds = malloc(sizeof(temp_thresholds));
93         temp_thresholds->warning = NULL;
94         temp_thresholds->critical = NULL;
96         if (warn_string != NULL) {
97                 if ((temp_thresholds->warning = parse_range_string(warn_string)) == NULL) {
98                         return 1;
99                 }
100         }
101         if (critical_string != NULL) {
102                 if ((temp_thresholds->critical = parse_range_string(critical_string)) == NULL) {
103                         return 1;
104                 }
105         }
107         if (*my_thresholds > 0) {       /* Not sure why, but sometimes could be -1 */
108                 /* printf("Freeing here: %d\n", *my_thresholds); */
109                 free(*my_thresholds);
110         }
111         *my_thresholds = temp_thresholds;
113         return 0;
116 void
117 set_thresholds(thresholds **my_thresholds, char *warn_string, char *critical_string)
119         if (_set_thresholds(my_thresholds, warn_string, critical_string) == 0) {
120                 return;
121         } else {
122                 die(STATE_UNKNOWN, _("Range format incorrect"));
123         }
126 void print_thresholds(const char *threshold_name, thresholds *my_threshold) {
127         printf("%s - ", threshold_name);
128         if (! my_threshold) {
129                 printf("Threshold not set");
130         } else {
131                 if (my_threshold->warning) {
132                         printf("Warning: start=%g end=%g; ", my_threshold->warning->start, my_threshold->warning->end);
133                 } else {
134                         printf("Warning not set; ");
135                 }
136                 if (my_threshold->critical) {
137                         printf("Critical: start=%g end=%g", my_threshold->critical->start, my_threshold->critical->end);
138                 } else {
139                         printf("Critical not set");
140                 }
141         }
142         printf("\n");
145 /* Returns TRUE if alert should be raised based on the range */
146 int
147 check_range(double value, range *my_range)
149         int false = FALSE;
150         int true = TRUE;
151         
152         if (my_range->alert_on == INSIDE) {
153                 false = TRUE;
154                 true = FALSE;
155         }
157         if (my_range->end_infinity == FALSE && my_range->start_infinity == FALSE) {
158                 if ((my_range->start <= value) && (value <= my_range->end)) {
159                         return false;
160                 } else {
161                         return true;
162                 }
163         } else if (my_range->start_infinity == FALSE && my_range->end_infinity == TRUE) {
164                 if (my_range->start <= value) {
165                         return false;
166                 } else {
167                         return true;
168                 }
169         } else if (my_range->start_infinity == TRUE && my_range->end_infinity == FALSE) {
170                 if (value <= my_range->end) {
171                         return false;
172                 } else {
173                         return true;
174                 }
175         } else {
176                 return false;
177         }
180 /* Returns status */
181 int
182 get_status(double value, thresholds *my_thresholds)
184         if (my_thresholds->critical != NULL) {
185                 if (check_range(value, my_thresholds->critical) == TRUE) {
186                         return STATE_CRITICAL;
187                 }
188         }
189         if (my_thresholds->warning != NULL) {
190                 if (check_range(value, my_thresholds->warning) == TRUE) {
191                         return STATE_WARNING;
192                 }
193         }
194         return STATE_OK;
197 char *np_escaped_string (const char *string) {
198         char *data;
199         int i, j=0;
200         data = strdup(string);
201         for (i=0; data[i]; i++) {
202                 if (data[i] == '\\') {
203                         switch(data[++i]) {
204                                 case 'n':
205                                         data[j++] = '\n';
206                                         break;
207                                 case 'r':
208                                         data[j++] = '\r';
209                                         break;
210                                 case 't':
211                                         data[j++] = '\t';
212                                         break;
213                                 case '\\':
214                                         data[j++] = '\\';
215                                         break;
216                                 default:
217                                         data[j++] = data[i];
218                         }
219                 } else {
220                         data[j++] = data[i];
221                 }
222         }
223         data[j] = '\0';
224         return data;