check_snmp.c 30 KB

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