check_snmp.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. /******************************************************************************
  2. *
  3. * CHECK_SNMP.C
  4. *
  5. * Program: SNMP plugin for Nagios
  6. * License: GPL
  7. * Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)
  8. *
  9. * Last Modified: $Date$
  10. *
  11. * Description:
  12. *
  13. * This plugin uses the 'snmpget' command included with the UCD-SNMP
  14. * package. If you don't have the package installed you will need to
  15. * download it from http://ucd-snmp.ucdavis.edu before you can use
  16. * this plugin.
  17. *
  18. * License Information:
  19. *
  20. * This program is free software; you can redistribute it and/or modify
  21. * it under the terms of the GNU General Public License as published by
  22. * the Free Software Foundation; either version 2 of the License, or
  23. * (at your option) any later version.
  24. *
  25. * This program is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. * GNU General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU General Public License
  31. * along with this program; if not, write to the Free Software
  32. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  33. *./plugins/check_snmp 127.0.0.1 -c public -o .1.3.6.1.4.1.2021.9.1.2.1
  34. *****************************************************************************/
  35. #include "common.h"
  36. #include "utils.h"
  37. #include "popen.h"
  38. #define PROGNAME check_snmp
  39. #define mark(a) ((a)!=0?"*":"")
  40. #define CHECK_UNDEF 0
  41. #define CRIT_PRESENT 1
  42. #define CRIT_STRING 2
  43. #define CRIT_REGEX 4
  44. #define CRIT_GT 8
  45. #define CRIT_LT 16
  46. #define CRIT_GE 32
  47. #define CRIT_LE 64
  48. #define CRIT_EQ 128
  49. #define CRIT_NE 256
  50. #define CRIT_RANGE 512
  51. #define WARN_PRESENT 1024
  52. #define WARN_STRING 2048
  53. #define WARN_REGEX 4096
  54. #define WARN_GT 8192
  55. #define WARN_LT 16384
  56. #define WARN_GE 32768
  57. #define WARN_LE 65536
  58. #define WARN_EQ 131072
  59. #define WARN_NE 262144
  60. #define WARN_RANGE 524288
  61. #define MAX_OIDS 8
  62. #define MAX_DELIM_LENGTH 8
  63. #define DEFAULT_DELIMITER "="
  64. #define DEFAULT_OUTPUT_DELIMITER " "
  65. void print_usage (void);
  66. void print_help (char *);
  67. int process_arguments (int, char **);
  68. int call_getopt (int, char **);
  69. int check_num (int);
  70. char *clarify_message (char *);
  71. int lu_getll (unsigned long *, char *);
  72. int lu_getul (unsigned long *, char *);
  73. char *thisarg (char *str);
  74. char *nextarg (char *str);
  75. #ifdef HAVE_REGEX_H
  76. #include <regex.h>
  77. char regex_expect[MAX_INPUT_BUFFER] = "";
  78. regex_t preg;
  79. regmatch_t pmatch[10];
  80. char timestamp[10] = "";
  81. char regex[MAX_INPUT_BUFFER];
  82. char errbuf[MAX_INPUT_BUFFER];
  83. int cflags = REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
  84. int eflags = 0;
  85. int errcode, excode;
  86. #endif
  87. char *server_address = NULL;
  88. char *community = NULL;
  89. char oid[MAX_INPUT_BUFFER] = "";
  90. char *label = NULL;
  91. char *units = NULL;
  92. char *port = NULL;
  93. char string_value[MAX_INPUT_BUFFER] = "";
  94. char **labels = NULL;
  95. char **unitv = NULL;
  96. int nlabels = 0;
  97. int labels_size = 8;
  98. int nunits = 0;
  99. int unitv_size = 8;
  100. unsigned long lower_warn_lim[MAX_OIDS];
  101. unsigned long upper_warn_lim[MAX_OIDS];
  102. unsigned long lower_crit_lim[MAX_OIDS];
  103. unsigned long upper_crit_lim[MAX_OIDS];
  104. unsigned long response_value[MAX_OIDS];
  105. int check_warning_value = FALSE;
  106. int check_critical_value = FALSE;
  107. int eval_method[MAX_OIDS];
  108. char *delimiter = NULL;
  109. char *output_delim = NULL;
  110. int
  111. main (int argc, char **argv)
  112. {
  113. int i = 0;
  114. int iresult = STATE_UNKNOWN;
  115. int found = 0;
  116. int result = STATE_DEPENDENT;
  117. char input_buffer[MAX_INPUT_BUFFER];
  118. char *command_line = NULL;
  119. char *response = NULL;
  120. char *outbuff = NULL;
  121. char *output = NULL;
  122. char *ptr = NULL;
  123. char *p2 = NULL;
  124. char *show = NULL;
  125. labels = malloc (labels_size);
  126. unitv = malloc (unitv_size);
  127. outbuff = strscpy (outbuff, "");
  128. for (i = 0; i < MAX_OIDS; i++)
  129. eval_method[i] = CHECK_UNDEF;
  130. i = 0;
  131. if (process_arguments (argc, argv) == ERROR)
  132. usage ("Incorrect arguments supplied\n");
  133. /* create the command line to execute */
  134. command_line = ssprintf
  135. (command_line,
  136. "%s -m ALL -v 1 %s %s %s",
  137. PATH_TO_SNMPGET, server_address, community, oid);
  138. /* run the command */
  139. child_process = spopen (command_line);
  140. if (child_process == NULL) {
  141. printf ("Could not open pipe: %s\n", command_line);
  142. exit (STATE_UNKNOWN);
  143. }
  144. child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  145. if (child_stderr == NULL) {
  146. printf ("Could not open stderr for %s\n", command_line);
  147. }
  148. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process))
  149. output = strscat (output, input_buffer);
  150. ptr = output;
  151. while (ptr) {
  152. ptr = strstr (ptr, delimiter);
  153. if (ptr == NULL)
  154. break;
  155. ptr += strlen (delimiter);
  156. ptr += strspn (ptr, " ");
  157. found++;
  158. if (ptr[0] == '"') {
  159. ptr++;
  160. response = strpcpy (response, ptr, "\"");
  161. ptr = strpbrk (ptr, "\"");
  162. ptr += strspn (ptr, "\"\n");
  163. }
  164. else {
  165. response = strpcpy (response, ptr, "\n");
  166. ptr = strpbrk (ptr, "\n");
  167. ptr += strspn (ptr, "\n");
  168. while
  169. (strstr (ptr, delimiter) &&
  170. strstr (ptr, "\n") && strstr (ptr, "\n") < strstr (ptr, delimiter)) {
  171. response = strpcat (response, ptr, "\n");
  172. ptr = strpbrk (ptr, "\n");
  173. }
  174. if (ptr && strstr (ptr, delimiter) == NULL) {
  175. response = strscat (response, ptr);
  176. ptr = NULL;
  177. }
  178. }
  179. if (strstr (response, "Gauge: "))
  180. show = strstr (response, "Gauge: ") + 7;
  181. else if (strstr (response, "Gauge32: "))
  182. show = strstr (response, "Gauge32: ") + 9;
  183. else
  184. show = response;
  185. p2 = show;
  186. if (eval_method[i] & CRIT_GT ||
  187. eval_method[i] & CRIT_LT ||
  188. eval_method[i] & CRIT_GE ||
  189. eval_method[i] & CRIT_LE ||
  190. eval_method[i] & CRIT_EQ ||
  191. eval_method[i] & CRIT_NE ||
  192. eval_method[i] & WARN_GT ||
  193. eval_method[i] & WARN_LT ||
  194. eval_method[i] & WARN_GE ||
  195. eval_method[i] & WARN_LE ||
  196. eval_method[i] & WARN_EQ || eval_method[i] & WARN_NE) {
  197. p2 = strpbrk (p2, "0123456789");
  198. response_value[i] = strtoul (p2, NULL, 10);
  199. iresult = check_num (i);
  200. show = ssprintf (show, "%lu", response_value[i]);
  201. }
  202. else if (eval_method[i] & CRIT_STRING) {
  203. if (strcmp (response, string_value))
  204. iresult = STATE_CRITICAL;
  205. else
  206. iresult = STATE_OK;
  207. }
  208. else if (eval_method[i] & CRIT_REGEX) {
  209. #ifdef HAVE_REGEX_H
  210. excode = regexec (&preg, response, 10, pmatch, eflags);
  211. if (excode == 0) {
  212. iresult = STATE_OK;
  213. }
  214. else if (excode != REG_NOMATCH) {
  215. regerror (excode, &preg, errbuf, MAX_INPUT_BUFFER);
  216. printf ("Execute Error: %s\n", errbuf);
  217. exit (STATE_CRITICAL);
  218. }
  219. else {
  220. iresult = STATE_CRITICAL;
  221. }
  222. #else
  223. printf ("SNMP UNKNOWN: call for regex which was not a compiled option");
  224. exit (STATE_UNKNOWN);
  225. #endif
  226. }
  227. else {
  228. if (response)
  229. iresult = STATE_OK;
  230. else if (eval_method[i] & CRIT_PRESENT)
  231. iresult = STATE_CRITICAL;
  232. else
  233. iresult = STATE_WARNING;
  234. }
  235. result = max_state (result, iresult);
  236. if (nlabels > 1 && i < nlabels && labels[i] != NULL)
  237. outbuff = ssprintf
  238. (outbuff,
  239. "%s%s%s %s%s%s",
  240. outbuff,
  241. (i == 0) ? " " : output_delim,
  242. labels[i], mark (iresult), show, mark (iresult));
  243. else
  244. outbuff = ssprintf
  245. (outbuff,
  246. "%s%s%s%s%s",
  247. outbuff,
  248. (i == 0) ? " " : output_delim, mark (iresult), show, mark (iresult));
  249. if (nunits > 0 && i < nunits)
  250. outbuff = ssprintf (outbuff, "%s %s", outbuff, unitv[i]);
  251. i++;
  252. } /* end while */
  253. if (found == 0)
  254. terminate
  255. (STATE_UNKNOWN,
  256. "%s problem - No data recieved from host\nCMD: %s\n",
  257. label, command_line);
  258. /* WARNING if output found on stderr */
  259. if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
  260. result = max_state (result, STATE_WARNING);
  261. /* close stderr */
  262. (void) fclose (child_stderr);
  263. /* close the pipe */
  264. if (spclose (child_process))
  265. result = max_state (result, STATE_WARNING);
  266. if (nunits > 0)
  267. printf ("%s %s -%s\n", label, state_text (result), outbuff);
  268. else
  269. printf ("%s %s -%s %s\n", label, state_text (result), outbuff, units);
  270. return result;
  271. }
  272. /* process command-line arguments */
  273. int
  274. process_arguments (int argc, char **argv)
  275. {
  276. int c;
  277. if (argc < 2)
  278. return ERROR;
  279. for (c = 1; c < argc; c++) {
  280. if (strcmp ("-to", argv[c]) == 0)
  281. strcpy (argv[c], "-t");
  282. if (strcmp ("-wv", argv[c]) == 0)
  283. strcpy (argv[c], "-w");
  284. if (strcmp ("-cv", argv[c]) == 0)
  285. strcpy (argv[c], "-c");
  286. }
  287. c = 0;
  288. while (c += (call_getopt (argc - c, &argv[c]))) {
  289. if (argc <= c)
  290. break;
  291. if (server_address == NULL)
  292. server_address = strscpy (NULL, argv[c]);
  293. }
  294. if (community == NULL)
  295. community = strscpy (NULL, "public");
  296. if (delimiter == NULL)
  297. delimiter = strscpy (NULL, DEFAULT_DELIMITER);
  298. if (output_delim == NULL)
  299. output_delim = strscpy (NULL, DEFAULT_OUTPUT_DELIMITER);
  300. if (label == NULL)
  301. label = strscpy (NULL, "SNMP");
  302. if (units == NULL)
  303. units = strscpy (NULL, "");
  304. if (port == NULL)
  305. port = strscpy(NULL,"161");
  306. if (port == NULL)
  307. port = strscpy(NULL,"161");
  308. return c;
  309. }
  310. int
  311. call_getopt (int argc, char **argv)
  312. {
  313. char *ptr;
  314. int c, i = 1;
  315. int j = 0, jj = 0;
  316. #ifdef HAVE_GETOPT_H
  317. int option_index = 0;
  318. static struct option long_options[] = {
  319. {"help", no_argument, 0, 'h'},
  320. {"version", no_argument, 0, 'V'},
  321. {"timeout", required_argument, 0, 't'},
  322. {"critical", required_argument, 0, 'c'},
  323. {"warning", required_argument, 0, 'w'},
  324. {"hostname", required_argument, 0, 'H'},
  325. {"community", required_argument, 0, 'C'},
  326. {"oid", required_argument, 0, 'o'},
  327. {"object", required_argument, 0, 'o'},
  328. {"delimiter", required_argument, 0, 'd'},
  329. {"output-delimiter", required_argument, 0, 'D'},
  330. {"string", required_argument, 0, 's'},
  331. {"regex", required_argument, 0, 'r'},
  332. {"ereg", required_argument, 0, 'r'},
  333. {"eregi", required_argument, 0, 'R'},
  334. {"label", required_argument, 0, 'l'},
  335. {"units", required_argument, 0, 'u'},
  336. {0, 0, 0, 0}
  337. };
  338. #endif
  339. while (1) {
  340. #ifdef HAVE_GETOPT_H
  341. c =
  342. getopt_long (argc, argv, "+?hVt:c:w:H:C:o:d:D:s:R:r:l:u:",
  343. long_options, &option_index);
  344. #else
  345. c = getopt (argc, argv, "+?hVt:c:w:H:C:o:d:D:s:R:r:l:u:");
  346. #endif
  347. if (c == -1 || c == EOF)
  348. break;
  349. i++;
  350. switch (c) {
  351. case 't':
  352. case 'c':
  353. case 'w':
  354. case 'H':
  355. case 'C':
  356. case 'o':
  357. case 'd':
  358. case 'D':
  359. case 's':
  360. case 'R':
  361. case 'r':
  362. case 'l':
  363. case 'u':
  364. case 'p':
  365. i++;
  366. }
  367. switch (c) {
  368. case '?': /* help */
  369. printf ("%s: Unknown argument: %s\n\n", my_basename (argv[0]), optarg);
  370. print_usage ();
  371. exit (STATE_UNKNOWN);
  372. case 'h': /* help */
  373. print_help (my_basename (argv[0]));
  374. exit (STATE_OK);
  375. case 'V': /* version */
  376. print_revision (my_basename (argv[0]), "$Revision$");
  377. exit (STATE_OK);
  378. case 't': /* timeout period */
  379. if (!is_integer (optarg)) {
  380. printf ("%s: Timeout Interval must be an integer!\n\n",
  381. my_basename (argv[0]));
  382. print_usage ();
  383. exit (STATE_UNKNOWN);
  384. }
  385. timeout_interval = atoi (optarg);
  386. break;
  387. case 'c': /* critical time threshold */
  388. if (strspn (optarg, "0123456789:,") < strlen (optarg)) {
  389. printf ("Invalid critical threshold: %s\n", optarg);
  390. print_usage ();
  391. exit (STATE_UNKNOWN);
  392. }
  393. for (ptr = optarg, jj = 0; ptr && jj < MAX_OIDS; jj++) {
  394. if (lu_getll (&lower_crit_lim[jj], ptr) == 1)
  395. eval_method[jj] |= CRIT_LT;
  396. if (lu_getul (&upper_crit_lim[jj], ptr) == 1)
  397. eval_method[jj] |= CRIT_GT;
  398. (ptr = index (ptr, ',')) ? ptr++ : ptr;
  399. }
  400. break;
  401. case 'w': /* warning time threshold */
  402. if (strspn (optarg, "0123456789:,") < strlen (optarg)) {
  403. printf ("Invalid warning threshold: %s\n", optarg);
  404. print_usage ();
  405. exit (STATE_UNKNOWN);
  406. }
  407. for (ptr = optarg, jj = 0; ptr && jj < MAX_OIDS; jj++) {
  408. if (lu_getll (&lower_warn_lim[jj], ptr) == 1)
  409. eval_method[jj] |= WARN_LT;
  410. if (lu_getul (&upper_warn_lim[jj], ptr) == 1)
  411. eval_method[jj] |= WARN_GT;
  412. (ptr = index (ptr, ',')) ? ptr++ : ptr;
  413. }
  414. break;
  415. case 'H': /* Host or server */
  416. server_address = strscpy (server_address, optarg);
  417. break;
  418. case 'C': /* group or community */
  419. community = strscpy (community, optarg);
  420. break;
  421. case 'o': /* object identifier */
  422. for (ptr = optarg; (ptr = index (ptr, ',')); ptr++)
  423. ptr[0] = ' ';
  424. strncpy (oid, optarg, sizeof (oid) - 1);
  425. oid[sizeof (oid) - 1] = 0;
  426. for (ptr = optarg, j = 1; (ptr = index (ptr, ' ')); ptr++)
  427. j++;
  428. break;
  429. case 'd': /* delimiter */
  430. delimiter = strscpy (delimiter, optarg);
  431. break;
  432. case 'D': /* output-delimiter */
  433. output_delim = strscpy (output_delim, optarg);
  434. break;
  435. case 's': /* string or substring */
  436. strncpy (string_value, optarg, sizeof (string_value) - 1);
  437. string_value[sizeof (string_value) - 1] = 0;
  438. eval_method[jj++] = CRIT_STRING;
  439. break;
  440. case 'R': /* regex */
  441. #ifdef HAVE_REGEX_H
  442. cflags = REG_ICASE;
  443. #endif
  444. case 'r': /* regex */
  445. #ifdef HAVE_REGEX_H
  446. cflags |= REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
  447. strncpy (regex_expect, optarg, sizeof (regex_expect) - 1);
  448. regex_expect[sizeof (regex_expect) - 1] = 0;
  449. errcode = regcomp (&preg, regex_expect, cflags);
  450. if (errcode != 0) {
  451. regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER);
  452. printf ("Could Not Compile Regular Expression");
  453. return ERROR;
  454. }
  455. eval_method[jj++] = CRIT_REGEX;
  456. #else
  457. printf ("SNMP UNKNOWN: call for regex which was not a compiled option");
  458. exit (STATE_UNKNOWN);
  459. #endif
  460. break;
  461. case 'l': /* label */
  462. label = optarg;
  463. nlabels++;
  464. if (nlabels >= labels_size) {
  465. labels_size += 8;
  466. labels = realloc (labels, labels_size);
  467. if (labels == NULL)
  468. terminate (STATE_UNKNOWN,
  469. "Could not realloc() labels[%d]", nlabels);
  470. }
  471. labels[nlabels - 1] = optarg;
  472. ptr = thisarg (optarg);
  473. if (strstr (ptr, "'") == ptr)
  474. labels[nlabels - 1] = ptr + 1;
  475. else
  476. labels[nlabels - 1] = ptr;
  477. while (ptr && (ptr = nextarg (ptr))) {
  478. if (nlabels >= labels_size) {
  479. labels_size += 8;
  480. labels = realloc (labels, labels_size);
  481. if (labels == NULL)
  482. terminate (STATE_UNKNOWN, "Could not realloc() labels\n");
  483. }
  484. labels++;
  485. ptr = thisarg (ptr);
  486. if (strstr (ptr, "'") == ptr)
  487. labels[nlabels - 1] = ptr + 1;
  488. else
  489. labels[nlabels - 1] = ptr;
  490. }
  491. break;
  492. case 'u': /* units */
  493. units = optarg;
  494. nunits++;
  495. if (nunits >= unitv_size) {
  496. unitv_size += 8;
  497. unitv = realloc (unitv, unitv_size);
  498. if (unitv == NULL)
  499. terminate (STATE_UNKNOWN,
  500. "Could not realloc() units [%d]\n", nunits);
  501. }
  502. unitv[nunits - 1] = optarg;
  503. ptr = thisarg (optarg);
  504. if (strstr (ptr, "'") == ptr)
  505. unitv[nunits - 1] = ptr + 1;
  506. else
  507. unitv[nunits - 1] = ptr;
  508. while (ptr && (ptr = nextarg (ptr))) {
  509. if (nunits >= unitv_size) {
  510. unitv_size += 8;
  511. unitv = realloc (unitv, unitv_size);
  512. if (units == NULL)
  513. terminate (STATE_UNKNOWN, "Could not realloc() units\n");
  514. }
  515. nunits++;
  516. ptr = thisarg (ptr);
  517. if (strstr (ptr, "'") == ptr)
  518. unitv[nunits - 1] = ptr + 1;
  519. else
  520. unitv[nunits - 1] = ptr;
  521. }
  522. break;
  523. }
  524. }
  525. return i;
  526. }
  527. void
  528. print_usage (void)
  529. {
  530. printf
  531. ("Usage: check_snmp -H <ip_address> -o <OID> [-w warn_range] [-c crit_range] \n"
  532. " [-C community] [-s string] [-r regex] [-R regexi] [-t timeout]\n"
  533. " [-l label] [-u units] [-d delimiter] [-D output-delimiter]\n"
  534. " check_snmp --help\n" " check_snmp --version\n");
  535. }
  536. void
  537. print_help (char *cmd)
  538. {
  539. printf ("Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)\n"
  540. "License: GPL\n\n");
  541. print_usage ();
  542. printf
  543. ("\nOptions:\n"
  544. " -h, --help\n"
  545. " Print detailed help screen\n"
  546. " -V, --version\n"
  547. " Print version information\n"
  548. " -H, --hostname=HOST\n"
  549. " Name or IP address of the device you wish to query\n"
  550. " -o, --oid=OID(s)\n"
  551. " Object identifier(s) whose value you wish to query\n"
  552. " -w, --warning=INTEGER_RANGE(s)\n"
  553. " Range(s) which will not result in a WARNING status\n"
  554. " -c, --critical=INTEGER_RANGE(s)\n"
  555. " Range(s) which will not result in a CRITICAL status\n"
  556. " -C, --community=STRING\n"
  557. " Optional community string for SNMP communication\n"
  558. " (default is \"public\")\n"
  559. " -u, --units=STRING\n"
  560. " Units label(s) for output data (e.g., 'sec.').\n"
  561. " -p, --port=STRING\n"
  562. " TCP port number target is listening on.\n"
  563. " -d, --delimiter=STRING\n"
  564. " Delimiter to use when parsing returned data. Default is \"%s\"\n"
  565. " Any data on the right hand side of the delimiter is considered\n"
  566. " to be the data that should be used in the evaluation.\n"
  567. " -t, --timeout=INTEGER\n"
  568. " Seconds to wait before plugin times out (see also nagios server timeout)\n"
  569. " -D, --output-delimiter=STRING\n"
  570. " Separates output on multiple OID requests\n"
  571. " -s, --string=STRING\n"
  572. " Return OK state (for that OID) if STRING is an exact match\n"
  573. " -r, --ereg=REGEX\n"
  574. " Return OK state (for that OID) if extended regular expression REGEX matches\n"
  575. " -R, --eregi=REGEX\n"
  576. " Return OK state (for that OID) if case-insensitive extended REGEX matches\n"
  577. " -l, --label=STRING\n"
  578. " Prefix label for output from plugin (default -s 'SNMP')\n\n"
  579. "- This plugin uses the 'snmpget' command included with the UCD-SNMP package.\n"
  580. " If you don't have the package installed, you will need to download it from\n"
  581. " http://ucd-snmp.ucdavis.edu before you can use this plugin.\n"
  582. "- Multiple OIDs may be indicated by a comma- or space-delimited list (lists with\n"
  583. " internal spaces must be quoted)\n"
  584. "- Ranges are inclusive and are indicated with colons. When specified as\n"
  585. " 'min:max' a STATE_OK will be returned if the result is within the indicated\n"
  586. " range or is equal to the upper or lower bound. A non-OK state will be\n"
  587. " returned if the result is outside the specified range.\n"
  588. "- If spcified in the order 'max:min' a non-OK state will be returned if the\n"
  589. " result is within the (inclusive) range.\n"
  590. "- Upper or lower bounds may be omitted to skip checking the respective limit.\n"
  591. "- Bare integers are interpreted as upper limits.\n"
  592. "- When checking multiple OIDs, separate ranges by commas like '-w 1:10,1:,:20'\n"
  593. "- Note that only one string and one regex may be checked at present\n"
  594. "- All evaluation methods other than PR, STR, and SUBSTR expect that the value\n"
  595. " returned from the SNMP query is an unsigned integer.\n\n",
  596. DEFAULT_DELIMITER);
  597. }
  598. char *
  599. clarify_message (char *msg)
  600. {
  601. int i = 0;
  602. int foo;
  603. char tmpmsg_c[MAX_INPUT_BUFFER];
  604. char *tmpmsg = (char *) &tmpmsg_c;
  605. tmpmsg = strcpy (tmpmsg, msg);
  606. if (!strncmp (tmpmsg, " Hex:", 5)) {
  607. tmpmsg = strtok (tmpmsg, ":");
  608. while ((tmpmsg = strtok (NULL, " "))) {
  609. foo = strtol (tmpmsg, NULL, 16);
  610. /* Translate chars that are not the same value in the printers
  611. * character set.
  612. */
  613. switch (foo) {
  614. case 208:
  615. {
  616. foo = 197;
  617. break;
  618. }
  619. case 216:
  620. {
  621. foo = 196;
  622. break;
  623. }
  624. }
  625. msg[i] = foo;
  626. i++;
  627. }
  628. msg[i] = 0;
  629. }
  630. return (msg);
  631. }
  632. int
  633. check_num (int i)
  634. {
  635. int result;
  636. result = STATE_OK;
  637. if (eval_method[i] & WARN_GT && eval_method[i] & WARN_LT &&
  638. lower_warn_lim[i] > upper_warn_lim[i]) {
  639. if (response_value[i] <= lower_warn_lim[i] &&
  640. response_value[i] >= upper_warn_lim[i]) {
  641. result = STATE_WARNING;
  642. }
  643. }
  644. else if
  645. ((eval_method[i] & WARN_GT && response_value[i] > upper_warn_lim[i]) ||
  646. (eval_method[i] & WARN_GE && response_value[i] >= upper_warn_lim[i]) ||
  647. (eval_method[i] & WARN_LT && response_value[i] < lower_warn_lim[i]) ||
  648. (eval_method[i] & WARN_LE && response_value[i] <= lower_warn_lim[i]) ||
  649. (eval_method[i] & WARN_EQ && response_value[i] == upper_warn_lim[i]) ||
  650. (eval_method[i] & WARN_NE && response_value[i] != upper_warn_lim[i])) {
  651. result = STATE_WARNING;
  652. }
  653. if (eval_method[i] & CRIT_GT && eval_method[i] & CRIT_LT &&
  654. lower_warn_lim[i] > upper_warn_lim[i]) {
  655. if (response_value[i] <= lower_crit_lim[i] &&
  656. response_value[i] >= upper_crit_lim[i]) {
  657. result = STATE_CRITICAL;
  658. }
  659. }
  660. else if
  661. ((eval_method[i] & CRIT_GT && response_value[i] > upper_crit_lim[i]) ||
  662. (eval_method[i] & CRIT_GE && response_value[i] >= upper_crit_lim[i]) ||
  663. (eval_method[i] & CRIT_LT && response_value[i] < lower_crit_lim[i]) ||
  664. (eval_method[i] & CRIT_LE && response_value[i] <= lower_crit_lim[i]) ||
  665. (eval_method[i] & CRIT_EQ && response_value[i] == upper_crit_lim[i]) ||
  666. (eval_method[i] & CRIT_NE && response_value[i] != upper_crit_lim[i])) {
  667. result = STATE_CRITICAL;
  668. }
  669. return result;
  670. }
  671. int
  672. lu_getll (unsigned long *ll, char *str)
  673. {
  674. char tmp[100];
  675. if (strchr (str, ':') == NULL)
  676. return 0;
  677. if (strchr (str, ',') != NULL && (strchr (str, ',') < strchr (str, ':')))
  678. return 0;
  679. if (sscanf (str, "%lu%[:]", ll, tmp) == 2)
  680. return 1;
  681. return 0;
  682. }
  683. int
  684. lu_getul (unsigned long *ul, char *str)
  685. {
  686. char tmp[100];
  687. if (sscanf (str, "%lu%[^,]", ul, tmp) == 1)
  688. return 1;
  689. if (sscanf (str, ":%lu%[^,]", ul, tmp) == 1)
  690. return 1;
  691. if (sscanf (str, "%*u:%lu%[^,]", ul, tmp) == 1)
  692. return 1;
  693. return 0;
  694. }
  695. /* trim leading whitespace
  696. if there is a leading quote, make sure it balances */
  697. char *
  698. thisarg (char *str)
  699. {
  700. str += strspn (str, " \t\r\n"); /* trim any leading whitespace */
  701. if (strstr (str, "'") == str) { /* handle SIMPLE quoted strings */
  702. if (strlen (str) == 1 || !strstr (str + 1, "'"))
  703. terminate (STATE_UNKNOWN, "Unbalanced quotes\n");
  704. }
  705. return str;
  706. }
  707. /* if there's a leading quote, advance to the trailing quote
  708. set the trailing quote to '\x0'
  709. if the string continues, advance beyond the comma */
  710. char *
  711. nextarg (char *str)
  712. {
  713. if (strstr (str, "'") == str) {
  714. if (strlen (str) > 1) {
  715. str = strstr (str + 1, "'");
  716. str[0] = 0;
  717. return (++str);
  718. }
  719. else {
  720. str[0] = 0;
  721. return NULL;
  722. }
  723. }
  724. if (strstr (str, ",") == str) {
  725. if (strlen (str) > 1) {
  726. str[0] = 0;
  727. return (++str);
  728. }
  729. else {
  730. str[0] = 0;
  731. return NULL;
  732. }
  733. }
  734. if ((str = strstr (str, ",")) && strlen (str) > 1) {
  735. str[0] = 0;
  736. return (++str);
  737. }
  738. return NULL;
  739. }