diff options
author | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-05-04 01:42:52 +0200 |
---|---|---|
committer | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-05-04 01:42:52 +0200 |
commit | 5a6adcb7db497fba7b89471a6d58dba80330ff4a (patch) | |
tree | 8db791660766ac2532105e894b1c73eee56a15aa /plugins-root/check_icmp.d | |
parent | eafee9c3f91879afa82749fa1d8cd2b0b53a5d5c (diff) | |
download | monitoring-plugins-5a6adcb7db497fba7b89471a6d58dba80330ff4a.tar.gz |
WIP - check_icmp refactor 2
Diffstat (limited to 'plugins-root/check_icmp.d')
-rw-r--r-- | plugins-root/check_icmp.d/check_icmp_helpers.c | 48 | ||||
-rw-r--r-- | plugins-root/check_icmp.d/check_icmp_helpers.h | 7 | ||||
-rw-r--r-- | plugins-root/check_icmp.d/config.h | 102 |
3 files changed, 154 insertions, 3 deletions
diff --git a/plugins-root/check_icmp.d/check_icmp_helpers.c b/plugins-root/check_icmp.d/check_icmp_helpers.c index 8f6d7362..2efe6e59 100644 --- a/plugins-root/check_icmp.d/check_icmp_helpers.c +++ b/plugins-root/check_icmp.d/check_icmp_helpers.c | |||
@@ -5,6 +5,9 @@ | |||
5 | #include "./check_icmp_helpers.h" | 5 | #include "./check_icmp_helpers.h" |
6 | #include "../../plugins/netutils.h" | 6 | #include "../../plugins/netutils.h" |
7 | 7 | ||
8 | // timeout as a global variable to make it available to the timeout handler | ||
9 | unsigned int timeout = DEFAULT_TIMEOUT; | ||
10 | |||
8 | check_icmp_config check_icmp_config_init() { | 11 | check_icmp_config check_icmp_config_init() { |
9 | check_icmp_config tmp = { | 12 | check_icmp_config tmp = { |
10 | .source_ip = NULL, | 13 | .source_ip = NULL, |
@@ -33,11 +36,14 @@ check_icmp_config check_icmp_config_init() { | |||
33 | .score = 80.0}, | 36 | .score = 80.0}, |
34 | .pid = {}, | 37 | .pid = {}, |
35 | .mode = MODE_RTA, | 38 | .mode = MODE_RTA, |
36 | .timeout = DEFAULT_TIMEOUT, | ||
37 | .ttl = DEFAULT_TTL, | 39 | .ttl = DEFAULT_TTL, |
38 | 40 | ||
39 | .packets = DEFAULT_NUMBER_OF_PACKETS, | 41 | .packets = DEFAULT_NUMBER_OF_PACKETS, |
42 | |||
40 | .number_of_targets = 0, | 43 | .number_of_targets = 0, |
44 | .targets = NULL, | ||
45 | |||
46 | .number_of_hosts = 0, | ||
41 | .hosts = NULL, | 47 | .hosts = NULL, |
42 | }; | 48 | }; |
43 | return tmp; | 49 | return tmp; |
@@ -140,3 +146,43 @@ check_icmp_target_container check_icmp_target_container_init() { | |||
140 | }; | 146 | }; |
141 | return tmp; | 147 | return tmp; |
142 | } | 148 | } |
149 | |||
150 | unsigned int ping_target_list_append(ping_target *list, ping_target *elem) { | ||
151 | if (elem == NULL || list == NULL) { | ||
152 | return 0; | ||
153 | } | ||
154 | |||
155 | while (list->next != NULL) { | ||
156 | list = list->next; | ||
157 | } | ||
158 | |||
159 | list->next = elem; | ||
160 | |||
161 | unsigned int result = 1; | ||
162 | |||
163 | while (elem->next != NULL) { | ||
164 | result++; | ||
165 | elem = elem->next; | ||
166 | } | ||
167 | |||
168 | return result; | ||
169 | } | ||
170 | |||
171 | void check_icmp_timeout_handler(int signal, siginfo_t * info, void *ucontext) { | ||
172 | // Ignore unused arguments | ||
173 | (void) info; | ||
174 | (void) ucontext; | ||
175 | mp_subcheck timeout_sc = mp_subcheck_init(); | ||
176 | timeout_sc = mp_set_subcheck_state(timeout_sc, socket_timeout_state); | ||
177 | |||
178 | if (signal == SIGALRM) { | ||
179 | xasprintf(&timeout_sc.output, _("timeout after %d seconds\n"), timeout); | ||
180 | } else { | ||
181 | xasprintf(&timeout_sc.output, _("timeout after %d seconds\n"), timeout); | ||
182 | } | ||
183 | |||
184 | mp_check overall = mp_check_init(); | ||
185 | mp_add_subcheck_to_check(&overall, timeout_sc); | ||
186 | |||
187 | mp_exit(overall); | ||
188 | } | ||
diff --git a/plugins-root/check_icmp.d/check_icmp_helpers.h b/plugins-root/check_icmp.d/check_icmp_helpers.h index 49f720ec..7e8a4d9f 100644 --- a/plugins-root/check_icmp.d/check_icmp_helpers.h +++ b/plugins-root/check_icmp.d/check_icmp_helpers.h | |||
@@ -15,7 +15,7 @@ typedef struct rta_host { | |||
15 | char *msg; /* icmp error message, if any */ | 15 | char *msg; /* icmp error message, if any */ |
16 | struct sockaddr_storage saddr_in; /* the address of this host */ | 16 | struct sockaddr_storage saddr_in; /* the address of this host */ |
17 | struct sockaddr_storage error_addr; /* stores address of error replies */ | 17 | struct sockaddr_storage error_addr; /* stores address of error replies */ |
18 | unsigned long long time_waited; /* total time waited, in usecs */ | 18 | time_t time_waited; /* total time waited, in usecs */ |
19 | unsigned int icmp_sent, icmp_recv, icmp_lost; /* counters */ | 19 | unsigned int icmp_sent, icmp_recv, icmp_lost; /* counters */ |
20 | unsigned char icmp_type, icmp_code; /* type and code from errors */ | 20 | unsigned char icmp_type, icmp_code; /* type and code from errors */ |
21 | unsigned short flags; /* control/status flags */ | 21 | unsigned short flags; /* control/status flags */ |
@@ -32,7 +32,7 @@ typedef struct rta_host { | |||
32 | double mos; /* Mean opinion score */ | 32 | double mos; /* Mean opinion score */ |
33 | double score; /* score */ | 33 | double score; /* score */ |
34 | 34 | ||
35 | unsigned int last_tdiff; | 35 | time_t last_tdiff; |
36 | unsigned int last_icmp_seq; /* Last ICMP_SEQ to check out of order pkts */ | 36 | unsigned int last_icmp_seq; /* Last ICMP_SEQ to check out of order pkts */ |
37 | unsigned char pl; /* measured packet loss */ | 37 | unsigned char pl; /* measured packet loss */ |
38 | 38 | ||
@@ -71,3 +71,6 @@ typedef struct { | |||
71 | } rta_host_create_wrapper; | 71 | } rta_host_create_wrapper; |
72 | 72 | ||
73 | rta_host_create_wrapper rta_host_create(char *name, struct sockaddr_storage *address); | 73 | rta_host_create_wrapper rta_host_create(char *name, struct sockaddr_storage *address); |
74 | unsigned int ping_target_list_append(ping_target *list, ping_target *elem); | ||
75 | |||
76 | void check_icmp_timeout_handler(int, siginfo_t *, void *); | ||
diff --git a/plugins-root/check_icmp.d/config.h b/plugins-root/check_icmp.d/config.h new file mode 100644 index 00000000..deae9bec --- /dev/null +++ b/plugins-root/check_icmp.d/config.h | |||
@@ -0,0 +1,102 @@ | |||
1 | #pragma once | ||
2 | |||
3 | #include "../../config.h" | ||
4 | #include "../../lib/states.h" | ||
5 | #include <stddef.h> | ||
6 | #include <netinet/in_systm.h> | ||
7 | #include <netinet/in.h> | ||
8 | #include <netinet/ip.h> | ||
9 | #include <netinet/ip6.h> | ||
10 | #include <netinet/ip_icmp.h> | ||
11 | #include <netinet/icmp6.h> | ||
12 | #include <arpa/inet.h> | ||
13 | #include "./check_icmp_helpers.h" | ||
14 | |||
15 | /* threshold structure. all values are maximum allowed, exclusive */ | ||
16 | typedef struct threshold { | ||
17 | unsigned char pl; /* max allowed packet loss in percent */ | ||
18 | unsigned int rta; /* roundtrip time average, microseconds */ | ||
19 | double jitter; /* jitter time average, microseconds */ | ||
20 | double mos; /* MOS */ | ||
21 | double score; /* Score */ | ||
22 | } threshold; | ||
23 | |||
24 | typedef struct { | ||
25 | char *source_ip; | ||
26 | |||
27 | bool order_mode; | ||
28 | bool mos_mode; | ||
29 | bool rta_mode; | ||
30 | bool pl_mode; | ||
31 | bool jitter_mode; | ||
32 | bool score_mode; | ||
33 | |||
34 | int min_hosts_alive; | ||
35 | unsigned short icmp_data_size; | ||
36 | unsigned short icmp_pkt_size; | ||
37 | unsigned int pkt_interval; | ||
38 | unsigned int target_interval; | ||
39 | threshold crit; | ||
40 | threshold warn; | ||
41 | pid_t pid; | ||
42 | |||
43 | int mode; | ||
44 | unsigned long ttl; | ||
45 | |||
46 | unsigned short packets; | ||
47 | |||
48 | unsigned short number_of_targets; | ||
49 | ping_target *targets; | ||
50 | |||
51 | unsigned short number_of_hosts; | ||
52 | check_icmp_target_container *hosts; | ||
53 | } check_icmp_config; | ||
54 | |||
55 | check_icmp_config check_icmp_config_init(); | ||
56 | |||
57 | /* the data structure */ | ||
58 | typedef struct icmp_ping_data { | ||
59 | struct timeval stime; /* timestamp (saved in protocol struct as well) */ | ||
60 | unsigned short ping_id; | ||
61 | } icmp_ping_data; | ||
62 | |||
63 | #define MAX_IP_PKT_SIZE 65536 /* (theoretical) max IP packet size */ | ||
64 | #define IP_HDR_SIZE 20 | ||
65 | #define MAX_PING_DATA (MAX_IP_PKT_SIZE - IP_HDR_SIZE - ICMP_MINLEN) | ||
66 | #define MIN_PING_DATA_SIZE sizeof(struct icmp_ping_data) | ||
67 | #define DEFAULT_PING_DATA_SIZE (MIN_PING_DATA_SIZE + 44) | ||
68 | |||
69 | /* 80 msec packet interval by default */ | ||
70 | #define DEFAULT_PKT_INTERVAL 80000 | ||
71 | #define DEFAULT_TARGET_INTERVAL 0 | ||
72 | |||
73 | #define DEFAULT_WARN_RTA 200000 | ||
74 | #define DEFAULT_CRIT_RTA 500000 | ||
75 | #define DEFAULT_WARN_PL 40 | ||
76 | #define DEFAULT_CRIT_PL 80 | ||
77 | |||
78 | #define DEFAULT_TIMEOUT 10 | ||
79 | #define DEFAULT_TTL 64 | ||
80 | |||
81 | /* the different modes of this program are as follows: | ||
82 | * MODE_RTA: send all packets no matter what (mimic check_icmp and check_ping) | ||
83 | * MODE_HOSTCHECK: Return immediately upon any sign of life | ||
84 | * In addition, sends packets to ALL addresses assigned | ||
85 | * to this host (as returned by gethostbyname() or | ||
86 | * gethostbyaddr() and expects one host only to be checked at | ||
87 | * a time. Therefore, any packet response what so ever will | ||
88 | * count as a sign of life, even when received outside | ||
89 | * crit.rta limit. Do not misspell any additional IP's. | ||
90 | * MODE_ALL: Requires packets from ALL requested IP to return OK (default). | ||
91 | * MODE_ICMP: implement something similar to check_icmp (MODE_RTA without | ||
92 | * tcp and udp args does this) | ||
93 | */ | ||
94 | #define MODE_RTA 0 | ||
95 | #define MODE_HOSTCHECK 1 | ||
96 | #define MODE_ALL 2 | ||
97 | #define MODE_ICMP 3 | ||
98 | |||
99 | #define DEFAULT_NUMBER_OF_PACKETS 5 | ||
100 | |||
101 | #define PACKET_BACKOFF_FACTOR 1.5 | ||
102 | #define TARGET_BACKOFF_FACTOR 1.5 | ||