summaryrefslogtreecommitdiffstats
path: root/plugins
diff options
context:
space:
mode:
authorLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2025-03-11 12:53:48 +0100
committerLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2025-03-11 12:53:48 +0100
commit94f81f6fc78003d4f6f7d75789e3c4b364747d8b (patch)
tree1d640702f24ba1da63aa807cda9696088ff2b30e /plugins
parent4d81e7942081834a5ef5334ae840d9f2843a4c5b (diff)
downloadmonitoring-plugins-94f81f6fc78003d4f6f7d75789e3c4b364747d8b.tar.gz
Refactor check_mrtgraf
Diffstat (limited to 'plugins')
-rw-r--r--plugins/Makefile.am1
-rw-r--r--plugins/check_mrtgraf.d/config.h30
-rw-r--r--plugins/check_mrtgtraf.c108
3 files changed, 87 insertions, 52 deletions
diff --git a/plugins/Makefile.am b/plugins/Makefile.am
index 9d310a15..38e2ed28 100644
--- a/plugins/Makefile.am
+++ b/plugins/Makefile.am
@@ -55,6 +55,7 @@ EXTRA_DIST = t \
55 check_dbi.d \ 55 check_dbi.d \
56 check_ssh.d \ 56 check_ssh.d \
57 check_dns.d \ 57 check_dns.d \
58 check_mrtgraf.d \
58 check_apt.d \ 59 check_apt.d \
59 check_by_ssh.d \ 60 check_by_ssh.d \
60 check_smtp.d \ 61 check_smtp.d \
diff --git a/plugins/check_mrtgraf.d/config.h b/plugins/check_mrtgraf.d/config.h
new file mode 100644
index 00000000..6d949b50
--- /dev/null
+++ b/plugins/check_mrtgraf.d/config.h
@@ -0,0 +1,30 @@
1#pragma once
2
3#include "../../config.h"
4#include <stddef.h>
5#include <stdlib.h>
6
7typedef struct {
8 char *log_file;
9 int expire_minutes;
10 bool use_average;
11 unsigned long incoming_warning_threshold;
12 unsigned long incoming_critical_threshold;
13 unsigned long outgoing_warning_threshold;
14 unsigned long outgoing_critical_threshold;
15
16} check_mrtgraf_config;
17
18check_mrtgraf_config check_mrtgraf_config_init() {
19 check_mrtgraf_config tmp = {
20 .log_file = NULL,
21 .expire_minutes = -1,
22 .use_average = true,
23
24 .incoming_warning_threshold = 0,
25 .incoming_critical_threshold = 0,
26 .outgoing_warning_threshold = 0,
27 .outgoing_critical_threshold = 0,
28 };
29 return tmp;
30}
diff --git a/plugins/check_mrtgtraf.c b/plugins/check_mrtgtraf.c
index 78dcda0d..c9e26099 100644
--- a/plugins/check_mrtgtraf.c
+++ b/plugins/check_mrtgtraf.c
@@ -29,25 +29,23 @@
29 * 29 *
30 *****************************************************************************/ 30 *****************************************************************************/
31 31
32#include "common.h"
33#include "utils.h"
34
35const char *progname = "check_mrtgtraf"; 32const char *progname = "check_mrtgtraf";
36const char *copyright = "1999-2024"; 33const char *copyright = "1999-2024";
37const char *email = "devel@monitoring-plugins.org"; 34const char *email = "devel@monitoring-plugins.org";
38 35
39static int process_arguments(int /*argc*/, char ** /*argv*/); 36#include "check_mrtgraf.d/config.h"
37#include "common.h"
38#include "utils.h"
39
40typedef struct {
41 int errorcode;
42 check_mrtgraf_config config;
43} check_mrtgraf_config_wrapper;
44
45static check_mrtgraf_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/);
40static void print_help(void); 46static void print_help(void);
41void print_usage(void); 47void print_usage(void);
42 48
43static char *log_file = NULL;
44static int expire_minutes = -1;
45static bool use_average = true;
46static unsigned long incoming_warning_threshold = 0L;
47static unsigned long incoming_critical_threshold = 0L;
48static unsigned long outgoing_warning_threshold = 0L;
49static unsigned long outgoing_critical_threshold = 0L;
50
51int main(int argc, char **argv) { 49int main(int argc, char **argv) {
52 setlocale(LC_ALL, ""); 50 setlocale(LC_ALL, "");
53 bindtextdomain(PACKAGE, LOCALEDIR); 51 bindtextdomain(PACKAGE, LOCALEDIR);
@@ -56,12 +54,15 @@ int main(int argc, char **argv) {
56 /* Parse extra opts if any */ 54 /* Parse extra opts if any */
57 argv = np_extra_opts(&argc, argv, progname); 55 argv = np_extra_opts(&argc, argv, progname);
58 56
59 if (process_arguments(argc, argv) == ERROR) { 57 check_mrtgraf_config_wrapper tmp_config = process_arguments(argc, argv);
58 if (tmp_config.errorcode == ERROR) {
60 usage4(_("Could not parse arguments")); 59 usage4(_("Could not parse arguments"));
61 } 60 }
62 61
62 const check_mrtgraf_config config = tmp_config.config;
63
63 /* open the MRTG log file for reading */ 64 /* open the MRTG log file for reading */
64 FILE *mrtg_log_file_ptr = fopen(log_file, "r"); 65 FILE *mrtg_log_file_ptr = fopen(config.log_file, "r");
65 if (mrtg_log_file_ptr == NULL) { 66 if (mrtg_log_file_ptr == NULL) {
66 usage4(_("Unable to open MRTG log file")); 67 usage4(_("Unable to open MRTG log file"));
67 } 68 }
@@ -120,14 +121,14 @@ int main(int argc, char **argv) {
120 /* make sure the MRTG data isn't too old */ 121 /* make sure the MRTG data isn't too old */
121 time_t current_time; 122 time_t current_time;
122 time(&current_time); 123 time(&current_time);
123 if ((expire_minutes > 0) && (current_time - timestamp) > (expire_minutes * 60)) { 124 if ((config.expire_minutes > 0) && (current_time - timestamp) > (config.expire_minutes * 60)) {
124 die(STATE_WARNING, _("MRTG data has expired (%d minutes old)\n"), (int)((current_time - timestamp) / 60)); 125 die(STATE_WARNING, _("MRTG data has expired (%d minutes old)\n"), (int)((current_time - timestamp) / 60));
125 } 126 }
126 127
127 unsigned long incoming_rate = 0L; 128 unsigned long incoming_rate = 0L;
128 unsigned long outgoing_rate = 0L; 129 unsigned long outgoing_rate = 0L;
129 /* else check the incoming/outgoing rates */ 130 /* else check the incoming/outgoing rates */
130 if (use_average) { 131 if (config.use_average) {
131 incoming_rate = average_incoming_rate; 132 incoming_rate = average_incoming_rate;
132 outgoing_rate = average_outgoing_rate; 133 outgoing_rate = average_outgoing_rate;
133 } else { 134 } else {
@@ -172,24 +173,26 @@ int main(int argc, char **argv) {
172 /* report outgoing traffic in MBytes/sec */ 173 /* report outgoing traffic in MBytes/sec */
173 else { 174 else {
174 strcpy(outgoing_speed_rating, "MB"); 175 strcpy(outgoing_speed_rating, "MB");
175 adjusted_outgoing_rate = (double)(outgoing_rate / 1024.0 / 1024.0); 176 adjusted_outgoing_rate = (outgoing_rate / 1024.0 / 1024.0);
176 } 177 }
177 178
178 int result = STATE_OK; 179 int result = STATE_OK;
179 if (incoming_rate > incoming_critical_threshold || outgoing_rate > outgoing_critical_threshold) { 180 if (incoming_rate > config.incoming_critical_threshold || outgoing_rate > config.outgoing_critical_threshold) {
180 result = STATE_CRITICAL; 181 result = STATE_CRITICAL;
181 } else if (incoming_rate > incoming_warning_threshold || outgoing_rate > outgoing_warning_threshold) { 182 } else if (incoming_rate > config.incoming_warning_threshold || outgoing_rate > config.outgoing_warning_threshold) {
182 result = STATE_WARNING; 183 result = STATE_WARNING;
183 } 184 }
184 185
185 char *error_message; 186 char *error_message;
186 xasprintf(&error_message, _("%s. In = %0.1f %s/s, %s. Out = %0.1f %s/s|%s %s\n"), (use_average) ? _("Avg") : _("Max"), 187 xasprintf(&error_message, _("%s. In = %0.1f %s/s, %s. Out = %0.1f %s/s|%s %s\n"), (config.use_average) ? _("Avg") : _("Max"),
187 adjusted_incoming_rate, incoming_speed_rating, (use_average) ? _("Avg") : _("Max"), adjusted_outgoing_rate, 188 adjusted_incoming_rate, incoming_speed_rating, (config.use_average) ? _("Avg") : _("Max"), adjusted_outgoing_rate,
188 outgoing_speed_rating, 189 outgoing_speed_rating,
189 fperfdata("in", adjusted_incoming_rate, incoming_speed_rating, (int)incoming_warning_threshold, incoming_warning_threshold, 190 fperfdata("in", adjusted_incoming_rate, incoming_speed_rating, (int)config.incoming_warning_threshold,
190 (int)incoming_critical_threshold, incoming_critical_threshold, true, 0, false, 0), 191 config.incoming_warning_threshold, (int)config.incoming_critical_threshold, config.incoming_critical_threshold,
191 fperfdata("out", adjusted_outgoing_rate, outgoing_speed_rating, (int)outgoing_warning_threshold, outgoing_warning_threshold, 192 true, 0, false, 0),
192 (int)outgoing_critical_threshold, outgoing_critical_threshold, true, 0, false, 0)); 193 fperfdata("out", adjusted_outgoing_rate, outgoing_speed_rating, (int)config.outgoing_warning_threshold,
194 config.outgoing_warning_threshold, (int)config.outgoing_critical_threshold, config.outgoing_critical_threshold,
195 true, 0, false, 0));
193 196
194 printf(_("Traffic %s - %s\n"), state_text(result), error_message); 197 printf(_("Traffic %s - %s\n"), state_text(result), error_message);
195 198
@@ -197,7 +200,7 @@ int main(int argc, char **argv) {
197} 200}
198 201
199/* process command-line arguments */ 202/* process command-line arguments */
200int process_arguments(int argc, char **argv) { 203check_mrtgraf_config_wrapper process_arguments(int argc, char **argv) {
201 static struct option longopts[] = {{"filename", required_argument, 0, 'F'}, 204 static struct option longopts[] = {{"filename", required_argument, 0, 'F'},
202 {"expires", required_argument, 0, 'e'}, 205 {"expires", required_argument, 0, 'e'},
203 {"aggregation", required_argument, 0, 'a'}, 206 {"aggregation", required_argument, 0, 'a'},
@@ -207,8 +210,13 @@ int process_arguments(int argc, char **argv) {
207 {"help", no_argument, 0, 'h'}, 210 {"help", no_argument, 0, 'h'},
208 {0, 0, 0, 0}}; 211 {0, 0, 0, 0}};
209 212
213 check_mrtgraf_config_wrapper result = {
214 .errorcode = OK,
215 .config = check_mrtgraf_config_init(),
216 };
210 if (argc < 2) { 217 if (argc < 2) {
211 return ERROR; 218 result.errorcode = ERROR;
219 return result;
212 } 220 }
213 221
214 for (int i = 1; i < argc; i++) { 222 for (int i = 1; i < argc; i++) {
@@ -223,7 +231,7 @@ int process_arguments(int argc, char **argv) {
223 231
224 int option_char; 232 int option_char;
225 int option = 0; 233 int option = 0;
226 while (1) { 234 while (true) {
227 option_char = getopt_long(argc, argv, "hVF:e:a:c:w:", longopts, &option); 235 option_char = getopt_long(argc, argv, "hVF:e:a:c:w:", longopts, &option);
228 236
229 if (option_char == -1 || option_char == EOF) { 237 if (option_char == -1 || option_char == EOF) {
@@ -232,23 +240,19 @@ int process_arguments(int argc, char **argv) {
232 240
233 switch (option_char) { 241 switch (option_char) {
234 case 'F': /* input file */ 242 case 'F': /* input file */
235 log_file = optarg; 243 result.config.log_file = optarg;
236 break; 244 break;
237 case 'e': /* expiration time */ 245 case 'e': /* expiration time */
238 expire_minutes = atoi(optarg); 246 result.config.expire_minutes = atoi(optarg);
239 break; 247 break;
240 case 'a': /* aggregation (AVE or MAX) */ 248 case 'a': /* aggregation (AVE or MAX) */
241 if (!strcmp(optarg, "MAX")) { 249 result.config.use_average = (bool)(strcmp(optarg, "MAX"));
242 use_average = false;
243 } else {
244 use_average = true;
245 }
246 break; 250 break;
247 case 'c': /* warning threshold */ 251 case 'c': /* warning threshold */
248 sscanf(optarg, "%lu,%lu", &incoming_critical_threshold, &outgoing_critical_threshold); 252 sscanf(optarg, "%lu,%lu", &result.config.incoming_critical_threshold, &result.config.outgoing_critical_threshold);
249 break; 253 break;
250 case 'w': /* critical threshold */ 254 case 'w': /* critical threshold */
251 sscanf(optarg, "%lu,%lu", &incoming_warning_threshold, &outgoing_warning_threshold); 255 sscanf(optarg, "%lu,%lu", &result.config.incoming_warning_threshold, &result.config.outgoing_warning_threshold);
252 break; 256 break;
253 case 'V': /* version */ 257 case 'V': /* version */
254 print_revision(progname, NP_VERSION); 258 print_revision(progname, NP_VERSION);
@@ -262,39 +266,39 @@ int process_arguments(int argc, char **argv) {
262 } 266 }
263 267
264 option_char = optind; 268 option_char = optind;
265 if (argc > option_char && log_file == NULL) { 269 if (argc > option_char && result.config.log_file == NULL) {
266 log_file = argv[option_char++]; 270 result.config.log_file = argv[option_char++];
267 } 271 }
268 272
269 if (argc > option_char && expire_minutes == -1) { 273 if (argc > option_char && result.config.expire_minutes == -1) {
270 expire_minutes = atoi(argv[option_char++]); 274 result.config.expire_minutes = atoi(argv[option_char++]);
271 } 275 }
272 276
273 if (argc > option_char && strcmp(argv[option_char], "MAX") == 0) { 277 if (argc > option_char && strcmp(argv[option_char], "MAX") == 0) {
274 use_average = false; 278 result.config.use_average = false;
275 option_char++; 279 option_char++;
276 } else if (argc > option_char && strcmp(argv[option_char], "AVG") == 0) { 280 } else if (argc > option_char && strcmp(argv[option_char], "AVG") == 0) {
277 use_average = true; 281 result.config.use_average = true;
278 option_char++; 282 option_char++;
279 } 283 }
280 284
281 if (argc > option_char && incoming_warning_threshold == 0) { 285 if (argc > option_char && result.config.incoming_warning_threshold == 0) {
282 incoming_warning_threshold = strtoul(argv[option_char++], NULL, 10); 286 result.config.incoming_warning_threshold = strtoul(argv[option_char++], NULL, 10);
283 } 287 }
284 288
285 if (argc > option_char && incoming_critical_threshold == 0) { 289 if (argc > option_char && result.config.incoming_critical_threshold == 0) {
286 incoming_critical_threshold = strtoul(argv[option_char++], NULL, 10); 290 result.config.incoming_critical_threshold = strtoul(argv[option_char++], NULL, 10);
287 } 291 }
288 292
289 if (argc > option_char && outgoing_warning_threshold == 0) { 293 if (argc > option_char && result.config.outgoing_warning_threshold == 0) {
290 outgoing_warning_threshold = strtoul(argv[option_char++], NULL, 10); 294 result.config.outgoing_warning_threshold = strtoul(argv[option_char++], NULL, 10);
291 } 295 }
292 296
293 if (argc > option_char && outgoing_critical_threshold == 0) { 297 if (argc > option_char && result.config.outgoing_critical_threshold == 0) {
294 outgoing_critical_threshold = strtoul(argv[option_char++], NULL, 10); 298 result.config.outgoing_critical_threshold = strtoul(argv[option_char++], NULL, 10);
295 } 299 }
296 300
297 return OK; 301 return result;
298} 302}
299 303
300void print_help(void) { 304void print_help(void) {