summaryrefslogtreecommitdiffstats
path: root/plugins/check_mrtgtraf.c
diff options
context:
space:
mode:
authorLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2025-03-11 13:23:32 +0100
committerGitHub <noreply@github.com>2025-03-11 13:23:32 +0100
commiteb31935abe12aee73178a1ce043f4979c885d158 (patch)
tree2228a152b13ca14635717d8a10df6280f2345a49 /plugins/check_mrtgtraf.c
parentd087a8820e03ad0b5aef0a904eb417aaca0699cb (diff)
parent67e41f1d57da40b6407da5434017d86abb7ae3bb (diff)
downloadmonitoring-plugins-eb31935abe12aee73178a1ce043f4979c885d158.tar.gz
Merge pull request #2089 from RincewindsHat/refactor/check_mrtgraf
Refactor/check mrtgraf
Diffstat (limited to 'plugins/check_mrtgtraf.c')
-rw-r--r--plugins/check_mrtgtraf.c134
1 files changed, 74 insertions, 60 deletions
diff --git a/plugins/check_mrtgtraf.c b/plugins/check_mrtgtraf.c
index e5a2e2ad..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,13 +54,18 @@ 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"));
60 }
61
62 const check_mrtgraf_config config = tmp_config.config;
61 63
62 /* open the MRTG log file for reading */ 64 /* open the MRTG log file for reading */
63 FILE *mrtg_log_file_ptr = fopen(log_file, "r"); 65 FILE *mrtg_log_file_ptr = fopen(config.log_file, "r");
64 if (mrtg_log_file_ptr == NULL) 66 if (mrtg_log_file_ptr == NULL) {
65 usage4(_("Unable to open MRTG log file")); 67 usage4(_("Unable to open MRTG log file"));
68 }
66 69
67 time_t timestamp = 0L; 70 time_t timestamp = 0L;
68 char input_buffer[MAX_INPUT_BUFFER]; 71 char input_buffer[MAX_INPUT_BUFFER];
@@ -76,13 +79,15 @@ int main(int argc, char **argv) {
76 line++; 79 line++;
77 80
78 /* skip the first line of the log file */ 81 /* skip the first line of the log file */
79 if (line == 1) 82 if (line == 1) {
80 continue; 83 continue;
84 }
81 85
82 /* break out of read loop */ 86 /* break out of read loop */
83 /* if we've passed the number of entries we want to read */ 87 /* if we've passed the number of entries we want to read */
84 if (line > 2) 88 if (line > 2) {
85 break; 89 break;
90 }
86 91
87 /* grab the timestamp */ 92 /* grab the timestamp */
88 char *temp_buffer = strtok(input_buffer, " "); 93 char *temp_buffer = strtok(input_buffer, " ");
@@ -109,19 +114,21 @@ int main(int argc, char **argv) {
109 fclose(mrtg_log_file_ptr); 114 fclose(mrtg_log_file_ptr);
110 115
111 /* if we couldn't read enough data, return an unknown error */ 116 /* if we couldn't read enough data, return an unknown error */
112 if (line <= 2) 117 if (line <= 2) {
113 usage4(_("Unable to process MRTG log file")); 118 usage4(_("Unable to process MRTG log file"));
119 }
114 120
115 /* make sure the MRTG data isn't too old */ 121 /* make sure the MRTG data isn't too old */
116 time_t current_time; 122 time_t current_time;
117 time(&current_time); 123 time(&current_time);
118 if ((expire_minutes > 0) && (current_time - timestamp) > (expire_minutes * 60)) 124 if ((config.expire_minutes > 0) && (current_time - timestamp) > (config.expire_minutes * 60)) {
119 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));
126 }
120 127
121 unsigned long incoming_rate = 0L; 128 unsigned long incoming_rate = 0L;
122 unsigned long outgoing_rate = 0L; 129 unsigned long outgoing_rate = 0L;
123 /* else check the incoming/outgoing rates */ 130 /* else check the incoming/outgoing rates */
124 if (use_average) { 131 if (config.use_average) {
125 incoming_rate = average_incoming_rate; 132 incoming_rate = average_incoming_rate;
126 outgoing_rate = average_outgoing_rate; 133 outgoing_rate = average_outgoing_rate;
127 } else { 134 } else {
@@ -166,24 +173,26 @@ int main(int argc, char **argv) {
166 /* report outgoing traffic in MBytes/sec */ 173 /* report outgoing traffic in MBytes/sec */
167 else { 174 else {
168 strcpy(outgoing_speed_rating, "MB"); 175 strcpy(outgoing_speed_rating, "MB");
169 adjusted_outgoing_rate = (double)(outgoing_rate / 1024.0 / 1024.0); 176 adjusted_outgoing_rate = (outgoing_rate / 1024.0 / 1024.0);
170 } 177 }
171 178
172 int result = STATE_OK; 179 int result = STATE_OK;
173 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) {
174 result = STATE_CRITICAL; 181 result = STATE_CRITICAL;
175 } 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) {
176 result = STATE_WARNING; 183 result = STATE_WARNING;
177 } 184 }
178 185
179 char *error_message; 186 char *error_message;
180 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"),
181 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,
182 outgoing_speed_rating, 189 outgoing_speed_rating,
183 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,
184 (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,
185 fperfdata("out", adjusted_outgoing_rate, outgoing_speed_rating, (int)outgoing_warning_threshold, outgoing_warning_threshold, 192 true, 0, false, 0),
186 (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));
187 196
188 printf(_("Traffic %s - %s\n"), state_text(result), error_message); 197 printf(_("Traffic %s - %s\n"), state_text(result), error_message);
189 198
@@ -191,7 +200,7 @@ int main(int argc, char **argv) {
191} 200}
192 201
193/* process command-line arguments */ 202/* process command-line arguments */
194int process_arguments(int argc, char **argv) { 203check_mrtgraf_config_wrapper process_arguments(int argc, char **argv) {
195 static struct option longopts[] = {{"filename", required_argument, 0, 'F'}, 204 static struct option longopts[] = {{"filename", required_argument, 0, 'F'},
196 {"expires", required_argument, 0, 'e'}, 205 {"expires", required_argument, 0, 'e'},
197 {"aggregation", required_argument, 0, 'a'}, 206 {"aggregation", required_argument, 0, 'a'},
@@ -201,44 +210,49 @@ int process_arguments(int argc, char **argv) {
201 {"help", no_argument, 0, 'h'}, 210 {"help", no_argument, 0, 'h'},
202 {0, 0, 0, 0}}; 211 {0, 0, 0, 0}};
203 212
204 if (argc < 2) 213 check_mrtgraf_config_wrapper result = {
205 return ERROR; 214 .errorcode = OK,
215 .config = check_mrtgraf_config_init(),
216 };
217 if (argc < 2) {
218 result.errorcode = ERROR;
219 return result;
220 }
206 221
207 for (int i = 1; i < argc; i++) { 222 for (int i = 1; i < argc; i++) {
208 if (strcmp("-to", argv[i]) == 0) 223 if (strcmp("-to", argv[i]) == 0) {
209 strcpy(argv[i], "-t"); 224 strcpy(argv[i], "-t");
210 else if (strcmp("-wt", argv[i]) == 0) 225 } else if (strcmp("-wt", argv[i]) == 0) {
211 strcpy(argv[i], "-w"); 226 strcpy(argv[i], "-w");
212 else if (strcmp("-ct", argv[i]) == 0) 227 } else if (strcmp("-ct", argv[i]) == 0) {
213 strcpy(argv[i], "-c"); 228 strcpy(argv[i], "-c");
229 }
214 } 230 }
215 231
216 int option_char; 232 int option_char;
217 int option = 0; 233 int option = 0;
218 while (1) { 234 while (true) {
219 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);
220 236
221 if (option_char == -1 || option_char == EOF) 237 if (option_char == -1 || option_char == EOF) {
222 break; 238 break;
239 }
223 240
224 switch (option_char) { 241 switch (option_char) {
225 case 'F': /* input file */ 242 case 'F': /* input file */
226 log_file = optarg; 243 result.config.log_file = optarg;
227 break; 244 break;
228 case 'e': /* expiration time */ 245 case 'e': /* expiration time */
229 expire_minutes = atoi(optarg); 246 result.config.expire_minutes = atoi(optarg);
230 break; 247 break;
231 case 'a': /* aggregation (AVE or MAX) */ 248 case 'a': /* aggregation (AVE or MAX) */
232 if (!strcmp(optarg, "MAX")) 249 result.config.use_average = (bool)(strcmp(optarg, "MAX"));
233 use_average = false;
234 else
235 use_average = true;
236 break; 250 break;
237 case 'c': /* warning threshold */ 251 case 'c': /* warning threshold */
238 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);
239 break; 253 break;
240 case 'w': /* critical threshold */ 254 case 'w': /* critical threshold */
241 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);
242 break; 256 break;
243 case 'V': /* version */ 257 case 'V': /* version */
244 print_revision(progname, NP_VERSION); 258 print_revision(progname, NP_VERSION);
@@ -252,39 +266,39 @@ int process_arguments(int argc, char **argv) {
252 } 266 }
253 267
254 option_char = optind; 268 option_char = optind;
255 if (argc > option_char && log_file == NULL) { 269 if (argc > option_char && result.config.log_file == NULL) {
256 log_file = argv[option_char++]; 270 result.config.log_file = argv[option_char++];
257 } 271 }
258 272
259 if (argc > option_char && expire_minutes == -1) { 273 if (argc > option_char && result.config.expire_minutes == -1) {
260 expire_minutes = atoi(argv[option_char++]); 274 result.config.expire_minutes = atoi(argv[option_char++]);
261 } 275 }
262 276
263 if (argc > option_char && strcmp(argv[option_char], "MAX") == 0) { 277 if (argc > option_char && strcmp(argv[option_char], "MAX") == 0) {
264 use_average = false; 278 result.config.use_average = false;
265 option_char++; 279 option_char++;
266 } else if (argc > option_char && strcmp(argv[option_char], "AVG") == 0) { 280 } else if (argc > option_char && strcmp(argv[option_char], "AVG") == 0) {
267 use_average = true; 281 result.config.use_average = true;
268 option_char++; 282 option_char++;
269 } 283 }
270 284
271 if (argc > option_char && incoming_warning_threshold == 0) { 285 if (argc > option_char && result.config.incoming_warning_threshold == 0) {
272 incoming_warning_threshold = strtoul(argv[option_char++], NULL, 10); 286 result.config.incoming_warning_threshold = strtoul(argv[option_char++], NULL, 10);
273 } 287 }
274 288
275 if (argc > option_char && incoming_critical_threshold == 0) { 289 if (argc > option_char && result.config.incoming_critical_threshold == 0) {
276 incoming_critical_threshold = strtoul(argv[option_char++], NULL, 10); 290 result.config.incoming_critical_threshold = strtoul(argv[option_char++], NULL, 10);
277 } 291 }
278 292
279 if (argc > option_char && outgoing_warning_threshold == 0) { 293 if (argc > option_char && result.config.outgoing_warning_threshold == 0) {
280 outgoing_warning_threshold = strtoul(argv[option_char++], NULL, 10); 294 result.config.outgoing_warning_threshold = strtoul(argv[option_char++], NULL, 10);
281 } 295 }
282 296
283 if (argc > option_char && outgoing_critical_threshold == 0) { 297 if (argc > option_char && result.config.outgoing_critical_threshold == 0) {
284 outgoing_critical_threshold = strtoul(argv[option_char++], NULL, 10); 298 result.config.outgoing_critical_threshold = strtoul(argv[option_char++], NULL, 10);
285 } 299 }
286 300
287 return OK; 301 return result;
288} 302}
289 303
290void print_help(void) { 304void print_help(void) {