check_snmp.c 28 KB

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