| 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 <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 |
|
| 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 |
|
| 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 |
|
| 145 |
|
| 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 |
} |