c0bc4dc55de583d8a23fa5a9c585077f2c36116c
1 /*****************************************************************************
2 *
3 * utils_base.c
4 *
5 * License: GPL
6 * Copyright (c) 2006 Nagios Plugins Development Team
7 *
8 * Last Modified: $Date$
9 *
10 * Library of useful functions for plugins
11 *
12 *
13 * This program is free software: you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation, either version 3 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 *
26 * $Id$
27 *
28 *****************************************************************************/
30 #include <stdarg.h>
31 #include "common.h"
32 #include "utils_base.h"
34 void
35 die (int result, const char *fmt, ...)
36 {
37 va_list ap;
38 va_start (ap, fmt);
39 vprintf (fmt, ap);
40 va_end (ap);
41 exit (result);
42 }
44 void set_range_start (range *this, double value) {
45 this->start = value;
46 this->start_infinity = FALSE;
47 }
49 void set_range_end (range *this, double value) {
50 this->end = value;
51 this->end_infinity = FALSE;
52 }
54 range
55 *parse_range_string (char *str) {
56 range *temp_range;
57 double start;
58 double end;
59 char *end_str;
61 temp_range = (range *) malloc(sizeof(range));
63 /* Set defaults */
64 temp_range->start = 0;
65 temp_range->start_infinity = FALSE;
66 temp_range->end = 0;
67 temp_range->end_infinity = TRUE;
68 temp_range->alert_on = OUTSIDE;
70 if (str[0] == '@') {
71 temp_range->alert_on = INSIDE;
72 str++;
73 }
75 end_str = index(str, ':');
76 if (end_str != NULL) {
77 if (str[0] == '~') {
78 temp_range->start_infinity = TRUE;
79 } else {
80 start = strtod(str, NULL); /* Will stop at the ':' */
81 set_range_start(temp_range, start);
82 }
83 end_str++; /* Move past the ':' */
84 } else {
85 end_str = str;
86 }
87 end = strtod(end_str, NULL);
88 if (strcmp(end_str, "") != 0) {
89 set_range_end(temp_range, end);
90 }
92 if (temp_range->start_infinity == TRUE ||
93 temp_range->end_infinity == TRUE ||
94 temp_range->start <= temp_range->end) {
95 return temp_range;
96 }
97 free(temp_range);
98 return NULL;
99 }
101 /* returns 0 if okay, otherwise 1 */
102 int
103 _set_thresholds(thresholds **my_thresholds, char *warn_string, char *critical_string)
104 {
105 thresholds *temp_thresholds = NULL;
107 temp_thresholds = malloc(sizeof(temp_thresholds));
109 temp_thresholds->warning = NULL;
110 temp_thresholds->critical = NULL;
112 if (warn_string != NULL) {
113 if ((temp_thresholds->warning = parse_range_string(warn_string)) == NULL) {
114 return NP_RANGE_UNPARSEABLE;
115 }
116 }
117 if (critical_string != NULL) {
118 if ((temp_thresholds->critical = parse_range_string(critical_string)) == NULL) {
119 return NP_RANGE_UNPARSEABLE;
120 }
121 }
123 if (*my_thresholds > 0) { /* Not sure why, but sometimes could be -1 */
124 /* printf("Freeing here: %d\n", *my_thresholds); */
125 free(*my_thresholds);
126 }
127 *my_thresholds = temp_thresholds;
129 return 0;
130 }
132 void
133 set_thresholds(thresholds **my_thresholds, char *warn_string, char *critical_string)
134 {
135 switch (_set_thresholds(my_thresholds, warn_string, critical_string)) {
136 case 0:
137 return;
138 case NP_RANGE_UNPARSEABLE:
139 die(STATE_UNKNOWN, _("Range format incorrect"));
140 case NP_WARN_WITHIN_CRIT:
141 die(STATE_UNKNOWN, _("Warning level is a subset of critical and will not be alerted"));
142 break;
143 }
144 }
146 void print_thresholds(const char *threshold_name, thresholds *my_threshold) {
147 printf("%s - ", threshold_name);
148 if (! my_threshold) {
149 printf("Threshold not set");
150 } else {
151 if (my_threshold->warning) {
152 printf("Warning: start=%g end=%g; ", my_threshold->warning->start, my_threshold->warning->end);
153 } else {
154 printf("Warning not set; ");
155 }
156 if (my_threshold->critical) {
157 printf("Critical: start=%g end=%g", my_threshold->critical->start, my_threshold->critical->end);
158 } else {
159 printf("Critical not set");
160 }
161 }
162 printf("\n");
163 }
165 /* Returns TRUE if alert should be raised based on the range */
166 int
167 check_range(double value, range *my_range)
168 {
169 int no = FALSE;
170 int yes = TRUE;
172 if (my_range->alert_on == INSIDE) {
173 no = TRUE;
174 yes = FALSE;
175 }
177 if (my_range->end_infinity == FALSE && my_range->start_infinity == FALSE) {
178 if ((my_range->start <= value) && (value <= my_range->end)) {
179 return no;
180 } else {
181 return yes;
182 }
183 } else if (my_range->start_infinity == FALSE && my_range->end_infinity == TRUE) {
184 if (my_range->start <= value) {
185 return no;
186 } else {
187 return yes;
188 }
189 } else if (my_range->start_infinity == TRUE && my_range->end_infinity == FALSE) {
190 if (value <= my_range->end) {
191 return no;
192 } else {
193 return yes;
194 }
195 } else {
196 return no;
197 }
198 }
200 /* Returns status */
201 int
202 get_status(double value, thresholds *my_thresholds)
203 {
204 if (my_thresholds->critical != NULL) {
205 if (check_range(value, my_thresholds->critical) == TRUE) {
206 return STATE_CRITICAL;
207 }
208 }
209 if (my_thresholds->warning != NULL) {
210 if (check_range(value, my_thresholds->warning) == TRUE) {
211 return STATE_WARNING;
212 }
213 }
214 return STATE_OK;
215 }
217 char *np_escaped_string (const char *string) {
218 char *data;
219 int i, j=0;
220 data = strdup(string);
221 for (i=0; data[i]; i++) {
222 if (data[i] == '\\') {
223 switch(data[++i]) {
224 case 'n':
225 data[j++] = '\n';
226 break;
227 case 'r':
228 data[j++] = '\r';
229 break;
230 case 't':
231 data[j++] = '\t';
232 break;
233 case '\\':
234 data[j++] = '\\';
235 break;
236 default:
237 data[j++] = data[i];
238 }
239 } else {
240 data[j++] = data[i];
241 }
242 }
243 data[j] = '\0';
244 return data;
245 }
247 int np_check_if_root(void) { return (geteuid() == 0); }
249 int np_warn_if_not_root(void) {
250 int status = np_check_if_root();
251 if(!status) {
252 printf(_("Warning: "));
253 printf(_("This plugin must be either run as root or setuid root.\n"));
254 printf(_("To run as root, you can use a tool like sudo.\n"));
255 printf(_("To set the setuid permissions, use the command:\n"));
256 /* XXX could we use something like progname? */
257 printf("\tchmod u+s yourpluginfile\n");
258 }
259 return status;
260 }