check_snmp.c 26 KB

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