check_snmp.c 29 KB

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