check_snmp.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  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 (_("Call for regex which was not a compiled option"));
  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 (_("call for regex which was not a compiled option"));
  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, _("Could not reallocate 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 reallocate 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, _("Could not reallocate units [%d]\n"), nunits);
  502. }
  503. unitv[nunits - 1] = optarg;
  504. ptr = thisarg (optarg);
  505. unitv[nunits - 1] = ptr;
  506. if (strstr (ptr, "'") == ptr)
  507. unitv[nunits - 1] = ptr + 1;
  508. while (ptr && (ptr = nextarg (ptr))) {
  509. if (nunits >= unitv_size) {
  510. unitv_size += 8;
  511. unitv = realloc (unitv, unitv_size);
  512. if (units == NULL)
  513. die (STATE_UNKNOWN, _("Could not realloc() units\n"));
  514. }
  515. nunits++;
  516. ptr = thisarg (ptr);
  517. if (strstr (ptr, "'") == ptr)
  518. unitv[nunits - 1] = ptr + 1;
  519. else
  520. unitv[nunits - 1] = ptr;
  521. }
  522. break;
  523. }
  524. }
  525. if (server_address == NULL)
  526. server_address = argv[optind];
  527. if (community == NULL)
  528. community = strdup (DEFAULT_COMMUNITY);
  529. return validate_arguments ();
  530. }
  531. /******************************************************************************
  532. @@-
  533. <sect3>
  534. <title>validate_arguments</title>
  535. <para>&PROTO_validate_arguments;</para>
  536. <para>Given a database name, this function returns TRUE if the string
  537. is a valid PostgreSQL database name, and returns false if it is
  538. not.</para>
  539. <para>Valid PostgreSQL database names are less than &NAMEDATALEN;
  540. characters long and consist of letters, numbers, and underscores. The
  541. first character cannot be a number, however.</para>
  542. </sect3>
  543. -@@
  544. ******************************************************************************/
  545. int
  546. validate_arguments ()
  547. {
  548. /* Need better checks to verify seclevel and authproto choices */
  549. if (seclevel == NULL)
  550. asprintf (&seclevel, "noAuthNoPriv");
  551. if (authproto == NULL )
  552. asprintf(&authproto, DEFAULT_AUTH_PROTOCOL);
  553. if (proto == NULL || (strcmp(proto,DEFAULT_PROTOCOL) == 0) ) { /* default protocol version */
  554. asprintf(&proto, DEFAULT_PROTOCOL);
  555. asprintf(&authpriv, "%s%s", "-c ", community);
  556. }
  557. else if ( strcmp (proto, "3") == 0 ) { /* snmpv3 args */
  558. asprintf(&proto, "%s", "3");
  559. if ( (strcmp(seclevel, "noAuthNoPriv") == 0) || seclevel == NULL ) {
  560. asprintf(&authpriv, "%s", "-l noAuthNoPriv" );
  561. }
  562. else if ( strcmp(seclevel, "authNoPriv") == 0 ) {
  563. if ( secname == NULL || authpasswd == NULL) {
  564. printf (_("Missing secname (%s) or authpassword (%s) ! \n"),secname, authpasswd );
  565. print_usage ();
  566. exit (STATE_UNKNOWN);
  567. }
  568. asprintf(&authpriv, "-l authNoPriv -a %s -u %s -A %s ", authproto, secname, authpasswd);
  569. }
  570. else if ( strcmp(seclevel, "authPriv") == 0 ) {
  571. if ( secname == NULL || authpasswd == NULL || privpasswd == NULL ) {
  572. printf (_("Missing secname (%s), authpassword (%s), or privpasswd (%s)! \n"),secname, authpasswd,privpasswd );
  573. print_usage ();
  574. exit (STATE_UNKNOWN);
  575. }
  576. asprintf(&authpriv, "-l authPriv -a %s -u %s -A %s -x DES -X %s ", authproto, secname, authpasswd, privpasswd);
  577. }
  578. }
  579. else {
  580. usage2 (_("Invalid SNMP version"), proto);
  581. }
  582. return OK;
  583. }
  584. char *
  585. clarify_message (char *msg)
  586. {
  587. int i = 0;
  588. int foo;
  589. char tmpmsg_c[MAX_INPUT_BUFFER];
  590. char *tmpmsg = (char *) &tmpmsg_c;
  591. tmpmsg = strcpy (tmpmsg, msg);
  592. if (!strncmp (tmpmsg, " Hex:", 5)) {
  593. tmpmsg = strtok (tmpmsg, ":");
  594. while ((tmpmsg = strtok (NULL, " "))) {
  595. foo = strtol (tmpmsg, NULL, 16);
  596. /* Translate chars that are not the same value in the printers
  597. * character set.
  598. */
  599. switch (foo) {
  600. case 208:
  601. {
  602. foo = 197;
  603. break;
  604. }
  605. case 216:
  606. {
  607. foo = 196;
  608. break;
  609. }
  610. }
  611. msg[i] = foo;
  612. i++;
  613. }
  614. msg[i] = 0;
  615. }
  616. return (msg);
  617. }
  618. int
  619. check_num (int i)
  620. {
  621. int result;
  622. result = STATE_OK;
  623. if (eval_method[i] & WARN_GT && eval_method[i] & WARN_LT &&
  624. lower_warn_lim[i] > upper_warn_lim[i]) {
  625. if (response_value[i] <= lower_warn_lim[i] &&
  626. response_value[i] >= upper_warn_lim[i]) {
  627. result = STATE_WARNING;
  628. }
  629. }
  630. else if
  631. ((eval_method[i] & WARN_GT && response_value[i] > upper_warn_lim[i]) ||
  632. (eval_method[i] & WARN_GE && response_value[i] >= upper_warn_lim[i]) ||
  633. (eval_method[i] & WARN_LT && response_value[i] < lower_warn_lim[i]) ||
  634. (eval_method[i] & WARN_LE && response_value[i] <= lower_warn_lim[i]) ||
  635. (eval_method[i] & WARN_EQ && response_value[i] == upper_warn_lim[i]) ||
  636. (eval_method[i] & WARN_NE && response_value[i] != upper_warn_lim[i])) {
  637. result = STATE_WARNING;
  638. }
  639. if (eval_method[i] & CRIT_GT && eval_method[i] & CRIT_LT &&
  640. lower_crit_lim[i] > upper_crit_lim[i]) {
  641. if (response_value[i] <= lower_crit_lim[i] &&
  642. response_value[i] >= upper_crit_lim[i]) {
  643. result = STATE_CRITICAL;
  644. }
  645. }
  646. else if
  647. ((eval_method[i] & CRIT_GT && response_value[i] > upper_crit_lim[i]) ||
  648. (eval_method[i] & CRIT_GE && response_value[i] >= upper_crit_lim[i]) ||
  649. (eval_method[i] & CRIT_LT && response_value[i] < lower_crit_lim[i]) ||
  650. (eval_method[i] & CRIT_LE && response_value[i] <= lower_crit_lim[i]) ||
  651. (eval_method[i] & CRIT_EQ && response_value[i] == upper_crit_lim[i]) ||
  652. (eval_method[i] & CRIT_NE && response_value[i] != upper_crit_lim[i])) {
  653. result = STATE_CRITICAL;
  654. }
  655. return result;
  656. }
  657. int
  658. lu_getll (unsigned long *ll, char *str)
  659. {
  660. char tmp[100];
  661. if (strchr (str, ':') == NULL)
  662. return 0;
  663. if (strchr (str, ',') != NULL && (strchr (str, ',') < strchr (str, ':')))
  664. return 0;
  665. if (sscanf (str, "%lu%[:]", ll, tmp) == 2)
  666. return 1;
  667. return 0;
  668. }
  669. int
  670. lu_getul (unsigned long *ul, char *str)
  671. {
  672. char tmp[100];
  673. if (sscanf (str, "%lu%[^,]", ul, tmp) == 1)
  674. return 1;
  675. if (sscanf (str, ":%lu%[^,]", ul, tmp) == 1)
  676. return 1;
  677. if (sscanf (str, "%*u:%lu%[^,]", ul, tmp) == 1)
  678. return 1;
  679. return 0;
  680. }
  681. /* trim leading whitespace
  682. if there is a leading quote, make sure it balances */
  683. char *
  684. thisarg (char *str)
  685. {
  686. str += strspn (str, " \t\r\n"); /* trim any leading whitespace */
  687. if (strstr (str, "'") == str) { /* handle SIMPLE quoted strings */
  688. if (strlen (str) == 1 || !strstr (str + 1, "'"))
  689. die (STATE_UNKNOWN, _("Unbalanced quotes\n"));
  690. }
  691. return str;
  692. }
  693. /* if there's a leading quote, advance to the trailing quote
  694. set the trailing quote to '\x0'
  695. if the string continues, advance beyond the comma */
  696. char *
  697. nextarg (char *str)
  698. {
  699. if (strstr (str, "'") == str) {
  700. str[0] = 0;
  701. if (strlen (str) > 1) {
  702. str = strstr (str + 1, "'");
  703. return (++str);
  704. }
  705. else {
  706. return NULL;
  707. }
  708. }
  709. if (strstr (str, ",") == str) {
  710. str[0] = 0;
  711. if (strlen (str) > 1) {
  712. return (++str);
  713. }
  714. else {
  715. return NULL;
  716. }
  717. }
  718. if ((str = strstr (str, ",")) && strlen (str) > 1) {
  719. str[0] = 0;
  720. return (++str);
  721. }
  722. return NULL;
  723. }
  724. void
  725. print_help (void)
  726. {
  727. print_revision (progname, revision);
  728. printf (COPYRIGHT, copyright, email);
  729. printf (_("\
  730. Check status of remote machines and obtain sustem information via SNMP\n\n"));
  731. print_usage ();
  732. printf (_(UT_HELP_VRSN));
  733. printf (_(UT_HOST_PORT), 'p', DEFAULT_PORT);
  734. /* SNMP and Authentication Protocol */
  735. printf (_("\
  736. -P, --protocol=[1|3]\n\
  737. SNMP protocol version\n\
  738. -L, --seclevel=[noAuthNoPriv|authNoPriv|authPriv]\n\
  739. SNMPv3 securityLevel\n\
  740. -a, --authproto=[MD5|SHA]\n\
  741. SNMPv3 auth proto\n"));
  742. /* Authentication Tokens*/
  743. printf (_("\
  744. -C, --community=STRING\n\
  745. Optional community string for SNMP communication\n\
  746. (default is \"%s\")\n\
  747. -U, --secname=USERNAME\n\
  748. SNMPv3 username\n\
  749. -A, --authpassword=PASSWORD\n\
  750. SNMPv3 authentication password\n\
  751. -X, --privpasswd=PASSWORD\n\
  752. SNMPv3 crypt passwd (DES)\n"), DEFAULT_COMMUNITY);
  753. /* OID Stuff */
  754. printf (_("\
  755. -o, --oid=OID(s)\n\
  756. Object identifier(s) whose value you wish to query\n\
  757. -m, --miblist=STRING\n\
  758. List of MIBS to be loaded (default = ALL)\n -d, --delimiter=STRING\n\
  759. Delimiter to use when parsing returned data. Default is \"%s\"\n\
  760. Any data on the right hand side of the delimiter is considered\n\
  761. to be the data that should be used in the evaluation.\n"), DEFAULT_DELIMITER);
  762. /* Tests Against Integers */
  763. printf (_("\
  764. -w, --warning=INTEGER_RANGE(s)\n\
  765. Range(s) which will not result in a WARNING status\n\
  766. -c, --critical=INTEGER_RANGE(s)\n\
  767. Range(s) which will not result in a CRITICAL status\n"));
  768. /* Tests Against Strings */
  769. printf (_("\
  770. -s, --string=STRING\n\
  771. Return OK state (for that OID) if STRING is an exact match\n\
  772. -r, --ereg=REGEX\n\
  773. Return OK state (for that OID) if extended regular expression REGEX matches\n\
  774. -R, --eregi=REGEX\n\
  775. Return OK state (for that OID) if case-insensitive extended REGEX matches\n\
  776. -l, --label=STRING\n\
  777. Prefix label for output from plugin (default -s 'SNMP')\n"));
  778. /* Output Formatting */
  779. printf (_("\
  780. -u, --units=STRING\n\
  781. Units label(s) for output data (e.g., 'sec.').\n\
  782. -D, --output-delimiter=STRING\n\
  783. Separates output on multiple OID requests\n"));
  784. printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
  785. printf (_(UT_VERBOSE));
  786. printf (_("\n\
  787. - This plugin uses the 'snmpget' command included with the NET-SNMP package.\n\
  788. If you don't have the package installed, you will need to download it from\n\
  789. http://net-snmp.sourceforge.net before you can use this plugin.\n"));
  790. printf (_("\
  791. - Multiple OIDs may be indicated by a comma- or space-delimited list (lists with\n\
  792. internal spaces must be quoted) [max 8 OIDs]\n"));
  793. printf (_("\
  794. - Ranges are inclusive and are indicated with colons. When specified as\n\
  795. 'min:max' a STATE_OK will be returned if the result is within the indicated\n\
  796. range or is equal to the upper or lower bound. A non-OK state will be\n\
  797. returned if the result is outside the specified range.\n"));
  798. printf (_("\
  799. - If specified in the order 'max:min' a non-OK state will be returned if the\n\
  800. result is within the (inclusive) range.\n"));
  801. printf (_("\
  802. - Upper or lower bounds may be omitted to skip checking the respective limit.\n\
  803. - Bare integers are interpreted as upper limits.\n\
  804. - When checking multiple OIDs, separate ranges by commas like '-w 1:10,1:,:20'\n\
  805. - Note that only one string and one regex may be checked at present\n\
  806. - All evaluation methods other than PR, STR, and SUBSTR expect that the value\n\
  807. returned from the SNMP query is an unsigned integer.\n"));
  808. printf (_(UT_SUPPORT));
  809. }
  810. void
  811. print_usage (void)
  812. {
  813. printf ("\
  814. Usage: %s -H <ip_address> -o <OID> [-w warn_range] [-c crit_range] \n\
  815. [-C community] [-s string] [-r regex] [-R regexi] [-t timeout]\n\
  816. [-l label] [-u units] [-p port-number] [-d delimiter]\n\
  817. [-D output-delimiter] [-m miblist] [-P snmp version]\n\
  818. [-L seclevel] [-U secname] [-a authproto] [-A authpasswd]\n\
  819. [-X privpasswd]\n", progname);
  820. }