summaryrefslogtreecommitdiffstats
path: root/plugins/check_mrtg.c
diff options
context:
space:
mode:
authorLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2025-03-11 12:16:43 +0100
committerLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2025-03-11 12:16:43 +0100
commitb3cb1bb45ae30ccf0e1266022c96ee6c24dfe754 (patch)
treeb724a25d04677e7451fdc05bd8f16b7ee7627a80 /plugins/check_mrtg.c
parent74c3f4811ccc10911cdeff5cd793a8f3a7dd74e5 (diff)
downloadmonitoring-plugins-b3cb1bb45ae30ccf0e1266022c96ee6c24dfe754.tar.gz
check_mrtg: clang-format
Diffstat (limited to 'plugins/check_mrtg.c')
-rw-r--r--plugins/check_mrtg.c66
1 files changed, 42 insertions, 24 deletions
diff --git a/plugins/check_mrtg.c b/plugins/check_mrtg.c
index 632e66fb..e355fa11 100644
--- a/plugins/check_mrtg.c
+++ b/plugins/check_mrtg.c
@@ -58,8 +58,9 @@ int main(int argc, char **argv) {
58 /* Parse extra opts if any */ 58 /* Parse extra opts if any */
59 argv = np_extra_opts(&argc, argv, progname); 59 argv = np_extra_opts(&argc, argv, progname);
60 60
61 if (process_arguments(argc, argv) == ERROR) 61 if (process_arguments(argc, argv) == ERROR) {
62 usage4(_("Could not parse arguments\n")); 62 usage4(_("Could not parse arguments\n"));
63 }
63 64
64 /* open the MRTG log file for reading */ 65 /* open the MRTG log file for reading */
65 FILE *mtrg_log_file = fopen(log_file, "r"); 66 FILE *mtrg_log_file = fopen(log_file, "r");
@@ -78,12 +79,14 @@ int main(int argc, char **argv) {
78 line++; 79 line++;
79 80
80 /* skip the first line of the log file */ 81 /* skip the first line of the log file */
81 if (line == 1) 82 if (line == 1) {
82 continue; 83 continue;
84 }
83 85
84 /* break out of read loop if we've passed the number of entries we want to read */ 86 /* break out of read loop if we've passed the number of entries we want to read */
85 if (line > 2) 87 if (line > 2) {
86 break; 88 break;
89 }
87 90
88 /* grab the timestamp */ 91 /* grab the timestamp */
89 char *temp_buffer = strtok(input_buffer, " "); 92 char *temp_buffer = strtok(input_buffer, " ");
@@ -91,23 +94,27 @@ int main(int argc, char **argv) {
91 94
92 /* grab the average value 1 rate */ 95 /* grab the average value 1 rate */
93 temp_buffer = strtok(NULL, " "); 96 temp_buffer = strtok(NULL, " ");
94 if (variable_number == 1) 97 if (variable_number == 1) {
95 average_value_rate = strtoul(temp_buffer, NULL, 10); 98 average_value_rate = strtoul(temp_buffer, NULL, 10);
99 }
96 100
97 /* grab the average value 2 rate */ 101 /* grab the average value 2 rate */
98 temp_buffer = strtok(NULL, " "); 102 temp_buffer = strtok(NULL, " ");
99 if (variable_number == 2) 103 if (variable_number == 2) {
100 average_value_rate = strtoul(temp_buffer, NULL, 10); 104 average_value_rate = strtoul(temp_buffer, NULL, 10);
105 }
101 106
102 /* grab the maximum value 1 rate */ 107 /* grab the maximum value 1 rate */
103 temp_buffer = strtok(NULL, " "); 108 temp_buffer = strtok(NULL, " ");
104 if (variable_number == 1) 109 if (variable_number == 1) {
105 maximum_value_rate = strtoul(temp_buffer, NULL, 10); 110 maximum_value_rate = strtoul(temp_buffer, NULL, 10);
111 }
106 112
107 /* grab the maximum value 2 rate */ 113 /* grab the maximum value 2 rate */
108 temp_buffer = strtok(NULL, " "); 114 temp_buffer = strtok(NULL, " ");
109 if (variable_number == 2) 115 if (variable_number == 2) {
110 maximum_value_rate = strtoul(temp_buffer, NULL, 10); 116 maximum_value_rate = strtoul(temp_buffer, NULL, 10);
117 }
111 } 118 }
112 119
113 /* close the log file */ 120 /* close the log file */
@@ -129,16 +136,18 @@ int main(int argc, char **argv) {
129 136
130 unsigned long rate = 0L; 137 unsigned long rate = 0L;
131 /* else check the incoming/outgoing rates */ 138 /* else check the incoming/outgoing rates */
132 if (use_average) 139 if (use_average) {
133 rate = average_value_rate; 140 rate = average_value_rate;
134 else 141 } else {
135 rate = maximum_value_rate; 142 rate = maximum_value_rate;
143 }
136 144
137 int result = STATE_OK; 145 int result = STATE_OK;
138 if (rate > value_critical_threshold) 146 if (rate > value_critical_threshold) {
139 result = STATE_CRITICAL; 147 result = STATE_CRITICAL;
140 else if (rate > value_warning_threshold) 148 } else if (rate > value_warning_threshold) {
141 result = STATE_WARNING; 149 result = STATE_WARNING;
150 }
142 151
143 printf("%s. %s = %lu %s|%s\n", (use_average) ? _("Avg") : _("Max"), label, rate, units, 152 printf("%s. %s = %lu %s|%s\n", (use_average) ? _("Avg") : _("Max"), label, rate, units,
144 perfdata(label, (long)rate, units, (int)value_warning_threshold, (long)value_warning_threshold, (int)value_critical_threshold, 153 perfdata(label, (long)rate, units, (int)value_warning_threshold, (long)value_warning_threshold, (int)value_critical_threshold,
@@ -155,16 +164,18 @@ int process_arguments(int argc, char **argv) {
155 {"label", required_argument, 0, 'l'}, {"units", required_argument, 0, 'u'}, {"variable", required_argument, 0, 'v'}, 164 {"label", required_argument, 0, 'l'}, {"units", required_argument, 0, 'u'}, {"variable", required_argument, 0, 'v'},
156 {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, {0, 0, 0, 0}}; 165 {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, {0, 0, 0, 0}};
157 166
158 if (argc < 2) 167 if (argc < 2) {
159 return ERROR; 168 return ERROR;
169 }
160 170
161 for (int i = 1; i < argc; i++) { 171 for (int i = 1; i < argc; i++) {
162 if (strcmp("-to", argv[i]) == 0) 172 if (strcmp("-to", argv[i]) == 0) {
163 strcpy(argv[i], "-t"); 173 strcpy(argv[i], "-t");
164 else if (strcmp("-wt", argv[i]) == 0) 174 } else if (strcmp("-wt", argv[i]) == 0) {
165 strcpy(argv[i], "-w"); 175 strcpy(argv[i], "-w");
166 else if (strcmp("-ct", argv[i]) == 0) 176 } else if (strcmp("-ct", argv[i]) == 0) {
167 strcpy(argv[i], "-c"); 177 strcpy(argv[i], "-c");
178 }
168 } 179 }
169 180
170 int option_char; 181 int option_char;
@@ -172,8 +183,9 @@ int process_arguments(int argc, char **argv) {
172 while (1) { 183 while (1) {
173 option_char = getopt_long(argc, argv, "hVF:e:a:v:c:w:l:u:", longopts, &option); 184 option_char = getopt_long(argc, argv, "hVF:e:a:v:c:w:l:u:", longopts, &option);
174 185
175 if (option_char == -1 || option_char == EOF) 186 if (option_char == -1 || option_char == EOF) {
176 break; 187 break;
188 }
177 189
178 switch (option_char) { 190 switch (option_char) {
179 case 'F': /* input file */ 191 case 'F': /* input file */
@@ -183,15 +195,17 @@ int process_arguments(int argc, char **argv) {
183 expire_minutes = atoi(optarg); 195 expire_minutes = atoi(optarg);
184 break; 196 break;
185 case 'a': /* port */ 197 case 'a': /* port */
186 if (!strcmp(optarg, "MAX")) 198 if (!strcmp(optarg, "MAX")) {
187 use_average = false; 199 use_average = false;
188 else 200 } else {
189 use_average = true; 201 use_average = true;
202 }
190 break; 203 break;
191 case 'v': 204 case 'v':
192 variable_number = atoi(optarg); 205 variable_number = atoi(optarg);
193 if (variable_number < 1 || variable_number > 2) 206 if (variable_number < 1 || variable_number > 2) {
194 usage4(_("Invalid variable number")); 207 usage4(_("Invalid variable number"));
208 }
195 break; 209 break;
196 case 'w': /* critical time threshold */ 210 case 'w': /* critical time threshold */
197 value_warning_threshold = strtoul(optarg, NULL, 10); 211 value_warning_threshold = strtoul(optarg, NULL, 10);
@@ -222,10 +236,11 @@ int process_arguments(int argc, char **argv) {
222 } 236 }
223 237
224 if (expire_minutes <= 0 && argc > option_char) { 238 if (expire_minutes <= 0 && argc > option_char) {
225 if (is_intpos(argv[option_char])) 239 if (is_intpos(argv[option_char])) {
226 expire_minutes = atoi(argv[option_char++]); 240 expire_minutes = atoi(argv[option_char++]);
227 else 241 } else {
228 die(STATE_UNKNOWN, _("%s is not a valid expiration time\nUse '%s -h' for additional help\n"), argv[option_char], progname); 242 die(STATE_UNKNOWN, _("%s is not a valid expiration time\nUse '%s -h' for additional help\n"), argv[option_char], progname);
243 }
229 } 244 }
230 245
231 if (argc > option_char && strcmp(argv[option_char], "MAX") == 0) { 246 if (argc > option_char && strcmp(argv[option_char], "MAX") == 0) {
@@ -264,14 +279,17 @@ int process_arguments(int argc, char **argv) {
264} 279}
265 280
266int validate_arguments(void) { 281int validate_arguments(void) {
267 if (variable_number == -1) 282 if (variable_number == -1) {
268 usage4(_("You must supply the variable number")); 283 usage4(_("You must supply the variable number"));
284 }
269 285
270 if (label == NULL) 286 if (label == NULL) {
271 label = strdup("value"); 287 label = strdup("value");
288 }
272 289
273 if (units == NULL) 290 if (units == NULL) {
274 units = strdup(""); 291 units = strdup("");
292 }
275 293
276 return OK; 294 return OK;
277} 295}