check_snmp.c 27 KB

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