1 /*****************************************************************************
5 * Library of useful functions for plugins
6 * These functions are tested with libtap. See tests/ directory
8 * Copyright (c) 2006 Nagios Plugin Development Team
13 ****************************************************************************/
17 #include "utils_base.h"
20 die (int result
, const char *fmt
, ...)
29 void set_range_start (range
*this, double value
) {
31 this->start_infinity
= FALSE
;
34 void set_range_end (range
*this, double value
) {
36 this->end_infinity
= FALSE
;
40 *parse_range_string (char *str
) {
46 temp_range
= (range
*) malloc(sizeof(range
));
49 temp_range
->start
= 0;
50 temp_range
->start_infinity
= FALSE
;
52 temp_range
->end_infinity
= TRUE
;
53 temp_range
->alert_on
= OUTSIDE
;
56 temp_range
->alert_on
= INSIDE
;
60 end_str
= index(str
, ':');
61 if (end_str
!= NULL
) {
63 temp_range
->start_infinity
= TRUE
;
65 start
= strtod(str
, NULL
); /* Will stop at the ':' */
66 set_range_start(temp_range
, start
);
68 end_str
++; /* Move past the ':' */
72 end
= strtod(end_str
, NULL
);
73 if (strcmp(end_str
, "") != 0) {
74 set_range_end(temp_range
, end
);
77 if (temp_range
->start_infinity
== TRUE
||
78 temp_range
->end_infinity
== TRUE
||
79 temp_range
->start
<= temp_range
->end
) {
86 /* returns 0 if okay, otherwise 1 */
88 _set_thresholds(thresholds
**my_thresholds
, char *warn_string
, char *critical_string
)
90 thresholds
*temp_thresholds
= NULL
;
92 temp_thresholds
= malloc(sizeof(temp_thresholds
));
94 temp_thresholds
->warning
= NULL
;
95 temp_thresholds
->critical
= NULL
;
97 if (warn_string
!= NULL
) {
98 if ((temp_thresholds
->warning
= parse_range_string(warn_string
)) == NULL
) {
99 return NP_RANGE_UNPARSEABLE
;
102 if (critical_string
!= NULL
) {
103 if ((temp_thresholds
->critical
= parse_range_string(critical_string
)) == NULL
) {
104 return NP_RANGE_UNPARSEABLE
;
108 if (*my_thresholds
> 0) { /* Not sure why, but sometimes could be -1 */
109 /* printf("Freeing here: %d\n", *my_thresholds); */
110 free(*my_thresholds
);
112 *my_thresholds
= temp_thresholds
;
118 set_thresholds(thresholds
**my_thresholds
, char *warn_string
, char *critical_string
)
120 switch (_set_thresholds(my_thresholds
, warn_string
, critical_string
)) {
123 case NP_RANGE_UNPARSEABLE
:
124 die(STATE_UNKNOWN
, _("Range format incorrect"));
125 case NP_WARN_WITHIN_CRIT
:
126 die(STATE_UNKNOWN
, _("Warning level is a subset of critical and will not be alerted"));
131 void print_thresholds(const char *threshold_name
, thresholds
*my_threshold
) {
132 printf("%s - ", threshold_name
);
133 if (! my_threshold
) {
134 printf("Threshold not set");
136 if (my_threshold
->warning
) {
137 printf("Warning: start=%g end=%g; ", my_threshold
->warning
->start
, my_threshold
->warning
->end
);
139 printf("Warning not set; ");
141 if (my_threshold
->critical
) {
142 printf("Critical: start=%g end=%g", my_threshold
->critical
->start
, my_threshold
->critical
->end
);
144 printf("Critical not set");
150 /* Returns TRUE if alert should be raised based on the range */
152 check_range(double value
, range
*my_range
)
157 if (my_range
->alert_on
== INSIDE
) {
162 if (my_range
->end_infinity
== FALSE
&& my_range
->start_infinity
== FALSE
) {
163 if ((my_range
->start
<= value
) && (value
<= my_range
->end
)) {
168 } else if (my_range
->start_infinity
== FALSE
&& my_range
->end_infinity
== TRUE
) {
169 if (my_range
->start
<= value
) {
174 } else if (my_range
->start_infinity
== TRUE
&& my_range
->end_infinity
== FALSE
) {
175 if (value
<= my_range
->end
) {
187 get_status(double value
, thresholds
*my_thresholds
)
189 if (my_thresholds
->critical
!= NULL
) {
190 if (check_range(value
, my_thresholds
->critical
) == TRUE
) {
191 return STATE_CRITICAL
;
194 if (my_thresholds
->warning
!= NULL
) {
195 if (check_range(value
, my_thresholds
->warning
) == TRUE
) {
196 return STATE_WARNING
;
202 char *np_escaped_string (const char *string
) {
205 data
= strdup(string
);
206 for (i
=0; data
[i
]; i
++) {
207 if (data
[i
] == '\\') {
232 int np_check_if_root(void) { return (geteuid() == 0); }
234 int np_warn_if_not_root(void) {
235 int status
= np_check_if_root();
237 printf(_("Warning: "));
238 printf(_("This plugin must be either run as root or setuid root.\n"));
239 printf(_("To run as root, you can use a tool like sudo.\n"));
240 printf(_("To set the setuid permissions, use the command:\n"));
241 /* XXX could we use something like progname? */
242 printf("\tchmod u+s yourpluginfile\n");