check_snmp.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  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 -p %s -m ALL -v 1 %s -c %s %s",
  137. PATH_TO_SNMPGET, port, 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. /*For consistency- full SNMP response every time */
  201. show = ssprintf (show, "%d", response);
  202. }
  203. else if (eval_method[i] & CRIT_STRING) {
  204. if (strcmp (response, string_value))
  205. iresult = STATE_CRITICAL;
  206. else
  207. iresult = STATE_OK;
  208. }
  209. else if (eval_method[i] & CRIT_REGEX) {
  210. #ifdef HAVE_REGEX_H
  211. excode = regexec (&preg, response, 10, pmatch, eflags);
  212. if (excode == 0) {
  213. iresult = STATE_OK;
  214. }
  215. else if (excode != REG_NOMATCH) {
  216. regerror (excode, &preg, errbuf, MAX_INPUT_BUFFER);
  217. printf ("Execute Error: %s\n", errbuf);
  218. exit (STATE_CRITICAL);
  219. }
  220. else {
  221. iresult = STATE_CRITICAL;
  222. }
  223. #else
  224. printf ("SNMP UNKNOWN: call for regex which was not a compiled option");
  225. exit (STATE_UNKNOWN);
  226. #endif
  227. }
  228. else {
  229. if (response)
  230. iresult = STATE_OK;
  231. else if (eval_method[i] & CRIT_PRESENT)
  232. iresult = STATE_CRITICAL;
  233. else
  234. iresult = STATE_WARNING;
  235. }
  236. result = max_state (result, iresult);
  237. if (nlabels > 1 && i < nlabels && labels[i] != NULL)
  238. outbuff = ssprintf
  239. (outbuff,
  240. "%s%s%s %s%s%s",
  241. outbuff,
  242. (i == 0) ? " " : output_delim,
  243. labels[i], mark (iresult), show, mark (iresult));
  244. else
  245. outbuff = ssprintf
  246. (outbuff,
  247. "%s%s%s%s%s",
  248. outbuff,
  249. (i == 0) ? " " : output_delim, mark (iresult), show, mark (iresult));
  250. if (nunits > 0 && i < nunits)
  251. outbuff = ssprintf (outbuff, "%s %s", outbuff, unitv[i]);
  252. i++;
  253. } /* end while */
  254. if (found == 0)
  255. terminate
  256. (STATE_UNKNOWN,
  257. "%s problem - No data recieved from host\nCMD: %s\n",
  258. label, command_line);
  259. /* WARNING if output found on stderr */
  260. if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
  261. result = max_state (result, STATE_WARNING);
  262. /* close stderr */
  263. (void) fclose (child_stderr);
  264. /* close the pipe */
  265. if (spclose (child_process))
  266. result = max_state (result, STATE_WARNING);
  267. if (nunits > 0)
  268. printf ("%s %s -%s\n", label, state_text (result), outbuff);
  269. else
  270. printf ("%s %s -%s %s\n", label, state_text (result), outbuff, units);
  271. return result;
  272. }
  273. /* process command-line arguments */
  274. int
  275. process_arguments (int argc, char **argv)
  276. {
  277. int c;
  278. if (argc < 2)
  279. return ERROR;
  280. for (c = 1; c < argc; c++) {
  281. if (strcmp ("-to", argv[c]) == 0)
  282. strcpy (argv[c], "-t");
  283. if (strcmp ("-wv", argv[c]) == 0)
  284. strcpy (argv[c], "-w");
  285. if (strcmp ("-cv", argv[c]) == 0)
  286. strcpy (argv[c], "-c");
  287. }
  288. c = 0;
  289. while (c += (call_getopt (argc - c, &argv[c]))) {
  290. if (argc <= c)
  291. break;
  292. if (server_address == NULL)
  293. server_address = strscpy (NULL, argv[c]);
  294. }
  295. if (community == NULL)
  296. community = strscpy (NULL, "public");
  297. if (delimiter == NULL)
  298. delimiter = strscpy (NULL, DEFAULT_DELIMITER);
  299. if (output_delim == NULL)
  300. output_delim = strscpy (NULL, DEFAULT_OUTPUT_DELIMITER);
  301. if (label == NULL)
  302. label = strscpy (NULL, "SNMP");
  303. if (units == NULL)
  304. units = strscpy (NULL, "");
  305. if (port == NULL)
  306. port = strscpy(NULL,"161");
  307. return c;
  308. }
  309. int
  310. call_getopt (int argc, char **argv)
  311. {
  312. char *ptr;
  313. int c, i = 1;
  314. int j = 0, jj = 0;
  315. #ifdef HAVE_GETOPT_H
  316. int option_index = 0;
  317. static struct option long_options[] = {
  318. {"help", no_argument, 0, 'h'},
  319. {"version", no_argument, 0, 'V'},
  320. {"timeout", required_argument, 0, 't'},
  321. {"critical", required_argument, 0, 'c'},
  322. {"warning", required_argument, 0, 'w'},
  323. {"hostname", required_argument, 0, 'H'},
  324. {"community", required_argument, 0, 'C'},
  325. {"oid", required_argument, 0, 'o'},
  326. {"object", required_argument, 0, 'o'},
  327. {"delimiter", required_argument, 0, 'd'},
  328. {"output-delimiter", required_argument, 0, 'D'},
  329. {"string", required_argument, 0, 's'},
  330. {"regex", required_argument, 0, 'r'},
  331. {"ereg", required_argument, 0, 'r'},
  332. {"eregi", required_argument, 0, 'R'},
  333. {"label", required_argument, 0, 'l'},
  334. {"units", required_argument, 0, 'u'},
  335. {"port", required_argument, 0, 'p'},
  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:p:",
  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:p:");
  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. case 'p': /* TCP port number */
  524. port = strscpy(port, optarg);
  525. break;
  526. }
  527. }
  528. return i;
  529. }
  530. void
  531. print_usage (void)
  532. {
  533. printf
  534. ("Usage: check_snmp -H <ip_address> -o <OID> [-w warn_range] [-c crit_range] \n"
  535. " [-C community] [-s string] [-r regex] [-R regexi] [-t timeout]\n"
  536. " [-l label] [-u units] [-p port-number] [-d delimiter] [-D output-delimiter]\n"
  537. " check_snmp --help\n" " check_snmp --version\n");
  538. }
  539. void
  540. print_help (char *cmd)
  541. {
  542. printf ("Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)\n"
  543. "License: GPL\n\n");
  544. print_usage ();
  545. printf
  546. ("\nOptions:\n"
  547. " -h, --help\n"
  548. " Print detailed help screen\n"
  549. " -V, --version\n"
  550. " Print version information\n"
  551. " -H, --hostname=HOST\n"
  552. " Name or IP address of the device you wish to query\n"
  553. " -o, --oid=OID(s)\n"
  554. " Object identifier(s) whose value you wish to query\n"
  555. " -w, --warning=INTEGER_RANGE(s)\n"
  556. " Range(s) which will not result in a WARNING status\n"
  557. " -c, --critical=INTEGER_RANGE(s)\n"
  558. " Range(s) which will not result in a CRITICAL status\n"
  559. " -C, --community=STRING\n"
  560. " Optional community string for SNMP communication\n"
  561. " (default is \"public\")\n"
  562. " -u, --units=STRING\n"
  563. " Units label(s) for output data (e.g., 'sec.').\n"
  564. " -p, --port=STRING\n"
  565. " UDP port number target is listening on.\n"
  566. " -d, --delimiter=STRING\n"
  567. " Delimiter to use when parsing returned data. Default is \"%s\"\n"
  568. " Any data on the right hand side of the delimiter is considered\n"
  569. " to be the data that should be used in the evaluation.\n"
  570. " -t, --timeout=INTEGER\n"
  571. " Seconds to wait before plugin times out (see also nagios server timeout)\n"
  572. " -D, --output-delimiter=STRING\n"
  573. " Separates output on multiple OID requests\n"
  574. " -s, --string=STRING\n"
  575. " Return OK state (for that OID) if STRING is an exact match\n"
  576. " -r, --ereg=REGEX\n"
  577. " Return OK state (for that OID) if extended regular expression REGEX matches\n"
  578. " -R, --eregi=REGEX\n"
  579. " Return OK state (for that OID) if case-insensitive extended REGEX matches\n"
  580. " -l, --label=STRING\n"
  581. " Prefix label for output from plugin (default -s 'SNMP')\n\n"
  582. "- This plugin uses the 'snmpget' command included with the UCD-SNMP package.\n"
  583. " If you don't have the package installed, you will need to download it from\n"
  584. " http://ucd-snmp.ucdavis.edu before you can use this plugin.\n"
  585. "- Multiple OIDs may be indicated by a comma- or space-delimited list (lists with\n"
  586. " internal spaces must be quoted)\n"
  587. "- Ranges are inclusive and are indicated with colons. When specified as\n"
  588. " 'min:max' a STATE_OK will be returned if the result is within the indicated\n"
  589. " range or is equal to the upper or lower bound. A non-OK state will be\n"
  590. " returned if the result is outside the specified range.\n"
  591. "- If spcified in the order 'max:min' a non-OK state will be returned if the\n"
  592. " result is within the (inclusive) range.\n"
  593. "- Upper or lower bounds may be omitted to skip checking the respective limit.\n"
  594. "- Bare integers are interpreted as upper limits.\n"
  595. "- When checking multiple OIDs, separate ranges by commas like '-w 1:10,1:,:20'\n"
  596. "- Note that only one string and one regex may be checked at present\n"
  597. "- All evaluation methods other than PR, STR, and SUBSTR expect that the value\n"
  598. " returned from the SNMP query is an unsigned integer.\n\n",
  599. DEFAULT_DELIMITER);
  600. }
  601. char *
  602. clarify_message (char *msg)
  603. {
  604. int i = 0;
  605. int foo;
  606. char tmpmsg_c[MAX_INPUT_BUFFER];
  607. char *tmpmsg = (char *) &tmpmsg_c;
  608. tmpmsg = strcpy (tmpmsg, msg);
  609. if (!strncmp (tmpmsg, " Hex:", 5)) {
  610. tmpmsg = strtok (tmpmsg, ":");
  611. while ((tmpmsg = strtok (NULL, " "))) {
  612. foo = strtol (tmpmsg, NULL, 16);
  613. /* Translate chars that are not the same value in the printers
  614. * character set.
  615. */
  616. switch (foo) {
  617. case 208:
  618. {
  619. foo = 197;
  620. break;
  621. }
  622. case 216:
  623. {
  624. foo = 196;
  625. break;
  626. }
  627. }
  628. msg[i] = foo;
  629. i++;
  630. }
  631. msg[i] = 0;
  632. }
  633. return (msg);
  634. }
  635. int
  636. check_num (int i)
  637. {
  638. int result;
  639. result = STATE_OK;
  640. if (eval_method[i] & WARN_GT && eval_method[i] & WARN_LT &&
  641. lower_warn_lim[i] > upper_warn_lim[i]) {
  642. if (response_value[i] <= lower_warn_lim[i] &&
  643. response_value[i] >= upper_warn_lim[i]) {
  644. result = STATE_WARNING;
  645. }
  646. }
  647. else if
  648. ((eval_method[i] & WARN_GT && response_value[i] > upper_warn_lim[i]) ||
  649. (eval_method[i] & WARN_GE && response_value[i] >= upper_warn_lim[i]) ||
  650. (eval_method[i] & WARN_LT && response_value[i] < lower_warn_lim[i]) ||
  651. (eval_method[i] & WARN_LE && response_value[i] <= lower_warn_lim[i]) ||
  652. (eval_method[i] & WARN_EQ && response_value[i] == upper_warn_lim[i]) ||
  653. (eval_method[i] & WARN_NE && response_value[i] != upper_warn_lim[i])) {
  654. result = STATE_WARNING;
  655. }
  656. if (eval_method[i] & CRIT_GT && eval_method[i] & CRIT_LT &&
  657. lower_warn_lim[i] > upper_warn_lim[i]) {
  658. if (response_value[i] <= lower_crit_lim[i] &&
  659. response_value[i] >= upper_crit_lim[i]) {
  660. result = STATE_CRITICAL;
  661. }
  662. }
  663. else if
  664. ((eval_method[i] & CRIT_GT && response_value[i] > upper_crit_lim[i]) ||
  665. (eval_method[i] & CRIT_GE && response_value[i] >= upper_crit_lim[i]) ||
  666. (eval_method[i] & CRIT_LT && response_value[i] < lower_crit_lim[i]) ||
  667. (eval_method[i] & CRIT_LE && response_value[i] <= lower_crit_lim[i]) ||
  668. (eval_method[i] & CRIT_EQ && response_value[i] == upper_crit_lim[i]) ||
  669. (eval_method[i] & CRIT_NE && response_value[i] != upper_crit_lim[i])) {
  670. result = STATE_CRITICAL;
  671. }
  672. return result;
  673. }
  674. int
  675. lu_getll (unsigned long *ll, char *str)
  676. {
  677. char tmp[100];
  678. if (strchr (str, ':') == NULL)
  679. return 0;
  680. if (strchr (str, ',') != NULL && (strchr (str, ',') < strchr (str, ':')))
  681. return 0;
  682. if (sscanf (str, "%lu%[:]", ll, tmp) == 2)
  683. return 1;
  684. return 0;
  685. }
  686. int
  687. lu_getul (unsigned long *ul, char *str)
  688. {
  689. char tmp[100];
  690. if (sscanf (str, "%lu%[^,]", ul, tmp) == 1)
  691. return 1;
  692. if (sscanf (str, ":%lu%[^,]", ul, tmp) == 1)
  693. return 1;
  694. if (sscanf (str, "%*u:%lu%[^,]", ul, tmp) == 1)
  695. return 1;
  696. return 0;
  697. }
  698. /* trim leading whitespace
  699. if there is a leading quote, make sure it balances */
  700. char *
  701. thisarg (char *str)
  702. {
  703. str += strspn (str, " \t\r\n"); /* trim any leading whitespace */
  704. if (strstr (str, "'") == str) { /* handle SIMPLE quoted strings */
  705. if (strlen (str) == 1 || !strstr (str + 1, "'"))
  706. terminate (STATE_UNKNOWN, "Unbalanced quotes\n");
  707. }
  708. return str;
  709. }
  710. /* if there's a leading quote, advance to the trailing quote
  711. set the trailing quote to '\x0'
  712. if the string continues, advance beyond the comma */
  713. char *
  714. nextarg (char *str)
  715. {
  716. if (strstr (str, "'") == str) {
  717. if (strlen (str) > 1) {
  718. str = strstr (str + 1, "'");
  719. str[0] = 0;
  720. return (++str);
  721. }
  722. else {
  723. str[0] = 0;
  724. return NULL;
  725. }
  726. }
  727. if (strstr (str, ",") == str) {
  728. if (strlen (str) > 1) {
  729. str[0] = 0;
  730. return (++str);
  731. }
  732. else {
  733. str[0] = 0;
  734. return NULL;
  735. }
  736. }
  737. if ((str = strstr (str, ",")) && strlen (str) > 1) {
  738. str[0] = 0;
  739. return (++str);
  740. }
  741. return NULL;
  742. }