| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#include "testutils.h" |
| 24 |
#include "thresholds.h" |
| 25 |
|
| 26 |
|
| 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 |
|
| 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) |