check_snmp.c 26 KB

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