check_snmp.c 27 KB

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