check_snmp.c 30 KB

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