| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#include <errno.h> |
| 23 |
#include <getopt.h> |
| 24 |
#include <regex.h> |
| 25 |
#include <stdio.h> |
| 26 |
#include <stdlib.h> |
| 27 |
#include <unistd.h> |
| 28 |
#include <string.h> |
| 29 |
#include <sys/types.h> |
| 30 |
#include <sys/socket.h> |
| 31 |
#include <sys/un.h> |
| 32 |
|
| 33 |
#include "common.h" |
| 34 |
#include "messages.h" |
| 35 |
#include "progname.h" |
| 36 |
#include "progversion.h" |
| 37 |
#include "system.h" |
| 38 |
|
| 39 |
static bool verbose = false; |
| 40 |
static const char *multipathd_socket = MULTIPATHD_SOCKET; |
| 41 |
|
| 42 |
static const char *program_copyright = |
| 43 |
"Copyright (C) 2013,2015 Davide Madrisan <" PACKAGE_BUGREPORT ">\n"; |
| 44 |
|
| 45 |
static struct option const longopts[] = { |
| 46 |
{(char *) "verbose", no_argument, NULL, 'v'}, |
| 47 |
{(char *) "help", no_argument, NULL, GETOPT_HELP_CHAR}, |
| 48 |
{(char *) "version", no_argument, NULL, GETOPT_VERSION_CHAR}, |
| 49 |
{NULL, 0, NULL, 0} |
| 50 |
}; |
| 51 |
|
| 52 |
static _Noreturn void |
| 53 |
usage (FILE * out) |
| 54 |
{ |
| 55 |
fprintf (out, "%s (" PACKAGE_NAME ") v%s\n", program_name, program_version); |
| 56 |
fputs ("This plugin checks the multipath topology status.\n", out); |
| 57 |
fputs (program_copyright, out); |
| 58 |
fputs (USAGE_HEADER, out); |
| 59 |
fprintf (out, " %s [OPTION]...\n", program_name); |
| 60 |
fputs (USAGE_OPTIONS, out); |
| 61 |
fputs (" -v, --verbose show details for command-line debugging " |
| 62 |
"(Nagios may truncate output)\n", out); |
| 63 |
fputs (USAGE_HELP, out); |
| 64 |
fputs (USAGE_VERSION, out); |
| 65 |
fputs (USAGE_EXAMPLES, out); |
| 66 |
fprintf (out, " %s\n", program_name); |
| 67 |
|
| 68 |
exit (out == stderr ? STATE_UNKNOWN : STATE_OK); |
| 69 |
} |
| 70 |
|
| 71 |
static _Noreturn void |
| 72 |
print_version (void) |
| 73 |
{ |
| 74 |
printf ("%s (" PACKAGE_NAME ") v%s\n", program_name, program_version); |
| 75 |
fputs (program_copyright, stdout); |
| 76 |
fputs (GPLv3_DISCLAIMER, stdout); |
| 77 |
|
| 78 |
exit (STATE_OK); |
| 79 |
} |
| 80 |
|
| 81 |
static size_t |
| 82 |
write_all (int fd, const void *buf, size_t len) |
| 83 |
{ |
| 84 |
size_t total = 0; |
| 85 |
|
| 86 |
while (len) |
| 87 |
{ |
| 88 |
ssize_t n = write (fd, buf, len); |
| 89 |
if (n < 0) |
| 90 |
{ |
| 91 |
if ((errno == EINTR) || (errno == EAGAIN)) |
| 92 |
continue; |
| 93 |
return total; |
| 94 |
} |
| 95 |
if (!n) |
| 96 |
return total; |
| 97 |
|
| 98 |
buf = n + (char *) buf; |
| 99 |
len -= n; |
| 100 |
total += n; |
| 101 |
} |
| 102 |
|
| 103 |
return total; |
| 104 |
} |
| 105 |
|
| 106 |
static size_t |
| 107 |
read_all (int fd, void *buf, size_t len) |
| 108 |
{ |
| 109 |
size_t total = 0; |
| 110 |
|
| 111 |
while (len) |
| 112 |
{ |
| 113 |
ssize_t n = read (fd, buf, len); |
| 114 |
if (n < 0) |
| 115 |
{ |
| 116 |
if ((errno == EINTR) || (errno == EAGAIN)) |
| 117 |
continue; |
| 118 |
return total; |
| 119 |
} |
| 120 |
if (!n) |
| 121 |
return total; |
| 122 |
|
| 123 |
buf = n + (char *) buf; |
| 124 |
len -= n; |
| 125 |
total += n; |
| 126 |
} |
| 127 |
|
| 128 |
return total; |
| 129 |
} |
| 130 |
|
| 131 |
static int |
| 132 |
multipathd_connect (void) |
| 133 |
{ |
| 134 |
int fd, len; |
| 135 |
union |
| 136 |
{ |
| 137 |
struct sockaddr addr; |
| 138 |
struct sockaddr_un ux; |
| 139 |
} u; |
| 140 |
|
| 141 |
memset (&u.ux, 0, sizeof (u.ux)); |
| 142 |
u.ux.sun_family = AF_LOCAL; |
| 143 |
|
| 144 |
if (multipathd_socket[0] == '@') |
| 145 |
{ |
| 146 |
|
| 147 |
len = strlen (multipathd_socket) + sizeof (sa_family_t); |
| 148 |
strncpy (u.ux.sun_path, multipathd_socket, len); |
| 149 |
u.ux.sun_path[0] = '\0'; |
| 150 |
} |
| 151 |
else |
| 152 |
{ |
| 153 |
strncpy (u.ux.sun_path, multipathd_socket, sizeof (u.ux.sun_path)); |
| 154 |
u.ux.sun_path[sizeof (u.ux.sun_path) - 1] = 0; |
| 155 |
len = sizeof (u.ux); |
| 156 |
} |
| 157 |
|
| 158 |
fd = socket (AF_LOCAL, SOCK_STREAM, 0); |
| 159 |
if (fd == -1) |
| 160 |
return -1; |
| 161 |
|
| 162 |
if (connect (fd, &u.addr, len) == -1) |
| 163 |
{ |
| 164 |
close (fd); |
| 165 |
return -1; |
| 166 |
} |
| 167 |
|
| 168 |
return fd; |
| 169 |
} |
| 170 |
|
| 171 |
static void |
| 172 |
multipathd_query (const char *query, char *buf, size_t bufsize) |
| 173 |
{ |
| 174 |
int sock; |
| 175 |
size_t len = strlen (query) + 1; |
| 176 |
|
| 177 |
if ((sock = multipathd_connect ()) < 0) |
| 178 |
plugin_error (STATE_UNKNOWN, errno, "cannot connect to %s", |
| 179 |
multipathd_socket); |
| 180 |
|
| 181 |
if (write_all (sock, &len, sizeof (len)) != sizeof (len)) |
| 182 |
plugin_error (STATE_UNKNOWN, 0, "failed to send message to multipathd"); |
| 183 |
|
| 184 |
if (write_all (sock, query, len) != len) |
| 185 |
plugin_error (STATE_UNKNOWN, 0, "failed to send message to multipathd"); |
| 186 |
|
| 187 |
if (read_all (sock, &len, sizeof (len)) != sizeof (len)) |
| 188 |
plugin_error (STATE_UNKNOWN, 0, |
| 189 |
"failed to receive message from multipathd"); |
| 190 |
|
| 191 |
if (len > bufsize) |
| 192 |
plugin_error (STATE_UNKNOWN, 0, "reply from multipathd too long"); |
| 193 |
|
| 194 |
if (read_all (sock, buf, len) != len) |
| 195 |
plugin_error (STATE_UNKNOWN, 0, |
| 196 |
"failed to receive message from multipathd"); |
| 197 |
|
| 198 |
close (sock); |
| 199 |
} |
| 200 |
|
| 201 |
static int |
| 202 |
check_for_faulty_paths (char *buf, size_t bufsize) |
| 203 |
{ |
| 204 |
char *str1, *saveptr1; |
| 205 |
char *dm_st_ok_pattern = "[ \t]+\\[?active\\]?[ \t]*\\[?ready\\]?[ \t]+"; |
| 206 |
int rc, row, faulty_paths = 0; |
| 207 |
regex_t regex; |
| 208 |
|
| 209 |
if ((rc = regcomp (®ex, dm_st_ok_pattern, REG_EXTENDED | REG_NOSUB))) |
| 210 |
{ |
| 211 |
regerror (rc, ®ex, buf, bufsize); |
| 212 |
plugin_error (STATE_UNKNOWN, 0, "regcomp() failed: %s", buf); |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
for (row = 1, str1 = buf;; row++, str1 = NULL) |
| 223 |
{ |
| 224 |
char *token = strtok_r (str1, "\n", &saveptr1); |
| 225 |
if (token == NULL) |
| 226 |
break; |
| 227 |
if (verbose) |
| 228 |
printf ("%s\n", token); |
| 229 |
if (row > 1 && regexec (®ex, token, (size_t) 0, NULL, 0)) |
| 230 |
{ |
| 231 |
faulty_paths++; |
| 232 |
if (verbose) |
| 233 |
printf (" \\ faulty path detected!\n"); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
regfree (®ex); |
| 239 |
return faulty_paths; |
| 240 |
} |
| 241 |
|
| 242 |
int |
| 243 |
main (int argc, char **argv) |
| 244 |
{ |
| 245 |
int c, faulty_paths; |
| 246 |
enum { bufsize = 10240 }; |
| 247 |
static char buffer[bufsize]; |
| 248 |
|
| 249 |
set_program_name (argv[0]); |
| 250 |
|
| 251 |
while ((c = getopt_long (argc, argv, "v" GETOPT_HELP_VERSION_STRING, |
| 252 |
longopts, NULL)) != -1) |
| 253 |
{ |
| 254 |
switch (c) |
| 255 |
{ |
| 256 |
default: |
| 257 |
usage (stderr); |
| 258 |
case 'v': |
| 259 |
verbose = true; |
| 260 |
break; |
| 261 |
|
| 262 |
case_GETOPT_HELP_CHAR |
| 263 |
case_GETOPT_VERSION_CHAR |
| 264 |
|
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
if (getuid () != 0) |
| 269 |
plugin_error (STATE_UNKNOWN, 0, "need to be root"); |
| 270 |
|
| 271 |
multipathd_query ("show paths", buffer, sizeof (buffer)); |
| 272 |
faulty_paths = check_for_faulty_paths (buffer, bufsize); |
| 273 |
|
| 274 |
if (faulty_paths > 0) |
| 275 |
{ |
| 276 |
printf ("%s %s: found %d faulty path(s)\n", program_name_short, |
| 277 |
state_text (STATE_CRITICAL), faulty_paths); |
| 278 |
|
| 279 |
return STATE_CRITICAL; |
| 280 |
} |
| 281 |
|
| 282 |
printf ("%s %s\n", program_name_short, state_text (STATE_OK)); |
| 283 |
|
| 284 |
return STATE_OK; |
| 285 |
} |