check_snmp.c 28 KB

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