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. asprintf
  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. asprintf (&show, "%lu", response_value[i]);
  201. /*asprintf (&show, "%s", 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. asprintf
  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. asprintf (&outbuff, "%s%s%s%s%s", outbuff,
  246. (i == 0) ? " " : output_delim, mark (iresult), show, mark (iresult));
  247. if (nunits > 0 && i < nunits)
  248. asprintf (&outbuff, "%s %s", outbuff, unitv[i]);
  249. i++;
  250. } /* end while */
  251. if (found == 0)
  252. terminate
  253. (STATE_UNKNOWN,
  254. "%s problem - No data recieved from host\nCMD: %s\n",
  255. label, command_line);
  256. /* WARNING if output found on stderr */
  257. if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
  258. result = max_state (result, STATE_WARNING);
  259. /* close stderr */
  260. (void) fclose (child_stderr);
  261. /* close the pipe */
  262. if (spclose (child_process))
  263. result = max_state (result, STATE_WARNING);
  264. if (nunits > 0)
  265. printf ("%s %s -%s\n", label, state_text (result), outbuff);
  266. else
  267. printf ("%s %s -%s %s\n", label, state_text (result), outbuff, units);
  268. return result;
  269. }
  270. /* process command-line arguments */
  271. int
  272. process_arguments (int argc, char **argv)
  273. {
  274. int c;
  275. if (argc < 2)
  276. return ERROR;
  277. for (c = 1; c < argc; c++) {
  278. if (strcmp ("-to", argv[c]) == 0)
  279. strcpy (argv[c], "-t");
  280. if (strcmp ("-wv", argv[c]) == 0)
  281. strcpy (argv[c], "-w");
  282. if (strcmp ("-cv", argv[c]) == 0)
  283. strcpy (argv[c], "-c");
  284. }
  285. c = 0;
  286. while (c += (call_getopt (argc - c, &argv[c]))) {
  287. if (argc <= c)
  288. break;
  289. if (server_address == NULL)
  290. server_address = strscpy (NULL, argv[c]);
  291. }
  292. if (community == NULL)
  293. community = strscpy (NULL, "public");
  294. if (delimiter == NULL)
  295. delimiter = strscpy (NULL, DEFAULT_DELIMITER);
  296. if (output_delim == NULL)
  297. output_delim = strscpy (NULL, DEFAULT_OUTPUT_DELIMITER);
  298. if (label == NULL)
  299. label = strscpy (NULL, "SNMP");
  300. if (units == NULL)
  301. units = strscpy (NULL, "");
  302. if (port == NULL)
  303. port = strscpy(NULL,"161");
  304. return c;
  305. }
  306. int
  307. call_getopt (int argc, char **argv)
  308. {
  309. char *ptr;
  310. int c, i = 1;
  311. int j = 0, jj = 0;
  312. #ifdef HAVE_GETOPT_H
  313. int option_index = 0;
  314. static struct option long_options[] = {
  315. {"help", no_argument, 0, 'h'},
  316. {"version", no_argument, 0, 'V'},
  317. {"timeout", required_argument, 0, 't'},
  318. {"critical", required_argument, 0, 'c'},
  319. {"warning", required_argument, 0, 'w'},
  320. {"hostname", required_argument, 0, 'H'},
  321. {"community", required_argument, 0, 'C'},
  322. {"oid", required_argument, 0, 'o'},
  323. {"object", required_argument, 0, 'o'},
  324. {"delimiter", required_argument, 0, 'd'},
  325. {"output-delimiter", required_argument, 0, 'D'},
  326. {"string", required_argument, 0, 's'},
  327. {"regex", required_argument, 0, 'r'},
  328. {"ereg", required_argument, 0, 'r'},
  329. {"eregi", required_argument, 0, 'R'},
  330. {"label", required_argument, 0, 'l'},
  331. {"units", required_argument, 0, 'u'},
  332. {"port", required_argument, 0, 'p'},
  333. {0, 0, 0, 0}
  334. };
  335. #endif
  336. while (1) {
  337. #ifdef HAVE_GETOPT_H
  338. c =
  339. getopt_long (argc, argv, "+?hVt:c:w:H:C:o:d:D:s:R:r:l:u:p:",
  340. long_options, &option_index);
  341. #else
  342. c = getopt (argc, argv, "+?hVt:c:w:H:C:o:d:D:s:R:r:l:u:p:");
  343. #endif
  344. if (c == -1 || c == EOF)
  345. break;
  346. i++;
  347. switch (c) {
  348. case 't':
  349. case 'c':
  350. case 'w':
  351. case 'H':
  352. case 'C':
  353. case 'o':
  354. case 'd':
  355. case 'D':
  356. case 's':
  357. case 'R':
  358. case 'r':
  359. case 'l':
  360. case 'u':
  361. case 'p':
  362. i++;
  363. }
  364. switch (c) {
  365. case '?': /* help */
  366. printf ("%s: Unknown argument: %s\n\n", my_basename (argv[0]), optarg);
  367. print_usage ();
  368. exit (STATE_UNKNOWN);
  369. case 'h': /* help */
  370. print_help (my_basename (argv[0]));
  371. exit (STATE_OK);
  372. case 'V': /* version */
  373. print_revision (my_basename (argv[0]), "$Revision$");
  374. exit (STATE_OK);
  375. case 't': /* timeout period */
  376. if (!is_integer (optarg)) {
  377. printf ("%s: Timeout Interval must be an integer!\n\n",
  378. my_basename (argv[0]));
  379. print_usage ();
  380. exit (STATE_UNKNOWN);
  381. }
  382. timeout_interval = atoi (optarg);
  383. break;
  384. case 'c': /* critical time threshold */
  385. if (strspn (optarg, "0123456789:,") < strlen (optarg)) {
  386. printf ("Invalid critical threshold: %s\n", optarg);
  387. print_usage ();
  388. exit (STATE_UNKNOWN);
  389. }
  390. for (ptr = optarg, jj = 0; ptr && jj < MAX_OIDS; jj++) {
  391. if (lu_getll (&lower_crit_lim[jj], ptr) == 1)
  392. eval_method[jj] |= CRIT_LT;
  393. if (lu_getul (&upper_crit_lim[jj], ptr) == 1)
  394. eval_method[jj] |= CRIT_GT;
  395. (ptr = index (ptr, ',')) ? ptr++ : ptr;
  396. }
  397. break;
  398. case 'w': /* warning time threshold */
  399. if (strspn (optarg, "0123456789:,") < strlen (optarg)) {
  400. printf ("Invalid warning threshold: %s\n", optarg);
  401. print_usage ();
  402. exit (STATE_UNKNOWN);
  403. }
  404. for (ptr = optarg, jj = 0; ptr && jj < MAX_OIDS; jj++) {
  405. if (lu_getll (&lower_warn_lim[jj], ptr) == 1)
  406. eval_method[jj] |= WARN_LT;
  407. if (lu_getul (&upper_warn_lim[jj], ptr) == 1)
  408. eval_method[jj] |= WARN_GT;
  409. (ptr = index (ptr, ',')) ? ptr++ : ptr;
  410. }
  411. break;
  412. case 'H': /* Host or server */
  413. server_address = strscpy (server_address, optarg);
  414. break;
  415. case 'C': /* group or community */
  416. community = strscpy (community, optarg);
  417. break;
  418. case 'o': /* object identifier */
  419. for (ptr = optarg; (ptr = index (ptr, ',')); ptr++)
  420. ptr[0] = ' ';
  421. strncpy (oid, optarg, sizeof (oid) - 1);
  422. oid[sizeof (oid) - 1] = 0;
  423. for (ptr = optarg, j = 1; (ptr = index (ptr, ' ')); ptr++)
  424. j++;
  425. break;
  426. case 'd': /* delimiter */
  427. delimiter = strscpy (delimiter, optarg);
  428. break;
  429. case 'D': /* output-delimiter */
  430. output_delim = strscpy (output_delim, optarg);
  431. break;
  432. case 's': /* string or substring */
  433. strncpy (string_value, optarg, sizeof (string_value) - 1);
  434. string_value[sizeof (string_value) - 1] = 0;
  435. eval_method[jj++] = CRIT_STRING;
  436. break;
  437. case 'R': /* regex */
  438. #ifdef HAVE_REGEX_H
  439. cflags = REG_ICASE;
  440. #endif
  441. case 'r': /* regex */
  442. #ifdef HAVE_REGEX_H
  443. cflags |= REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
  444. strncpy (regex_expect, optarg, sizeof (regex_expect) - 1);
  445. regex_expect[sizeof (regex_expect) - 1] = 0;
  446. errcode = regcomp (&preg, regex_expect, cflags);
  447. if (errcode != 0) {
  448. regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER);
  449. printf ("Could Not Compile Regular Expression");
  450. return ERROR;
  451. }
  452. eval_method[jj++] = CRIT_REGEX;
  453. #else
  454. printf ("SNMP UNKNOWN: call for regex which was not a compiled option");
  455. exit (STATE_UNKNOWN);
  456. #endif
  457. break;
  458. case 'l': /* label */
  459. label = optarg;
  460. nlabels++;
  461. if (nlabels >= labels_size) {
  462. labels_size += 8;
  463. labels = realloc (labels, labels_size);
  464. if (labels == NULL)
  465. terminate (STATE_UNKNOWN,
  466. "Could not realloc() labels[%d]", nlabels);
  467. }
  468. labels[nlabels - 1] = optarg;
  469. ptr = thisarg (optarg);
  470. if (strstr (ptr, "'") == ptr)
  471. labels[nlabels - 1] = ptr + 1;
  472. else
  473. labels[nlabels - 1] = ptr;
  474. while (ptr && (ptr = nextarg (ptr))) {
  475. if (nlabels >= labels_size) {
  476. labels_size += 8;
  477. labels = realloc (labels, labels_size);
  478. if (labels == NULL)
  479. terminate (STATE_UNKNOWN, "Could not realloc() labels\n");
  480. }
  481. labels++;
  482. ptr = thisarg (ptr);
  483. if (strstr (ptr, "'") == ptr)
  484. labels[nlabels - 1] = ptr + 1;
  485. else
  486. labels[nlabels - 1] = ptr;
  487. }
  488. break;
  489. case 'u': /* units */
  490. units = optarg;
  491. nunits++;
  492. if (nunits >= unitv_size) {
  493. unitv_size += 8;
  494. unitv = realloc (unitv, unitv_size);
  495. if (unitv == NULL)
  496. terminate (STATE_UNKNOWN,
  497. "Could not realloc() units [%d]\n", nunits);
  498. }
  499. unitv[nunits - 1] = optarg;
  500. ptr = thisarg (optarg);
  501. if (strstr (ptr, "'") == ptr)
  502. unitv[nunits - 1] = ptr + 1;
  503. else
  504. unitv[nunits - 1] = ptr;
  505. while (ptr && (ptr = nextarg (ptr))) {
  506. if (nunits >= unitv_size) {
  507. unitv_size += 8;
  508. unitv = realloc (unitv, unitv_size);
  509. if (units == NULL)
  510. terminate (STATE_UNKNOWN, "Could not realloc() units\n");
  511. }
  512. nunits++;
  513. ptr = thisarg (ptr);
  514. if (strstr (ptr, "'") == ptr)
  515. unitv[nunits - 1] = ptr + 1;
  516. else
  517. unitv[nunits - 1] = ptr;
  518. }
  519. break;
  520. case 'p': /* TCP port number */
  521. port = strscpy(port, optarg);
  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] [-p port-number] [-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. " UDP 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. }