summaryrefslogtreecommitdiffstats
path: root/lib/utils_base.c
diff options
context:
space:
mode:
authorTon Voon <tonvoon@users.sourceforge.net>2006-07-13 23:58:00 +0000
committerTon Voon <tonvoon@users.sourceforge.net>2006-07-13 23:58:00 +0000
commit5912398b9723545ecd061650667cbb238be85743 (patch)
treee5d8353cc8d8c278bfc22bd926f613da63f5e83f /lib/utils_base.c
parent548083b2ea865474915fc8a9ddd361e997585a02 (diff)
downloadmonitoring-plugins-5912398b9723545ecd061650667cbb238be85743.tar.gz
Major fixes to check_disk. Now should return same data as df
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@1452 f882894a-f735-0410-b71e-b25c423dba1c
Diffstat (limited to 'lib/utils_base.c')
-rw-r--r--lib/utils_base.c225
1 files changed, 225 insertions, 0 deletions
diff --git a/lib/utils_base.c b/lib/utils_base.c
new file mode 100644
index 00000000..5a45f84a
--- /dev/null
+++ b/lib/utils_base.c
@@ -0,0 +1,225 @@
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 ****************************************************************************/
14
15#include "common.h"
16#include "utils_base.h"
17
18void
19die (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}
27
28void set_range_start (range *this, double value) {
29 this->start = value;
30 this->start_infinity = FALSE;
31}
32
33void set_range_end (range *this, double value) {
34 this->end = value;
35 this->end_infinity = FALSE;
36}
37
38range
39*parse_range_string (char *str) {
40 range *temp_range;
41 double start;
42 double end;
43 char *end_str;
44
45 temp_range = (range *) malloc(sizeof(range));
46
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;
53
54 if (str[0] == '@') {
55 temp_range->alert_on = INSIDE;
56 str++;
57 }
58
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 }
75
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}
84
85/* returns 0 if okay, otherwise 1 */
86int
87_set_thresholds(thresholds **my_thresholds, char *warn_string, char *critical_string)
88{
89 thresholds *temp_thresholds = NULL;
90
91 temp_thresholds = malloc(sizeof(temp_thresholds));
92
93 temp_thresholds->warning = NULL;
94 temp_thresholds->critical = NULL;
95
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 }
106
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;
112
113 return 0;
114}
115
116void
117set_thresholds(thresholds **my_thresholds, char *warn_string, char *critical_string)
118{
119 if (_set_thresholds(my_thresholds, warn_string, critical_string) == 0) {
120 return;
121 } else {
122 die(STATE_UNKNOWN, _("Range format incorrect"));
123 }
124}
125
126void 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");
143}
144
145/* Returns TRUE if alert should be raised based on the range */
146int
147check_range(double value, range *my_range)
148{
149 int false = FALSE;
150 int true = TRUE;
151
152 if (my_range->alert_on == INSIDE) {
153 false = TRUE;
154 true = FALSE;
155 }
156
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 }
178}
179
180/* Returns status */
181int
182get_status(double value, thresholds *my_thresholds)
183{
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;
195}
196
197char *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;
225}