check_snmp.c 28 KB

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