check_snmp.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  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 = NULL;
  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. /* initialize some args */
  368. asprintf (&oid, "");
  369. while (1) {
  370. #ifdef HAVE_GETOPT_H
  371. c =
  372. getopt_long (argc, argv, "+?hVt:c:w:H:C:o:e:E:d:D:s:R:r:l:u:p:",
  373. long_options, &option_index);
  374. #else
  375. c = getopt (argc, argv, "+?hVt:c:w:H:C:o:e:E:d:D:s:R:r:l:u:p:");
  376. #endif
  377. if (c == -1 || c == EOF)
  378. break;
  379. switch (c) {
  380. case '?': /* usage */
  381. usage2 ("Unknown argument", optarg);
  382. case 'h': /* help */
  383. print_help ();
  384. exit (STATE_OK);
  385. case 'V': /* version */
  386. print_revision (PROGNAME, REVISION);
  387. exit (STATE_OK);
  388. case 'v': /* verbose */
  389. verbose = TRUE;
  390. break;
  391. case 't': /* timeout period */
  392. if (!is_integer (optarg))
  393. usage2 ("Timeout Interval must be an integer", optarg);
  394. timeout_interval = atoi (optarg);
  395. break;
  396. case 'e': /* PRELIMINARY - may change */
  397. eval_method[j] |= WARN_PRESENT;
  398. for (ptr = optarg; (ptr = index (ptr, ',')); ptr++)
  399. ptr[0] = ' '; /* relpace comma with space */
  400. for (ptr = optarg; (ptr = index (ptr, ' ')); ptr++)
  401. eval_method[++j] |= WARN_PRESENT;
  402. asprintf (&oid, "%s %s", oid, optarg);
  403. break;
  404. case 'E': /* PRELIMINARY - may change */
  405. eval_method[j] |= WARN_PRESENT;
  406. for (ptr = optarg; (ptr = index (ptr, ',')); ptr++)
  407. ptr[0] = ' '; /* relpace comma with space */
  408. for (ptr = optarg; (ptr = index (ptr, ' ')); ptr++)
  409. eval_method[++j] |= CRIT_PRESENT;
  410. asprintf (&oid, "%s %s", oid, optarg);
  411. break;
  412. case 'c': /* critical time threshold */
  413. if (strspn (optarg, "0123456789:,") < strlen (optarg)) {
  414. printf ("Invalid critical threshold: %s\n", optarg);
  415. print_usage ();
  416. exit (STATE_UNKNOWN);
  417. }
  418. for (ptr = optarg, jj = 0; ptr && jj < MAX_OIDS; jj++) {
  419. if (lu_getll (&lower_crit_lim[jj], ptr) == 1)
  420. eval_method[jj] |= CRIT_LT;
  421. if (lu_getul (&upper_crit_lim[jj], ptr) == 1)
  422. eval_method[jj] |= CRIT_GT;
  423. (ptr = index (ptr, ',')) ? ptr++ : ptr;
  424. }
  425. break;
  426. case 'w': /* warning time threshold */
  427. if (strspn (optarg, "0123456789:,") < strlen (optarg)) {
  428. printf ("Invalid warning threshold: %s\n", optarg);
  429. print_usage ();
  430. exit (STATE_UNKNOWN);
  431. }
  432. for (ptr = optarg, jj = 0; ptr && jj < MAX_OIDS; jj++) {
  433. if (lu_getll (&lower_warn_lim[jj], ptr) == 1)
  434. eval_method[jj] |= WARN_LT;
  435. if (lu_getul (&upper_warn_lim[jj], ptr) == 1)
  436. eval_method[jj] |= WARN_GT;
  437. (ptr = index (ptr, ',')) ? ptr++ : ptr;
  438. }
  439. break;
  440. case 'H': /* Host or server */
  441. server_address = strscpy (server_address, optarg);
  442. break;
  443. case 'C': /* group or community */
  444. community = strscpy (community, optarg);
  445. break;
  446. case 'o': /* object identifier */
  447. for (ptr = optarg; (ptr = index (ptr, ',')); ptr++)
  448. ptr[0] = ' '; /* relpace comma with space */
  449. for (ptr = optarg; (ptr = index (ptr, ' ')); ptr++) {
  450. j++; /* count OIDs */
  451. }
  452. asprintf (&oid, "%s %s", 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. if (port == NULL)
  585. asprintf (&port, DEFAULT_PORT);
  586. return OK;
  587. }
  588. void
  589. print_help (void)
  590. {
  591. print_revision (PROGNAME, REVISION);
  592. printf
  593. ("Copyright (c) %s %s <%s>\n\n%s\n",
  594. COPYRIGHT, AUTHOR, EMAIL, SUMMARY);
  595. print_usage ();
  596. printf
  597. ("\nOptions:\n" LONGOPTIONS "\n" DESCRIPTION "\n" NOTES "\n",
  598. DEFAULT_COMMUNITY, DEFAULT_PORT, DEFAULT_DELIMITER, DEFAULT_TIMEOUT);
  599. support ();
  600. }
  601. void
  602. print_usage (void)
  603. {
  604. printf
  605. ("Usage:\n" " %s %s\n"
  606. " %s (-h | --help) for detailed help\n"
  607. " %s (-V | --version) for version information\n",
  608. PROGNAME, OPTIONS, PROGNAME, PROGNAME);
  609. }
  610. char *
  611. clarify_message (char *msg)
  612. {
  613. int i = 0;
  614. int foo;
  615. char tmpmsg_c[MAX_INPUT_BUFFER];
  616. char *tmpmsg = (char *) &tmpmsg_c;
  617. tmpmsg = strcpy (tmpmsg, msg);
  618. if (!strncmp (tmpmsg, " Hex:", 5)) {
  619. tmpmsg = strtok (tmpmsg, ":");
  620. while ((tmpmsg = strtok (NULL, " "))) {
  621. foo = strtol (tmpmsg, NULL, 16);
  622. /* Translate chars that are not the same value in the printers
  623. * character set.
  624. */
  625. switch (foo) {
  626. case 208:
  627. {
  628. foo = 197;
  629. break;
  630. }
  631. case 216:
  632. {
  633. foo = 196;
  634. break;
  635. }
  636. }
  637. msg[i] = foo;
  638. i++;
  639. }
  640. msg[i] = 0;
  641. }
  642. return (msg);
  643. }
  644. int
  645. check_num (int i)
  646. {
  647. int result;
  648. result = STATE_OK;
  649. if (eval_method[i] & WARN_GT && eval_method[i] & WARN_LT &&
  650. lower_warn_lim[i] > upper_warn_lim[i]) {
  651. if (response_value[i] <= lower_warn_lim[i] &&
  652. response_value[i] >= upper_warn_lim[i]) {
  653. result = STATE_WARNING;
  654. }
  655. }
  656. else if
  657. ((eval_method[i] & WARN_GT && response_value[i] > upper_warn_lim[i]) ||
  658. (eval_method[i] & WARN_GE && response_value[i] >= upper_warn_lim[i]) ||
  659. (eval_method[i] & WARN_LT && response_value[i] < lower_warn_lim[i]) ||
  660. (eval_method[i] & WARN_LE && response_value[i] <= lower_warn_lim[i]) ||
  661. (eval_method[i] & WARN_EQ && response_value[i] == upper_warn_lim[i]) ||
  662. (eval_method[i] & WARN_NE && response_value[i] != upper_warn_lim[i])) {
  663. result = STATE_WARNING;
  664. }
  665. if (eval_method[i] & CRIT_GT && eval_method[i] & CRIT_LT &&
  666. lower_warn_lim[i] > upper_warn_lim[i]) {
  667. if (response_value[i] <= lower_crit_lim[i] &&
  668. response_value[i] >= upper_crit_lim[i]) {
  669. result = STATE_CRITICAL;
  670. }
  671. }
  672. else if
  673. ((eval_method[i] & CRIT_GT && response_value[i] > upper_crit_lim[i]) ||
  674. (eval_method[i] & CRIT_GE && response_value[i] >= upper_crit_lim[i]) ||
  675. (eval_method[i] & CRIT_LT && response_value[i] < lower_crit_lim[i]) ||
  676. (eval_method[i] & CRIT_LE && response_value[i] <= lower_crit_lim[i]) ||
  677. (eval_method[i] & CRIT_EQ && response_value[i] == upper_crit_lim[i]) ||
  678. (eval_method[i] & CRIT_NE && response_value[i] != upper_crit_lim[i])) {
  679. result = STATE_CRITICAL;
  680. }
  681. return result;
  682. }
  683. int
  684. lu_getll (unsigned long *ll, char *str)
  685. {
  686. char tmp[100];
  687. if (strchr (str, ':') == NULL)
  688. return 0;
  689. if (strchr (str, ',') != NULL && (strchr (str, ',') < strchr (str, ':')))
  690. return 0;
  691. if (sscanf (str, "%lu%[:]", ll, tmp) == 2)
  692. return 1;
  693. return 0;
  694. }
  695. int
  696. lu_getul (unsigned long *ul, char *str)
  697. {
  698. char tmp[100];
  699. if (sscanf (str, "%lu%[^,]", ul, tmp) == 1)
  700. return 1;
  701. if (sscanf (str, ":%lu%[^,]", ul, tmp) == 1)
  702. return 1;
  703. if (sscanf (str, "%*u:%lu%[^,]", ul, tmp) == 1)
  704. return 1;
  705. return 0;
  706. }
  707. /* trim leading whitespace
  708. if there is a leading quote, make sure it balances */
  709. char *
  710. thisarg (char *str)
  711. {
  712. str += strspn (str, " \t\r\n"); /* trim any leading whitespace */
  713. if (strstr (str, "'") == str) { /* handle SIMPLE quoted strings */
  714. if (strlen (str) == 1 || !strstr (str + 1, "'"))
  715. terminate (STATE_UNKNOWN, "Unbalanced quotes\n");
  716. }
  717. return str;
  718. }
  719. /* if there's a leading quote, advance to the trailing quote
  720. set the trailing quote to '\x0'
  721. if the string continues, advance beyond the comma */
  722. char *
  723. nextarg (char *str)
  724. {
  725. if (strstr (str, "'") == str) {
  726. if (strlen (str) > 1) {
  727. str = strstr (str + 1, "'");
  728. str[0] = 0;
  729. return (++str);
  730. }
  731. else {
  732. str[0] = 0;
  733. return NULL;
  734. }
  735. }
  736. if (strstr (str, ",") == str) {
  737. if (strlen (str) > 1) {
  738. str[0] = 0;
  739. return (++str);
  740. }
  741. else {
  742. str[0] = 0;
  743. return NULL;
  744. }
  745. }
  746. if ((str = strstr (str, ",")) && strlen (str) > 1) {
  747. str[0] = 0;
  748. return (++str);
  749. }
  750. return NULL;
  751. }