Reverted check_procs for solaris back to using pst3 due to truncation
[monitoring-plugins.git] / lib / utils_base.c
bloba36eb68082fb13d7f866b4109df9d7e318e351eb
1 /*****************************************************************************
3 * utils_base.c
5 * License: GPL
6 * Copyright (c) 2006 Nagios Plugins Development Team
8 * Last Modified: $Date$
10 * Library of useful functions for plugins
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.
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.
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/>.
26 * $Id$
28 *****************************************************************************/
30 #include <stdarg.h>
31 #include "common.h"
32 #include "utils_base.h"
34 void
35 die (int result, const char *fmt, ...)
37 va_list ap;
38 va_start (ap, fmt);
39 vprintf (fmt, ap);
40 va_end (ap);
41 exit (result);
44 void set_range_start (range *this, double value) {
45 this->start = value;
46 this->start_infinity = FALSE;
49 void set_range_end (range *this, double value) {
50 this->end = value;
51 this->end_infinity = FALSE;
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++;
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);
83 end_str++; /* Move past the ':' */
84 } else {
85 end_str = str;
87 end = strtod(end_str, NULL);
88 if (strcmp(end_str, "") != 0) {
89 set_range_end(temp_range, end);
92 if (temp_range->start_infinity == TRUE ||
93 temp_range->end_infinity == TRUE ||
94 temp_range->start <= temp_range->end) {
95 return temp_range;
97 free(temp_range);
98 return NULL;
101 /* returns 0 if okay, otherwise 1 */
103 _set_thresholds(thresholds **my_thresholds, char *warn_string, char *critical_string)
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;
117 if (critical_string != NULL) {
118 if ((temp_thresholds->critical = parse_range_string(critical_string)) == NULL) {
119 return NP_RANGE_UNPARSEABLE;
123 *my_thresholds = temp_thresholds;
125 return 0;
128 void
129 set_thresholds(thresholds **my_thresholds, char *warn_string, char *critical_string)
131 switch (_set_thresholds(my_thresholds, warn_string, critical_string)) {
132 case 0:
133 return;
134 case NP_RANGE_UNPARSEABLE:
135 die(STATE_UNKNOWN, _("Range format incorrect"));
136 case NP_WARN_WITHIN_CRIT:
137 die(STATE_UNKNOWN, _("Warning level is a subset of critical and will not be alerted"));
138 break;
142 void print_thresholds(const char *threshold_name, thresholds *my_threshold) {
143 printf("%s - ", threshold_name);
144 if (! my_threshold) {
145 printf("Threshold not set");
146 } else {
147 if (my_threshold->warning) {
148 printf("Warning: start=%g end=%g; ", my_threshold->warning->start, my_threshold->warning->end);
149 } else {
150 printf("Warning not set; ");
152 if (my_threshold->critical) {
153 printf("Critical: start=%g end=%g", my_threshold->critical->start, my_threshold->critical->end);
154 } else {
155 printf("Critical not set");
158 printf("\n");
161 /* Returns TRUE if alert should be raised based on the range */
163 check_range(double value, range *my_range)
165 int no = FALSE;
166 int yes = TRUE;
168 if (my_range->alert_on == INSIDE) {
169 no = TRUE;
170 yes = FALSE;
173 if (my_range->end_infinity == FALSE && my_range->start_infinity == FALSE) {
174 if ((my_range->start <= value) && (value <= my_range->end)) {
175 return no;
176 } else {
177 return yes;
179 } else if (my_range->start_infinity == FALSE && my_range->end_infinity == TRUE) {
180 if (my_range->start <= value) {
181 return no;
182 } else {
183 return yes;
185 } else if (my_range->start_infinity == TRUE && my_range->end_infinity == FALSE) {
186 if (value <= my_range->end) {
187 return no;
188 } else {
189 return yes;
191 } else {
192 return no;
196 /* Returns status */
198 get_status(double value, thresholds *my_thresholds)
200 if (my_thresholds->critical != NULL) {
201 if (check_range(value, my_thresholds->critical) == TRUE) {
202 return STATE_CRITICAL;
205 if (my_thresholds->warning != NULL) {
206 if (check_range(value, my_thresholds->warning) == TRUE) {
207 return STATE_WARNING;
210 return STATE_OK;
213 char *np_escaped_string (const char *string) {
214 char *data;
215 int i, j=0;
216 data = strdup(string);
217 for (i=0; data[i]; i++) {
218 if (data[i] == '\\') {
219 switch(data[++i]) {
220 case 'n':
221 data[j++] = '\n';
222 break;
223 case 'r':
224 data[j++] = '\r';
225 break;
226 case 't':
227 data[j++] = '\t';
228 break;
229 case '\\':
230 data[j++] = '\\';
231 break;
232 default:
233 data[j++] = data[i];
235 } else {
236 data[j++] = data[i];
239 data[j] = '\0';
240 return data;
243 int np_check_if_root(void) { return (geteuid() == 0); }
245 int np_warn_if_not_root(void) {
246 int status = np_check_if_root();
247 if(!status) {
248 printf(_("Warning: "));
249 printf(_("This plugin must be either run as root or setuid root.\n"));
250 printf(_("To run as root, you can use a tool like sudo.\n"));
251 printf(_("To set the setuid permissions, use the command:\n"));
252 /* XXX could we use something like progname? */
253 printf("\tchmod u+s yourpluginfile\n");
255 return status;