check_snmp.c 26 KB

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