check_snmp.c 29 KB

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