check_snmp.c 30 KB

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