check_snmp.c 26 KB

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