check_snmp.c 28 KB

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