check_snmp.c 27 KB

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