check_snmp.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  1. /******************************************************************************
  2. *
  3. * Nagios check_snmp plugin
  4. *
  5. * License: GPL
  6. * Copyright (c) 1999-2006 nagios-plugins team
  7. *
  8. * Last Modified: $Date$
  9. *
  10. * Description:
  11. *
  12. * This file contains the check_snmp plugin
  13. *
  14. * License Information:
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation; either version 2 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  29. *
  30. * $Id$
  31. *
  32. ******************************************************************************/
  33. const char *progname = "check_snmp";
  34. const char *revision = "$Revision$";
  35. const char *copyright = "1999-2006";
  36. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  37. #include "common.h"
  38. #include "utils.h"
  39. #include "popen.h"
  40. #define DEFAULT_COMMUNITY "public"
  41. #define DEFAULT_PORT "161"
  42. #define DEFAULT_MIBLIST "ALL"
  43. #define DEFAULT_PROTOCOL "1"
  44. #define DEFAULT_TIMEOUT 1
  45. #define DEFAULT_RETRIES 5
  46. #define DEFAULT_AUTH_PROTOCOL "MD5"
  47. #define DEFAULT_DELIMITER "="
  48. #define DEFAULT_OUTPUT_DELIMITER " "
  49. #define mark(a) ((a)!=0?"*":"")
  50. #define CHECK_UNDEF 0
  51. #define CRIT_PRESENT 1
  52. #define CRIT_STRING 2
  53. #define CRIT_REGEX 4
  54. #define CRIT_GT 8
  55. #define CRIT_LT 16
  56. #define CRIT_GE 32
  57. #define CRIT_LE 64
  58. #define CRIT_EQ 128
  59. #define CRIT_NE 256
  60. #define CRIT_RANGE 512
  61. #define WARN_PRESENT 1024
  62. #define WARN_STRING 2048
  63. #define WARN_REGEX 4096
  64. #define WARN_GT 8192
  65. #define WARN_LT 16384
  66. #define WARN_GE 32768
  67. #define WARN_LE 65536
  68. #define WARN_EQ 131072
  69. #define WARN_NE 262144
  70. #define WARN_RANGE 524288
  71. #define MAX_OIDS 8
  72. #define MAX_DELIM_LENGTH 8
  73. int process_arguments (int, char **);
  74. int validate_arguments (void);
  75. char *clarify_message (char *);
  76. int check_num (int);
  77. int lu_getll (unsigned long *, char *);
  78. int lu_getul (unsigned long *, char *);
  79. char *thisarg (char *str);
  80. char *nextarg (char *str);
  81. void print_usage (void);
  82. void print_help (void);
  83. #include "regex.h"
  84. char regex_expect[MAX_INPUT_BUFFER] = "";
  85. regex_t preg;
  86. regmatch_t pmatch[10];
  87. char timestamp[10] = "";
  88. char errbuf[MAX_INPUT_BUFFER] = "";
  89. char perfstr[MAX_INPUT_BUFFER] = "";
  90. int cflags = REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
  91. int eflags = 0;
  92. int errcode, excode;
  93. char *server_address = NULL;
  94. char *community = NULL;
  95. char *authpriv = NULL;
  96. char *proto = NULL;
  97. char *seclevel = NULL;
  98. char *secname = NULL;
  99. char *authproto = NULL;
  100. char *authpasswd = NULL;
  101. char *privpasswd = NULL;
  102. char *oid;
  103. char *label;
  104. char *units;
  105. char *port;
  106. char string_value[MAX_INPUT_BUFFER] = "";
  107. char **labels = NULL;
  108. char **unitv = NULL;
  109. size_t nlabels = 0;
  110. size_t labels_size = 8;
  111. size_t nunits = 0;
  112. size_t unitv_size = 8;
  113. int verbose = FALSE;
  114. int usesnmpgetnext = FALSE;
  115. unsigned long lower_warn_lim[MAX_OIDS];
  116. unsigned long upper_warn_lim[MAX_OIDS];
  117. unsigned long lower_crit_lim[MAX_OIDS];
  118. unsigned long upper_crit_lim[MAX_OIDS];
  119. unsigned long response_value[MAX_OIDS];
  120. int check_warning_value = FALSE;
  121. int check_critical_value = FALSE;
  122. int retries = 0;
  123. unsigned long eval_method[MAX_OIDS];
  124. char *delimiter;
  125. char *output_delim;
  126. char *miblist = NULL;
  127. int needmibs = FALSE;
  128. int
  129. main (int argc, char **argv)
  130. {
  131. int i = 0;
  132. int iresult = STATE_UNKNOWN;
  133. int found = 0;
  134. int result = STATE_DEPENDENT;
  135. char input_buffer[MAX_INPUT_BUFFER];
  136. char *command_line = NULL;
  137. char *response = NULL;
  138. char *outbuff;
  139. char *output;
  140. char *ptr = NULL;
  141. char *p2 = NULL;
  142. char *show = NULL;
  143. char type[8] = "";
  144. setlocale (LC_ALL, "");
  145. bindtextdomain (PACKAGE, LOCALEDIR);
  146. textdomain (PACKAGE);
  147. labels = malloc (labels_size);
  148. unitv = malloc (unitv_size);
  149. for (i = 0; i < MAX_OIDS; i++)
  150. eval_method[i] = CHECK_UNDEF;
  151. i = 0;
  152. oid = strdup ("");
  153. label = strdup ("SNMP");
  154. units = strdup ("");
  155. port = strdup (DEFAULT_PORT);
  156. outbuff = strdup ("");
  157. output = strdup ("");
  158. delimiter = strdup (" = ");
  159. output_delim = strdup (DEFAULT_OUTPUT_DELIMITER);
  160. /* miblist = strdup (DEFAULT_MIBLIST); */
  161. timeout_interval = DEFAULT_TIMEOUT;
  162. retries = DEFAULT_RETRIES;
  163. if (process_arguments (argc, argv) == ERROR)
  164. usage4 (_("Could not parse arguments"));
  165. /* create the command line to execute */
  166. if(usesnmpgetnext == TRUE) {
  167. asprintf(&command_line, "%s -t %d -r %d -m %s -v %s %s %s:%s %s",
  168. PATH_TO_SNMPGETNEXT, timeout_interval, retries, miblist, proto,
  169. authpriv, server_address, port, oid);
  170. }else{
  171. asprintf (&command_line, "%s -t %d -r %d -m %s -v %s %s %s:%s %s",
  172. PATH_TO_SNMPGET, timeout_interval, retries, miblist, proto,
  173. authpriv, server_address, port, oid);
  174. }
  175. if (verbose)
  176. printf ("%s\n", command_line);
  177. /* run the command */
  178. child_process = spopen (command_line);
  179. if (child_process == NULL) {
  180. printf (_("Could not open pipe: %s\n"), command_line);
  181. exit (STATE_UNKNOWN);
  182. }
  183. child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  184. if (child_stderr == NULL) {
  185. printf (_("Could not open stderr for %s\n"), command_line);
  186. }
  187. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process))
  188. asprintf (&output, "%s%s", output, input_buffer);
  189. if (verbose)
  190. printf ("%s\n", output);
  191. ptr = output;
  192. strcat(perfstr, "| ");
  193. while (ptr) {
  194. char *foo;
  195. foo = strstr (ptr, delimiter);
  196. strncat(perfstr, ptr, foo-ptr);
  197. ptr = foo;
  198. if (ptr == NULL)
  199. break;
  200. ptr += strlen (delimiter);
  201. ptr += strspn (ptr, " ");
  202. found++;
  203. if (ptr[0] == '"') {
  204. ptr++;
  205. response = strpcpy (response, ptr, "\"");
  206. ptr = strpbrk (ptr, "\"");
  207. ptr += strspn (ptr, "\"\n");
  208. }
  209. else {
  210. response = strpcpy (response, ptr, "\n");
  211. ptr = strpbrk (ptr, "\n");
  212. ptr += strspn (ptr, "\n");
  213. while
  214. (strstr (ptr, delimiter) &&
  215. strstr (ptr, "\n") && strstr (ptr, "\n") < strstr (ptr, delimiter)) {
  216. response = strpcat (response, ptr, "\n");
  217. ptr = strpbrk (ptr, "\n");
  218. }
  219. if (ptr && strstr (ptr, delimiter) == NULL) {
  220. asprintf (&response, "%s%s", response, ptr);
  221. ptr = NULL;
  222. }
  223. }
  224. /* We strip out the datatype indicator for PHBs */
  225. /* Clean up type array - Sol10 does not necessarily zero it out */
  226. bzero(type, sizeof(type));
  227. if (strstr (response, "Gauge: "))
  228. show = strstr (response, "Gauge: ") + 7;
  229. else if (strstr (response, "Gauge32: "))
  230. show = strstr (response, "Gauge32: ") + 9;
  231. else if (strstr (response, "Counter32: ")) {
  232. show = strstr (response, "Counter32: ") + 11;
  233. strcpy(type, "c");
  234. }
  235. else if (strstr (response, "INTEGER: "))
  236. show = strstr (response, "INTEGER: ") + 9;
  237. else if (strstr (response, "STRING: "))
  238. show = strstr (response, "STRING: ") + 8;
  239. else
  240. show = response;
  241. p2 = show;
  242. iresult = STATE_DEPENDENT;
  243. /* Process this block for integer comparisons */
  244. if (eval_method[i] & CRIT_GT ||
  245. eval_method[i] & CRIT_LT ||
  246. eval_method[i] & CRIT_GE ||
  247. eval_method[i] & CRIT_LE ||
  248. eval_method[i] & CRIT_EQ ||
  249. eval_method[i] & CRIT_NE ||
  250. eval_method[i] & WARN_GT ||
  251. eval_method[i] & WARN_LT ||
  252. eval_method[i] & WARN_GE ||
  253. eval_method[i] & WARN_LE ||
  254. eval_method[i] & WARN_EQ ||
  255. eval_method[i] & WARN_NE) {
  256. p2 = strpbrk (p2, "0123456789");
  257. if (p2 == NULL)
  258. die (STATE_UNKNOWN,_("No valid data returned"));
  259. response_value[i] = strtoul (p2, NULL, 10);
  260. iresult = check_num (i);
  261. asprintf (&show, "%lu", response_value[i]);
  262. }
  263. /* Process this block for string matching */
  264. else if (eval_method[i] & CRIT_STRING) {
  265. if (strcmp (show, string_value))
  266. iresult = STATE_CRITICAL;
  267. else
  268. iresult = STATE_OK;
  269. }
  270. /* Process this block for regex matching */
  271. else if (eval_method[i] & CRIT_REGEX) {
  272. excode = regexec (&preg, response, 10, pmatch, eflags);
  273. if (excode == 0) {
  274. iresult = STATE_OK;
  275. }
  276. else if (excode != REG_NOMATCH) {
  277. regerror (excode, &preg, errbuf, MAX_INPUT_BUFFER);
  278. printf (_("Execute Error: %s\n"), errbuf);
  279. exit (STATE_CRITICAL);
  280. }
  281. else {
  282. iresult = STATE_CRITICAL;
  283. }
  284. }
  285. /* Process this block for existence-nonexistence checks */
  286. else {
  287. if (eval_method[i] & CRIT_PRESENT)
  288. iresult = STATE_CRITICAL;
  289. else if (eval_method[i] & WARN_PRESENT)
  290. iresult = STATE_WARNING;
  291. else if (response && iresult == STATE_DEPENDENT)
  292. iresult = STATE_OK;
  293. }
  294. /* Result is the worst outcome of all the OIDs tested */
  295. result = max_state (result, iresult);
  296. /* Prepend a label for this OID if there is one */
  297. if (nlabels > (size_t)1 && (size_t)i < nlabels && labels[i] != NULL)
  298. asprintf (&outbuff, "%s%s%s %s%s%s", outbuff,
  299. (i == 0) ? " " : output_delim,
  300. labels[i], mark (iresult), show, mark (iresult));
  301. else
  302. asprintf (&outbuff, "%s%s%s%s%s", outbuff, (i == 0) ? " " : output_delim,
  303. mark (iresult), show, mark (iresult));
  304. /* Append a unit string for this OID if there is one */
  305. if (nunits > (size_t)0 && (size_t)i < nunits && unitv[i] != NULL)
  306. asprintf (&outbuff, "%s %s", outbuff, unitv[i]);
  307. i++;
  308. strcat(perfstr, "=");
  309. strcat(perfstr, show);
  310. if (type)
  311. strcat(perfstr, type);
  312. strcat(perfstr, " ");
  313. } /* end while (ptr) */
  314. if (found == 0)
  315. die (STATE_UNKNOWN,
  316. _("%s problem - No data received from host\nCMD: %s\n"),
  317. label,
  318. command_line);
  319. /* WARNING if output found on stderr */
  320. if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
  321. result = max_state (result, STATE_WARNING);
  322. /* close stderr */
  323. (void) fclose (child_stderr);
  324. /* close the pipe */
  325. if (spclose (child_process))
  326. result = max_state (result, STATE_WARNING);
  327. /* if (nunits == 1 || i == 1) */
  328. /* printf ("%s %s -%s %s\n", label, state_text (result), outbuff, units); */
  329. /* else */
  330. printf ("%s %s -%s %s \n", label, state_text (result), outbuff, perfstr);
  331. return result;
  332. }
  333. /* process command-line arguments */
  334. int
  335. process_arguments (int argc, char **argv)
  336. {
  337. char *ptr;
  338. int c = 1;
  339. int j = 0, jj = 0, ii = 0;
  340. int option = 0;
  341. static struct option longopts[] = {
  342. STD_LONG_OPTS,
  343. {"community", required_argument, 0, 'C'},
  344. {"oid", required_argument, 0, 'o'},
  345. {"object", required_argument, 0, 'o'},
  346. {"delimiter", required_argument, 0, 'd'},
  347. {"output-delimiter", required_argument, 0, 'D'},
  348. {"string", required_argument, 0, 's'},
  349. {"timeout", required_argument, 0, 't'},
  350. {"regex", required_argument, 0, 'r'},
  351. {"ereg", required_argument, 0, 'r'},
  352. {"eregi", required_argument, 0, 'R'},
  353. {"label", required_argument, 0, 'l'},
  354. {"units", required_argument, 0, 'u'},
  355. {"port", required_argument, 0, 'p'},
  356. {"retries", required_argument, 0, 'e'},
  357. {"miblist", required_argument, 0, 'm'},
  358. {"protocol", required_argument, 0, 'P'},
  359. {"seclevel", required_argument, 0, 'L'},
  360. {"secname", required_argument, 0, 'U'},
  361. {"authproto", required_argument, 0, 'a'},
  362. {"authpasswd", required_argument, 0, 'A'},
  363. {"privpasswd", required_argument, 0, 'X'},
  364. {"next", no_argument, 0, 'n'},
  365. {0, 0, 0, 0}
  366. };
  367. if (argc < 2)
  368. return ERROR;
  369. /* reverse compatibility for very old non-POSIX usage forms */
  370. for (c = 1; c < argc; c++) {
  371. if (strcmp ("-to", argv[c]) == 0)
  372. strcpy (argv[c], "-t");
  373. if (strcmp ("-wv", argv[c]) == 0)
  374. strcpy (argv[c], "-w");
  375. if (strcmp ("-cv", argv[c]) == 0)
  376. strcpy (argv[c], "-c");
  377. }
  378. while (1) {
  379. c = getopt_long (argc, argv, "nhvVt:c:w:H:C:o:e:E:d:D:s:t:R:r:l:u:p:m:P:L:U:a:A:X:",
  380. longopts, &option);
  381. if (c == -1 || c == EOF)
  382. break;
  383. switch (c) {
  384. case '?': /* usage */
  385. usage2 (_("Unknown argument"), optarg);
  386. case 'h': /* help */
  387. print_help ();
  388. exit (STATE_OK);
  389. case 'V': /* version */
  390. print_revision (progname, revision);
  391. exit (STATE_OK);
  392. case 'v': /* verbose */
  393. verbose = TRUE;
  394. break;
  395. /* Connection info */
  396. case 'C': /* group or community */
  397. community = optarg;
  398. break;
  399. case 'H': /* Host or server */
  400. server_address = optarg;
  401. break;
  402. case 'p': /* TCP port number */
  403. port = optarg;
  404. break;
  405. case 'm': /* List of MIBS */
  406. miblist = optarg;
  407. break;
  408. case 'n': /* usesnmpgetnext */
  409. usesnmpgetnext = TRUE;
  410. break;
  411. case 'P': /* SNMP protocol version */
  412. proto = optarg;
  413. break;
  414. case 'L': /* security level */
  415. seclevel = optarg;
  416. break;
  417. case 'U': /* security username */
  418. secname = optarg;
  419. break;
  420. case 'a': /* auth protocol */
  421. authproto = optarg;
  422. break;
  423. case 'A': /* auth passwd */
  424. authpasswd = optarg;
  425. break;
  426. case 'X': /* priv passwd */
  427. privpasswd = optarg;
  428. break;
  429. case 't': /* timeout period */
  430. if (!is_integer (optarg))
  431. usage2 (_("Timeout interval must be a positive integer"), optarg);
  432. else
  433. timeout_interval = atoi (optarg);
  434. break;
  435. /* Test parameters */
  436. case 'c': /* critical time threshold */
  437. if (strspn (optarg, "0123456789:,") < strlen (optarg))
  438. usage2 (_("Invalid critical threshold: %s\n"), optarg);
  439. for (ptr = optarg; ptr && jj < MAX_OIDS; jj++) {
  440. if (lu_getll (&lower_crit_lim[jj], ptr) == 1)
  441. eval_method[jj] |= CRIT_LT;
  442. if (lu_getul (&upper_crit_lim[jj], ptr) == 1)
  443. eval_method[jj] |= CRIT_GT;
  444. (ptr = index (ptr, ',')) ? ptr++ : ptr;
  445. }
  446. break;
  447. case 'w': /* warning time threshold */
  448. if (strspn (optarg, "0123456789:,") < strlen (optarg))
  449. usage2 (_("Invalid warning threshold: %s\n"), optarg);
  450. for (ptr = optarg; ptr && ii < MAX_OIDS; ii++) {
  451. if (lu_getll (&lower_warn_lim[ii], ptr) == 1)
  452. eval_method[ii] |= WARN_LT;
  453. if (lu_getul (&upper_warn_lim[ii], ptr) == 1)
  454. eval_method[ii] |= WARN_GT;
  455. (ptr = index (ptr, ',')) ? ptr++ : ptr;
  456. }
  457. break;
  458. case 'e': /* PRELIMINARY - may change */
  459. case 'E': /* PRELIMINARY - may change */
  460. if (!is_integer (optarg))
  461. usage2 (_("Retries interval must be a positive integer"), optarg);
  462. else
  463. retries = atoi(optarg);
  464. break;
  465. case 'o': /* object identifier */
  466. if ( strspn( optarg, "0123456789." ) != strlen( optarg ) ) {
  467. /*
  468. * we have something other than digits and periods, so we
  469. * have a mib variable, rather than just an SNMP OID, so
  470. * we have to actually read the mib files
  471. */
  472. needmibs = TRUE;
  473. }
  474. for (ptr = optarg; (ptr = index (ptr, ',')); ptr++)
  475. ptr[0] = ' '; /* relpace comma with space */
  476. for (ptr = optarg; (ptr = index (ptr, ' ')); ptr++)
  477. j++; /* count OIDs */
  478. asprintf (&oid, "%s %s", (oid?oid:""), optarg);
  479. if (c == 'E' || c == 'e') {
  480. jj++;
  481. ii++;
  482. }
  483. if (c == 'E')
  484. eval_method[j+1] |= WARN_PRESENT;
  485. else if (c == 'e')
  486. eval_method[j+1] |= CRIT_PRESENT;
  487. break;
  488. case 's': /* string or substring */
  489. strncpy (string_value, optarg, sizeof (string_value) - 1);
  490. string_value[sizeof (string_value) - 1] = 0;
  491. eval_method[jj++] = CRIT_STRING;
  492. ii++;
  493. break;
  494. case 'R': /* regex */
  495. cflags = REG_ICASE;
  496. case 'r': /* regex */
  497. cflags |= REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
  498. strncpy (regex_expect, optarg, sizeof (regex_expect) - 1);
  499. regex_expect[sizeof (regex_expect) - 1] = 0;
  500. errcode = regcomp (&preg, regex_expect, cflags);
  501. if (errcode != 0) {
  502. regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER);
  503. printf (_("Could Not Compile Regular Expression"));
  504. return ERROR;
  505. }
  506. eval_method[jj++] = CRIT_REGEX;
  507. ii++;
  508. break;
  509. /* Format */
  510. case 'd': /* delimiter */
  511. delimiter = strscpy (delimiter, optarg);
  512. break;
  513. case 'D': /* output-delimiter */
  514. output_delim = strscpy (output_delim, optarg);
  515. break;
  516. case 'l': /* label */
  517. label = optarg;
  518. nlabels++;
  519. if (nlabels >= labels_size) {
  520. labels_size += 8;
  521. labels = realloc (labels, labels_size);
  522. if (labels == NULL)
  523. die (STATE_UNKNOWN, _("Could not reallocate labels[%d]"), (int)nlabels);
  524. }
  525. labels[nlabels - 1] = optarg;
  526. ptr = thisarg (optarg);
  527. labels[nlabels - 1] = ptr;
  528. if (strstr (ptr, "'") == ptr)
  529. labels[nlabels - 1] = ptr + 1;
  530. while (ptr && (ptr = nextarg (ptr))) {
  531. if (nlabels >= labels_size) {
  532. labels_size += 8;
  533. labels = realloc (labels, labels_size);
  534. if (labels == NULL)
  535. die (STATE_UNKNOWN, _("Could not reallocate labels\n"));
  536. }
  537. labels++;
  538. ptr = thisarg (ptr);
  539. if (strstr (ptr, "'") == ptr)
  540. labels[nlabels - 1] = ptr + 1;
  541. else
  542. labels[nlabels - 1] = ptr;
  543. }
  544. break;
  545. case 'u': /* units */
  546. units = optarg;
  547. nunits++;
  548. if (nunits >= unitv_size) {
  549. unitv_size += 8;
  550. unitv = realloc (unitv, unitv_size);
  551. if (unitv == NULL)
  552. die (STATE_UNKNOWN, _("Could not reallocate units [%d]\n"), (int)nunits);
  553. }
  554. unitv[nunits - 1] = optarg;
  555. ptr = thisarg (optarg);
  556. unitv[nunits - 1] = ptr;
  557. if (strstr (ptr, "'") == ptr)
  558. unitv[nunits - 1] = ptr + 1;
  559. while (ptr && (ptr = nextarg (ptr))) {
  560. if (nunits >= unitv_size) {
  561. unitv_size += 8;
  562. unitv = realloc (unitv, unitv_size);
  563. if (units == NULL)
  564. die (STATE_UNKNOWN, _("Could not realloc() units\n"));
  565. }
  566. nunits++;
  567. ptr = thisarg (ptr);
  568. if (strstr (ptr, "'") == ptr)
  569. unitv[nunits - 1] = ptr + 1;
  570. else
  571. unitv[nunits - 1] = ptr;
  572. }
  573. break;
  574. }
  575. }
  576. if (server_address == NULL)
  577. server_address = argv[optind];
  578. if (community == NULL)
  579. community = strdup (DEFAULT_COMMUNITY);
  580. return validate_arguments ();
  581. }
  582. /******************************************************************************
  583. @@-
  584. <sect3>
  585. <title>validate_arguments</title>
  586. <para>&PROTO_validate_arguments;</para>
  587. <para>Checks to see if the default miblist needs to be loaded. Also verifies
  588. the authentication and authorization combinations based on protocol version
  589. selected.</para>
  590. <para></para>
  591. </sect3>
  592. -@@
  593. ******************************************************************************/
  594. int
  595. validate_arguments ()
  596. {
  597. /* check whether to load locally installed MIBS (CPU/disk intensive) */
  598. if (miblist == NULL) {
  599. if ( needmibs == TRUE ) {
  600. miblist = strdup (DEFAULT_MIBLIST);
  601. }else{
  602. miblist = "''"; /* don't read any mib files for numeric oids */
  603. }
  604. }
  605. /* Need better checks to verify seclevel and authproto choices */
  606. if (seclevel == NULL)
  607. asprintf (&seclevel, "noAuthNoPriv");
  608. if (authproto == NULL )
  609. asprintf(&authproto, DEFAULT_AUTH_PROTOCOL);
  610. if (proto == NULL || (strcmp(proto,DEFAULT_PROTOCOL) == 0) ) { /* default protocol version */
  611. asprintf(&proto, DEFAULT_PROTOCOL);
  612. asprintf(&authpriv, "%s%s", "-c ", community);
  613. }
  614. else if ( strcmp (proto, "2c") == 0 ) { /* snmpv2c args */
  615. asprintf(&authpriv, "%s%s", "-c ", community);
  616. }
  617. else if ( strcmp (proto, "3") == 0 ) { /* snmpv3 args */
  618. asprintf(&proto, "%s", "3");
  619. if ( (strcmp(seclevel, "noAuthNoPriv") == 0) || seclevel == NULL ) {
  620. asprintf(&authpriv, "%s", "-l noAuthNoPriv" );
  621. }
  622. else if ( strcmp(seclevel, "authNoPriv") == 0 ) {
  623. if ( secname == NULL || authpasswd == NULL) {
  624. printf (_("Missing secname (%s) or authpassword (%s) ! \n"),secname, authpasswd );
  625. print_usage ();
  626. exit (STATE_UNKNOWN);
  627. }
  628. asprintf(&authpriv, "-l authNoPriv -a %s -u %s -A %s ", authproto, secname, authpasswd);
  629. }
  630. else if ( strcmp(seclevel, "authPriv") == 0 ) {
  631. if ( secname == NULL || authpasswd == NULL || privpasswd == NULL ) {
  632. printf (_("Missing secname (%s), authpassword (%s), or privpasswd (%s)! \n"),secname, authpasswd,privpasswd );
  633. print_usage ();
  634. exit (STATE_UNKNOWN);
  635. }
  636. asprintf(&authpriv, "-l authPriv -a %s -u %s -A %s -x DES -X %s ", authproto, secname, authpasswd, privpasswd);
  637. }
  638. }
  639. else {
  640. usage2 (_("Invalid SNMP version"), proto);
  641. }
  642. return OK;
  643. }
  644. char *
  645. clarify_message (char *msg)
  646. {
  647. int i = 0;
  648. int foo;
  649. char tmpmsg_c[MAX_INPUT_BUFFER];
  650. char *tmpmsg = (char *) &tmpmsg_c;
  651. tmpmsg = strcpy (tmpmsg, msg);
  652. if (!strncmp (tmpmsg, " Hex:", 5)) {
  653. tmpmsg = strtok (tmpmsg, ":");
  654. while ((tmpmsg = strtok (NULL, " "))) {
  655. foo = strtol (tmpmsg, NULL, 16);
  656. /* Translate chars that are not the same value in the printers
  657. * character set.
  658. */
  659. switch (foo) {
  660. case 208:
  661. {
  662. foo = 197;
  663. break;
  664. }
  665. case 216:
  666. {
  667. foo = 196;
  668. break;
  669. }
  670. }
  671. msg[i] = foo;
  672. i++;
  673. }
  674. msg[i] = 0;
  675. }
  676. return (msg);
  677. }
  678. int
  679. check_num (int i)
  680. {
  681. int result;
  682. result = STATE_OK;
  683. if (eval_method[i] & WARN_GT && eval_method[i] & WARN_LT &&
  684. lower_warn_lim[i] > upper_warn_lim[i]) {
  685. if (response_value[i] <= lower_warn_lim[i] &&
  686. response_value[i] >= upper_warn_lim[i]) {
  687. result = STATE_WARNING;
  688. }
  689. }
  690. else if
  691. ((eval_method[i] & WARN_GT && response_value[i] > upper_warn_lim[i]) ||
  692. (eval_method[i] & WARN_GE && response_value[i] >= upper_warn_lim[i]) ||
  693. (eval_method[i] & WARN_LT && response_value[i] < lower_warn_lim[i]) ||
  694. (eval_method[i] & WARN_LE && response_value[i] <= lower_warn_lim[i]) ||
  695. (eval_method[i] & WARN_EQ && response_value[i] == upper_warn_lim[i]) ||
  696. (eval_method[i] & WARN_NE && response_value[i] != upper_warn_lim[i])) {
  697. result = STATE_WARNING;
  698. }
  699. if (eval_method[i] & CRIT_GT && eval_method[i] & CRIT_LT &&
  700. lower_crit_lim[i] > upper_crit_lim[i]) {
  701. if (response_value[i] <= lower_crit_lim[i] &&
  702. response_value[i] >= upper_crit_lim[i]) {
  703. result = STATE_CRITICAL;
  704. }
  705. }
  706. else if
  707. ((eval_method[i] & CRIT_GT && response_value[i] > upper_crit_lim[i]) ||
  708. (eval_method[i] & CRIT_GE && response_value[i] >= upper_crit_lim[i]) ||
  709. (eval_method[i] & CRIT_LT && response_value[i] < lower_crit_lim[i]) ||
  710. (eval_method[i] & CRIT_LE && response_value[i] <= lower_crit_lim[i]) ||
  711. (eval_method[i] & CRIT_EQ && response_value[i] == upper_crit_lim[i]) ||
  712. (eval_method[i] & CRIT_NE && response_value[i] != upper_crit_lim[i])) {
  713. result = STATE_CRITICAL;
  714. }
  715. return result;
  716. }
  717. int
  718. lu_getll (unsigned long *ll, char *str)
  719. {
  720. char tmp[100];
  721. if (strchr (str, ':') == NULL)
  722. return 0;
  723. if (strchr (str, ',') != NULL && (strchr (str, ',') < strchr (str, ':')))
  724. return 0;
  725. if (sscanf (str, "%lu%[:]", ll, tmp) == 2)
  726. return 1;
  727. return 0;
  728. }
  729. int
  730. lu_getul (unsigned long *ul, char *str)
  731. {
  732. char tmp[100];
  733. if (sscanf (str, "%lu%[^,]", ul, tmp) == 1)
  734. return 1;
  735. if (sscanf (str, ":%lu%[^,]", ul, tmp) == 1)
  736. return 1;
  737. if (sscanf (str, "%*u:%lu%[^,]", ul, tmp) == 1)
  738. return 1;
  739. return 0;
  740. }
  741. /* trim leading whitespace
  742. if there is a leading quote, make sure it balances */
  743. char *
  744. thisarg (char *str)
  745. {
  746. str += strspn (str, " \t\r\n"); /* trim any leading whitespace */
  747. if (strstr (str, "'") == str) { /* handle SIMPLE quoted strings */
  748. if (strlen (str) == 1 || !strstr (str + 1, "'"))
  749. die (STATE_UNKNOWN, _("Unbalanced quotes\n"));
  750. }
  751. return str;
  752. }
  753. /* if there's a leading quote, advance to the trailing quote
  754. set the trailing quote to '\x0'
  755. if the string continues, advance beyond the comma */
  756. char *
  757. nextarg (char *str)
  758. {
  759. if (strstr (str, "'") == str) {
  760. str[0] = 0;
  761. if (strlen (str) > 1) {
  762. str = strstr (str + 1, "'");
  763. return (++str);
  764. }
  765. else {
  766. return NULL;
  767. }
  768. }
  769. if (strstr (str, ",") == str) {
  770. str[0] = 0;
  771. if (strlen (str) > 1) {
  772. return (++str);
  773. }
  774. else {
  775. return NULL;
  776. }
  777. }
  778. if ((str = strstr (str, ",")) && strlen (str) > 1) {
  779. str[0] = 0;
  780. return (++str);
  781. }
  782. return NULL;
  783. }
  784. void
  785. print_help (void)
  786. {
  787. print_revision (progname, revision);
  788. printf (COPYRIGHT, copyright, email);
  789. printf ("%s\n", _("Check status of remote machines and obtain sustem information via SNMP"));
  790. printf ("\n\n");
  791. print_usage ();
  792. printf (_(UT_HELP_VRSN));
  793. printf (_(UT_HOST_PORT), 'p', DEFAULT_PORT);
  794. /* SNMP and Authentication Protocol */
  795. printf (" %s\n", "-n, --next");
  796. printf (" %s\n", _("Use SNMP GETNEXT instead of SNMP GET"));
  797. printf (" %s\n", "-P, --protocol=[1|2c|3]");
  798. printf (" %s\n", _("SNMP protocol version"));
  799. printf (" %s\n", "-L, --seclevel=[noAuthNoPriv|authNoPriv|authPriv]");
  800. printf (" %s\n", _("SNMPv3 securityLevel"));
  801. printf (" %s\n", "-a, --authproto=[MD5|SHA]");
  802. printf (" %s\n", _("SNMPv3 auth proto"));
  803. /* Authentication Tokens*/
  804. printf (" %s\n", "-C, --community=STRING");
  805. printf (" %s\n", _("Optional community string for SNMP communication"));
  806. printf (_("(default is \"%s\")"),DEFAULT_COMMUNITY);
  807. printf (" %s\n", "-U, --secname=USERNAME");
  808. printf (" %s\n", _("SNMPv3 username"));
  809. printf (" %s\n", "-A, --authpassword=PASSWORD");
  810. printf (" %s\n", _("SNMPv3 authentication password"));
  811. printf (" %s\n", "-X, --privpasswd=PASSWORD");
  812. printf (" %s\n", _("SNMPv3 crypt passwd (DES)"));
  813. /* OID Stuff */
  814. printf (" %s\n", "-o, --oid=OID(s)");
  815. printf (" %s\n", _("Object identifier(s) or SNMP variables whose value you wish to query"));
  816. printf (" %s\n", "-m, --miblist=STRING");
  817. printf (" %s\n", _("List of MIBS to be loaded (default = none if using numeric oids or 'ALL'"));
  818. printf (" %s\n", _("for symbolic oids.)"));
  819. printf (" %s\n", "-d, --delimiter=STRING");
  820. printf (_(" Delimiter to use when parsing returned data. Default is \"%s\""), DEFAULT_DELIMITER);
  821. printf (" %s\n", _("Any data on the right hand side of the delimiter is considered"));
  822. printf (" %s\n", _("to be the data that should be used in the evaluation."));
  823. /* Tests Against Integers */
  824. printf (" %s\n", "-w, --warning=INTEGER_RANGE(s)");
  825. printf (" %s\n", _("Range(s) which will not result in a WARNING status"));
  826. printf (" %s\n", "-c, --critical=INTEGER_RANGE(s)");
  827. printf (" %s\n", _("Range(s) which will not result in a CRITICAL status"));
  828. /* Tests Against Strings */
  829. printf (" %s\n", "-s, --string=STRING");
  830. printf (" %s\n", _("Return OK state (for that OID) if STRING is an exact match"));
  831. printf (" %s\n", "-r, --ereg=REGEX");
  832. printf (" %s\n", _("Return OK state (for that OID) if extended regular expression REGEX matches"));
  833. printf (" %s\n", "-R, --eregi=REGEX");
  834. printf (" %s\n", _("Return OK state (for that OID) if case-insensitive extended REGEX matches"));
  835. printf (" %s\n", "-l, --label=STRING");
  836. printf (" %s\n", _("Prefix label for output from plugin (default -s 'SNMP')"));
  837. /* Output Formatting */
  838. printf (" %s\n", "-u, --units=STRING");
  839. printf (" %s\n", _("Units label(s) for output data (e.g., 'sec.')."));
  840. printf (" %s\n", "-D, --output-delimiter=STRING");
  841. printf (" %s\n", _("Separates output on multiple OID requests"));
  842. printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
  843. printf (_(UT_VERBOSE));
  844. printf ("%s\n", _("This plugin uses the 'snmpget' command included with the NET-SNMP package."));
  845. printf ("%s\n", _("if you don't have the package installed, you will need to download it from"));
  846. printf ("%s\n", _("http://net-snmp.sourceforge.net before you can use this plugin."));
  847. printf ("%s\n", _("- Multiple OIDs may be indicated by a comma- or space-delimited list (lists with"));
  848. printf ("%s\n", _(" internal spaces must be quoted) [max 8 OIDs]"));
  849. printf ("%s\n", _("- Ranges are inclusive and are indicated with colons. When specified as"));
  850. printf ("%s\n", _(" 'min:max' a STATE_OK will be returned if the result is within the indicated"));
  851. printf ("%s\n", _(" range or is equal to the upper or lower bound. A non-OK state will be"));
  852. printf ("%s\n", _(" returned if the result is outside the specified range."));
  853. printf ("%s\n", _("- If specified in the order 'max:min' a non-OK state will be returned if the"));
  854. printf ("%s\n", _(" result is within the (inclusive) range."));
  855. printf ("%s\n", _("- Upper or lower bounds may be omitted to skip checking the respective limit."));
  856. printf ("%s\n", _("- Bare integers are interpreted as upper limits."));
  857. printf ("%s\n", _("- When checking multiple OIDs, separate ranges by commas like '-w 1:10,1:,:20'"));
  858. printf ("%s\n", _("- Note that only one string and one regex may be checked at present"));
  859. printf ("%s\n", _("- All evaluation methods other than PR, STR, and SUBSTR expect that the value"));
  860. printf ("%s\n", _(" returned from the SNMP query is an unsigned integer."));
  861. printf (_(UT_SUPPORT));
  862. }
  863. void
  864. print_usage (void)
  865. {
  866. printf (_("Usage:"));
  867. printf ("%s -H <ip_address> -o <OID> [-w warn_range] [-c crit_range] \n\
  868. [-C community] [-s string] [-r regex] [-R regexi]\n\
  869. [-t timeout] [-e retries]\n\
  870. [-l label] [-u units] [-p port-number] [-d delimiter]\n\
  871. [-D output-delimiter] [-m miblist] [-P snmp version]\n\
  872. [-L seclevel] [-U secname] [-a authproto] [-A authpasswd]\n\
  873. [-X privpasswd]\n", progname);
  874. }