check_snmp.c 23 KB

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