check_snmp.c 26 KB

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