Debian Codegraph @

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

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 /*
3 * License: GPLv3+
4 * Copyright (c) 2013,2014,2024 Davide Madrisan <[email protected]>
5 *
6 * A Nagios plugin to check for readonly filesystems.
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 #include <getopt.h>
23 #include <mntent.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28
29 #include "common.h"
30 #include "string-macros.h"
31 #include "messages.h"
32 #include "mountlist.h"
33 #include "progname.h"
34 #include "progversion.h"
35 #include "system.h"
36
37 static const char *program_copyright =
38 "Copyright (C) 2013,2014,2024 Davide Madrisan <" PACKAGE_BUGREPORT ">\n";
39
40 /* Linked list of mounted file systems. */
41 static struct mount_entry *mount_list;
42
43 static struct option const longopts[] = {
44 {(char *) "list", no_argument, NULL, 'l'},
45 {(char *) "help", no_argument, NULL, GETOPT_HELP_CHAR},
46 {(char *) "version", no_argument, NULL, GETOPT_VERSION_CHAR},
47 {NULL, 0, NULL, 0}
48 };
49
50 static _Noreturn void
51 usage (FILE * out)
52 {
53 fprintf (out, "%s (" PACKAGE_NAME ") v%s\n", program_name, program_version);
54 fputs ("This plugin checks whether the given filesystems are mounted.\n",
55 out);
56 fputs (program_copyright, out);
57 fputs (USAGE_HEADER, out);
58 fprintf (out, " %s [FILESYSTEM]...\n", program_name);
59 fputs (USAGE_OPTIONS, out);
60 fputs (" -l, --list list the mounted filesystems and exit\n", out);
61 fputs (USAGE_HELP, out);
62 fputs (USAGE_VERSION, out);
63 fputs (USAGE_EXAMPLES, out);
64 fprintf (out, " %s /mnt/nfs-data /mnt/cdrom\n", program_name);
65
66 exit (out == stderr ? STATE_UNKNOWN : STATE_OK);
67 }
68
69 static _Noreturn void
70 print_version (void)
71 {
72 printf ("%s (" PACKAGE_NAME ") v%s\n", program_name, program_version);
73 fputs (program_copyright, stdout);
74 fputs (GPLv3_DISCLAIMER, stdout);
75
76 exit (STATE_OK);
77 }
78
79 static nagstatus
80 check_entry (char const *mountpoint)
81 {
82 struct mount_entry *me;
83
84 for (me = mount_list; me; me = me->me_next)
85 if (STREQ (me->me_mountdir, mountpoint))
86 return STATE_OK;
87
88 return STATE_CRITICAL;
89 }
90
91 int
92 main (int argc, char **argv)
93 {
94 int c, i;
95 bool just_list_fs = false;
96 nagstatus status = STATE_OK;
97
98 set_program_name (argv[0]);
99
100 while ((c = getopt_long (argc, argv, "l" GETOPT_HELP_VERSION_STRING, longopts,
101 NULL)) != -1)
102 {
103 switch (c)
104 {
105 default:
106 usage (stderr);
107 case 'l':
108 just_list_fs = true;
109 break;
110
111 case_GETOPT_HELP_CHAR
112 case_GETOPT_VERSION_CHAR
113
114 }
115 }
116
117 if (argc <= optind && (false == just_list_fs))
118 usage (stderr);
119
120 mount_list = read_file_system_list (false);
121
122 if (NULL == mount_list)
123 /* Couldn't read the table of mounted file systems. */
124 plugin_error (STATE_UNKNOWN, 0,
125 "cannot read table of mounted file systems");
126
127 if (just_list_fs) {
128 struct mount_entry *me;
129 printf ("--- List of mounted filesystems ---\n");
130 for (me = mount_list; me; me = me->me_next)
131 printf ("%s on %s type %s (%s)\n"
132 , me->me_devname
133 , me->me_mountdir
134 , me->me_type
135 , me->me_opts);
136
137 return STATE_UNKNOWN;
138 }
139
140 for (i = optind; i < argc; ++i)
141 if (STATE_CRITICAL == check_entry (argv[i]))
142 status = STATE_CRITICAL;
143 else
144 /* This is allowed. See:
145 * http://www.open-std.org/JTC1/sc22/wg14/www/docs/n1256.pdf */
146 argv[i] = NULL;
147
148 printf ("filesystems %s", state_text (status));
149 for (i = optind; i < argc; ++i)
150 if (argv[i])
151 printf (" %s", argv[i]);
152
153 if (STATE_CRITICAL == status)
154 printf (" unmounted!");
155 putchar ('\n');
156
157 return status;
158 }