Debian Codegraph @

madrisan-nagios-plugins-linux/nagios-plugins-linux/lib/thresholds.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 /*
3 * License: GPL
4 * Copyright (c) 2006 Nagios Plugins Development Team
5 *
6 * Library of useful functions for nagios plugins
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include <errno.h>
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "common.h"
29 #include "string-macros.h"
30 #include "system.h"
31 #include "thresholds.h"
32 #include "xalloc.h"
33
34 /*
35 * Returns TRUE if alert should be raised based on the range
36 */
37 int
38 check_range (double value, range * my_range)
39 {
40 bool no = false;
41 bool yes = true;
42
43 if (my_range->alert_on == NP_RANGE_INSIDE)
44 {
45 no = true;
46 yes = false;
47 }
48
49 if (my_range->end_infinity == false && my_range->start_infinity == false)
50 {
51 if ((my_range->start <= value) && (value <= my_range->end))
52 return no;
53 else
54 return yes;
55 }
56 else if (my_range->start_infinity == false
57 && my_range->end_infinity == true)
58 {
59 if (my_range->start <= value)
60 return no;
61 else
62 return yes;
63 }
64 else if (my_range->start_infinity == true
65 && my_range->end_infinity == false)
66 {
67 if (value <= my_range->end)
68 return no;
69 else
70 return yes;
71 }
72 else
73 return no;
74 }
75
76 int
77 get_status (double value, thresholds * my_thresholds)
78 {
79 if (my_thresholds->critical != NULL)
80 {
81 if (check_range (value, my_thresholds->critical) == true)
82 return STATE_CRITICAL;
83 }
84 if (my_thresholds->warning != NULL)
85 {
86 if (check_range (value, my_thresholds->warning) == true)
87 return STATE_WARNING;
88 }
89 return STATE_OK;
90 }
91
92 void
93 set_range_start (range * this, double value)
94 {
95 this->start = value;
96 this->start_infinity = false;
97 }
98
99 void
100 set_range_end (range * this, double value)
101 {
102 this->end = value;
103 this->end_infinity = false;
104 }
105
106 range *
107 parse_range_string (char *str)
108 {
109 range *temp_range;
110 double end;
111 char *end_str;
112
113 temp_range = (range *) xmalloc (sizeof (range));
114
115 /*
116 * Set defaults
117 */
118 temp_range->start = 0;
119 temp_range->start_infinity = false;
120 temp_range->end = 0;
121 temp_range->end_infinity = true;
122 temp_range->alert_on = NP_RANGE_OUTSIDE;
123
124 if (str[0] == '@')
125 {
126 temp_range->alert_on = NP_RANGE_INSIDE;
127 str++;
128 }
129
130 end_str = index (str, ':');
131 if (end_str != NULL)
132 {
133 if (str[0] == '~')
134 temp_range->start_infinity = true;
135 else
136 {
137 double start = strtod (str, NULL); /* Will stop at the ':' */
138 set_range_start (temp_range, start);
139 }
140 end_str++; /* Move past the ':' */
141 }
142 else
143 {
144 end_str = str;
145 }
146 end = strtod (end_str, NULL);
147 if (STRNEQ (end_str, ""))
148 set_range_end (temp_range, end);
149
150 if (temp_range->start_infinity == true ||
151 temp_range->end_infinity == true ||
152 temp_range->start <= temp_range->end)
153 {
154 return temp_range;
155 }
156 free (temp_range);
157 return NULL;
158 }
159
160 /*
161 * Returns 0 if okay, otherwise 1 (NP_RANGE_UNPARSEABLE)
162 */
163 int
164 set_thresholds (thresholds ** my_thresholds, char *warn_string,
165 char *critical_string)
166 {
167 thresholds *temp_thresholds = NULL;
168
169 if ((temp_thresholds = malloc (sizeof (thresholds))) == NULL)
170 {
171 printf ("Cannot allocate memory: %s", strerror (errno));
172 exit (STATE_UNKNOWN);
173 }
174
175 temp_thresholds->warning = NULL;
176 temp_thresholds->critical = NULL;
177
178 if (warn_string != NULL)
179 {
180 if ((temp_thresholds->warning =
181 parse_range_string (warn_string)) == NULL)
182 {
183 free (temp_thresholds);
184 return NP_RANGE_UNPARSEABLE;
185 }
186 }
187 if (critical_string != NULL)
188 {
189 if ((temp_thresholds->critical =
190 parse_range_string (critical_string)) == NULL)
191 {
192 free (temp_thresholds);
193 return NP_RANGE_UNPARSEABLE;
194 }
195 }
196
197 *my_thresholds = temp_thresholds;
198
199 return 0;
200 }
201
202 /*
203 * Returns true if the warning and critical thresholds are expressed
204 * as percentages (when defined), false otherwise
205 */
206
207 bool
208 thresholds_expressed_as_percentages (char *warn_string, char *critical_string)
209 {
210 if ((warn_string && !STRCONTAINS (warn_string, "%"))
211 || (critical_string && !STRCONTAINS (critical_string, "%")))
212 return false;
213
214 return true;
215 }