check_snmp.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  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) == ERROR)
  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 (show, 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. usage2 (_("Unknown argument"), optarg);
  344. case 'h': /* help */
  345. print_help ();
  346. exit (STATE_OK);
  347. case 'V': /* version */
  348. print_revision (progname, revision);
  349. exit (STATE_OK);
  350. case 'v': /* verbose */
  351. verbose = TRUE;
  352. break;
  353. /* Connection info */
  354. case 'C': /* group or community */
  355. community = optarg;
  356. break;
  357. case 'H': /* Host or server */
  358. server_address = optarg;
  359. break;
  360. case 'p': /* TCP port number */
  361. port = optarg;
  362. break;
  363. case 'm': /* List of MIBS */
  364. miblist = optarg;
  365. break;
  366. case 'P': /* SNMP protocol version */
  367. proto = optarg;
  368. break;
  369. case 'L': /* security level */
  370. seclevel = optarg;
  371. break;
  372. case 'U': /* security username */
  373. secname = optarg;
  374. break;
  375. case 'a': /* auth protocol */
  376. authproto = optarg;
  377. break;
  378. case 'A': /* auth passwd */
  379. authpasswd = optarg;
  380. break;
  381. case 'X': /* priv passwd */
  382. privpasswd = optarg;
  383. break;
  384. case 't': /* timeout period */
  385. if (!is_integer (optarg))
  386. usage2 (_("Timeout interval must be a positive integer"), optarg);
  387. else
  388. timeout_interval = atoi (optarg);
  389. break;
  390. /* Test parameters */
  391. case 'c': /* critical time threshold */
  392. if (strspn (optarg, "0123456789:,") < strlen (optarg))
  393. usage2 (_("Invalid critical threshold: %s\n"), optarg);
  394. for (ptr = optarg; ptr && jj < MAX_OIDS; jj++) {
  395. if (lu_getll (&lower_crit_lim[jj], ptr) == 1)
  396. eval_method[jj] |= CRIT_LT;
  397. if (lu_getul (&upper_crit_lim[jj], ptr) == 1)
  398. eval_method[jj] |= CRIT_GT;
  399. (ptr = index (ptr, ',')) ? ptr++ : ptr;
  400. }
  401. break;
  402. case 'w': /* warning time threshold */
  403. if (strspn (optarg, "0123456789:,") < strlen (optarg))
  404. usage2 (_("Invalid warning threshold: %s\n"), optarg);
  405. for (ptr = optarg; ptr && ii < MAX_OIDS; ii++) {
  406. if (lu_getll (&lower_warn_lim[ii], ptr) == 1)
  407. eval_method[ii] |= WARN_LT;
  408. if (lu_getul (&upper_warn_lim[ii], ptr) == 1)
  409. eval_method[ii] |= WARN_GT;
  410. (ptr = index (ptr, ',')) ? ptr++ : ptr;
  411. }
  412. break;
  413. case 'o': /* object identifier */
  414. case 'e': /* PRELIMINARY - may change */
  415. case 'E': /* PRELIMINARY - may change */
  416. for (ptr = optarg; (ptr = index (ptr, ',')); ptr++)
  417. ptr[0] = ' '; /* relpace comma with space */
  418. for (ptr = optarg; (ptr = index (ptr, ' ')); ptr++)
  419. j++; /* count OIDs */
  420. asprintf (&oid, "%s %s", (oid?oid:""), optarg);
  421. if (c == 'E' || c == 'e') {
  422. jj++;
  423. ii++;
  424. }
  425. if (c == 'E')
  426. eval_method[j+1] |= WARN_PRESENT;
  427. else if (c == 'e')
  428. eval_method[j+1] |= CRIT_PRESENT;
  429. break;
  430. case 's': /* string or substring */
  431. strncpy (string_value, optarg, sizeof (string_value) - 1);
  432. string_value[sizeof (string_value) - 1] = 0;
  433. eval_method[jj++] = CRIT_STRING;
  434. ii++;
  435. break;
  436. case 'R': /* regex */
  437. #ifdef HAVE_REGEX_H
  438. cflags = REG_ICASE;
  439. #endif
  440. case 'r': /* regex */
  441. #ifdef HAVE_REGEX_H
  442. cflags |= REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
  443. strncpy (regex_expect, optarg, sizeof (regex_expect) - 1);
  444. regex_expect[sizeof (regex_expect) - 1] = 0;
  445. errcode = regcomp (&preg, regex_expect, cflags);
  446. if (errcode != 0) {
  447. regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER);
  448. printf (_("Could Not Compile Regular Expression"));
  449. return ERROR;
  450. }
  451. eval_method[jj++] = CRIT_REGEX;
  452. ii++;
  453. #else
  454. printf (_("%s UNKNOWN: call for regex which was not a compiled option"), label);
  455. exit (STATE_UNKNOWN);
  456. #endif
  457. break;
  458. /* Format */
  459. case 'd': /* delimiter */
  460. delimiter = strscpy (delimiter, optarg);
  461. break;
  462. case 'D': /* output-delimiter */
  463. output_delim = strscpy (output_delim, optarg);
  464. break;
  465. case 'l': /* label */
  466. label = optarg;
  467. nlabels++;
  468. if (nlabels >= labels_size) {
  469. labels_size += 8;
  470. labels = realloc (labels, labels_size);
  471. if (labels == NULL)
  472. die (STATE_UNKNOWN,
  473. _("Could not realloc() labels[%d]"), nlabels);
  474. }
  475. labels[nlabels - 1] = optarg;
  476. ptr = thisarg (optarg);
  477. labels[nlabels - 1] = ptr;
  478. if (strstr (ptr, "'") == ptr)
  479. labels[nlabels - 1] = ptr + 1;
  480. while (ptr && (ptr = nextarg (ptr))) {
  481. if (nlabels >= labels_size) {
  482. labels_size += 8;
  483. labels = realloc (labels, labels_size);
  484. if (labels == NULL)
  485. die (STATE_UNKNOWN, _("Could not realloc() labels\n"));
  486. }
  487. labels++;
  488. ptr = thisarg (ptr);
  489. if (strstr (ptr, "'") == ptr)
  490. labels[nlabels - 1] = ptr + 1;
  491. else
  492. labels[nlabels - 1] = ptr;
  493. }
  494. break;
  495. case 'u': /* units */
  496. units = optarg;
  497. nunits++;
  498. if (nunits >= unitv_size) {
  499. unitv_size += 8;
  500. unitv = realloc (unitv, unitv_size);
  501. if (unitv == NULL)
  502. die (STATE_UNKNOWN,
  503. _("Could not realloc() units [%d]\n"), nunits);
  504. }
  505. unitv[nunits - 1] = optarg;
  506. ptr = thisarg (optarg);
  507. unitv[nunits - 1] = ptr;
  508. if (strstr (ptr, "'") == ptr)
  509. unitv[nunits - 1] = ptr + 1;
  510. while (ptr && (ptr = nextarg (ptr))) {
  511. if (nunits >= unitv_size) {
  512. unitv_size += 8;
  513. unitv = realloc (unitv, unitv_size);
  514. if (units == NULL)
  515. die (STATE_UNKNOWN, _("Could not realloc() units\n"));
  516. }
  517. nunits++;
  518. ptr = thisarg (ptr);
  519. if (strstr (ptr, "'") == ptr)
  520. unitv[nunits - 1] = ptr + 1;
  521. else
  522. unitv[nunits - 1] = ptr;
  523. }
  524. break;
  525. }
  526. }
  527. if (server_address == NULL)
  528. server_address = argv[optind];
  529. if (community == NULL)
  530. community = strdup (DEFAULT_COMMUNITY);
  531. return validate_arguments ();
  532. }
  533. /******************************************************************************
  534. @@-
  535. <sect3>
  536. <title>validate_arguments</title>
  537. <para>&PROTO_validate_arguments;</para>
  538. <para>Given a database name, this function returns TRUE if the string
  539. is a valid PostgreSQL database name, and returns false if it is
  540. not.</para>
  541. <para>Valid PostgreSQL database names are less than &NAMEDATALEN;
  542. characters long and consist of letters, numbers, and underscores. The
  543. first character cannot be a number, however.</para>
  544. </sect3>
  545. -@@
  546. ******************************************************************************/
  547. int
  548. validate_arguments ()
  549. {
  550. /* Need better checks to verify seclevel and authproto choices */
  551. if (seclevel == NULL)
  552. asprintf (&seclevel, "noAuthNoPriv");
  553. if (authproto == NULL )
  554. asprintf(&authproto, DEFAULT_AUTH_PROTOCOL);
  555. if (proto == NULL || (strcmp(proto,DEFAULT_PROTOCOL) == 0) ) { /* default protocol version */
  556. asprintf(&proto, DEFAULT_PROTOCOL);
  557. asprintf(&authpriv, "%s%s", "-c ", community);
  558. }
  559. else if ( strcmp (proto, "3") == 0 ) { /* snmpv3 args */
  560. asprintf(&proto, "%s", "3");
  561. if ( (strcmp(seclevel, "noAuthNoPriv") == 0) || seclevel == NULL ) {
  562. asprintf(&authpriv, "%s", "-l noAuthNoPriv" );
  563. }
  564. else if ( strcmp(seclevel, "authNoPriv") == 0 ) {
  565. if ( secname == NULL || authpasswd == NULL) {
  566. printf (_("Missing secname (%s) or authpassword (%s) ! \n)"),secname, authpasswd );
  567. print_usage ();
  568. exit (STATE_UNKNOWN);
  569. }
  570. asprintf(&authpriv, "-l authNoPriv -a %s -u %s -A %s ", authproto, secname, authpasswd);
  571. }
  572. else if ( strcmp(seclevel, "authPriv") == 0 ) {
  573. if ( secname == NULL || authpasswd == NULL || privpasswd == NULL ) {
  574. printf (_("Missing secname (%s), authpassword (%s), or privpasswd (%s)! \n"),secname, authpasswd,privpasswd );
  575. print_usage ();
  576. exit (STATE_UNKNOWN);
  577. }
  578. asprintf(&authpriv, "-l authPriv -a %s -u %s -A %s -x DES -X %s ", authproto, secname, authpasswd, privpasswd);
  579. }
  580. }
  581. else {
  582. printf (_("Invalid SNMP version: %s\n"), proto);
  583. print_usage ();
  584. exit (STATE_UNKNOWN);
  585. }
  586. return OK;
  587. }
  588. char *
  589. clarify_message (char *msg)
  590. {
  591. int i = 0;
  592. int foo;
  593. char tmpmsg_c[MAX_INPUT_BUFFER];
  594. char *tmpmsg = (char *) &tmpmsg_c;
  595. tmpmsg = strcpy (tmpmsg, msg);
  596. if (!strncmp (tmpmsg, " Hex:", 5)) {
  597. tmpmsg = strtok (tmpmsg, ":");
  598. while ((tmpmsg = strtok (NULL, " "))) {
  599. foo = strtol (tmpmsg, NULL, 16);
  600. /* Translate chars that are not the same value in the printers
  601. * character set.
  602. */
  603. switch (foo) {
  604. case 208:
  605. {
  606. foo = 197;
  607. break;
  608. }
  609. case 216:
  610. {
  611. foo = 196;
  612. break;
  613. }
  614. }
  615. msg[i] = foo;
  616. i++;
  617. }
  618. msg[i] = 0;
  619. }
  620. return (msg);
  621. }
  622. int
  623. check_num (int i)
  624. {
  625. int result;
  626. result = STATE_OK;
  627. if (eval_method[i] & WARN_GT && eval_method[i] & WARN_LT &&
  628. lower_warn_lim[i] > upper_warn_lim[i]) {
  629. if (response_value[i] <= lower_warn_lim[i] &&
  630. response_value[i] >= upper_warn_lim[i]) {
  631. result = STATE_WARNING;
  632. }
  633. }
  634. else if
  635. ((eval_method[i] & WARN_GT && response_value[i] > upper_warn_lim[i]) ||
  636. (eval_method[i] & WARN_GE && response_value[i] >= upper_warn_lim[i]) ||
  637. (eval_method[i] & WARN_LT && response_value[i] < lower_warn_lim[i]) ||
  638. (eval_method[i] & WARN_LE && response_value[i] <= lower_warn_lim[i]) ||
  639. (eval_method[i] & WARN_EQ && response_value[i] == upper_warn_lim[i]) ||
  640. (eval_method[i] & WARN_NE && response_value[i] != upper_warn_lim[i])) {
  641. result = STATE_WARNING;
  642. }
  643. if (eval_method[i] & CRIT_GT && eval_method[i] & CRIT_LT &&
  644. lower_crit_lim[i] > upper_crit_lim[i]) {
  645. if (response_value[i] <= lower_crit_lim[i] &&
  646. response_value[i] >= upper_crit_lim[i]) {
  647. result = STATE_CRITICAL;
  648. }
  649. }
  650. else if
  651. ((eval_method[i] & CRIT_GT && response_value[i] > upper_crit_lim[i]) ||
  652. (eval_method[i] & CRIT_GE && response_value[i] >= upper_crit_lim[i]) ||
  653. (eval_method[i] & CRIT_LT && response_value[i] < lower_crit_lim[i]) ||
  654. (eval_method[i] & CRIT_LE && response_value[i] <= lower_crit_lim[i]) ||
  655. (eval_method[i] & CRIT_EQ && response_value[i] == upper_crit_lim[i]) ||
  656. (eval_method[i] & CRIT_NE && response_value[i] != upper_crit_lim[i])) {
  657. result = STATE_CRITICAL;
  658. }
  659. return result;
  660. }
  661. int
  662. lu_getll (unsigned long *ll, char *str)
  663. {
  664. char tmp[100];
  665. if (strchr (str, ':') == NULL)
  666. return 0;
  667. if (strchr (str, ',') != NULL && (strchr (str, ',') < strchr (str, ':')))
  668. return 0;
  669. if (sscanf (str, "%lu%[:]", ll, tmp) == 2)
  670. return 1;
  671. return 0;
  672. }
  673. int
  674. lu_getul (unsigned long *ul, char *str)
  675. {
  676. char tmp[100];
  677. if (sscanf (str, "%lu%[^,]", ul, tmp) == 1)
  678. return 1;
  679. if (sscanf (str, ":%lu%[^,]", ul, tmp) == 1)
  680. return 1;
  681. if (sscanf (str, "%*u:%lu%[^,]", ul, tmp) == 1)
  682. return 1;
  683. return 0;
  684. }
  685. /* trim leading whitespace
  686. if there is a leading quote, make sure it balances */
  687. char *
  688. thisarg (char *str)
  689. {
  690. str += strspn (str, " \t\r\n"); /* trim any leading whitespace */
  691. if (strstr (str, "'") == str) { /* handle SIMPLE quoted strings */
  692. if (strlen (str) == 1 || !strstr (str + 1, "'"))
  693. die (STATE_UNKNOWN, _("Unbalanced quotes\n"));
  694. }
  695. return str;
  696. }
  697. /* if there's a leading quote, advance to the trailing quote
  698. set the trailing quote to '\x0'
  699. if the string continues, advance beyond the comma */
  700. char *
  701. nextarg (char *str)
  702. {
  703. if (strstr (str, "'") == str) {
  704. str[0] = 0;
  705. if (strlen (str) > 1) {
  706. str = strstr (str + 1, "'");
  707. return (++str);
  708. }
  709. else {
  710. return NULL;
  711. }
  712. }
  713. if (strstr (str, ",") == str) {
  714. str[0] = 0;
  715. if (strlen (str) > 1) {
  716. return (++str);
  717. }
  718. else {
  719. return NULL;
  720. }
  721. }
  722. if ((str = strstr (str, ",")) && strlen (str) > 1) {
  723. str[0] = 0;
  724. return (++str);
  725. }
  726. return NULL;
  727. }
  728. void
  729. print_help (void)
  730. {
  731. print_revision (progname, revision);
  732. printf (COPYRIGHT, copyright, email);
  733. printf (_("\
  734. Check status of remote machines and obtain sustem information via SNMP\n\n"));
  735. print_usage ();
  736. printf (_(UT_HELP_VRSN));
  737. printf (_(UT_HOST_PORT), 'p', DEFAULT_PORT);
  738. /* SNMP and Authentication Protocol */
  739. printf (_("\
  740. -P, --protocol=[1|3]\n\
  741. SNMP protocol version\n\
  742. -L, --seclevel=[noAuthNoPriv|authNoPriv|authPriv]\n\
  743. SNMPv3 securityLevel\n\
  744. -a, --authproto=[MD5|SHA]\n\
  745. SNMPv3 auth proto\n"));
  746. /* Authentication Tokens*/
  747. printf (_("\
  748. -C, --community=STRING\n\
  749. Optional community string for SNMP communication\n\
  750. (default is \"%s\")\n\
  751. -U, --secname=USERNAME\n\
  752. SNMPv3 username\n\
  753. -A, --authpassword=PASSWORD\n\
  754. SNMPv3 authentication password\n\
  755. -X, --privpasswd=PASSWORD\n\
  756. SNMPv3 crypt passwd (DES)\n"), DEFAULT_COMMUNITY);
  757. /* OID Stuff */
  758. printf (_("\
  759. -o, --oid=OID(s)\n\
  760. Object identifier(s) whose value you wish to query\n\
  761. -m, --miblist=STRING\n\
  762. List of MIBS to be loaded (default = ALL)\n -d, --delimiter=STRING\n\
  763. Delimiter to use when parsing returned data. Default is \"%s\"\n\
  764. Any data on the right hand side of the delimiter is considered\n\
  765. to be the data that should be used in the evaluation.\n"), DEFAULT_DELIMITER);
  766. /* Tests Against Integers */
  767. printf (_("\
  768. -w, --warning=INTEGER_RANGE(s)\n\
  769. Range(s) which will not result in a WARNING status\n\
  770. -c, --critical=INTEGER_RANGE(s)\n\
  771. Range(s) which will not result in a CRITICAL status\n"));
  772. /* Tests Against Strings */
  773. printf (_("\
  774. -s, --string=STRING\n\
  775. Return OK state (for that OID) if STRING is an exact match\n\
  776. -r, --ereg=REGEX\n\
  777. Return OK state (for that OID) if extended regular expression REGEX matches\n\
  778. -R, --eregi=REGEX\n\
  779. Return OK state (for that OID) if case-insensitive extended REGEX matches\n\
  780. -l, --label=STRING\n\
  781. Prefix label for output from plugin (default -s 'SNMP')\n"));
  782. /* Output Formatting */
  783. printf (_("\
  784. -u, --units=STRING\n\
  785. Units label(s) for output data (e.g., 'sec.').\n\
  786. -D, --output-delimiter=STRING\n\
  787. Separates output on multiple OID requests\n"));
  788. printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
  789. printf (_(UT_VERBOSE));
  790. printf (_("\n\
  791. - This plugin uses the 'snmpget' command included with the NET-SNMP package.\n\
  792. If you don't have the package installed, you will need to download it from\n\
  793. http://net-snmp.sourceforge.net before you can use this plugin.\n"));
  794. printf (_("\
  795. - Multiple OIDs may be indicated by a comma- or space-delimited list (lists with\n\
  796. internal spaces must be quoted) [max 8 OIDs]\n"));
  797. printf (_("\
  798. - Ranges are inclusive and are indicated with colons. When specified as\n\
  799. 'min:max' a STATE_OK will be returned if the result is within the indicated\n\
  800. range or is equal to the upper or lower bound. A non-OK state will be\n\
  801. returned if the result is outside the specified range.\n"));
  802. printf (_("\
  803. - If specified in the order 'max:min' a non-OK state will be returned if the\n\
  804. result is within the (inclusive) range.\n"));
  805. printf (_("\
  806. - Upper or lower bounds may be omitted to skip checking the respective limit.\n\
  807. - Bare integers are interpreted as upper limits.\n\
  808. - When checking multiple OIDs, separate ranges by commas like '-w 1:10,1:,:20'\n\
  809. - Note that only one string and one regex may be checked at present\n\
  810. - All evaluation methods other than PR, STR, and SUBSTR expect that the value\n\
  811. returned from the SNMP query is an unsigned integer.\n"));
  812. printf (_(UT_SUPPORT));
  813. }
  814. void
  815. print_usage (void)
  816. {
  817. printf ("\
  818. Usage: %s -H <ip_address> -o <OID> [-w warn_range] [-c crit_range] \n\
  819. [-C community] [-s string] [-r regex] [-R regexi] [-t timeout]\n\
  820. [-l label] [-u units] [-p port-number] [-d delimiter]\n\
  821. [-D output-delimiter] [-m miblist] [-P snmp version]\n\
  822. [-L seclevel] [-U secname] [-a authproto] [-A authpasswd]\n\
  823. [-X privpasswd]\n", progname);
  824. }