diff options
author | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-07-06 22:49:04 +0200 |
---|---|---|
committer | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-07-06 22:49:04 +0200 |
commit | 92c7b91002b64187a67463e79ae3d306f5de923c (patch) | |
tree | 0a167d65d488fd07641789e45c6863e2b982fe43 /plugins-root/check_icmp.d | |
parent | bb4ce15997a3023c5c4f2bb434b37699797272da (diff) | |
parent | e63acdb83d9edcc8c1e244fc34043b4876b74f16 (diff) | |
download | monitoring-plugins-92c7b910.tar.gz |
Merge branch 'master' into refactor/check_load
Diffstat (limited to 'plugins-root/check_icmp.d')
-rw-r--r-- | plugins-root/check_icmp.d/check_icmp_helpers.c | 134 | ||||
-rw-r--r-- | plugins-root/check_icmp.d/check_icmp_helpers.h | 68 | ||||
-rw-r--r-- | plugins-root/check_icmp.d/config.h | 115 |
3 files changed, 317 insertions, 0 deletions
diff --git a/plugins-root/check_icmp.d/check_icmp_helpers.c b/plugins-root/check_icmp.d/check_icmp_helpers.c new file mode 100644 index 00000000..d56fbd8b --- /dev/null +++ b/plugins-root/check_icmp.d/check_icmp_helpers.c | |||
@@ -0,0 +1,134 @@ | |||
1 | #include "./config.h" | ||
2 | #include <math.h> | ||
3 | #include <netinet/in.h> | ||
4 | #include <sys/socket.h> | ||
5 | #include "./check_icmp_helpers.h" | ||
6 | #include "../../plugins/netutils.h" | ||
7 | |||
8 | // timeout as a global variable to make it available to the timeout handler | ||
9 | unsigned int timeout = DEFAULT_TIMEOUT; | ||
10 | |||
11 | check_icmp_config check_icmp_config_init() { | ||
12 | check_icmp_config tmp = { | ||
13 | .modes = | ||
14 | { | ||
15 | .order_mode = false, | ||
16 | .mos_mode = false, | ||
17 | .rta_mode = false, | ||
18 | .pl_mode = false, | ||
19 | .jitter_mode = false, | ||
20 | .score_mode = false, | ||
21 | }, | ||
22 | |||
23 | .min_hosts_alive = -1, | ||
24 | .crit = {.pl = DEFAULT_CRIT_PL, | ||
25 | .rta = DEFAULT_CRIT_RTA, | ||
26 | .jitter = 50.0, | ||
27 | .mos = 3.0, | ||
28 | .score = 70.0}, | ||
29 | .warn = {.pl = DEFAULT_WARN_PL, | ||
30 | .rta = DEFAULT_WARN_RTA, | ||
31 | .jitter = 40.0, | ||
32 | .mos = 3.5, | ||
33 | .score = 80.0}, | ||
34 | |||
35 | .ttl = DEFAULT_TTL, | ||
36 | .icmp_data_size = DEFAULT_PING_DATA_SIZE, | ||
37 | .target_interval = 0, | ||
38 | .number_of_packets = DEFAULT_NUMBER_OF_PACKETS, | ||
39 | |||
40 | .source_ip = NULL, | ||
41 | .need_v4 = false, | ||
42 | .need_v6 = false, | ||
43 | |||
44 | .sender_id = 0, | ||
45 | |||
46 | .mode = MODE_RTA, | ||
47 | |||
48 | .number_of_targets = 0, | ||
49 | .targets = NULL, | ||
50 | |||
51 | .number_of_hosts = 0, | ||
52 | .hosts = NULL, | ||
53 | |||
54 | .output_format_is_set = false, | ||
55 | }; | ||
56 | return tmp; | ||
57 | } | ||
58 | |||
59 | ping_target ping_target_init() { | ||
60 | ping_target tmp = { | ||
61 | .rtmin = INFINITY, | ||
62 | |||
63 | .jitter_min = INFINITY, | ||
64 | |||
65 | .found_out_of_order_packets = false, | ||
66 | }; | ||
67 | |||
68 | return tmp; | ||
69 | } | ||
70 | |||
71 | check_icmp_state check_icmp_state_init() { | ||
72 | check_icmp_state tmp = {.icmp_sent = 0, .icmp_lost = 0, .icmp_recv = 0, .targets_down = 0}; | ||
73 | |||
74 | return tmp; | ||
75 | } | ||
76 | |||
77 | ping_target_create_wrapper ping_target_create(struct sockaddr_storage address) { | ||
78 | ping_target_create_wrapper result = { | ||
79 | .errorcode = OK, | ||
80 | }; | ||
81 | |||
82 | struct sockaddr_storage *tmp_addr = &address; | ||
83 | |||
84 | /* disregard obviously stupid addresses | ||
85 | * (I didn't find an ipv6 equivalent to INADDR_NONE) */ | ||
86 | if (((tmp_addr->ss_family == AF_INET && | ||
87 | (((struct sockaddr_in *)tmp_addr)->sin_addr.s_addr == INADDR_NONE || | ||
88 | ((struct sockaddr_in *)tmp_addr)->sin_addr.s_addr == INADDR_ANY))) || | ||
89 | (tmp_addr->ss_family == AF_INET6 && | ||
90 | (((struct sockaddr_in6 *)tmp_addr)->sin6_addr.s6_addr == in6addr_any.s6_addr))) { | ||
91 | result.errorcode = ERROR; | ||
92 | return result; | ||
93 | } | ||
94 | |||
95 | /* add the fresh ip */ | ||
96 | ping_target target = ping_target_init(); | ||
97 | |||
98 | /* fill out the sockaddr_storage struct */ | ||
99 | target.address = address; | ||
100 | |||
101 | result.host = target; | ||
102 | |||
103 | return result; | ||
104 | } | ||
105 | |||
106 | check_icmp_target_container check_icmp_target_container_init() { | ||
107 | check_icmp_target_container tmp = { | ||
108 | .name = NULL, | ||
109 | .number_of_targets = 0, | ||
110 | .target_list = NULL, | ||
111 | }; | ||
112 | return tmp; | ||
113 | } | ||
114 | |||
115 | unsigned int ping_target_list_append(ping_target *list, ping_target *elem) { | ||
116 | if (elem == NULL || list == NULL) { | ||
117 | return 0; | ||
118 | } | ||
119 | |||
120 | while (list->next != NULL) { | ||
121 | list = list->next; | ||
122 | } | ||
123 | |||
124 | list->next = elem; | ||
125 | |||
126 | unsigned int result = 1; | ||
127 | |||
128 | while (elem->next != NULL) { | ||
129 | result++; | ||
130 | elem = elem->next; | ||
131 | } | ||
132 | |||
133 | return result; | ||
134 | } | ||
diff --git a/plugins-root/check_icmp.d/check_icmp_helpers.h b/plugins-root/check_icmp.d/check_icmp_helpers.h new file mode 100644 index 00000000..dc6ea40b --- /dev/null +++ b/plugins-root/check_icmp.d/check_icmp_helpers.h | |||
@@ -0,0 +1,68 @@ | |||
1 | #pragma once | ||
2 | |||
3 | #include "../../lib/states.h" | ||
4 | #include <netinet/in_systm.h> | ||
5 | #include <netinet/in.h> | ||
6 | #include <netinet/ip.h> | ||
7 | #include <netinet/ip6.h> | ||
8 | #include <netinet/ip_icmp.h> | ||
9 | #include <netinet/icmp6.h> | ||
10 | #include <arpa/inet.h> | ||
11 | |||
12 | typedef struct ping_target { | ||
13 | unsigned short id; /* id in **table, and icmp pkts */ | ||
14 | char *msg; /* icmp error message, if any */ | ||
15 | |||
16 | struct sockaddr_storage address; /* the address of this host */ | ||
17 | struct sockaddr_storage error_addr; /* stores address of error replies */ | ||
18 | time_t time_waited; /* total time waited, in usecs */ | ||
19 | unsigned int icmp_sent, icmp_recv, icmp_lost; /* counters */ | ||
20 | unsigned char icmp_type, icmp_code; /* type and code from errors */ | ||
21 | unsigned short flags; /* control/status flags */ | ||
22 | |||
23 | double rtmax; /* max rtt */ | ||
24 | double rtmin; /* min rtt */ | ||
25 | |||
26 | double jitter; /* measured jitter */ | ||
27 | double jitter_max; /* jitter rtt maximum */ | ||
28 | double jitter_min; /* jitter rtt minimum */ | ||
29 | |||
30 | time_t last_tdiff; | ||
31 | unsigned int last_icmp_seq; /* Last ICMP_SEQ to check out of order pkts */ | ||
32 | |||
33 | bool found_out_of_order_packets; | ||
34 | |||
35 | struct ping_target *next; | ||
36 | } ping_target; | ||
37 | |||
38 | ping_target ping_target_init(); | ||
39 | |||
40 | typedef struct { | ||
41 | char *name; | ||
42 | ping_target *target_list; | ||
43 | unsigned int number_of_targets; | ||
44 | } check_icmp_target_container; | ||
45 | |||
46 | check_icmp_target_container check_icmp_target_container_init(); | ||
47 | |||
48 | typedef struct { | ||
49 | unsigned int icmp_sent; | ||
50 | unsigned int icmp_recv; | ||
51 | unsigned int icmp_lost; | ||
52 | unsigned short targets_down; | ||
53 | } check_icmp_state; | ||
54 | |||
55 | check_icmp_state check_icmp_state_init(); | ||
56 | |||
57 | typedef struct { | ||
58 | int errorcode; | ||
59 | ping_target host; | ||
60 | } ping_target_create_wrapper; | ||
61 | |||
62 | typedef struct { | ||
63 | int socket4; | ||
64 | int socket6; | ||
65 | } check_icmp_socket_set; | ||
66 | |||
67 | ping_target_create_wrapper ping_target_create(struct sockaddr_storage address); | ||
68 | unsigned int ping_target_list_append(ping_target *list, ping_target *elem); | ||
diff --git a/plugins-root/check_icmp.d/config.h b/plugins-root/check_icmp.d/config.h new file mode 100644 index 00000000..c348bef5 --- /dev/null +++ b/plugins-root/check_icmp.d/config.h | |||
@@ -0,0 +1,115 @@ | |||
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 <stdint.h> | ||
14 | #include "./check_icmp_helpers.h" | ||
15 | #include "output.h" | ||
16 | |||
17 | /* threshold structure. all values are maximum allowed, exclusive */ | ||
18 | typedef struct { | ||
19 | unsigned char pl; /* max allowed packet loss in percent */ | ||
20 | time_t rta; /* roundtrip time average, microseconds */ | ||
21 | double jitter; /* jitter time average, microseconds */ | ||
22 | double mos; /* MOS */ | ||
23 | double score; /* Score */ | ||
24 | } check_icmp_threshold; | ||
25 | |||
26 | /* the different modes of this program are as follows: | ||
27 | * MODE_RTA: send all packets no matter what (mimic check_icmp and check_ping) | ||
28 | * MODE_HOSTCHECK: Return immediately upon any sign of life | ||
29 | * In addition, sends packets to ALL addresses assigned | ||
30 | * to this host (as returned by gethostbyname() or | ||
31 | * gethostbyaddr() and expects one host only to be checked at | ||
32 | * a time. Therefore, any packet response what so ever will | ||
33 | * count as a sign of life, even when received outside | ||
34 | * crit.rta limit. Do not misspell any additional IP's. | ||
35 | * MODE_ALL: Requires packets from ALL requested IP to return OK (default). | ||
36 | * MODE_ICMP: Default Mode | ||
37 | */ | ||
38 | typedef enum { | ||
39 | MODE_RTA, | ||
40 | MODE_HOSTCHECK, | ||
41 | MODE_ALL, | ||
42 | MODE_ICMP, | ||
43 | } check_icmp_execution_mode; | ||
44 | |||
45 | typedef struct { | ||
46 | bool order_mode; | ||
47 | bool mos_mode; | ||
48 | bool rta_mode; | ||
49 | bool pl_mode; | ||
50 | bool jitter_mode; | ||
51 | bool score_mode; | ||
52 | } check_icmp_mode_switches; | ||
53 | |||
54 | typedef struct { | ||
55 | check_icmp_mode_switches modes; | ||
56 | |||
57 | int min_hosts_alive; | ||
58 | check_icmp_threshold crit; | ||
59 | check_icmp_threshold warn; | ||
60 | |||
61 | unsigned long ttl; | ||
62 | unsigned short icmp_data_size; | ||
63 | time_t target_interval; | ||
64 | unsigned short number_of_packets; | ||
65 | |||
66 | char *source_ip; | ||
67 | bool need_v4; | ||
68 | bool need_v6; | ||
69 | |||
70 | uint16_t sender_id; // PID of the main process, which is used as an ID in packets | ||
71 | |||
72 | check_icmp_execution_mode mode; | ||
73 | |||
74 | unsigned short number_of_targets; | ||
75 | ping_target *targets; | ||
76 | |||
77 | unsigned short number_of_hosts; | ||
78 | check_icmp_target_container *hosts; | ||
79 | |||
80 | mp_output_format output_format; | ||
81 | bool output_format_is_set; | ||
82 | } check_icmp_config; | ||
83 | |||
84 | check_icmp_config check_icmp_config_init(); | ||
85 | |||
86 | /* the data structure */ | ||
87 | typedef struct icmp_ping_data { | ||
88 | struct timeval stime; /* timestamp (saved in protocol struct as well) */ | ||
89 | unsigned short ping_id; | ||
90 | } icmp_ping_data; | ||
91 | |||
92 | #define MAX_IP_PKT_SIZE 65536 /* (theoretical) max IP packet size */ | ||
93 | #define IP_HDR_SIZE 20 | ||
94 | #define MAX_PING_DATA (MAX_IP_PKT_SIZE - IP_HDR_SIZE - ICMP_MINLEN) | ||
95 | #define MIN_PING_DATA_SIZE sizeof(struct icmp_ping_data) | ||
96 | #define DEFAULT_PING_DATA_SIZE (MIN_PING_DATA_SIZE + 44) | ||
97 | |||
98 | /* 80 msec packet interval by default */ | ||
99 | // DEPRECATED, remove when removing the option | ||
100 | #define DEFAULT_PKT_INTERVAL 80000 | ||
101 | |||
102 | #define DEFAULT_TARGET_INTERVAL 0 | ||
103 | |||
104 | #define DEFAULT_WARN_RTA 200000 | ||
105 | #define DEFAULT_CRIT_RTA 500000 | ||
106 | #define DEFAULT_WARN_PL 40 | ||
107 | #define DEFAULT_CRIT_PL 80 | ||
108 | |||
109 | #define DEFAULT_TIMEOUT 10 | ||
110 | #define DEFAULT_TTL 64 | ||
111 | |||
112 | #define DEFAULT_NUMBER_OF_PACKETS 5 | ||
113 | |||
114 | #define PACKET_BACKOFF_FACTOR 1.5 | ||
115 | #define TARGET_BACKOFF_FACTOR 1.5 | ||