check_snmp.c 26 KB

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