summaryrefslogtreecommitdiffstats
path: root/lib/thresholds.c
blob: ddefae373216d7242099e207f80beab789348675 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "./thresholds.h"
#include "./utils_base.h"
#include "perfdata.h"

#include <stddef.h>

mp_thresholds mp_thresholds_init() {
	mp_thresholds tmp = {
		.critical = {},
		.critical_is_set = false,
		.warning = {},
		.warning_is_set = false,
	};
	return tmp;
}

char *fmt_threshold_warning(const thresholds th) {
	if (th.warning == NULL) {
		return "";
	}

	return fmt_range(*th.warning);
}

char *fmt_threshold_critical(const thresholds th) {
	if (th.critical == NULL) {
		return "";
	}
	return fmt_range(*th.critical);
}

mp_perfdata mp_pd_set_thresholds(mp_perfdata perfdata, mp_thresholds threshold) {
	if (threshold.critical_is_set) {
		perfdata.crit = threshold.critical;
		perfdata.crit_present = true;
	}

	if (threshold.warning_is_set) {
		perfdata.warn = threshold.warning;
		perfdata.warn_present = true;
	}

	return perfdata;
}

mp_state_enum mp_get_pd_status(mp_perfdata perfdata) {
	if (perfdata.crit_present) {
		if (mp_check_range(perfdata.value, perfdata.crit)) {
			return STATE_CRITICAL;
		}
	}
	if (perfdata.warn_present) {
		if (mp_check_range(perfdata.value, perfdata.warn)) {
			return STATE_CRITICAL;
		}
	}

	return STATE_OK;
}