check_snmp.c 26 KB

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