diff options
Diffstat (limited to 'plugins/check_nntp.c')
| -rw-r--r-- | plugins/check_nntp.c | 418 |
1 files changed, 418 insertions, 0 deletions
diff --git a/plugins/check_nntp.c b/plugins/check_nntp.c new file mode 100644 index 00000000..4bdc83ab --- /dev/null +++ b/plugins/check_nntp.c | |||
| @@ -0,0 +1,418 @@ | |||
| 1 | /****************************************************************************** | ||
| 2 | * | ||
| 3 | * CHECK_NNTP.C | ||
| 4 | * | ||
| 5 | * Program: NNTP plugin for Nagios | ||
| 6 | * License: GPL | ||
| 7 | * Copyright (c) 1999 Ethan Galstad (nagios@nagios.org) | ||
| 8 | * | ||
| 9 | * $Id$ | ||
| 10 | * | ||
| 11 | * Description: | ||
| 12 | * | ||
| 13 | * This plugin will attempt to open an NNTP connection with the host. | ||
| 14 | * Successul connects return STATE_OK, refusals and timeouts return | ||
| 15 | * STATE_CRITICAL, other errors return STATE_UNKNOWN. Successful | ||
| 16 | * connects, but incorrect reponse messages from the host result in | ||
| 17 | * STATE_WARNING return values. An invalid newsgroup (if the -g | ||
| 18 | * option is used) results in a STATE_WARNING value. | ||
| 19 | * | ||
| 20 | * License Information: | ||
| 21 | * | ||
| 22 | * This program is free software; you can redistribute it and/or modify | ||
| 23 | * it under the terms of the GNU General Public License as published by | ||
| 24 | * the Free Software Foundation; either version 2 of the License, or | ||
| 25 | * (at your option) any later version. | ||
| 26 | * | ||
| 27 | * This program is distributed in the hope that it will be useful, | ||
| 28 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 29 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 30 | * GNU General Public License for more details. | ||
| 31 | * | ||
| 32 | * You should have received a copy of the GNU General Public License | ||
| 33 | * along with this program; if not, write to the Free Software | ||
| 34 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
| 35 | * | ||
| 36 | *****************************************************************************/ | ||
| 37 | |||
| 38 | #include "config.h" | ||
| 39 | #include "common.h" | ||
| 40 | #include "netutils.h" | ||
| 41 | #include "utils.h" | ||
| 42 | |||
| 43 | #define PROGNAME "check_nntp" | ||
| 44 | |||
| 45 | #define PORT 119 | ||
| 46 | #define EXPECT1A "200" /* posting allowed */ | ||
| 47 | #define EXPECT1B "201" /* posting not allowed */ | ||
| 48 | #define EXPECT "NNTP" | ||
| 49 | #define QUIT "QUIT\r\n" | ||
| 50 | #define MODEREADER "MODE READER\r\n" | ||
| 51 | |||
| 52 | int process_arguments (int, char **); | ||
| 53 | int call_getopt (int, char **); | ||
| 54 | int validate_arguments (void); | ||
| 55 | int check_disk (int usp, int free_disk); | ||
| 56 | void print_help (void); | ||
| 57 | void print_usage (void); | ||
| 58 | |||
| 59 | int server_port = PORT; | ||
| 60 | char *server_expect = NULL; | ||
| 61 | char *server_address = NULL; | ||
| 62 | char *newsgroup = NULL; | ||
| 63 | int check_newsgroup = FALSE; | ||
| 64 | int send_modereader = FALSE; | ||
| 65 | int warning_time = 0; | ||
| 66 | int check_warning_time = FALSE; | ||
| 67 | int critical_time = 0; | ||
| 68 | int check_critical_time = FALSE; | ||
| 69 | int verbose = FALSE; | ||
| 70 | |||
| 71 | |||
| 72 | int | ||
| 73 | main (int argc, char **argv) | ||
| 74 | { | ||
| 75 | char buffer[MAX_INPUT_BUFFER] = ""; | ||
| 76 | int sd; | ||
| 77 | int result; | ||
| 78 | int recv_result; | ||
| 79 | |||
| 80 | if (process_arguments (argc, argv) != OK) | ||
| 81 | usage ("Invalid command arguments supplied\n"); | ||
| 82 | |||
| 83 | /* initialize alarm signal handling */ | ||
| 84 | signal (SIGALRM, socket_timeout_alarm_handler); | ||
| 85 | |||
| 86 | /* set socket timeout */ | ||
| 87 | alarm (socket_timeout); | ||
| 88 | |||
| 89 | /* try to connect to the host at the given port number */ | ||
| 90 | time (&start_time); | ||
| 91 | result = my_tcp_connect (server_address, server_port, &sd); | ||
| 92 | |||
| 93 | /* we connected */ | ||
| 94 | if (result == STATE_OK) { | ||
| 95 | |||
| 96 | /* watch for the NNTP connection string */ | ||
| 97 | recv_result = recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0); | ||
| 98 | |||
| 99 | /* return a WARNING status if we couldn't read any data */ | ||
| 100 | if (recv_result == -1) { | ||
| 101 | |||
| 102 | printf ("recv() failed\n"); | ||
| 103 | result = STATE_WARNING; | ||
| 104 | |||
| 105 | } | ||
| 106 | else { | ||
| 107 | |||
| 108 | /* strip carriage returns from buffer */ | ||
| 109 | strip (buffer); | ||
| 110 | |||
| 111 | /* make sure we find the response we are looking for */ | ||
| 112 | if (strstr (buffer, EXPECT1A) != buffer | ||
| 113 | && strstr (buffer, EXPECT1B) != buffer) { | ||
| 114 | printf ("Invalid NNTP response received from host: %s\n", buffer); | ||
| 115 | result = STATE_WARNING; | ||
| 116 | } | ||
| 117 | |||
| 118 | /* optionally send mode reader */ | ||
| 119 | /* added 2000-09-18 by Andreas M. Kirchwitz <amk@krell.snafu.de> including some other minor fixes */ | ||
| 120 | if (send_modereader == TRUE) { | ||
| 121 | send (sd, MODEREADER, strlen (MODEREADER), 0); | ||
| 122 | recv_result = recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0); | ||
| 123 | if (recv_result == -1) | ||
| 124 | strcpy (buffer, ""); | ||
| 125 | else | ||
| 126 | buffer[recv_result] = 0; | ||
| 127 | strip (buffer); | ||
| 128 | if ((strstr (buffer, EXPECT1A) != buffer) | ||
| 129 | && (strstr (buffer, EXPECT1B) != buffer)) { | ||
| 130 | printf ("Unexpected NNTP response received from host"); | ||
| 131 | result = STATE_WARNING; | ||
| 132 | } | ||
| 133 | } | ||
| 134 | |||
| 135 | if (server_expect && !strstr (buffer, server_expect)) { | ||
| 136 | printf ("Invalid NNTP header received from host: %s\n", buffer); | ||
| 137 | result = STATE_WARNING; | ||
| 138 | } | ||
| 139 | |||
| 140 | if (check_newsgroup == TRUE) { | ||
| 141 | sprintf (buffer, "GROUP %s\r\n", newsgroup); | ||
| 142 | send (sd, buffer, strlen (buffer), 0); | ||
| 143 | recv_result = recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0); | ||
| 144 | if (recv_result == -1) | ||
| 145 | strcpy (buffer, ""); | ||
| 146 | else | ||
| 147 | buffer[recv_result] = 0; | ||
| 148 | strip (buffer); | ||
| 149 | if (strstr (buffer, "211") != buffer) { | ||
| 150 | printf ("NNTP problem - newsgroup '%s' not found\n", newsgroup); | ||
| 151 | result = STATE_WARNING; | ||
| 152 | } | ||
| 153 | } | ||
| 154 | |||
| 155 | if (result == STATE_OK) { | ||
| 156 | time (&end_time); | ||
| 157 | if (check_critical_time == TRUE | ||
| 158 | && (end_time - start_time) > critical_time) result = | ||
| 159 | STATE_CRITICAL; | ||
| 160 | else if (check_warning_time == TRUE | ||
| 161 | && (end_time - start_time) > warning_time) result = | ||
| 162 | STATE_WARNING; | ||
| 163 | if (verbose) | ||
| 164 | printf ("NNTP %s - %d sec. response time, %s\n", | ||
| 165 | (result == STATE_OK) ? "ok" : "problem", | ||
| 166 | (int) (end_time - start_time), buffer); | ||
| 167 | else | ||
| 168 | printf ("NNTP %s - %d second response time\n", | ||
| 169 | (result == STATE_OK) ? "ok" : "problem", | ||
| 170 | (int) (end_time - start_time)); | ||
| 171 | } | ||
| 172 | |||
| 173 | } | ||
| 174 | |||
| 175 | /* close the connection */ | ||
| 176 | send (sd, QUIT, strlen (QUIT), 0); | ||
| 177 | close (sd); | ||
| 178 | |||
| 179 | } | ||
| 180 | |||
| 181 | /* reset the alarm timeout */ | ||
| 182 | alarm (0); | ||
| 183 | |||
| 184 | return result; | ||
| 185 | } | ||
| 186 | |||
| 187 | |||
| 188 | |||
| 189 | |||
| 190 | |||
| 191 | |||
| 192 | /* process command-line arguments */ | ||
| 193 | int | ||
| 194 | process_arguments (int argc, char **argv) | ||
| 195 | { | ||
| 196 | int c; | ||
| 197 | |||
| 198 | if (argc < 2) | ||
| 199 | return ERROR; | ||
| 200 | |||
| 201 | for (c = 1; c < argc; c++) { | ||
| 202 | if (strcmp ("-to", argv[c]) == 0) | ||
| 203 | strcpy (argv[c], "-t"); | ||
| 204 | else if (strcmp ("-wt", argv[c]) == 0) | ||
| 205 | strcpy (argv[c], "-w"); | ||
| 206 | else if (strcmp ("-ct", argv[c]) == 0) | ||
| 207 | strcpy (argv[c], "-c"); | ||
| 208 | } | ||
| 209 | |||
| 210 | |||
| 211 | |||
| 212 | c = 0; | ||
| 213 | while ((c += (call_getopt (argc - c, &argv[c]))) < argc) { | ||
| 214 | |||
| 215 | if (is_option (argv[c])) | ||
| 216 | continue; | ||
| 217 | |||
| 218 | if (server_address == NULL) { | ||
| 219 | if (is_host (argv[c])) { | ||
| 220 | server_address = argv[c]; | ||
| 221 | } | ||
| 222 | else { | ||
| 223 | usage ("Invalid host name"); | ||
| 224 | } | ||
| 225 | } | ||
| 226 | } | ||
| 227 | |||
| 228 | if (server_address == NULL) | ||
| 229 | server_address = strscpy (NULL, "127.0.0.1"); | ||
| 230 | |||
| 231 | /* if (server_expect==NULL) */ | ||
| 232 | /* server_expect=strscpy(NULL,EXPECT); */ | ||
| 233 | |||
| 234 | return validate_arguments (); | ||
| 235 | } | ||
| 236 | |||
| 237 | |||
| 238 | |||
| 239 | |||
| 240 | |||
| 241 | |||
| 242 | int | ||
| 243 | call_getopt (int argc, char **argv) | ||
| 244 | { | ||
| 245 | int c, i = 0; | ||
| 246 | |||
| 247 | #ifdef HAVE_GETOPT_H | ||
| 248 | int option_index = 0; | ||
| 249 | static struct option long_options[] = { | ||
| 250 | {"hostname", required_argument, 0, 'H'}, | ||
| 251 | {"expect", required_argument, 0, 'e'}, | ||
| 252 | {"critical", required_argument, 0, 'c'}, | ||
| 253 | {"warning", required_argument, 0, 'w'}, | ||
| 254 | {"port", required_argument, 0, 'P'}, | ||
| 255 | {"modereader", required_argument, 0, 'M'}, | ||
| 256 | {"verbose", no_argument, 0, 'v'}, | ||
| 257 | {"version", no_argument, 0, 'V'}, | ||
| 258 | {"help", no_argument, 0, 'h'}, | ||
| 259 | {0, 0, 0, 0} | ||
| 260 | }; | ||
| 261 | #endif | ||
| 262 | |||
| 263 | while (1) { | ||
| 264 | #ifdef HAVE_GETOPT_H | ||
| 265 | c = | ||
| 266 | getopt_long (argc, argv, "+hVMvt:p:e:c:w:H:", long_options, | ||
| 267 | &option_index); | ||
| 268 | #else | ||
| 269 | c = getopt (argc, argv, "+?hVMvt:p:e:c:w:H:"); | ||
| 270 | #endif | ||
| 271 | |||
| 272 | i++; | ||
| 273 | |||
| 274 | if (c == -1 || c == EOF || c == 1) | ||
| 275 | break; | ||
| 276 | |||
| 277 | switch (c) { | ||
| 278 | case 't': | ||
| 279 | case 'p': | ||
| 280 | case 'e': | ||
| 281 | case 'c': | ||
| 282 | case 'w': | ||
| 283 | case 'H': | ||
| 284 | i++; | ||
| 285 | } | ||
| 286 | |||
| 287 | switch (c) { | ||
| 288 | case 'H': /* hostname */ | ||
| 289 | if (is_host (optarg)) { | ||
| 290 | server_address = optarg; | ||
| 291 | } | ||
| 292 | else { | ||
| 293 | usage ("Invalid host name\n"); | ||
| 294 | } | ||
| 295 | break; | ||
| 296 | case 'p': /* port */ | ||
| 297 | if (is_intpos (optarg)) { | ||
| 298 | server_port = atoi (optarg); | ||
| 299 | } | ||
| 300 | else { | ||
| 301 | usage ("Server port must be a positive integer\n"); | ||
| 302 | } | ||
| 303 | break; | ||
| 304 | case 'M': /* mode reader */ | ||
| 305 | send_modereader = TRUE; | ||
| 306 | break; | ||
| 307 | case 'e': /* expect */ | ||
| 308 | server_expect = optarg; | ||
| 309 | break; | ||
| 310 | case 'g': /* newsgroup */ | ||
| 311 | newsgroup = optarg; | ||
| 312 | check_newsgroup = TRUE; | ||
| 313 | break; | ||
| 314 | case 'c': /* critical time threshold */ | ||
| 315 | if (is_intnonneg (optarg)) { | ||
| 316 | critical_time = atoi (optarg); | ||
| 317 | check_critical_time = TRUE; | ||
| 318 | } | ||
| 319 | else { | ||
| 320 | usage ("Critical time must be a nonnegative integer\n"); | ||
| 321 | } | ||
| 322 | break; | ||
| 323 | case 'w': /* warning time threshold */ | ||
| 324 | if (is_intnonneg (optarg)) { | ||
| 325 | warning_time = atoi (optarg); | ||
| 326 | check_warning_time = TRUE; | ||
| 327 | } | ||
| 328 | else { | ||
| 329 | usage ("Warning time must be a nonnegative integer\n"); | ||
| 330 | } | ||
| 331 | break; | ||
| 332 | case 'v': /* verbose */ | ||
| 333 | verbose = TRUE; | ||
| 334 | break; | ||
| 335 | case 't': /* timeout */ | ||
| 336 | if (is_intnonneg (optarg)) { | ||
| 337 | socket_timeout = atoi (optarg); | ||
| 338 | } | ||
| 339 | else { | ||
| 340 | usage ("Time interval must be a nonnegative integer\n"); | ||
| 341 | } | ||
| 342 | break; | ||
| 343 | case 'V': /* version */ | ||
| 344 | print_revision (PROGNAME, "$Revision$"); | ||
| 345 | exit (STATE_OK); | ||
| 346 | case 'h': /* help */ | ||
| 347 | print_help (); | ||
| 348 | exit (STATE_OK); | ||
| 349 | case '?': /* help */ | ||
| 350 | usage ("Invalid argument\n"); | ||
| 351 | } | ||
| 352 | } | ||
| 353 | return i; | ||
| 354 | } | ||
| 355 | |||
| 356 | |||
| 357 | |||
| 358 | |||
| 359 | |||
| 360 | int | ||
| 361 | validate_arguments (void) | ||
| 362 | { | ||
| 363 | return OK; | ||
| 364 | } | ||
| 365 | |||
| 366 | |||
| 367 | |||
| 368 | |||
| 369 | |||
| 370 | void | ||
| 371 | print_help (void) | ||
| 372 | { | ||
| 373 | print_revision (PROGNAME, "$Revision$"); | ||
| 374 | printf | ||
| 375 | ("Copyright (c) 2000 Ethan Galstad/Karl DeBisschop\n\n" | ||
| 376 | "This plugin tests the NNTP service on the specified host.\n\n"); | ||
| 377 | print_usage (); | ||
| 378 | printf | ||
| 379 | ("\nOptions:\n" | ||
| 380 | " -H, --hostname=STRING or IPADDRESS\n" | ||
| 381 | " Check server on the indicated host\n" | ||
| 382 | " -p, --port=INTEGER\n" | ||
| 383 | " Make connection on the indicated port (default: %d)\n" | ||
| 384 | " -e, --expect=STRING\n" | ||
| 385 | " String to expect in first line of server response (default: %s)\n" | ||
| 386 | " -g, --group=STRING\n" | ||
| 387 | " Newsgroup to poll\n" | ||
| 388 | " -w, --warning=INTEGER\n" | ||
| 389 | " Seconds necessary to result in a warning status\n" | ||
| 390 | " -c, --critical=INTEGER\n" | ||
| 391 | " Seconds necessary to result in a critical status\n" | ||
| 392 | " -t, --timeout=INTEGER\n" | ||
| 393 | " Seconds before connection attempt times out (default: %d)\n" | ||
| 394 | " -M\n" | ||
| 395 | " Send \"MODE READER\" after initial connect.\n" | ||
| 396 | " -v, --verbose\n" | ||
| 397 | " Print extra information (command-line use only)\n" | ||
| 398 | " -h, --help\n" | ||
| 399 | " Print detailed help screen\n" | ||
| 400 | " -V, --version\n" | ||
| 401 | " Print version information\n\n", | ||
| 402 | PORT, EXPECT, DEFAULT_SOCKET_TIMEOUT); | ||
| 403 | support (); | ||
| 404 | } | ||
| 405 | |||
| 406 | |||
| 407 | |||
| 408 | |||
| 409 | |||
| 410 | void | ||
| 411 | print_usage (void) | ||
| 412 | { | ||
| 413 | printf | ||
| 414 | ("Usage: %s -H host [-e expect] [-g group] [-p port] [-w warn] [-c crit]\n" | ||
| 415 | " [-t timeout] [-v]\n" | ||
| 416 | " %s --help\n" | ||
| 417 | " %s --version\n", PROGNAME, PROGNAME, PROGNAME); | ||
| 418 | } | ||
