diff options
author | RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> | 2024-10-31 11:26:54 (GMT) |
---|---|---|
committer | RincewindsHat <12514511+RincewindsHat@users.noreply.github.com> | 2024-10-31 11:26:54 (GMT) |
commit | 0062a32af18914ff26ca1714388734c66848663a (patch) | |
tree | b4a205827d6b861bc0c55ec85615262b0dbc654e /plugins | |
parent | e170dbef11954d1590c0c077688494bf0bc4d110 (diff) | |
download | monitoring-plugins-0062a32af18914ff26ca1714388734c66848663a.tar.gz |
check_mrtg: cosmetic fixes, linter fixes
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/check_mrtg.c | 101 |
1 files changed, 48 insertions, 53 deletions
diff --git a/plugins/check_mrtg.c b/plugins/check_mrtg.c index e82a0f9..632e66f 100644 --- a/plugins/check_mrtg.c +++ b/plugins/check_mrtg.c | |||
@@ -36,7 +36,7 @@ const char *email = "devel@monitoring-plugins.org"; | |||
36 | #include "common.h" | 36 | #include "common.h" |
37 | #include "utils.h" | 37 | #include "utils.h" |
38 | 38 | ||
39 | static int process_arguments(int, char **); | 39 | static int process_arguments(int /*argc*/, char ** /*argv*/); |
40 | static int validate_arguments(void); | 40 | static int validate_arguments(void); |
41 | static void print_help(void); | 41 | static void print_help(void); |
42 | void print_usage(void); | 42 | void print_usage(void); |
@@ -51,17 +51,6 @@ static char *label; | |||
51 | static char *units; | 51 | static char *units; |
52 | 52 | ||
53 | int main(int argc, char **argv) { | 53 | int main(int argc, char **argv) { |
54 | int result = STATE_OK; | ||
55 | FILE *fp; | ||
56 | int line; | ||
57 | char input_buffer[MAX_INPUT_BUFFER]; | ||
58 | char *temp_buffer; | ||
59 | time_t current_time; | ||
60 | time_t timestamp = 0L; | ||
61 | unsigned long average_value_rate = 0L; | ||
62 | unsigned long maximum_value_rate = 0L; | ||
63 | unsigned long rate = 0L; | ||
64 | |||
65 | setlocale(LC_ALL, ""); | 54 | setlocale(LC_ALL, ""); |
66 | bindtextdomain(PACKAGE, LOCALEDIR); | 55 | bindtextdomain(PACKAGE, LOCALEDIR); |
67 | textdomain(PACKAGE); | 56 | textdomain(PACKAGE); |
@@ -73,14 +62,18 @@ int main(int argc, char **argv) { | |||
73 | usage4(_("Could not parse arguments\n")); | 62 | usage4(_("Could not parse arguments\n")); |
74 | 63 | ||
75 | /* open the MRTG log file for reading */ | 64 | /* open the MRTG log file for reading */ |
76 | fp = fopen(log_file, "r"); | 65 | FILE *mtrg_log_file = fopen(log_file, "r"); |
77 | if (fp == NULL) { | 66 | if (mtrg_log_file == NULL) { |
78 | printf(_("Unable to open MRTG log file\n")); | 67 | printf(_("Unable to open MRTG log file\n")); |
79 | return STATE_UNKNOWN; | 68 | return STATE_UNKNOWN; |
80 | } | 69 | } |
81 | 70 | ||
82 | line = 0; | 71 | time_t timestamp = 0L; |
83 | while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, fp)) { | 72 | unsigned long average_value_rate = 0L; |
73 | unsigned long maximum_value_rate = 0L; | ||
74 | char input_buffer[MAX_INPUT_BUFFER]; | ||
75 | int line = 0; | ||
76 | while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, mtrg_log_file)) { | ||
84 | 77 | ||
85 | line++; | 78 | line++; |
86 | 79 | ||
@@ -93,7 +86,7 @@ int main(int argc, char **argv) { | |||
93 | break; | 86 | break; |
94 | 87 | ||
95 | /* grab the timestamp */ | 88 | /* grab the timestamp */ |
96 | temp_buffer = strtok(input_buffer, " "); | 89 | char *temp_buffer = strtok(input_buffer, " "); |
97 | timestamp = strtoul(temp_buffer, NULL, 10); | 90 | timestamp = strtoul(temp_buffer, NULL, 10); |
98 | 91 | ||
99 | /* grab the average value 1 rate */ | 92 | /* grab the average value 1 rate */ |
@@ -118,7 +111,7 @@ int main(int argc, char **argv) { | |||
118 | } | 111 | } |
119 | 112 | ||
120 | /* close the log file */ | 113 | /* close the log file */ |
121 | fclose(fp); | 114 | fclose(mtrg_log_file); |
122 | 115 | ||
123 | /* if we couldn't read enough data, return an unknown error */ | 116 | /* if we couldn't read enough data, return an unknown error */ |
124 | if (line <= 2) { | 117 | if (line <= 2) { |
@@ -127,18 +120,21 @@ int main(int argc, char **argv) { | |||
127 | } | 120 | } |
128 | 121 | ||
129 | /* make sure the MRTG data isn't too old */ | 122 | /* make sure the MRTG data isn't too old */ |
123 | time_t current_time; | ||
130 | time(¤t_time); | 124 | time(¤t_time); |
131 | if (expire_minutes > 0 && (current_time - timestamp) > (expire_minutes * 60)) { | 125 | if (expire_minutes > 0 && (current_time - timestamp) > (expire_minutes * 60)) { |
132 | printf(_("MRTG data has expired (%d minutes old)\n"), (int)((current_time - timestamp) / 60)); | 126 | printf(_("MRTG data has expired (%d minutes old)\n"), (int)((current_time - timestamp) / 60)); |
133 | return STATE_WARNING; | 127 | return STATE_WARNING; |
134 | } | 128 | } |
135 | 129 | ||
130 | unsigned long rate = 0L; | ||
136 | /* else check the incoming/outgoing rates */ | 131 | /* else check the incoming/outgoing rates */ |
137 | if (use_average) | 132 | if (use_average) |
138 | rate = average_value_rate; | 133 | rate = average_value_rate; |
139 | else | 134 | else |
140 | rate = maximum_value_rate; | 135 | rate = maximum_value_rate; |
141 | 136 | ||
137 | int result = STATE_OK; | ||
142 | if (rate > value_critical_threshold) | 138 | if (rate > value_critical_threshold) |
143 | result = STATE_CRITICAL; | 139 | result = STATE_CRITICAL; |
144 | else if (rate > value_warning_threshold) | 140 | else if (rate > value_warning_threshold) |
@@ -153,9 +149,6 @@ int main(int argc, char **argv) { | |||
153 | 149 | ||
154 | /* process command-line arguments */ | 150 | /* process command-line arguments */ |
155 | int process_arguments(int argc, char **argv) { | 151 | int process_arguments(int argc, char **argv) { |
156 | int c; | ||
157 | |||
158 | int option = 0; | ||
159 | static struct option longopts[] = { | 152 | static struct option longopts[] = { |
160 | {"logfile", required_argument, 0, 'F'}, {"expires", required_argument, 0, 'e'}, {"aggregation", required_argument, 0, 'a'}, | 153 | {"logfile", required_argument, 0, 'F'}, {"expires", required_argument, 0, 'e'}, {"aggregation", required_argument, 0, 'a'}, |
161 | {"variable", required_argument, 0, 'v'}, {"critical", required_argument, 0, 'c'}, {"warning", required_argument, 0, 'w'}, | 154 | {"variable", required_argument, 0, 'v'}, {"critical", required_argument, 0, 'c'}, {"warning", required_argument, 0, 'w'}, |
@@ -165,22 +158,24 @@ int process_arguments(int argc, char **argv) { | |||
165 | if (argc < 2) | 158 | if (argc < 2) |
166 | return ERROR; | 159 | return ERROR; |
167 | 160 | ||
168 | for (c = 1; c < argc; c++) { | 161 | for (int i = 1; i < argc; i++) { |
169 | if (strcmp("-to", argv[c]) == 0) | 162 | if (strcmp("-to", argv[i]) == 0) |
170 | strcpy(argv[c], "-t"); | 163 | strcpy(argv[i], "-t"); |
171 | else if (strcmp("-wt", argv[c]) == 0) | 164 | else if (strcmp("-wt", argv[i]) == 0) |
172 | strcpy(argv[c], "-w"); | 165 | strcpy(argv[i], "-w"); |
173 | else if (strcmp("-ct", argv[c]) == 0) | 166 | else if (strcmp("-ct", argv[i]) == 0) |
174 | strcpy(argv[c], "-c"); | 167 | strcpy(argv[i], "-c"); |
175 | } | 168 | } |
176 | 169 | ||
170 | int option_char; | ||
171 | int option = 0; | ||
177 | while (1) { | 172 | while (1) { |
178 | c = getopt_long(argc, argv, "hVF:e:a:v:c:w:l:u:", longopts, &option); | 173 | option_char = getopt_long(argc, argv, "hVF:e:a:v:c:w:l:u:", longopts, &option); |
179 | 174 | ||
180 | if (c == -1 || c == EOF) | 175 | if (option_char == -1 || option_char == EOF) |
181 | break; | 176 | break; |
182 | 177 | ||
183 | switch (c) { | 178 | switch (option_char) { |
184 | case 'F': /* input file */ | 179 | case 'F': /* input file */ |
185 | log_file = optarg; | 180 | log_file = optarg; |
186 | break; | 181 | break; |
@@ -221,48 +216,48 @@ int process_arguments(int argc, char **argv) { | |||
221 | } | 216 | } |
222 | } | 217 | } |
223 | 218 | ||
224 | c = optind; | 219 | option_char = optind; |
225 | if (log_file == NULL && argc > c) { | 220 | if (log_file == NULL && argc > option_char) { |
226 | log_file = argv[c++]; | 221 | log_file = argv[option_char++]; |
227 | } | 222 | } |
228 | 223 | ||
229 | if (expire_minutes <= 0 && argc > c) { | 224 | if (expire_minutes <= 0 && argc > option_char) { |
230 | if (is_intpos(argv[c])) | 225 | if (is_intpos(argv[option_char])) |
231 | expire_minutes = atoi(argv[c++]); | 226 | expire_minutes = atoi(argv[option_char++]); |
232 | else | 227 | else |
233 | die(STATE_UNKNOWN, _("%s is not a valid expiration time\nUse '%s -h' for additional help\n"), argv[c], progname); | 228 | die(STATE_UNKNOWN, _("%s is not a valid expiration time\nUse '%s -h' for additional help\n"), argv[option_char], progname); |
234 | } | 229 | } |
235 | 230 | ||
236 | if (argc > c && strcmp(argv[c], "MAX") == 0) { | 231 | if (argc > option_char && strcmp(argv[option_char], "MAX") == 0) { |
237 | use_average = false; | 232 | use_average = false; |
238 | c++; | 233 | option_char++; |
239 | } else if (argc > c && strcmp(argv[c], "AVG") == 0) { | 234 | } else if (argc > option_char && strcmp(argv[option_char], "AVG") == 0) { |
240 | use_average = true; | 235 | use_average = true; |
241 | c++; | 236 | option_char++; |
242 | } | 237 | } |
243 | 238 | ||
244 | if (argc > c && variable_number == -1) { | 239 | if (argc > option_char && variable_number == -1) { |
245 | variable_number = atoi(argv[c++]); | 240 | variable_number = atoi(argv[option_char++]); |
246 | if (variable_number < 1 || variable_number > 2) { | 241 | if (variable_number < 1 || variable_number > 2) { |
247 | printf("%s :", argv[c]); | 242 | printf("%s :", argv[option_char]); |
248 | usage(_("Invalid variable number\n")); | 243 | usage(_("Invalid variable number\n")); |
249 | } | 244 | } |
250 | } | 245 | } |
251 | 246 | ||
252 | if (argc > c && value_warning_threshold == 0) { | 247 | if (argc > option_char && value_warning_threshold == 0) { |
253 | value_warning_threshold = strtoul(argv[c++], NULL, 10); | 248 | value_warning_threshold = strtoul(argv[option_char++], NULL, 10); |
254 | } | 249 | } |
255 | 250 | ||
256 | if (argc > c && value_critical_threshold == 0) { | 251 | if (argc > option_char && value_critical_threshold == 0) { |
257 | value_critical_threshold = strtoul(argv[c++], NULL, 10); | 252 | value_critical_threshold = strtoul(argv[option_char++], NULL, 10); |
258 | } | 253 | } |
259 | 254 | ||
260 | if (argc > c && strlen(label) == 0) { | 255 | if (argc > option_char && strlen(label) == 0) { |
261 | label = argv[c++]; | 256 | label = argv[option_char++]; |
262 | } | 257 | } |
263 | 258 | ||
264 | if (argc > c && strlen(units) == 0) { | 259 | if (argc > option_char && strlen(units) == 0) { |
265 | units = argv[c++]; | 260 | units = argv[option_char++]; |
266 | } | 261 | } |
267 | 262 | ||
268 | return validate_arguments(); | 263 | return validate_arguments(); |