check_snmp.c 27 KB

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