1 /*****************************************************************************
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/>.
28 *****************************************************************************/
32 #include "utils_base.h"
35 die (int result
, const char *fmt
, ...)
44 void set_range_start (range
*this, double value
) {
46 this->start_infinity
= FALSE
;
49 void set_range_end (range
*this, double value
) {
51 this->end_infinity
= FALSE
;
55 *parse_range_string (char *str
) {
61 temp_range
= (range
*) malloc(sizeof(range
));
64 temp_range
->start
= 0;
65 temp_range
->start_infinity
= FALSE
;
67 temp_range
->end_infinity
= TRUE
;
68 temp_range
->alert_on
= OUTSIDE
;
71 temp_range
->alert_on
= INSIDE
;
75 end_str
= index(str
, ':');
76 if (end_str
!= NULL
) {
78 temp_range
->start_infinity
= TRUE
;
80 start
= strtod(str
, NULL
); /* Will stop at the ':' */
81 set_range_start(temp_range
, start
);
83 end_str
++; /* Move past the ':' */
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
) {
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
;
129 set_thresholds(thresholds
**my_thresholds
, char *warn_string
, char *critical_string
)
131 switch (_set_thresholds(my_thresholds
, warn_string
, critical_string
)) {
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"));
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");
147 if (my_threshold
->warning
) {
148 printf("Warning: start=%g end=%g; ", my_threshold
->warning
->start
, my_threshold
->warning
->end
);
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
);
155 printf("Critical not set");
161 /* Returns TRUE if alert should be raised based on the range */
163 check_range(double value
, range
*my_range
)
168 if (my_range
->alert_on
== INSIDE
) {
173 if (my_range
->end_infinity
== FALSE
&& my_range
->start_infinity
== FALSE
) {
174 if ((my_range
->start
<= value
) && (value
<= my_range
->end
)) {
179 } else if (my_range
->start_infinity
== FALSE
&& my_range
->end_infinity
== TRUE
) {
180 if (my_range
->start
<= value
) {
185 } else if (my_range
->start_infinity
== TRUE
&& my_range
->end_infinity
== FALSE
) {
186 if (value
<= my_range
->end
) {
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
;
213 char *np_escaped_string (const char *string
) {
216 data
= strdup(string
);
217 for (i
=0; data
[i
]; i
++) {
218 if (data
[i
] == '\\') {
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();
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");