check_snmp.c 22 KB

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