summaryrefslogtreecommitdiffstats
path: root/plugins
diff options
context:
space:
mode:
authorLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2025-03-12 18:01:31 +0100
committerLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2025-03-12 18:01:31 +0100
commit53e102b6ace40138df373e078916d1a345ec5aa0 (patch)
treefe3ec389dd3171255928932ac19765fbaaf51829 /plugins
parent399121ed0d69a83cce483ce6b762ad48937a3c70 (diff)
downloadmonitoring-plugins-53e102b6ace40138df373e078916d1a345ec5aa0.tar.gz
check_time: clang-format
Diffstat (limited to 'plugins')
-rw-r--r--plugins/check_time.c85
1 files changed, 51 insertions, 34 deletions
diff --git a/plugins/check_time.c b/plugins/check_time.c
index d1f50683..02b152c0 100644
--- a/plugins/check_time.c
+++ b/plugins/check_time.c
@@ -68,8 +68,9 @@ int main(int argc, char **argv) {
68 /* Parse extra opts if any */ 68 /* Parse extra opts if any */
69 argv = np_extra_opts(&argc, argv, progname); 69 argv = np_extra_opts(&argc, argv, progname);
70 70
71 if (process_arguments(argc, argv) == ERROR) 71 if (process_arguments(argc, argv) == ERROR) {
72 usage4(_("Could not parse arguments")); 72 usage4(_("Could not parse arguments"));
73 }
73 74
74 /* initialize alarm signal handling */ 75 /* initialize alarm signal handling */
75 signal(SIGALRM, socket_timeout_alarm_handler); 76 signal(SIGALRM, socket_timeout_alarm_handler);
@@ -88,23 +89,25 @@ int main(int argc, char **argv) {
88 } 89 }
89 90
90 if (result != STATE_OK) { 91 if (result != STATE_OK) {
91 if (check_critical_time) 92 if (check_critical_time) {
92 result = STATE_CRITICAL; 93 result = STATE_CRITICAL;
93 else if (check_warning_time) 94 } else if (check_warning_time) {
94 result = STATE_WARNING; 95 result = STATE_WARNING;
95 else 96 } else {
96 result = STATE_UNKNOWN; 97 result = STATE_UNKNOWN;
98 }
97 die(result, _("TIME UNKNOWN - could not connect to server %s, port %d\n"), server_address, server_port); 99 die(result, _("TIME UNKNOWN - could not connect to server %s, port %d\n"), server_address, server_port);
98 } 100 }
99 101
100 if (use_udp) { 102 if (use_udp) {
101 if (send(socket, "", 0, 0) < 0) { 103 if (send(socket, "", 0, 0) < 0) {
102 if (check_critical_time) 104 if (check_critical_time) {
103 result = STATE_CRITICAL; 105 result = STATE_CRITICAL;
104 else if (check_warning_time) 106 } else if (check_warning_time) {
105 result = STATE_WARNING; 107 result = STATE_WARNING;
106 else 108 } else {
107 result = STATE_UNKNOWN; 109 result = STATE_UNKNOWN;
110 }
108 die(result, _("TIME UNKNOWN - could not send UDP request to server %s, port %d\n"), server_address, server_port); 111 die(result, _("TIME UNKNOWN - could not send UDP request to server %s, port %d\n"), server_address, server_port);
109 } 112 }
110 } 113 }
@@ -121,38 +124,43 @@ int main(int argc, char **argv) {
121 124
122 /* return a WARNING status if we couldn't read any data */ 125 /* return a WARNING status if we couldn't read any data */
123 if (result <= 0) { 126 if (result <= 0) {
124 if (check_critical_time) 127 if (check_critical_time) {
125 result = STATE_CRITICAL; 128 result = STATE_CRITICAL;
126 else if (check_warning_time) 129 } else if (check_warning_time) {
127 result = STATE_WARNING; 130 result = STATE_WARNING;
128 else 131 } else {
129 result = STATE_UNKNOWN; 132 result = STATE_UNKNOWN;
133 }
130 die(result, _("TIME UNKNOWN - no data received from server %s, port %d\n"), server_address, server_port); 134 die(result, _("TIME UNKNOWN - no data received from server %s, port %d\n"), server_address, server_port);
131 } 135 }
132 136
133 result = STATE_OK; 137 result = STATE_OK;
134 138
135 time_t conntime = (end_time - start_time); 139 time_t conntime = (end_time - start_time);
136 if (check_critical_time && conntime > critical_time) 140 if (check_critical_time && conntime > critical_time) {
137 result = STATE_CRITICAL; 141 result = STATE_CRITICAL;
138 else if (check_warning_time && conntime > warning_time) 142 } else if (check_warning_time && conntime > warning_time) {
139 result = STATE_WARNING; 143 result = STATE_WARNING;
144 }
140 145
141 if (result != STATE_OK) 146 if (result != STATE_OK) {
142 die(result, _("TIME %s - %d second response time|%s\n"), state_text(result), (int)conntime, 147 die(result, _("TIME %s - %d second response time|%s\n"), state_text(result), (int)conntime,
143 perfdata("time", (long)conntime, "s", check_warning_time, (long)warning_time, check_critical_time, (long)critical_time, true, 0, 148 perfdata("time", (long)conntime, "s", check_warning_time, (long)warning_time, check_critical_time, (long)critical_time, true, 0,
144 false, 0)); 149 false, 0));
150 }
145 151
146 server_time = ntohl(raw_server_time) - UNIX_EPOCH; 152 server_time = ntohl(raw_server_time) - UNIX_EPOCH;
147 if (server_time > (unsigned long)end_time) 153 if (server_time > (unsigned long)end_time) {
148 diff_time = server_time - (unsigned long)end_time; 154 diff_time = server_time - (unsigned long)end_time;
149 else 155 } else {
150 diff_time = (unsigned long)end_time - server_time; 156 diff_time = (unsigned long)end_time - server_time;
157 }
151 158
152 if (check_critical_diff && diff_time > critical_diff) 159 if (check_critical_diff && diff_time > critical_diff) {
153 result = STATE_CRITICAL; 160 result = STATE_CRITICAL;
154 else if (check_warning_diff && diff_time > warning_diff) 161 } else if (check_warning_diff && diff_time > warning_diff) {
155 result = STATE_WARNING; 162 result = STATE_WARNING;
163 }
156 164
157 printf(_("TIME %s - %lu second time difference|%s %s\n"), state_text(result), diff_time, 165 printf(_("TIME %s - %lu second time difference|%s %s\n"), state_text(result), diff_time,
158 perfdata("time", (long)conntime, "s", check_warning_time, (long)warning_time, check_critical_time, (long)critical_time, true, 0, 166 perfdata("time", (long)conntime, "s", check_warning_time, (long)warning_time, check_critical_time, (long)critical_time, true, 0,
@@ -175,20 +183,22 @@ int process_arguments(int argc, char **argv) {
175 {"help", no_argument, 0, 'h'}, 183 {"help", no_argument, 0, 'h'},
176 {0, 0, 0, 0}}; 184 {0, 0, 0, 0}};
177 185
178 if (argc < 2) 186 if (argc < 2) {
179 usage("\n"); 187 usage("\n");
188 }
180 189
181 for (int i = 1; i < argc; i++) { 190 for (int i = 1; i < argc; i++) {
182 if (strcmp("-to", argv[i]) == 0) 191 if (strcmp("-to", argv[i]) == 0) {
183 strcpy(argv[i], "-t"); 192 strcpy(argv[i], "-t");
184 else if (strcmp("-wd", argv[i]) == 0) 193 } else if (strcmp("-wd", argv[i]) == 0) {
185 strcpy(argv[i], "-w"); 194 strcpy(argv[i], "-w");
186 else if (strcmp("-cd", argv[i]) == 0) 195 } else if (strcmp("-cd", argv[i]) == 0) {
187 strcpy(argv[i], "-c"); 196 strcpy(argv[i], "-c");
188 else if (strcmp("-wt", argv[i]) == 0) 197 } else if (strcmp("-wt", argv[i]) == 0) {
189 strcpy(argv[i], "-W"); 198 strcpy(argv[i], "-W");
190 else if (strcmp("-ct", argv[i]) == 0) 199 } else if (strcmp("-ct", argv[i]) == 0) {
191 strcpy(argv[i], "-C"); 200 strcpy(argv[i], "-C");
201 }
192 } 202 }
193 203
194 int option_char; 204 int option_char;
@@ -196,8 +206,9 @@ int process_arguments(int argc, char **argv) {
196 int option = 0; 206 int option = 0;
197 option_char = getopt_long(argc, argv, "hVH:w:c:W:C:p:t:u", longopts, &option); 207 option_char = getopt_long(argc, argv, "hVH:w:c:W:C:p:t:u", longopts, &option);
198 208
199 if (option_char == -1 || option_char == EOF) 209 if (option_char == -1 || option_char == EOF) {
200 break; 210 break;
211 }
201 212
202 switch (option_char) { 213 switch (option_char) {
203 case '?': /* print short usage statement if args not parsable */ 214 case '?': /* print short usage statement if args not parsable */
@@ -209,8 +220,9 @@ int process_arguments(int argc, char **argv) {
209 print_revision(progname, NP_VERSION); 220 print_revision(progname, NP_VERSION);
210 exit(STATE_UNKNOWN); 221 exit(STATE_UNKNOWN);
211 case 'H': /* hostname */ 222 case 'H': /* hostname */
212 if (!is_host(optarg)) 223 if (!is_host(optarg)) {
213 usage2(_("Invalid hostname/address"), optarg); 224 usage2(_("Invalid hostname/address"), optarg);
225 }
214 server_address = optarg; 226 server_address = optarg;
215 break; 227 break;
216 case 'w': /* warning-variance */ 228 case 'w': /* warning-variance */
@@ -244,30 +256,34 @@ int process_arguments(int argc, char **argv) {
244 } 256 }
245 break; 257 break;
246 case 'W': /* warning-connect */ 258 case 'W': /* warning-connect */
247 if (!is_intnonneg(optarg)) 259 if (!is_intnonneg(optarg)) {
248 usage4(_("Warning threshold must be a positive integer")); 260 usage4(_("Warning threshold must be a positive integer"));
249 else 261 } else {
250 warning_time = atoi(optarg); 262 warning_time = atoi(optarg);
263 }
251 check_warning_time = true; 264 check_warning_time = true;
252 break; 265 break;
253 case 'C': /* critical-connect */ 266 case 'C': /* critical-connect */
254 if (!is_intnonneg(optarg)) 267 if (!is_intnonneg(optarg)) {
255 usage4(_("Critical threshold must be a positive integer")); 268 usage4(_("Critical threshold must be a positive integer"));
256 else 269 } else {
257 critical_time = atoi(optarg); 270 critical_time = atoi(optarg);
271 }
258 check_critical_time = true; 272 check_critical_time = true;
259 break; 273 break;
260 case 'p': /* port */ 274 case 'p': /* port */
261 if (!is_intnonneg(optarg)) 275 if (!is_intnonneg(optarg)) {
262 usage4(_("Port must be a positive integer")); 276 usage4(_("Port must be a positive integer"));
263 else 277 } else {
264 server_port = atoi(optarg); 278 server_port = atoi(optarg);
279 }
265 break; 280 break;
266 case 't': /* timeout */ 281 case 't': /* timeout */
267 if (!is_intnonneg(optarg)) 282 if (!is_intnonneg(optarg)) {
268 usage2(_("Timeout interval must be a positive integer"), optarg); 283 usage2(_("Timeout interval must be a positive integer"), optarg);
269 else 284 } else {
270 socket_timeout = atoi(optarg); 285 socket_timeout = atoi(optarg);
286 }
271 break; 287 break;
272 case 'u': /* udp */ 288 case 'u': /* udp */
273 use_udp = true; 289 use_udp = true;
@@ -277,8 +293,9 @@ int process_arguments(int argc, char **argv) {
277 option_char = optind; 293 option_char = optind;
278 if (server_address == NULL) { 294 if (server_address == NULL) {
279 if (argc > option_char) { 295 if (argc > option_char) {
280 if (!is_host(argv[option_char])) 296 if (!is_host(argv[option_char])) {
281 usage2(_("Invalid hostname/address"), optarg); 297 usage2(_("Invalid hostname/address"), optarg);
298 }
282 server_address = argv[option_char]; 299 server_address = argv[option_char];
283 } else { 300 } else {
284 usage4(_("Hostname was not supplied")); 301 usage4(_("Hostname was not supplied"));