check_snmp.c 28 KB

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