Debian Codegraph @

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

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 /*
3 * License: GPLv3+
4 * Copyright (c) 2019 Davide Madrisan <[email protected]>
5 *
6 * A library to manage the Nagios perfdata.
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 * This software takes some ideas and code from procps-3.2.8 (free).
22 */
23
24 #include "common.h"
25 #include "logging.h"
26 #include "system.h"
27 #include "thresholds.h"
28 #include "units.h"
29
30 int
31 get_perfdata_limit (range * threshold, unsigned long base,
32 unsigned long long *limit, bool percent)
33 {
34 if (NULL == threshold)
35 return -1;
36
37 double threshold_limit;
38
39 if (NP_RANGE_INSIDE == threshold->alert_on)
40 {
41 /* example: @10:20 >= 10 and <= 20, (inside the range of {10 .. 20}) */
42 dbg ("threshold {%g, %g} with alert_on set to true\n",
43 threshold->start, threshold->end);
44 return -1;
45 }
46 else if (threshold->start_infinity)
47 {
48 /* example: ~:10 > 10, (outside the range of {-\infty .. 10}) */
49 dbg ("threshold {%g, %g} with start_infinity set to true\n",
50 threshold->start, threshold->end);
51 return -1;
52 }
53 else if (threshold->end_infinity)
54 {
55 /* example: 10: < 10, (outside {10 .. \infty}) */
56 dbg ("threshold {%g, %g} with end_infinity set to true\n",
57 threshold->start, threshold->end);
58 threshold_limit = threshold->start;
59 }
60 else
61 {
62 /* example: 10 < 0 or > 10, (outside the range of {0 .. 10})
63 * ~:10 > 10, (outside the range of {-\infty .. 10}) */
64 dbg ("threshold {%g, %g}\n", threshold->start, threshold->end);
65 threshold_limit = threshold->end;
66 }
67
68 *limit = (unsigned long long) (base * threshold_limit);
69 if (percent)
70 *limit = (unsigned long long) (*limit / 100.0);
71
72 return 0;
73 }
74
75 int
76 get_perfdata_limit_converted (range * threshold, unsigned long base,
77 int shift, unsigned long long *limit,
78 bool percent)
79 {
80 int error = get_perfdata_limit (threshold, base, limit, percent);
81 if (0 != error)
82 return error;
83
84 *limit = UNIT_CONVERT (*limit, shift);
85 return 0;
86 }