4
0

check_snmp.c 29 KB

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