| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#include <getopt.h> |
| 23 |
#include <mntent.h> |
| 24 |
#include <fcntl.h> |
| 25 |
#include <stdio.h> |
| 26 |
#include <stdlib.h> |
| 27 |
#include <string.h> |
| 28 |
#include <sys/types.h> |
| 29 |
#include <sys/stat.h> |
| 30 |
#include <unistd.h> |
| 31 |
|
| 32 |
#include "common.h" |
| 33 |
#include "string-macros.h" |
| 34 |
#include "messages.h" |
| 35 |
#include "mountlist.h" |
| 36 |
#include "xalloc.h" |
| 37 |
#include "progname.h" |
| 38 |
#include "progversion.h" |
| 39 |
|
| 40 |
static const char *program_copyright = |
| 41 |
"Copyright (C) 2013-2015 Davide Madrisan <" PACKAGE_BUGREPORT ">\n"; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
struct fs_type_list |
| 46 |
{ |
| 47 |
char *fs_name; |
| 48 |
struct fs_type_list *fs_next; |
| 49 |
}; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
static struct fs_type_list *fs_select_list; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
static struct fs_type_list *fs_exclude_list; |
| 67 |
|
| 68 |
|
| 69 |
static struct mount_entry *mount_list; |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
static bool show_all_fs; |
| 74 |
|
| 75 |
|
| 76 |
static bool show_local_fs; |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
static bool verbose = false; |
| 81 |
|
| 82 |
static struct option const longopts[] = { |
| 83 |
{(char *) "all", no_argument, NULL, 'a'}, |
| 84 |
{(char *) "local", no_argument, NULL, 'l'}, |
| 85 |
{(char *) "type", required_argument, NULL, 'T'}, |
| 86 |
{(char *) "exclude-type", required_argument, NULL, 'X'}, |
| 87 |
{(char *) "verbose", no_argument, NULL, 'v'}, |
| 88 |
{(char *) "help", no_argument, NULL, GETOPT_HELP_CHAR}, |
| 89 |
{(char *) "version", no_argument, NULL, GETOPT_VERSION_CHAR}, |
| 90 |
{NULL, 0, NULL, 0} |
| 91 |
}; |
| 92 |
|
| 93 |
static _Noreturn void |
| 94 |
usage (FILE * out) |
| 95 |
{ |
| 96 |
fprintf (out, "%s (" PACKAGE_NAME ") v%s\n", program_name, program_version); |
| 97 |
fputs ("This plugin checks for readonly filesystems.\n", out); |
| 98 |
fputs (program_copyright, out); |
| 99 |
fputs (USAGE_HEADER, out); |
| 100 |
fprintf (out, " %s [OPTION]... [FILESYSTEM]...\n", program_name); |
| 101 |
fputs (USAGE_OPTIONS, out); |
| 102 |
fputs (" -a, --all include dummy file systems\n", out); |
| 103 |
fputs (" -l, --local limit listing to local file systems\n", |
| 104 |
out); |
| 105 |
fputs (" -T, --type=TYPE limit listing to file systems of type TYPE\n", |
| 106 |
out); |
| 107 |
fputs (" -X, --exclude-type=TYPE " |
| 108 |
"limit listing to file systems not of type TYPE\n", out); |
| 109 |
fputs (" -v, --verbose show details for command-line debugging " |
| 110 |
"(Nagios may truncate output)\n", out); |
| 111 |
fputs (USAGE_HELP, out); |
| 112 |
fputs (USAGE_VERSION, out); |
| 113 |
fputs (USAGE_EXAMPLES, out); |
| 114 |
fprintf (out, " %s -l -T ext3 -T ext4\n", program_name); |
| 115 |
fprintf (out, " %s -l -X vfat\n", program_name); |
| 116 |
|
| 117 |
exit (out == stderr ? STATE_UNKNOWN : STATE_OK); |
| 118 |
} |
| 119 |
|
| 120 |
static _Noreturn void |
| 121 |
print_version (void) |
| 122 |
{ |
| 123 |
printf ("%s (" PACKAGE_NAME ") v%s\n", program_name, program_version); |
| 124 |
fputs (program_copyright, stdout); |
| 125 |
fputs (GPLv3_DISCLAIMER, stdout); |
| 126 |
|
| 127 |
exit (STATE_OK); |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
static void |
| 133 |
add_fs_type (const char *fstype) |
| 134 |
{ |
| 135 |
struct fs_type_list *fsp; |
| 136 |
|
| 137 |
fsp = xmalloc (sizeof *fsp); |
| 138 |
fsp->fs_name = (char *) fstype; |
| 139 |
fsp->fs_next = fs_select_list; |
| 140 |
fs_select_list = fsp; |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
static bool |
| 146 |
selected_fstype (const char *fstype) |
| 147 |
{ |
| 148 |
const struct fs_type_list *fsp; |
| 149 |
|
| 150 |
if (fs_select_list == NULL || fstype == NULL) |
| 151 |
return true; |
| 152 |
for (fsp = fs_select_list; fsp; fsp = fsp->fs_next) |
| 153 |
if (STREQ (fstype, fsp->fs_name)) |
| 154 |
return true; |
| 155 |
return false; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
static void |
| 161 |
add_excluded_fs_type (const char *fstype) |
| 162 |
{ |
| 163 |
struct fs_type_list *fsp; |
| 164 |
|
| 165 |
fsp = xmalloc (sizeof *fsp); |
| 166 |
fsp->fs_name = (char *) fstype; |
| 167 |
fsp->fs_next = fs_exclude_list; |
| 168 |
fs_exclude_list = fsp; |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
static bool |
| 174 |
excluded_fstype (const char *fstype) |
| 175 |
{ |
| 176 |
const struct fs_type_list *fsp; |
| 177 |
|
| 178 |
if (fs_exclude_list == NULL || fstype == NULL) |
| 179 |
return false; |
| 180 |
for (fsp = fs_exclude_list; fsp; fsp = fsp->fs_next) |
| 181 |
if (STREQ (fstype, fsp->fs_name)) |
| 182 |
return true; |
| 183 |
return false; |
| 184 |
} |
| 185 |
|
| 186 |
static bool |
| 187 |
skip_mount_entry (struct mount_entry *me) |
| 188 |
{ |
| 189 |
if (me->me_remote && show_local_fs) |
| 190 |
return true; |
| 191 |
|
| 192 |
if (me->me_dummy && !show_all_fs) |
| 193 |
return true; |
| 194 |
|
| 195 |
if (!selected_fstype (me->me_type) || excluded_fstype (me->me_type)) |
| 196 |
return true; |
| 197 |
|
| 198 |
return false; |
| 199 |
} |
| 200 |
|
| 201 |
static int |
| 202 |
check_all_entries (char **ro_filesystems) |
| 203 |
{ |
| 204 |
struct mount_entry *me; |
| 205 |
int status = STATE_OK; |
| 206 |
char *p; |
| 207 |
size_t len; |
| 208 |
|
| 209 |
for (me = mount_list; me; me = me->me_next) |
| 210 |
{ |
| 211 |
if (skip_mount_entry (me)) |
| 212 |
continue; |
| 213 |
|
| 214 |
if (verbose) |
| 215 |
printf ("%-10s %s type %s (%s) %s\n", |
| 216 |
me->me_devname, me->me_mountdir, me->me_type, me->me_opts, |
| 217 |
(me->me_readonly) ? "<< read-only" : ""); |
| 218 |
if (me->me_readonly) |
| 219 |
{ |
| 220 |
if (*ro_filesystems == NULL) |
| 221 |
*ro_filesystems = xstrdup (me->me_mountdir); |
| 222 |
else |
| 223 |
{ |
| 224 |
len = strlen (*ro_filesystems) + strlen (me->me_mountdir) + 2; |
| 225 |
p = xrealloc (*ro_filesystems, len); |
| 226 |
sprintf (p, "%s %s", *ro_filesystems, me->me_mountdir); |
| 227 |
*ro_filesystems = p; |
| 228 |
} |
| 229 |
status = STATE_CRITICAL; |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
return status; |
| 234 |
} |
| 235 |
|
| 236 |
static int |
| 237 |
check_entry (char const *name) |
| 238 |
{ |
| 239 |
struct mount_entry *me; |
| 240 |
|
| 241 |
if (name == NULL) |
| 242 |
return STATE_OK; |
| 243 |
|
| 244 |
for (me = mount_list; me; me = me->me_next) |
| 245 |
if (STREQ (me->me_mountdir, name)) |
| 246 |
{ |
| 247 |
if (skip_mount_entry (me)) |
| 248 |
return STATE_OK; |
| 249 |
|
| 250 |
if (verbose) |
| 251 |
printf ("%-10s %s type %s (%s) %s\n", |
| 252 |
me->me_devname, me->me_mountdir, me->me_type, me->me_opts, |
| 253 |
(me->me_readonly) ? "<< read-only" : ""); |
| 254 |
|
| 255 |
if (me->me_readonly) |
| 256 |
return STATE_CRITICAL; |
| 257 |
} |
| 258 |
|
| 259 |
return STATE_OK; |
| 260 |
} |
| 261 |
|
| 262 |
int |
| 263 |
main (int argc, char **argv) |
| 264 |
{ |
| 265 |
int c, i; |
| 266 |
int status = STATE_OK; |
| 267 |
char *ro_filesystems = NULL; |
| 268 |
|
| 269 |
fs_select_list = NULL; |
| 270 |
fs_exclude_list = NULL; |
| 271 |
|
| 272 |
show_local_fs = false; |
| 273 |
show_all_fs = false; |
| 274 |
|
| 275 |
set_program_name (argv[0]); |
| 276 |
|
| 277 |
while ((c = getopt_long (argc, argv, |
| 278 |
"alT:X:v" GETOPT_HELP_VERSION_STRING, longopts, NULL)) != -1) |
| 279 |
{ |
| 280 |
switch (c) |
| 281 |
{ |
| 282 |
default: |
| 283 |
usage (stderr); |
| 284 |
case 'a': |
| 285 |
show_all_fs = true; |
| 286 |
break; |
| 287 |
case 'l': |
| 288 |
show_local_fs = true; |
| 289 |
break; |
| 290 |
case 'T': |
| 291 |
add_fs_type (optarg); |
| 292 |
break; |
| 293 |
case 'X': |
| 294 |
add_excluded_fs_type (optarg); |
| 295 |
break; |
| 296 |
case 'v': |
| 297 |
verbose = true; |
| 298 |
break; |
| 299 |
|
| 300 |
case_GETOPT_HELP_CHAR |
| 301 |
case_GETOPT_VERSION_CHAR |
| 302 |
|
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
{ |
| 308 |
struct fs_type_list *fs_incl; |
| 309 |
for (fs_incl = fs_select_list; fs_incl; fs_incl = fs_incl->fs_next) |
| 310 |
{ |
| 311 |
struct fs_type_list *fs_excl; |
| 312 |
for (fs_excl = fs_exclude_list; fs_excl; fs_excl = fs_excl->fs_next) |
| 313 |
{ |
| 314 |
if (STREQ (fs_incl->fs_name, fs_excl->fs_name)) |
| 315 |
plugin_error (STATE_UNKNOWN, 0, |
| 316 |
"file system type `%s' both selected and " |
| 317 |
"excluded", |
| 318 |
fs_incl->fs_name); |
| 319 |
} |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
if (optind < argc) |
| 324 |
{ |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
for (i = optind; i < argc; ++i) |
| 329 |
{ |
| 330 |
int fd = open (argv[i], O_RDONLY | O_NOCTTY); |
| 331 |
if (fd < 0) |
| 332 |
plugin_error (STATE_UNKNOWN, 0, "cannot open `%s'", argv[i]); |
| 333 |
close (fd); |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
mount_list = |
| 338 |
read_file_system_list ((fs_select_list != NULL |
| 339 |
|| fs_exclude_list != NULL || show_local_fs)); |
| 340 |
|
| 341 |
if (NULL == mount_list) |
| 342 |
|
| 343 |
plugin_error (STATE_UNKNOWN, 0, |
| 344 |
"cannot read table of mounted file systems"); |
| 345 |
|
| 346 |
if (optind < argc) |
| 347 |
{ |
| 348 |
for (i = optind; i < argc; ++i) |
| 349 |
{ |
| 350 |
if (STATE_CRITICAL == check_entry (argv[i])) |
| 351 |
status = STATE_CRITICAL; |
| 352 |
else |
| 353 |
argv[i] = NULL; |
| 354 |
} |
| 355 |
|
| 356 |
printf ("%s %s", program_name_short, state_text (status)); |
| 357 |
if (STATE_CRITICAL == status) |
| 358 |
{ |
| 359 |
for (i = optind; i < argc; ++i) |
| 360 |
if (argv[i]) |
| 361 |
printf (" %s", argv[i]); |
| 362 |
printf (" readonly!"); |
| 363 |
} |
| 364 |
|
| 365 |
putchar ('\n'); |
| 366 |
return status; |
| 367 |
} |
| 368 |
|
| 369 |
status = check_all_entries (&ro_filesystems); |
| 370 |
|
| 371 |
if (STATE_CRITICAL == status) |
| 372 |
{ |
| 373 |
printf ("%s %s: %s readonly!\n", program_name_short, state_text (status), |
| 374 |
ro_filesystems); |
| 375 |
free (ro_filesystems); |
| 376 |
} |
| 377 |
else |
| 378 |
printf ("%s %s\n", program_name_short, state_text (status)); |
| 379 |
|
| 380 |
return status; |
| 381 |
} |