diff options
-rw-r--r-- | plugins/check_mrtgtraf.c | 112 |
1 files changed, 52 insertions, 60 deletions
diff --git a/plugins/check_mrtgtraf.c b/plugins/check_mrtgtraf.c index fc720b0e..e5a2e2ad 100644 --- a/plugins/check_mrtgtraf.c +++ b/plugins/check_mrtgtraf.c | |||
@@ -36,8 +36,7 @@ const char *progname = "check_mrtgtraf"; | |||
36 | const char *copyright = "1999-2024"; | 36 | const char *copyright = "1999-2024"; |
37 | const char *email = "devel@monitoring-plugins.org"; | 37 | const char *email = "devel@monitoring-plugins.org"; |
38 | 38 | ||
39 | static int process_arguments(int, char **); | 39 | static int process_arguments(int /*argc*/, char ** /*argv*/); |
40 | static int validate_arguments(void); | ||
41 | static void print_help(void); | 40 | static void print_help(void); |
42 | void print_usage(void); | 41 | void print_usage(void); |
43 | 42 | ||
@@ -50,25 +49,6 @@ static unsigned long outgoing_warning_threshold = 0L; | |||
50 | static unsigned long outgoing_critical_threshold = 0L; | 49 | static unsigned long outgoing_critical_threshold = 0L; |
51 | 50 | ||
52 | int main(int argc, char **argv) { | 51 | int main(int argc, char **argv) { |
53 | int result = STATE_OK; | ||
54 | FILE *fp; | ||
55 | int line; | ||
56 | char input_buffer[MAX_INPUT_BUFFER]; | ||
57 | char *temp_buffer; | ||
58 | time_t current_time; | ||
59 | char *error_message; | ||
60 | time_t timestamp = 0L; | ||
61 | unsigned long average_incoming_rate = 0L; | ||
62 | unsigned long average_outgoing_rate = 0L; | ||
63 | unsigned long maximum_incoming_rate = 0L; | ||
64 | unsigned long maximum_outgoing_rate = 0L; | ||
65 | unsigned long incoming_rate = 0L; | ||
66 | unsigned long outgoing_rate = 0L; | ||
67 | double adjusted_incoming_rate = 0.0; | ||
68 | double adjusted_outgoing_rate = 0.0; | ||
69 | char incoming_speed_rating[8]; | ||
70 | char outgoing_speed_rating[8]; | ||
71 | |||
72 | setlocale(LC_ALL, ""); | 52 | setlocale(LC_ALL, ""); |
73 | bindtextdomain(PACKAGE, LOCALEDIR); | 53 | bindtextdomain(PACKAGE, LOCALEDIR); |
74 | textdomain(PACKAGE); | 54 | textdomain(PACKAGE); |
@@ -80,12 +60,18 @@ int main(int argc, char **argv) { | |||
80 | usage4(_("Could not parse arguments")); | 60 | usage4(_("Could not parse arguments")); |
81 | 61 | ||
82 | /* open the MRTG log file for reading */ | 62 | /* open the MRTG log file for reading */ |
83 | fp = fopen(log_file, "r"); | 63 | FILE *mrtg_log_file_ptr = fopen(log_file, "r"); |
84 | if (fp == NULL) | 64 | if (mrtg_log_file_ptr == NULL) |
85 | usage4(_("Unable to open MRTG log file")); | 65 | usage4(_("Unable to open MRTG log file")); |
86 | 66 | ||
87 | line = 0; | 67 | time_t timestamp = 0L; |
88 | while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, fp)) { | 68 | char input_buffer[MAX_INPUT_BUFFER]; |
69 | unsigned long average_incoming_rate = 0L; | ||
70 | unsigned long average_outgoing_rate = 0L; | ||
71 | unsigned long maximum_incoming_rate = 0L; | ||
72 | unsigned long maximum_outgoing_rate = 0L; | ||
73 | int line = 0; | ||
74 | while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, mrtg_log_file_ptr)) { | ||
89 | 75 | ||
90 | line++; | 76 | line++; |
91 | 77 | ||
@@ -99,7 +85,7 @@ int main(int argc, char **argv) { | |||
99 | break; | 85 | break; |
100 | 86 | ||
101 | /* grab the timestamp */ | 87 | /* grab the timestamp */ |
102 | temp_buffer = strtok(input_buffer, " "); | 88 | char *temp_buffer = strtok(input_buffer, " "); |
103 | timestamp = strtoul(temp_buffer, NULL, 10); | 89 | timestamp = strtoul(temp_buffer, NULL, 10); |
104 | 90 | ||
105 | /* grab the average incoming transfer rate */ | 91 | /* grab the average incoming transfer rate */ |
@@ -120,17 +106,20 @@ int main(int argc, char **argv) { | |||
120 | } | 106 | } |
121 | 107 | ||
122 | /* close the log file */ | 108 | /* close the log file */ |
123 | fclose(fp); | 109 | fclose(mrtg_log_file_ptr); |
124 | 110 | ||
125 | /* if we couldn't read enough data, return an unknown error */ | 111 | /* if we couldn't read enough data, return an unknown error */ |
126 | if (line <= 2) | 112 | if (line <= 2) |
127 | usage4(_("Unable to process MRTG log file")); | 113 | usage4(_("Unable to process MRTG log file")); |
128 | 114 | ||
129 | /* make sure the MRTG data isn't too old */ | 115 | /* make sure the MRTG data isn't too old */ |
116 | time_t current_time; | ||
130 | time(¤t_time); | 117 | time(¤t_time); |
131 | if ((expire_minutes > 0) && (current_time - timestamp) > (expire_minutes * 60)) | 118 | if ((expire_minutes > 0) && (current_time - timestamp) > (expire_minutes * 60)) |
132 | die(STATE_WARNING, _("MRTG data has expired (%d minutes old)\n"), (int)((current_time - timestamp) / 60)); | 119 | die(STATE_WARNING, _("MRTG data has expired (%d minutes old)\n"), (int)((current_time - timestamp) / 60)); |
133 | 120 | ||
121 | unsigned long incoming_rate = 0L; | ||
122 | unsigned long outgoing_rate = 0L; | ||
134 | /* else check the incoming/outgoing rates */ | 123 | /* else check the incoming/outgoing rates */ |
135 | if (use_average) { | 124 | if (use_average) { |
136 | incoming_rate = average_incoming_rate; | 125 | incoming_rate = average_incoming_rate; |
@@ -140,6 +129,8 @@ int main(int argc, char **argv) { | |||
140 | outgoing_rate = maximum_outgoing_rate; | 129 | outgoing_rate = maximum_outgoing_rate; |
141 | } | 130 | } |
142 | 131 | ||
132 | double adjusted_incoming_rate = 0.0; | ||
133 | char incoming_speed_rating[8]; | ||
143 | /* report incoming traffic in Bytes/sec */ | 134 | /* report incoming traffic in Bytes/sec */ |
144 | if (incoming_rate < 1024) { | 135 | if (incoming_rate < 1024) { |
145 | strcpy(incoming_speed_rating, "B"); | 136 | strcpy(incoming_speed_rating, "B"); |
@@ -158,6 +149,8 @@ int main(int argc, char **argv) { | |||
158 | adjusted_incoming_rate = (double)(incoming_rate / 1024.0 / 1024.0); | 149 | adjusted_incoming_rate = (double)(incoming_rate / 1024.0 / 1024.0); |
159 | } | 150 | } |
160 | 151 | ||
152 | double adjusted_outgoing_rate = 0.0; | ||
153 | char outgoing_speed_rating[8]; | ||
161 | /* report outgoing traffic in Bytes/sec */ | 154 | /* report outgoing traffic in Bytes/sec */ |
162 | if (outgoing_rate < 1024) { | 155 | if (outgoing_rate < 1024) { |
163 | strcpy(outgoing_speed_rating, "B"); | 156 | strcpy(outgoing_speed_rating, "B"); |
@@ -176,12 +169,14 @@ int main(int argc, char **argv) { | |||
176 | adjusted_outgoing_rate = (double)(outgoing_rate / 1024.0 / 1024.0); | 169 | adjusted_outgoing_rate = (double)(outgoing_rate / 1024.0 / 1024.0); |
177 | } | 170 | } |
178 | 171 | ||
172 | int result = STATE_OK; | ||
179 | if (incoming_rate > incoming_critical_threshold || outgoing_rate > outgoing_critical_threshold) { | 173 | if (incoming_rate > incoming_critical_threshold || outgoing_rate > outgoing_critical_threshold) { |
180 | result = STATE_CRITICAL; | 174 | result = STATE_CRITICAL; |
181 | } else if (incoming_rate > incoming_warning_threshold || outgoing_rate > outgoing_warning_threshold) { | 175 | } else if (incoming_rate > incoming_warning_threshold || outgoing_rate > outgoing_warning_threshold) { |
182 | result = STATE_WARNING; | 176 | result = STATE_WARNING; |
183 | } | 177 | } |
184 | 178 | ||
179 | char *error_message; | ||
185 | xasprintf(&error_message, _("%s. In = %0.1f %s/s, %s. Out = %0.1f %s/s|%s %s\n"), (use_average) ? _("Avg") : _("Max"), | 180 | xasprintf(&error_message, _("%s. In = %0.1f %s/s, %s. Out = %0.1f %s/s|%s %s\n"), (use_average) ? _("Avg") : _("Max"), |
186 | adjusted_incoming_rate, incoming_speed_rating, (use_average) ? _("Avg") : _("Max"), adjusted_outgoing_rate, | 181 | adjusted_incoming_rate, incoming_speed_rating, (use_average) ? _("Avg") : _("Max"), adjusted_outgoing_rate, |
187 | outgoing_speed_rating, | 182 | outgoing_speed_rating, |
@@ -197,9 +192,6 @@ int main(int argc, char **argv) { | |||
197 | 192 | ||
198 | /* process command-line arguments */ | 193 | /* process command-line arguments */ |
199 | int process_arguments(int argc, char **argv) { | 194 | int process_arguments(int argc, char **argv) { |
200 | int c; | ||
201 | |||
202 | int option = 0; | ||
203 | static struct option longopts[] = {{"filename", required_argument, 0, 'F'}, | 195 | static struct option longopts[] = {{"filename", required_argument, 0, 'F'}, |
204 | {"expires", required_argument, 0, 'e'}, | 196 | {"expires", required_argument, 0, 'e'}, |
205 | {"aggregation", required_argument, 0, 'a'}, | 197 | {"aggregation", required_argument, 0, 'a'}, |
@@ -212,22 +204,24 @@ int process_arguments(int argc, char **argv) { | |||
212 | if (argc < 2) | 204 | if (argc < 2) |
213 | return ERROR; | 205 | return ERROR; |
214 | 206 | ||
215 | for (c = 1; c < argc; c++) { | 207 | for (int i = 1; i < argc; i++) { |
216 | if (strcmp("-to", argv[c]) == 0) | 208 | if (strcmp("-to", argv[i]) == 0) |
217 | strcpy(argv[c], "-t"); | 209 | strcpy(argv[i], "-t"); |
218 | else if (strcmp("-wt", argv[c]) == 0) | 210 | else if (strcmp("-wt", argv[i]) == 0) |
219 | strcpy(argv[c], "-w"); | 211 | strcpy(argv[i], "-w"); |
220 | else if (strcmp("-ct", argv[c]) == 0) | 212 | else if (strcmp("-ct", argv[i]) == 0) |
221 | strcpy(argv[c], "-c"); | 213 | strcpy(argv[i], "-c"); |
222 | } | 214 | } |
223 | 215 | ||
216 | int option_char; | ||
217 | int option = 0; | ||
224 | while (1) { | 218 | while (1) { |
225 | c = getopt_long(argc, argv, "hVF:e:a:c:w:", longopts, &option); | 219 | option_char = getopt_long(argc, argv, "hVF:e:a:c:w:", longopts, &option); |
226 | 220 | ||
227 | if (c == -1 || c == EOF) | 221 | if (option_char == -1 || option_char == EOF) |
228 | break; | 222 | break; |
229 | 223 | ||
230 | switch (c) { | 224 | switch (option_char) { |
231 | case 'F': /* input file */ | 225 | case 'F': /* input file */ |
232 | log_file = optarg; | 226 | log_file = optarg; |
233 | break; | 227 | break; |
@@ -257,44 +251,42 @@ int process_arguments(int argc, char **argv) { | |||
257 | } | 251 | } |
258 | } | 252 | } |
259 | 253 | ||
260 | c = optind; | 254 | option_char = optind; |
261 | if (argc > c && log_file == NULL) { | 255 | if (argc > option_char && log_file == NULL) { |
262 | log_file = argv[c++]; | 256 | log_file = argv[option_char++]; |
263 | } | 257 | } |
264 | 258 | ||
265 | if (argc > c && expire_minutes == -1) { | 259 | if (argc > option_char && expire_minutes == -1) { |
266 | expire_minutes = atoi(argv[c++]); | 260 | expire_minutes = atoi(argv[option_char++]); |
267 | } | 261 | } |
268 | 262 | ||
269 | if (argc > c && strcmp(argv[c], "MAX") == 0) { | 263 | if (argc > option_char && strcmp(argv[option_char], "MAX") == 0) { |
270 | use_average = false; | 264 | use_average = false; |
271 | c++; | 265 | option_char++; |
272 | } else if (argc > c && strcmp(argv[c], "AVG") == 0) { | 266 | } else if (argc > option_char && strcmp(argv[option_char], "AVG") == 0) { |
273 | use_average = true; | 267 | use_average = true; |
274 | c++; | 268 | option_char++; |
275 | } | 269 | } |
276 | 270 | ||
277 | if (argc > c && incoming_warning_threshold == 0) { | 271 | if (argc > option_char && incoming_warning_threshold == 0) { |
278 | incoming_warning_threshold = strtoul(argv[c++], NULL, 10); | 272 | incoming_warning_threshold = strtoul(argv[option_char++], NULL, 10); |
279 | } | 273 | } |
280 | 274 | ||
281 | if (argc > c && incoming_critical_threshold == 0) { | 275 | if (argc > option_char && incoming_critical_threshold == 0) { |
282 | incoming_critical_threshold = strtoul(argv[c++], NULL, 10); | 276 | incoming_critical_threshold = strtoul(argv[option_char++], NULL, 10); |
283 | } | 277 | } |
284 | 278 | ||
285 | if (argc > c && outgoing_warning_threshold == 0) { | 279 | if (argc > option_char && outgoing_warning_threshold == 0) { |
286 | outgoing_warning_threshold = strtoul(argv[c++], NULL, 10); | 280 | outgoing_warning_threshold = strtoul(argv[option_char++], NULL, 10); |
287 | } | 281 | } |
288 | 282 | ||
289 | if (argc > c && outgoing_critical_threshold == 0) { | 283 | if (argc > option_char && outgoing_critical_threshold == 0) { |
290 | outgoing_critical_threshold = strtoul(argv[c++], NULL, 10); | 284 | outgoing_critical_threshold = strtoul(argv[option_char++], NULL, 10); |
291 | } | 285 | } |
292 | 286 | ||
293 | return validate_arguments(); | 287 | return OK; |
294 | } | 288 | } |
295 | 289 | ||
296 | int validate_arguments(void) { return OK; } | ||
297 | |||
298 | void print_help(void) { | 290 | void print_help(void) { |
299 | print_revision(progname, NP_VERSION); | 291 | print_revision(progname, NP_VERSION); |
300 | 292 | ||