Debian Codegraph @

madrisan-nagios-plugins-linux/nagios-plugins-linux/plugins/check_fc.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 /*
3 * License: GPLv3+
4 * Copyright (c) 2015,2022 Davide Madrisan <[email protected]>
5 *
6 * A Nagios plugin that monitors the status of the fiber channel ports
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23 #include <dirent.h>
24 #include <errno.h>
25 #include <getopt.h>
26 #include <limits.h>
27 #include <stdint.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32
33 #include "common.h"
34 #include "logging.h"
35 #include "string-macros.h"
36 #include "messages.h"
37 #include "progname.h"
38 #include "progversion.h"
39 #include "sysfsparser.h"
40 #include "thresholds.h"
41 #include "xstrton.h"
42
43 static const char *program_copyright =
44 "Copyright (C) 2015,2022 Davide Madrisan <" PACKAGE_BUGREPORT ">\n";
45
46 static struct option const longopts[] = {
47 {(char *) "fchostinfo", no_argument, NULL, 'i'},
48 {(char *) "critical", required_argument, NULL, 'c'},
49 {(char *) "warning", required_argument, NULL, 'w'},
50 {(char *) "verbose", no_argument, NULL, 'v'},
51 {(char *) "help", no_argument, NULL, GETOPT_HELP_CHAR},
52 {(char *) "version", no_argument, NULL, GETOPT_VERSION_CHAR},
53 {NULL, 0, NULL, 0}
54 };
55
56 static _Noreturn void
57 usage (FILE * out)
58 {
59 fprintf (out, "%s (" PACKAGE_NAME ") v%s\n", program_name, program_version);
60 fputs ("This plugin monitors the status of the fiber status ports.\n", out);
61 fputs (program_copyright, out);
62 fputs (USAGE_HEADER, out);
63 fprintf (out, " %s -w COUNTER -c COUNTER [delay [count]]\n", program_name);
64 fputs (USAGE_OPTIONS, out);
65 fputs (" -w, --warning COUNTER warning threshold\n", out);
66 fputs (" -c, --critical COUNTER critical threshold\n", out);
67 fputs (" -v, --verbose show details for command-line debugging "
68 "(Nagios may truncate output)\n", out);
69 fputs (" -i, --fchostinfo show the fc_host class object attributes\n",
70 out);
71 fputs (USAGE_HELP, out);
72 fputs (USAGE_VERSION, out);
73 fprintf (out, " delay is the delay between updates in seconds "
74 "(default: %dsec)\n", DELAY_DEFAULT);
75 fprintf (out, " count is the number of updates "
76 "(default: %d)\n", COUNT_DEFAULT);
77 fputs ("\t1 means the total inbound/outbound traffic from boottime.\n", out);
78 fputs (USAGE_EXAMPLES, out);
79 fprintf (out, " %s -c 2:\n", program_name);
80 fprintf (out, " %s -i -v\n", program_name);
81
82 exit (out == stderr ? STATE_UNKNOWN : STATE_OK);
83 }
84
85 static _Noreturn void
86 print_version (void)
87 {
88 printf ("%s (" PACKAGE_NAME ") v%s\n", program_name, program_version);
89 fputs (program_copyright, stdout);
90 fputs (GPLv3_DISCLAIMER, stdout);
91
92 exit (STATE_OK);
93 }
94
95 #define PATH_SYS_FC PATH_SYS "/class"
96 #define PATH_SYS_FC_HOST PATH_SYS_FC "/fc_host"
97
98 void
99 fc_host_summary (bool verbose)
100 {
101 DIR *dirp;
102 struct dirent *dp;
103 char *line, path[PATH_MAX];
104
105 sysfsparser_opendir(&dirp, PATH_SYS_FC_HOST);
106
107 /* Scan entries under /sys/class/fc_host directory */
108 while ((dp = sysfsparser_readfilename(dirp, DT_DIR | DT_LNK)))
109 {
110 DIR *dirp_host;
111 struct dirent *dp_host;
112
113 printf ("Class Device = \"%s\"\n", dp->d_name);
114
115 if (!verbose)
116 continue;
117
118 snprintf (path, PATH_MAX, "%s/%s/device", PATH_SYS_FC_HOST, dp->d_name);
119 char *cdevpath = realpath (path, NULL);
120 printf ("Class Device path = \"%s\"\n", cdevpath);
121
122 sysfsparser_opendir(&dirp_host, "%s/%s", PATH_SYS_FC_HOST, dp->d_name);
123
124 /* https://www.kernel.org/doc/Documentation/scsi/scsi_fc_transport.txt */
125 while ((dp_host = sysfsparser_readfilename(dirp_host, DT_REG)))
126 {
127 snprintf (path, PATH_MAX, "%s/%s/%s",
128 PATH_SYS_FC_HOST, dp->d_name, dp_host->d_name);
129 if ((line = sysfsparser_getline ("%s", path)))
130 {
131 fprintf (stdout, "%25s = \"%s\"\n", dp_host->d_name, line);
132 free (line);
133 }
134 }
135
136 fputs ("\n", stdout);
137
138 sysfsparser_closedir (dirp_host);
139 free (cdevpath);
140 }
141
142 sysfsparser_closedir (dirp);
143 }
144
145 static uint64_t
146 fc_host_get_statistic (const char *which, const char *host)
147 {
148 unsigned long long value;
149 int err;
150
151 err = sysfsparser_getvalue (&value, PATH_SYS_FC_HOST "/%s/statistics/%s",
152 host, which);
153 if (err < 0)
154 plugin_error (STATE_UNKNOWN, 0,
155 "an error has occurred while reading "
156 PATH_SYS_FC_HOST "/%s/statistics/%s",
157 host, which);
158
159 dbg (PATH_SYS_FC_HOST "/%s/statistics/%s = %llu\n",
160 host, which, value);
161
162 return value;
163 }
164
165 #define fc_get_stat(name) \
166 static uint64_t fc_stat_##name(const char *host) \
167 { \
168 return fc_host_get_statistic (#name, host); \
169 }
170 fc_get_stat(rx_frames);
171 fc_get_stat(tx_frames);
172 fc_get_stat(error_frames);
173 fc_get_stat(invalid_crc_count);
174 fc_get_stat(link_failure_count);
175 fc_get_stat(loss_of_signal_count);
176 fc_get_stat(loss_of_sync_count);
177
178 /* see <http://osxr.org/linux/source/drivers/scsi/scsi_transport_fc.c> */
179 typedef struct fc_host_statistics {
180 uint64_t rx_frames;
181 uint64_t tx_frames;
182 uint64_t error_frames;
183 uint64_t invalid_crc_count;
184 uint64_t link_failure_count;
185 uint64_t loss_of_signal_count;
186 uint64_t loss_of_sync_count;
187 } fc_host_statistics;
188
189 static void
190 fc_host_status (int *n_ports, int *n_online, fc_host_statistics *stats,
191 unsigned int delay, unsigned int count)
192 {
193 DIR *dirp;
194 struct dirent *dp;
195 char path[PATH_MAX];
196 uint64_t drx_frames_tot = 0, dtx_frames_tot = 0;
197
198 *n_ports = *n_online = 0;
199 sysfsparser_opendir(&dirp, PATH_SYS_FC_HOST);
200
201 /* Scan entries under /sys/class/fc_host directory */
202 while ((dp = sysfsparser_readfilename(dirp, DT_DIR | DT_LNK)))
203 {
204 (*n_ports)++;
205 snprintf (path, PATH_MAX, "%s/%s/port_state",
206 PATH_SYS_FC_HOST, dp->d_name);
207
208 char *line = sysfsparser_getline ("%s", path);
209 if (STREQ (line, "Online"))
210 (*n_online)++;
211
212 free (line);
213
214 /* collect some statistics */
215 uint64_t rx_frames[2], tx_frames[2];
216 unsigned int i, tog = 0;
217
218 stats->rx_frames = rx_frames[0] = fc_stat_rx_frames (dp->d_name);
219 stats->tx_frames = tx_frames[0] = fc_stat_tx_frames (dp->d_name);
220
221 for (i = 1; i < count; i++)
222 {
223 sleep (delay);
224 tog = !tog;
225
226 rx_frames[tog] = fc_stat_rx_frames (dp->d_name);
227 stats->rx_frames = rx_frames[tog] - rx_frames[!tog];
228 tx_frames[tog] = fc_stat_tx_frames (dp->d_name);
229 stats->tx_frames = tx_frames[tog] - tx_frames[!tog];
230 }
231
232 drx_frames_tot += stats->rx_frames;
233 dtx_frames_tot += stats->tx_frames;
234
235 stats->error_frames += fc_stat_error_frames (dp->d_name);
236 stats->invalid_crc_count += fc_stat_invalid_crc_count (dp->d_name);
237 stats->link_failure_count += fc_stat_link_failure_count (dp->d_name);
238 stats->loss_of_signal_count +=
239 fc_stat_loss_of_signal_count (dp->d_name);
240 stats->loss_of_sync_count += fc_stat_loss_of_sync_count (dp->d_name);
241 }
242
243 sysfsparser_closedir (dirp);
244
245 stats->rx_frames = drx_frames_tot;
246 stats->tx_frames = dtx_frames_tot;
247 }
248
249 #undef PATH_SYS_FC
250 #undef PATH_SYS_FC_HOST
251
252 int
253 main (int argc, char **argv)
254 {
255 int c, n_ports, n_online;
256 bool verbose = false, summary = false;
257 char *critical = NULL, *warning = NULL;
258 nagstatus status = STATE_OK;
259 thresholds *my_threshold = NULL;
260 unsigned long count, delay;
261
262 set_program_name (argv[0]);
263
264 while ((c = getopt_long (argc, argv,
265 "c:w:vi" GETOPT_HELP_VERSION_STRING,
266 longopts, NULL)) != -1)
267 {
268 switch (c)
269 {
270 default:
271 usage (stderr);
272 case 'i':
273 summary = true;
274 break;
275 case 'c':
276 critical = optarg;
277 break;
278 case 'w':
279 warning = optarg;
280 break;
281 case 'v':
282 verbose = true;
283 break;
284
285 case_GETOPT_HELP_CHAR
286 case_GETOPT_VERSION_CHAR
287
288 }
289 }
290
291 if (summary)
292 {
293 sysfsparser_check_for_sysfs ();
294 fc_host_summary (verbose);
295 return STATE_UNKNOWN;
296 }
297
298 delay = DELAY_DEFAULT, count = COUNT_DEFAULT;
299 if (optind < argc)
300 {
301 delay = strtol_or_err (argv[optind++], "failed to parse argument");
302
303 if (delay < 1)
304 plugin_error (STATE_UNKNOWN, 0, "delay must be positive integer");
305 else if (DELAY_MAX < delay)
306 plugin_error (STATE_UNKNOWN, 0,
307 "too large delay value (greater than %d)", DELAY_MAX);
308 }
309
310 if (optind < argc)
311 {
312 count = strtol_or_err (argv[optind++], "failed to parse argument");
313 if (COUNT_MAX < count)
314 plugin_error (STATE_UNKNOWN, 0,
315 "too large count value (greater than %d)", COUNT_MAX);
316 }
317
318 sysfsparser_check_for_sysfs ();
319
320 status = set_thresholds (&my_threshold, warning, critical);
321 if (status == NP_RANGE_UNPARSEABLE)
322 usage (stderr);
323
324 fc_host_statistics stats = {0};
325
326 fc_host_status (&n_ports, &n_online, &stats, delay, count);
327 status = get_status (n_online, my_threshold);
328
329 printf ("%s %s - Fiber Channel ports status: %d/%d Online "
330 "| rx_frames=%llu tx_frames=%llu"
331 " error_frames=%llu"
332 " invalid_crc_count=%llu"
333 " link_failure_count=%llu"
334 " loss_of_signal_count=%llu"
335 " loss_of_sync_count=%llu\n",
336 program_name_short, state_text (status),
337 n_online, n_ports,
338 (unsigned long long) stats.rx_frames,
339 (unsigned long long) stats.tx_frames,
340 (unsigned long long) stats.error_frames,
341 (unsigned long long) stats.invalid_crc_count,
342 (unsigned long long) stats.link_failure_count,
343 (unsigned long long) stats.loss_of_signal_count,
344 (unsigned long long) stats.loss_of_sync_count
345 );
346
347 return status;
348 }