Debian Codegraph @

madrisan-nagios-plugins-linux/nagios-plugins-linux/tests/tsclock_thresholds.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 /*
3 * License: GPLv3+
4 * Copyright (c) 2016 Davide Madrisan <[email protected]>
5 *
6 * Unit test for check_clock.c
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
23 #include "testutils.h"
24 #include "thresholds.h"
25
26 /* silence the compiler's warning 'function defined but not used' */
27 static _Noreturn void print_version (void) __attribute__((unused));
28 static _Noreturn void usage (FILE * out) __attribute__((unused));
29
30 #define NPL_TESTING
31 # include "../plugins/check_clock.c"
32 #undef NPL_TESTING
33
34 static long
35 get_current_datetime ()
36 {
37 struct tm *tm;
38 time_t t;
39 char outstr[32];
40 char *end = NULL;
41
42 t = time (NULL);
43 tm = localtime (&t);
44 if (strftime (outstr, sizeof (outstr), "%s", tm) == 0)
45 return EXIT_AM_HARDFAIL;
46
47 return strtol (outstr, &end, 10);
48 }
49
50 struct test_data
51 {
52 char *w;
53 char *c;
54 long delta;
55 };
56
57 static int
58 test_clock_timedelta (const void *tdata)
59 {
60 unsigned long refclock;
61 long timedelta;
62 thresholds *my_threshold = NULL;
63
64 const struct test_data *data = tdata;
65 long w_threshold = strtol (data->w, NULL, 10),
66 c_threshold = strtol (data->c, NULL, 10);
67
68 nagstatus status = set_thresholds (&my_threshold, data->w, data->c);
69 if (status == NP_RANGE_UNPARSEABLE)
70 return EXIT_AM_HARDFAIL;
71
72 refclock = data->delta + get_current_datetime ();
73 timedelta = get_timedelta (refclock, false);
74
75 status = get_status (labs (timedelta), my_threshold);
76 free (my_threshold);
77
78 if (data->delta <= w_threshold && status == STATE_OK)
79 return 0;
80 if (data->delta <= c_threshold && status == STATE_WARNING)
81 return 0;
82 if (data->delta > c_threshold && status == STATE_CRITICAL)
83 return 0;
84
85 return -1;
86 }
87
88 static int
89 mymain (void)
90 {
91 int ret = 0;
92 /* set the warning and critical thresholds */
93 struct test_data data = { .w = "20", .c = "40" };
94
95 #define DO_TEST(MSG, FUNC, DELTA) \
96 do \
97 { \
98 data.delta = DELTA; \
99 if (test_run (MSG, FUNC, &data) < 0) \
100 ret = -1; \
101 } \
102 while (0)
103
104 DO_TEST ("check clock for ok condition", test_clock_timedelta, 0);
105 DO_TEST ("check clock for warning condition", test_clock_timedelta, 30);
106 DO_TEST ("check clock for critical condition", test_clock_timedelta, 50);
107
108 return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
109 }
110
111 TEST_MAIN (mymain)