check_snmp.c 29 KB

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