check_snmp.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  1. /******************************************************************************
  2. *
  3. * Program: SNMP plugin for Nagios
  4. * License: GPL
  5. *
  6. * License Information:
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. *
  22. *./plugins/check_snmp 127.0.0.1 -c public -o .1.3.6.1.4.1.2021.9.1.2.1
  23. *
  24. *****************************************************************************/
  25. #define PROGNAME "check_snmp"
  26. #define REVISION "$Revision$"
  27. #define COPYRIGHT "1999-2002"
  28. #define AUTHOR "Ethan Galstad"
  29. #define EMAIL "nagios@nagios.org"
  30. #define SUMMARY "Check status of remote machines using SNMP.\n"
  31. #define OPTIONS "\
  32. -H <ip_address> -o <OID> [-w warn_range] [-c crit_range] \n\
  33. [-C community] [-s string] [-r regex] [-R regexi] [-t timeout]\n\
  34. [-l label] [-u units] [-p port-number] [-d delimiter]\n\
  35. [-D output-delimiter]"
  36. #define LONGOPTIONS "\
  37. -H, --hostname=HOST\n\
  38. Name or IP address of the device you wish to query\n\
  39. -o, --oid=OID(s)\n\
  40. Object identifier(s) whose value you wish to query\n\
  41. -w, --warning=INTEGER_RANGE(s)\n\
  42. Range(s) which will not result in a WARNING status\n\
  43. -c, --critical=INTEGER_RANGE(s)\n\
  44. Range(s) which will not result in a CRITICAL status\n\
  45. -C, --community=STRING\n\
  46. Optional community string for SNMP communication\n\
  47. (default is \"%s\")\n\
  48. -u, --units=STRING\n\
  49. Units label(s) for output data (e.g., 'sec.').\n\
  50. -p, --port=STRING\n\
  51. UDP port number target is listening on. Default is \"%s\"\n\
  52. -d, --delimiter=STRING\n\
  53. Delimiter to use when parsing returned data. Default is \"%s\"\n\
  54. Any data on the right hand side of the delimiter is considered\n\
  55. to be the data that should be used in the evaluation.\n\
  56. -t, --timeout=INTEGER\n\
  57. Seconds to wait before plugin times out (see also nagios server timeout).\n\
  58. Default is %d seconds\n\
  59. -D, --output-delimiter=STRING\n\
  60. Separates output on multiple OID requests\n\
  61. -s, --string=STRING\n\
  62. Return OK state (for that OID) if STRING is an exact match\n\
  63. -r, --ereg=REGEX\n\
  64. Return OK state (for that OID) if extended regular expression REGEX matches\n\
  65. -R, --eregi=REGEX\n\
  66. Return OK state (for that OID) if case-insensitive extended REGEX matches\n\
  67. -l, --label=STRING\n\
  68. Prefix label for output from plugin (default -s 'SNMP')\n\
  69. -v, --verbose\n\
  70. Debugging the output\n\
  71. "
  72. #define NOTES "\
  73. - This plugin uses the 'snmpget' command included with the NET-SNMP package.\n\
  74. If you don't have the package installed, you will need to download it from\n\
  75. http://net-snmp.sourceforge.net before you can use this plugin.\n\
  76. - Multiple OIDs may be indicated by a comma- or space-delimited list (lists with\n\
  77. internal spaces must be quoted) [max 8 OIDs]\n\
  78. - Ranges are inclusive and are indicated with colons. When specified as\n\
  79. 'min:max' a STATE_OK will be returned if the result is within the indicated\n\
  80. range or is equal to the upper or lower bound. A non-OK state will be\n\
  81. returned if the result is outside the specified range.\n\
  82. - If specified in the order 'max:min' a non-OK state will be returned if the\n\
  83. result is within the (inclusive) range.\n\
  84. - Upper or lower bounds may be omitted to skip checking the respective limit.\n\
  85. - Bare integers are interpreted as upper limits.\n\
  86. - When checking multiple OIDs, separate ranges by commas like '-w 1:10,1:,:20'\n\
  87. - Note that only one string and one regex may be checked at present\n\
  88. - All evaluation methods other than PR, STR, and SUBSTR expect that the value\n\
  89. returned from the SNMP query is an unsigned integer.\n"
  90. #define DESCRIPTION "\
  91. This plugin gets system information on a remote server via snmp.\n"
  92. #define DEFAULT_COMMUNITY "public"
  93. #define DEFAULT_PORT "161"
  94. #define DEFAULT_TIMEOUT 10
  95. #include "common.h"
  96. #include "utils.h"
  97. #include "popen.h"
  98. #define mark(a) ((a)!=0?"*":"")
  99. #define CHECK_UNDEF 0
  100. #define CRIT_PRESENT 1
  101. #define CRIT_STRING 2
  102. #define CRIT_REGEX 4
  103. #define CRIT_GT 8
  104. #define CRIT_LT 16
  105. #define CRIT_GE 32
  106. #define CRIT_LE 64
  107. #define CRIT_EQ 128
  108. #define CRIT_NE 256
  109. #define CRIT_RANGE 512
  110. #define WARN_PRESENT 1024
  111. #define WARN_STRING 2048
  112. #define WARN_REGEX 4096
  113. #define WARN_GT 8192
  114. #define WARN_LT 16384
  115. #define WARN_GE 32768
  116. #define WARN_LE 65536
  117. #define WARN_EQ 131072
  118. #define WARN_NE 262144
  119. #define WARN_RANGE 524288
  120. #define MAX_OIDS 8
  121. #define MAX_DELIM_LENGTH 8
  122. #define DEFAULT_DELIMITER "="
  123. #define DEFAULT_OUTPUT_DELIMITER " "
  124. void print_usage (void);
  125. void print_help (void);
  126. int process_arguments (int, char **);
  127. int validate_arguments (void);
  128. int check_num (int);
  129. char *clarify_message (char *);
  130. int lu_getll (unsigned long *, char *);
  131. int lu_getul (unsigned long *, char *);
  132. char *thisarg (char *str);
  133. char *nextarg (char *str);
  134. #ifdef HAVE_REGEX_H
  135. #include <regex.h>
  136. char regex_expect[MAX_INPUT_BUFFER] = "";
  137. regex_t preg;
  138. regmatch_t pmatch[10];
  139. char timestamp[10] = "";
  140. char regex[MAX_INPUT_BUFFER];
  141. char errbuf[MAX_INPUT_BUFFER];
  142. int cflags = REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
  143. int eflags = 0;
  144. int errcode, excode;
  145. #endif
  146. char *server_address = NULL;
  147. char *community = NULL;
  148. char *oid = "";
  149. char *label = NULL;
  150. char *units = NULL;
  151. char *port = DEFAULT_PORT;
  152. char string_value[MAX_INPUT_BUFFER] = "";
  153. char **labels = NULL;
  154. char **unitv = NULL;
  155. int nlabels = 0;
  156. int labels_size = 8;
  157. int nunits = 0;
  158. int unitv_size = 8;
  159. int verbose = FALSE;
  160. unsigned long lower_warn_lim[MAX_OIDS];
  161. unsigned long upper_warn_lim[MAX_OIDS];
  162. unsigned long lower_crit_lim[MAX_OIDS];
  163. unsigned long upper_crit_lim[MAX_OIDS];
  164. unsigned long response_value[MAX_OIDS];
  165. int check_warning_value = FALSE;
  166. int check_critical_value = FALSE;
  167. int eval_method[MAX_OIDS];
  168. char *delimiter = NULL;
  169. char *output_delim = NULL;
  170. int
  171. main (int argc, char **argv)
  172. {
  173. int i = 0;
  174. int iresult = STATE_UNKNOWN;
  175. int found = 0;
  176. int result = STATE_DEPENDENT;
  177. char input_buffer[MAX_INPUT_BUFFER];
  178. char *command_line = NULL;
  179. char *response = NULL;
  180. char *outbuff = "";
  181. char *output = NULL;
  182. char *ptr = NULL;
  183. char *p2 = NULL;
  184. char *show = NULL;
  185. labels = malloc (labels_size);
  186. unitv = malloc (unitv_size);
  187. for (i = 0; i < MAX_OIDS; i++)
  188. eval_method[i] = CHECK_UNDEF;
  189. i = 0;
  190. if (process_arguments (argc, argv) == ERROR)
  191. usage ("Incorrect arguments supplied\n");
  192. /* create the command line to execute */
  193. asprintf (&command_line, "%s -m ALL -v 1 -c %s %s:%s %s",
  194. PATH_TO_SNMPGET, community, server_address, port, oid);
  195. if (verbose)
  196. printf ("%s\n", command_line);
  197. /* run the command */
  198. child_process = spopen (command_line);
  199. if (child_process == NULL) {
  200. printf ("Could not open pipe: %s\n", command_line);
  201. exit (STATE_UNKNOWN);
  202. }
  203. child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  204. if (child_stderr == NULL) {
  205. printf ("Could not open stderr for %s\n", command_line);
  206. }
  207. asprintf (&output, "");
  208. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process))
  209. asprintf (&output, "%s%s", output, input_buffer);
  210. if (verbose)
  211. printf ("%s\n", output);
  212. ptr = output;
  213. while (ptr) {
  214. ptr = strstr (ptr, delimiter);
  215. if (ptr == NULL)
  216. break;
  217. ptr += strlen (delimiter);
  218. ptr += strspn (ptr, " ");
  219. found++;
  220. if (ptr[0] == '"') {
  221. ptr++;
  222. response = strpcpy (response, ptr, "\"");
  223. ptr = strpbrk (ptr, "\"");
  224. ptr += strspn (ptr, "\"\n");
  225. }
  226. else {
  227. response = strpcpy (response, ptr, "\n");
  228. ptr = strpbrk (ptr, "\n");
  229. ptr += strspn (ptr, "\n");
  230. while
  231. (strstr (ptr, delimiter) &&
  232. strstr (ptr, "\n") && strstr (ptr, "\n") < strstr (ptr, delimiter)) {
  233. response = strpcat (response, ptr, "\n");
  234. ptr = strpbrk (ptr, "\n");
  235. }
  236. if (ptr && strstr (ptr, delimiter) == NULL) {
  237. response = strscat (response, ptr);
  238. ptr = NULL;
  239. }
  240. }
  241. if (strstr (response, "Gauge: "))
  242. show = strstr (response, "Gauge: ") + 7;
  243. else if (strstr (response, "Gauge32: "))
  244. show = strstr (response, "Gauge32: ") + 9;
  245. else
  246. show = response;
  247. p2 = show;
  248. iresult = STATE_DEPENDENT;
  249. if (eval_method[i] & CRIT_PRESENT) {
  250. iresult = STATE_CRITICAL;
  251. } else if (eval_method[i] & WARN_PRESENT) {
  252. iresult = STATE_WARNING;
  253. }
  254. if (eval_method[i] & CRIT_GT ||
  255. eval_method[i] & CRIT_LT ||
  256. eval_method[i] & CRIT_GE ||
  257. eval_method[i] & CRIT_LE ||
  258. eval_method[i] & CRIT_EQ ||
  259. eval_method[i] & CRIT_NE ||
  260. eval_method[i] & WARN_GT ||
  261. eval_method[i] & WARN_LT ||
  262. eval_method[i] & WARN_GE ||
  263. eval_method[i] & WARN_LE ||
  264. eval_method[i] & WARN_EQ || eval_method[i] & WARN_NE) {
  265. p2 = strpbrk (p2, "0123456789");
  266. response_value[i] = strtoul (p2, NULL, 10);
  267. iresult = check_num (i);
  268. asprintf (&show, "%lu", response_value[i]);
  269. /*asprintf (&show, "%s", response); */
  270. }
  271. else if (eval_method[i] & CRIT_STRING) {
  272. if (strcmp (response, string_value))
  273. iresult = STATE_CRITICAL;
  274. else
  275. iresult = STATE_OK;
  276. }
  277. else if (eval_method[i] & CRIT_REGEX) {
  278. #ifdef HAVE_REGEX_H
  279. excode = regexec (&preg, response, 10, pmatch, eflags);
  280. if (excode == 0) {
  281. iresult = STATE_OK;
  282. }
  283. else if (excode != REG_NOMATCH) {
  284. regerror (excode, &preg, errbuf, MAX_INPUT_BUFFER);
  285. printf ("Execute Error: %s\n", errbuf);
  286. exit (STATE_CRITICAL);
  287. }
  288. else {
  289. iresult = STATE_CRITICAL;
  290. }
  291. #else
  292. printf ("SNMP UNKNOWN: call for regex which was not a compiled option");
  293. exit (STATE_UNKNOWN);
  294. #endif
  295. }
  296. if (response && iresult == STATE_DEPENDENT)
  297. iresult = STATE_OK;
  298. else if (eval_method[i] & CRIT_PRESENT)
  299. iresult = STATE_CRITICAL;
  300. else
  301. iresult = STATE_WARNING;
  302. result = max_state (result, iresult);
  303. if (nlabels > 1 && i < nlabels && labels[i] != NULL)
  304. asprintf (&outbuff, "%s%s%s %s%s%s", outbuff,
  305. (i == 0) ? " " : output_delim,
  306. labels[i], mark (iresult), show, mark (iresult));
  307. else
  308. asprintf (&outbuff, "%s%s%s%s%s", outbuff, (i == 0) ? " " : output_delim,
  309. mark (iresult), show, mark (iresult));
  310. if (nunits > 0 && i < nunits)
  311. asprintf (&outbuff, "%s %s", outbuff, unitv[i]);
  312. i++;
  313. } /* end while */
  314. if (found == 0)
  315. terminate
  316. (STATE_UNKNOWN,
  317. "%s problem - No data recieved from host\nCMD: %s\n",
  318. label, command_line);
  319. /* WARNING if output found on stderr */
  320. if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
  321. result = max_state (result, STATE_WARNING);
  322. /* close stderr */
  323. (void) fclose (child_stderr);
  324. /* close the pipe */
  325. if (spclose (child_process))
  326. result = max_state (result, STATE_WARNING);
  327. if (nunits > 0)
  328. printf ("%s %s -%s %s\n", label, state_text (result), outbuff, units);
  329. else
  330. printf ("%s %s -%s\n", label, state_text (result), outbuff);
  331. return result;
  332. }
  333. /* process command-line arguments */
  334. int
  335. process_arguments (int argc, char **argv)
  336. {
  337. char *ptr;
  338. int c = 1;
  339. int j = 0, jj = 0;
  340. #ifdef HAVE_GETOPT_H
  341. int option_index = 0;
  342. static struct option long_options[] = {
  343. STD_LONG_OPTS,
  344. {"community", required_argument, 0, 'C'},
  345. {"oid", required_argument, 0, 'o'},
  346. {"object", required_argument, 0, 'o'},
  347. {"delimiter", required_argument, 0, 'd'},
  348. {"output-delimiter", required_argument, 0, 'D'},
  349. {"string", required_argument, 0, 's'},
  350. {"regex", required_argument, 0, 'r'},
  351. {"ereg", required_argument, 0, 'r'},
  352. {"eregi", required_argument, 0, 'R'},
  353. {"label", required_argument, 0, 'l'},
  354. {"units", required_argument, 0, 'u'},
  355. {"port", required_argument, 0, 'p'},
  356. {0, 0, 0, 0}
  357. };
  358. #endif
  359. if (argc < 2)
  360. return ERROR;
  361. /* reverse compatibility for very old non-POSIX usage forms */
  362. for (c = 1; c < argc; c++) {
  363. if (strcmp ("-to", argv[c]) == 0)
  364. strcpy (argv[c], "-t");
  365. if (strcmp ("-wv", argv[c]) == 0)
  366. strcpy (argv[c], "-w");
  367. if (strcmp ("-cv", argv[c]) == 0)
  368. strcpy (argv[c], "-c");
  369. }
  370. while (1) {
  371. #ifdef HAVE_GETOPT_H
  372. c =
  373. getopt_long (argc, argv, "hvVt:c:w:H:C:o:e:E:d:D:s:R:r:l:u:p:",
  374. long_options, &option_index);
  375. #else
  376. c = getopt (argc, argv, "hvVt:c:w:H:C:o:e:E:d:D:s:R:r:l:u:p:");
  377. #endif
  378. if (c == -1 || c == EOF)
  379. break;
  380. switch (c) {
  381. case '?': /* usage */
  382. usage3 ("Unknown argument", optopt);
  383. case 'h': /* help */
  384. print_help ();
  385. exit (STATE_OK);
  386. case 'V': /* version */
  387. print_revision (PROGNAME, REVISION);
  388. exit (STATE_OK);
  389. case 'v': /* verbose */
  390. verbose = TRUE;
  391. break;
  392. case 't': /* timeout period */
  393. if (!is_integer (optarg))
  394. usage2 ("Timeout Interval must be an integer", optarg);
  395. timeout_interval = atoi (optarg);
  396. break;
  397. case 'e': /* PRELIMINARY - may change */
  398. eval_method[j] |= WARN_PRESENT;
  399. for (ptr = optarg; (ptr = index (ptr, ',')); ptr++)
  400. ptr[0] = ' '; /* relpace comma with space */
  401. for (ptr = optarg; (ptr = index (ptr, ' ')); ptr++)
  402. eval_method[++j] |= WARN_PRESENT;
  403. asprintf (&oid, "%s %s", (oid?oid:""), optarg);
  404. break;
  405. case 'E': /* PRELIMINARY - may change */
  406. eval_method[j] |= WARN_PRESENT;
  407. for (ptr = optarg; (ptr = index (ptr, ',')); ptr++)
  408. ptr[0] = ' '; /* relpace comma with space */
  409. for (ptr = optarg; (ptr = index (ptr, ' ')); ptr++)
  410. eval_method[++j] |= CRIT_PRESENT;
  411. asprintf (&oid, "%s %s", (oid?oid:""), optarg);
  412. break;
  413. case 'c': /* critical time threshold */
  414. if (strspn (optarg, "0123456789:,") < strlen (optarg)) {
  415. printf ("Invalid critical threshold: %s\n", optarg);
  416. print_usage ();
  417. exit (STATE_UNKNOWN);
  418. }
  419. for (ptr = optarg, jj = 0; ptr && jj < MAX_OIDS; jj++) {
  420. if (lu_getll (&lower_crit_lim[jj], ptr) == 1)
  421. eval_method[jj] |= CRIT_LT;
  422. if (lu_getul (&upper_crit_lim[jj], ptr) == 1)
  423. eval_method[jj] |= CRIT_GT;
  424. (ptr = index (ptr, ',')) ? ptr++ : ptr;
  425. }
  426. break;
  427. case 'w': /* warning time threshold */
  428. if (strspn (optarg, "0123456789:,") < strlen (optarg)) {
  429. printf ("Invalid warning threshold: %s\n", optarg);
  430. print_usage ();
  431. exit (STATE_UNKNOWN);
  432. }
  433. for (ptr = optarg, jj = 0; ptr && jj < MAX_OIDS; jj++) {
  434. if (lu_getll (&lower_warn_lim[jj], ptr) == 1)
  435. eval_method[jj] |= WARN_LT;
  436. if (lu_getul (&upper_warn_lim[jj], ptr) == 1)
  437. eval_method[jj] |= WARN_GT;
  438. (ptr = index (ptr, ',')) ? ptr++ : ptr;
  439. }
  440. break;
  441. case 'H': /* Host or server */
  442. server_address = strscpy (server_address, optarg);
  443. break;
  444. case 'C': /* group or community */
  445. community = strscpy (community, optarg);
  446. break;
  447. case 'o': /* object identifier */
  448. for (ptr = optarg; (ptr = index (ptr, ',')); ptr++)
  449. ptr[0] = ' '; /* relpace comma with space */
  450. for (ptr = optarg; (ptr = index (ptr, ' ')); ptr++)
  451. j++; /* count OIDs */
  452. asprintf (&oid, "%s %s", (oid?oid:""), optarg);
  453. break;
  454. case 'd': /* delimiter */
  455. delimiter = strscpy (delimiter, optarg);
  456. break;
  457. case 'D': /* output-delimiter */
  458. output_delim = strscpy (output_delim, optarg);
  459. break;
  460. case 's': /* string or substring */
  461. strncpy (string_value, optarg, sizeof (string_value) - 1);
  462. string_value[sizeof (string_value) - 1] = 0;
  463. eval_method[jj++] = CRIT_STRING;
  464. break;
  465. case 'R': /* regex */
  466. #ifdef HAVE_REGEX_H
  467. cflags = REG_ICASE;
  468. #endif
  469. case 'r': /* regex */
  470. #ifdef HAVE_REGEX_H
  471. cflags |= REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
  472. strncpy (regex_expect, optarg, sizeof (regex_expect) - 1);
  473. regex_expect[sizeof (regex_expect) - 1] = 0;
  474. errcode = regcomp (&preg, regex_expect, cflags);
  475. if (errcode != 0) {
  476. regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER);
  477. printf ("Could Not Compile Regular Expression");
  478. return ERROR;
  479. }
  480. eval_method[jj++] = CRIT_REGEX;
  481. #else
  482. printf ("SNMP UNKNOWN: call for regex which was not a compiled option");
  483. exit (STATE_UNKNOWN);
  484. #endif
  485. break;
  486. case 'l': /* label */
  487. label = optarg;
  488. nlabels++;
  489. if (nlabels >= labels_size) {
  490. labels_size += 8;
  491. labels = realloc (labels, labels_size);
  492. if (labels == NULL)
  493. terminate (STATE_UNKNOWN,
  494. "Could not realloc() labels[%d]", nlabels);
  495. }
  496. labels[nlabels - 1] = optarg;
  497. ptr = thisarg (optarg);
  498. if (strstr (ptr, "'") == ptr)
  499. labels[nlabels - 1] = ptr + 1;
  500. else
  501. labels[nlabels - 1] = ptr;
  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. terminate (STATE_UNKNOWN, "Could not realloc() 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. terminate (STATE_UNKNOWN,
  525. "Could not realloc() units [%d]\n", nunits);
  526. }
  527. unitv[nunits - 1] = optarg;
  528. ptr = thisarg (optarg);
  529. if (strstr (ptr, "'") == ptr)
  530. unitv[nunits - 1] = ptr + 1;
  531. else
  532. unitv[nunits - 1] = ptr;
  533. while (ptr && (ptr = nextarg (ptr))) {
  534. if (nunits >= unitv_size) {
  535. unitv_size += 8;
  536. unitv = realloc (unitv, unitv_size);
  537. if (units == NULL)
  538. terminate (STATE_UNKNOWN, "Could not realloc() units\n");
  539. }
  540. nunits++;
  541. ptr = thisarg (ptr);
  542. if (strstr (ptr, "'") == ptr)
  543. unitv[nunits - 1] = ptr + 1;
  544. else
  545. unitv[nunits - 1] = ptr;
  546. }
  547. break;
  548. case 'p': /* TCP port number */
  549. port = strscpy(port, optarg);
  550. break;
  551. }
  552. }
  553. if (server_address == NULL)
  554. asprintf (&server_address, argv[optind]);
  555. return validate_arguments ();
  556. }
  557. /******************************************************************************
  558. @@-
  559. <sect3>
  560. <title>validate_arguments</title>
  561. <para>&PROTO_validate_arguments;</para>
  562. <para>Given a database name, this function returns TRUE if the string
  563. is a valid PostgreSQL database name, and returns false if it is
  564. not.</para>
  565. <para>Valid PostgreSQL database names are less than &NAMEDATALEN;
  566. characters long and consist of letters, numbers, and underscores. The
  567. first character cannot be a number, however.</para>
  568. </sect3>
  569. -@@
  570. ******************************************************************************/
  571. int
  572. validate_arguments ()
  573. {
  574. if (community == NULL)
  575. asprintf (&community, DEFAULT_COMMUNITY);
  576. if (delimiter == NULL)
  577. asprintf (&delimiter, DEFAULT_DELIMITER);
  578. if (output_delim == NULL)
  579. asprintf (&output_delim, DEFAULT_OUTPUT_DELIMITER);
  580. if (label == NULL)
  581. asprintf (&label, "SNMP");
  582. if (units == NULL)
  583. asprintf (&units, "");
  584. return OK;
  585. }
  586. void
  587. print_help (void)
  588. {
  589. print_revision (PROGNAME, REVISION);
  590. printf
  591. ("Copyright (c) %s %s <%s>\n\n%s\n",
  592. COPYRIGHT, AUTHOR, EMAIL, SUMMARY);
  593. print_usage ();
  594. printf
  595. ("\nOptions:\n" LONGOPTIONS "\n" DESCRIPTION "\n" NOTES "\n",
  596. DEFAULT_COMMUNITY, DEFAULT_PORT, DEFAULT_DELIMITER, DEFAULT_TIMEOUT);
  597. support ();
  598. }
  599. void
  600. print_usage (void)
  601. {
  602. printf
  603. ("Usage:\n" " %s %s\n"
  604. " %s (-h | --help) for detailed help\n"
  605. " %s (-V | --version) for version information\n",
  606. PROGNAME, OPTIONS, PROGNAME, PROGNAME);
  607. }
  608. char *
  609. clarify_message (char *msg)
  610. {
  611. int i = 0;
  612. int foo;
  613. char tmpmsg_c[MAX_INPUT_BUFFER];
  614. char *tmpmsg = (char *) &tmpmsg_c;
  615. tmpmsg = strcpy (tmpmsg, msg);
  616. if (!strncmp (tmpmsg, " Hex:", 5)) {
  617. tmpmsg = strtok (tmpmsg, ":");
  618. while ((tmpmsg = strtok (NULL, " "))) {
  619. foo = strtol (tmpmsg, NULL, 16);
  620. /* Translate chars that are not the same value in the printers
  621. * character set.
  622. */
  623. switch (foo) {
  624. case 208:
  625. {
  626. foo = 197;
  627. break;
  628. }
  629. case 216:
  630. {
  631. foo = 196;
  632. break;
  633. }
  634. }
  635. msg[i] = foo;
  636. i++;
  637. }
  638. msg[i] = 0;
  639. }
  640. return (msg);
  641. }
  642. int
  643. check_num (int i)
  644. {
  645. int result;
  646. result = STATE_OK;
  647. if (eval_method[i] & WARN_GT && eval_method[i] & WARN_LT &&
  648. lower_warn_lim[i] > upper_warn_lim[i]) {
  649. if (response_value[i] <= lower_warn_lim[i] &&
  650. response_value[i] >= upper_warn_lim[i]) {
  651. result = STATE_WARNING;
  652. }
  653. }
  654. else if
  655. ((eval_method[i] & WARN_GT && response_value[i] > upper_warn_lim[i]) ||
  656. (eval_method[i] & WARN_GE && response_value[i] >= upper_warn_lim[i]) ||
  657. (eval_method[i] & WARN_LT && response_value[i] < lower_warn_lim[i]) ||
  658. (eval_method[i] & WARN_LE && response_value[i] <= lower_warn_lim[i]) ||
  659. (eval_method[i] & WARN_EQ && response_value[i] == upper_warn_lim[i]) ||
  660. (eval_method[i] & WARN_NE && response_value[i] != upper_warn_lim[i])) {
  661. result = STATE_WARNING;
  662. }
  663. if (eval_method[i] & CRIT_GT && eval_method[i] & CRIT_LT &&
  664. lower_warn_lim[i] > upper_warn_lim[i]) {
  665. if (response_value[i] <= lower_crit_lim[i] &&
  666. response_value[i] >= upper_crit_lim[i]) {
  667. result = STATE_CRITICAL;
  668. }
  669. }
  670. else if
  671. ((eval_method[i] & CRIT_GT && response_value[i] > upper_crit_lim[i]) ||
  672. (eval_method[i] & CRIT_GE && response_value[i] >= upper_crit_lim[i]) ||
  673. (eval_method[i] & CRIT_LT && response_value[i] < lower_crit_lim[i]) ||
  674. (eval_method[i] & CRIT_LE && response_value[i] <= lower_crit_lim[i]) ||
  675. (eval_method[i] & CRIT_EQ && response_value[i] == upper_crit_lim[i]) ||
  676. (eval_method[i] & CRIT_NE && response_value[i] != upper_crit_lim[i])) {
  677. result = STATE_CRITICAL;
  678. }
  679. return result;
  680. }
  681. int
  682. lu_getll (unsigned long *ll, char *str)
  683. {
  684. char tmp[100];
  685. if (strchr (str, ':') == NULL)
  686. return 0;
  687. if (strchr (str, ',') != NULL && (strchr (str, ',') < strchr (str, ':')))
  688. return 0;
  689. if (sscanf (str, "%lu%[:]", ll, tmp) == 2)
  690. return 1;
  691. return 0;
  692. }
  693. int
  694. lu_getul (unsigned long *ul, char *str)
  695. {
  696. char tmp[100];
  697. if (sscanf (str, "%lu%[^,]", ul, tmp) == 1)
  698. return 1;
  699. if (sscanf (str, ":%lu%[^,]", ul, tmp) == 1)
  700. return 1;
  701. if (sscanf (str, "%*u:%lu%[^,]", ul, tmp) == 1)
  702. return 1;
  703. return 0;
  704. }
  705. /* trim leading whitespace
  706. if there is a leading quote, make sure it balances */
  707. char *
  708. thisarg (char *str)
  709. {
  710. str += strspn (str, " \t\r\n"); /* trim any leading whitespace */
  711. if (strstr (str, "'") == str) { /* handle SIMPLE quoted strings */
  712. if (strlen (str) == 1 || !strstr (str + 1, "'"))
  713. terminate (STATE_UNKNOWN, "Unbalanced quotes\n");
  714. }
  715. return str;
  716. }
  717. /* if there's a leading quote, advance to the trailing quote
  718. set the trailing quote to '\x0'
  719. if the string continues, advance beyond the comma */
  720. char *
  721. nextarg (char *str)
  722. {
  723. if (strstr (str, "'") == str) {
  724. if (strlen (str) > 1) {
  725. str = strstr (str + 1, "'");
  726. str[0] = 0;
  727. return (++str);
  728. }
  729. else {
  730. str[0] = 0;
  731. return NULL;
  732. }
  733. }
  734. if (strstr (str, ",") == str) {
  735. if (strlen (str) > 1) {
  736. str[0] = 0;
  737. return (++str);
  738. }
  739. else {
  740. str[0] = 0;
  741. return NULL;
  742. }
  743. }
  744. if ((str = strstr (str, ",")) && strlen (str) > 1) {
  745. str[0] = 0;
  746. return (++str);
  747. }
  748. return NULL;
  749. }