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