check_snmp.c 22 KB

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