check_snmp.c 29 KB

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