check_snmp.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  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 = NULL;
  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 = NULL;
  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. outbuff = strscpy (outbuff, "");
  185. for (i = 0; i < MAX_OIDS; i++)
  186. eval_method[i] = CHECK_UNDEF;
  187. i = 0;
  188. if (process_arguments (argc, argv) == ERROR)
  189. usage ("Incorrect arguments supplied\n");
  190. /* create the command line to execute */
  191. asprintf (&command_line, "%s -p %s -m ALL -v 1 %s -c %s %s",
  192. PATH_TO_SNMPGET, port, server_address, community, oid);
  193. if (verbose)
  194. printf ("%s\n", command_line);
  195. /* run the command */
  196. child_process = spopen (command_line);
  197. if (child_process == NULL) {
  198. printf ("Could not open pipe: %s\n", command_line);
  199. exit (STATE_UNKNOWN);
  200. }
  201. child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  202. if (child_stderr == NULL) {
  203. printf ("Could not open stderr for %s\n", command_line);
  204. }
  205. asprintf (&output, "");
  206. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process))
  207. asprintf (&output, "%s%s", output, input_buffer);
  208. if (verbose)
  209. printf ("%s\n", output);
  210. ptr = output;
  211. while (ptr) {
  212. ptr = strstr (ptr, delimiter);
  213. if (ptr == NULL)
  214. break;
  215. ptr += strlen (delimiter);
  216. ptr += strspn (ptr, " ");
  217. found++;
  218. if (ptr[0] == '"') {
  219. ptr++;
  220. response = strpcpy (response, ptr, "\"");
  221. ptr = strpbrk (ptr, "\"");
  222. ptr += strspn (ptr, "\"\n");
  223. }
  224. else {
  225. response = strpcpy (response, ptr, "\n");
  226. ptr = strpbrk (ptr, "\n");
  227. ptr += strspn (ptr, "\n");
  228. while
  229. (strstr (ptr, delimiter) &&
  230. strstr (ptr, "\n") && strstr (ptr, "\n") < strstr (ptr, delimiter)) {
  231. response = strpcat (response, ptr, "\n");
  232. ptr = strpbrk (ptr, "\n");
  233. }
  234. if (ptr && strstr (ptr, delimiter) == NULL) {
  235. response = strscat (response, ptr);
  236. ptr = NULL;
  237. }
  238. }
  239. if (strstr (response, "Gauge: "))
  240. show = strstr (response, "Gauge: ") + 7;
  241. else if (strstr (response, "Gauge32: "))
  242. show = strstr (response, "Gauge32: ") + 9;
  243. else
  244. show = response;
  245. p2 = show;
  246. iresult = STATE_DEPENDENT;
  247. if (eval_method[i] & CRIT_PRESENT) {
  248. iresult = STATE_CRITICAL;
  249. } else if (eval_method[i] & WARN_PRESENT) {
  250. iresult = STATE_WARNING;
  251. }
  252. if (eval_method[i] & CRIT_GT ||
  253. eval_method[i] & CRIT_LT ||
  254. eval_method[i] & CRIT_GE ||
  255. eval_method[i] & CRIT_LE ||
  256. eval_method[i] & CRIT_EQ ||
  257. eval_method[i] & CRIT_NE ||
  258. eval_method[i] & WARN_GT ||
  259. eval_method[i] & WARN_LT ||
  260. eval_method[i] & WARN_GE ||
  261. eval_method[i] & WARN_LE ||
  262. eval_method[i] & WARN_EQ || eval_method[i] & WARN_NE) {
  263. p2 = strpbrk (p2, "0123456789");
  264. response_value[i] = strtoul (p2, NULL, 10);
  265. iresult = check_num (i);
  266. asprintf (&show, "%lu", response_value[i]);
  267. /*asprintf (&show, "%s", response); */
  268. }
  269. else if (eval_method[i] & CRIT_STRING) {
  270. if (strcmp (response, string_value))
  271. iresult = STATE_CRITICAL;
  272. else
  273. iresult = STATE_OK;
  274. }
  275. else if (eval_method[i] & CRIT_REGEX) {
  276. #ifdef HAVE_REGEX_H
  277. excode = regexec (&preg, response, 10, pmatch, eflags);
  278. if (excode == 0) {
  279. iresult = STATE_OK;
  280. }
  281. else if (excode != REG_NOMATCH) {
  282. regerror (excode, &preg, errbuf, MAX_INPUT_BUFFER);
  283. printf ("Execute Error: %s\n", errbuf);
  284. exit (STATE_CRITICAL);
  285. }
  286. else {
  287. iresult = STATE_CRITICAL;
  288. }
  289. #else
  290. printf ("SNMP UNKNOWN: call for regex which was not a compiled option");
  291. exit (STATE_UNKNOWN);
  292. #endif
  293. }
  294. if (response && iresult == STATE_DEPENDENT)
  295. iresult = STATE_OK;
  296. else if (eval_method[i] & CRIT_PRESENT)
  297. iresult = STATE_CRITICAL;
  298. else
  299. iresult = STATE_WARNING;
  300. result = max_state (result, iresult);
  301. if (nlabels > 1 && i < nlabels && labels[i] != NULL)
  302. asprintf (&outbuff, "%s%s%s %s%s%s", outbuff,
  303. (i == 0) ? " " : output_delim,
  304. labels[i], mark (iresult), show, mark (iresult));
  305. else
  306. asprintf (&outbuff, "%s%s%s%s%s", outbuff, (i == 0) ? " " : output_delim,
  307. mark (iresult), show, mark (iresult));
  308. if (nunits > 0 && i < nunits)
  309. asprintf (&outbuff, "%s %s", outbuff, unitv[i]);
  310. i++;
  311. } /* end while */
  312. if (found == 0)
  313. terminate
  314. (STATE_UNKNOWN,
  315. "%s problem - No data recieved from host\nCMD: %s\n",
  316. label, command_line);
  317. /* WARNING if output found on stderr */
  318. if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
  319. result = max_state (result, STATE_WARNING);
  320. /* close stderr */
  321. (void) fclose (child_stderr);
  322. /* close the pipe */
  323. if (spclose (child_process))
  324. result = max_state (result, STATE_WARNING);
  325. if (nunits > 0)
  326. printf ("%s %s -%s\n", label, state_text (result), outbuff);
  327. else
  328. printf ("%s %s -%s %s\n", label, state_text (result), outbuff, units);
  329. return result;
  330. }
  331. /* process command-line arguments */
  332. int
  333. process_arguments (int argc, char **argv)
  334. {
  335. char *ptr;
  336. int c = 1;
  337. int j = 0, jj = 0;
  338. #ifdef HAVE_GETOPT_H
  339. int option_index = 0;
  340. static struct option long_options[] = {
  341. STD_LONG_OPTS,
  342. {"community", required_argument, 0, 'C'},
  343. {"oid", required_argument, 0, 'o'},
  344. {"object", required_argument, 0, 'o'},
  345. {"delimiter", required_argument, 0, 'd'},
  346. {"output-delimiter", required_argument, 0, 'D'},
  347. {"string", required_argument, 0, 's'},
  348. {"regex", required_argument, 0, 'r'},
  349. {"ereg", required_argument, 0, 'r'},
  350. {"eregi", required_argument, 0, 'R'},
  351. {"label", required_argument, 0, 'l'},
  352. {"units", required_argument, 0, 'u'},
  353. {"port", required_argument, 0, 'p'},
  354. {0, 0, 0, 0}
  355. };
  356. #endif
  357. if (argc < 2)
  358. return ERROR;
  359. /* reverse compatibility for very old non-POSIX usage forms */
  360. for (c = 1; c < argc; c++) {
  361. if (strcmp ("-to", argv[c]) == 0)
  362. strcpy (argv[c], "-t");
  363. if (strcmp ("-wv", argv[c]) == 0)
  364. strcpy (argv[c], "-w");
  365. if (strcmp ("-cv", argv[c]) == 0)
  366. strcpy (argv[c], "-c");
  367. }
  368. /* initialize some args */
  369. asprintf (&oid, "");
  370. while (1) {
  371. #ifdef HAVE_GETOPT_H
  372. c =
  373. getopt_long (argc, argv, "+?hVt: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, "+?hVt: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. usage2 ("Unknown argument", optarg);
  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, 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, 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. }
  453. asprintf (&oid, "%s %s", oid, optarg);
  454. break;
  455. case 'd': /* delimiter */
  456. delimiter = strscpy (delimiter, optarg);
  457. break;
  458. case 'D': /* output-delimiter */
  459. output_delim = strscpy (output_delim, optarg);
  460. break;
  461. case 's': /* string or substring */
  462. strncpy (string_value, optarg, sizeof (string_value) - 1);
  463. string_value[sizeof (string_value) - 1] = 0;
  464. eval_method[jj++] = CRIT_STRING;
  465. break;
  466. case 'R': /* regex */
  467. #ifdef HAVE_REGEX_H
  468. cflags = REG_ICASE;
  469. #endif
  470. case 'r': /* regex */
  471. #ifdef HAVE_REGEX_H
  472. cflags |= REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
  473. strncpy (regex_expect, optarg, sizeof (regex_expect) - 1);
  474. regex_expect[sizeof (regex_expect) - 1] = 0;
  475. errcode = regcomp (&preg, regex_expect, cflags);
  476. if (errcode != 0) {
  477. regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER);
  478. printf ("Could Not Compile Regular Expression");
  479. return ERROR;
  480. }
  481. eval_method[jj++] = CRIT_REGEX;
  482. #else
  483. printf ("SNMP UNKNOWN: call for regex which was not a compiled option");
  484. exit (STATE_UNKNOWN);
  485. #endif
  486. break;
  487. case 'l': /* label */
  488. label = optarg;
  489. nlabels++;
  490. if (nlabels >= labels_size) {
  491. labels_size += 8;
  492. labels = realloc (labels, labels_size);
  493. if (labels == NULL)
  494. terminate (STATE_UNKNOWN,
  495. "Could not realloc() labels[%d]", nlabels);
  496. }
  497. labels[nlabels - 1] = optarg;
  498. ptr = thisarg (optarg);
  499. if (strstr (ptr, "'") == ptr)
  500. labels[nlabels - 1] = ptr + 1;
  501. else
  502. labels[nlabels - 1] = ptr;
  503. while (ptr && (ptr = nextarg (ptr))) {
  504. if (nlabels >= labels_size) {
  505. labels_size += 8;
  506. labels = realloc (labels, labels_size);
  507. if (labels == NULL)
  508. terminate (STATE_UNKNOWN, "Could not realloc() labels\n");
  509. }
  510. labels++;
  511. ptr = thisarg (ptr);
  512. if (strstr (ptr, "'") == ptr)
  513. labels[nlabels - 1] = ptr + 1;
  514. else
  515. labels[nlabels - 1] = ptr;
  516. }
  517. break;
  518. case 'u': /* units */
  519. units = optarg;
  520. nunits++;
  521. if (nunits >= unitv_size) {
  522. unitv_size += 8;
  523. unitv = realloc (unitv, unitv_size);
  524. if (unitv == NULL)
  525. terminate (STATE_UNKNOWN,
  526. "Could not realloc() units [%d]\n", nunits);
  527. }
  528. unitv[nunits - 1] = optarg;
  529. ptr = thisarg (optarg);
  530. if (strstr (ptr, "'") == ptr)
  531. unitv[nunits - 1] = ptr + 1;
  532. else
  533. unitv[nunits - 1] = ptr;
  534. while (ptr && (ptr = nextarg (ptr))) {
  535. if (nunits >= unitv_size) {
  536. unitv_size += 8;
  537. unitv = realloc (unitv, unitv_size);
  538. if (units == NULL)
  539. terminate (STATE_UNKNOWN, "Could not realloc() units\n");
  540. }
  541. nunits++;
  542. ptr = thisarg (ptr);
  543. if (strstr (ptr, "'") == ptr)
  544. unitv[nunits - 1] = ptr + 1;
  545. else
  546. unitv[nunits - 1] = ptr;
  547. }
  548. break;
  549. case 'p': /* TCP port number */
  550. port = strscpy(port, optarg);
  551. break;
  552. }
  553. }
  554. if (server_address == NULL)
  555. asprintf (&server_address, argv[optind]);
  556. return validate_arguments ();
  557. }
  558. /******************************************************************************
  559. @@-
  560. <sect3>
  561. <title>validate_arguments</title>
  562. <para>&PROTO_validate_arguments;</para>
  563. <para>Given a database name, this function returns TRUE if the string
  564. is a valid PostgreSQL database name, and returns false if it is
  565. not.</para>
  566. <para>Valid PostgreSQL database names are less than &NAMEDATALEN;
  567. characters long and consist of letters, numbers, and underscores. The
  568. first character cannot be a number, however.</para>
  569. </sect3>
  570. -@@
  571. ******************************************************************************/
  572. int
  573. validate_arguments ()
  574. {
  575. if (community == NULL)
  576. asprintf (&community, DEFAULT_COMMUNITY);
  577. if (delimiter == NULL)
  578. asprintf (&delimiter, DEFAULT_DELIMITER);
  579. if (output_delim == NULL)
  580. asprintf (&output_delim, DEFAULT_OUTPUT_DELIMITER);
  581. if (label == NULL)
  582. asprintf (&label, "SNMP");
  583. if (units == NULL)
  584. asprintf (&units, "");
  585. if (port == NULL)
  586. asprintf (&port, DEFAULT_PORT);
  587. return OK;
  588. }
  589. void
  590. print_help (void)
  591. {
  592. print_revision (PROGNAME, REVISION);
  593. printf
  594. ("Copyright (c) %s %s <%s>\n\n%s\n",
  595. COPYRIGHT, AUTHOR, EMAIL, SUMMARY);
  596. print_usage ();
  597. printf
  598. ("\nOptions:\n" LONGOPTIONS "\n" DESCRIPTION "\n" NOTES "\n",
  599. DEFAULT_COMMUNITY, DEFAULT_PORT, DEFAULT_DELIMITER, DEFAULT_TIMEOUT);
  600. support ();
  601. }
  602. void
  603. print_usage (void)
  604. {
  605. printf
  606. ("Usage:\n" " %s %s\n"
  607. " %s (-h | --help) for detailed help\n"
  608. " %s (-V | --version) for version information\n",
  609. PROGNAME, OPTIONS, PROGNAME, PROGNAME);
  610. }
  611. char *
  612. clarify_message (char *msg)
  613. {
  614. int i = 0;
  615. int foo;
  616. char tmpmsg_c[MAX_INPUT_BUFFER];
  617. char *tmpmsg = (char *) &tmpmsg_c;
  618. tmpmsg = strcpy (tmpmsg, msg);
  619. if (!strncmp (tmpmsg, " Hex:", 5)) {
  620. tmpmsg = strtok (tmpmsg, ":");
  621. while ((tmpmsg = strtok (NULL, " "))) {
  622. foo = strtol (tmpmsg, NULL, 16);
  623. /* Translate chars that are not the same value in the printers
  624. * character set.
  625. */
  626. switch (foo) {
  627. case 208:
  628. {
  629. foo = 197;
  630. break;
  631. }
  632. case 216:
  633. {
  634. foo = 196;
  635. break;
  636. }
  637. }
  638. msg[i] = foo;
  639. i++;
  640. }
  641. msg[i] = 0;
  642. }
  643. return (msg);
  644. }
  645. int
  646. check_num (int i)
  647. {
  648. int result;
  649. result = STATE_OK;
  650. if (eval_method[i] & WARN_GT && eval_method[i] & WARN_LT &&
  651. lower_warn_lim[i] > upper_warn_lim[i]) {
  652. if (response_value[i] <= lower_warn_lim[i] &&
  653. response_value[i] >= upper_warn_lim[i]) {
  654. result = STATE_WARNING;
  655. }
  656. }
  657. else if
  658. ((eval_method[i] & WARN_GT && response_value[i] > upper_warn_lim[i]) ||
  659. (eval_method[i] & WARN_GE && response_value[i] >= upper_warn_lim[i]) ||
  660. (eval_method[i] & WARN_LT && response_value[i] < lower_warn_lim[i]) ||
  661. (eval_method[i] & WARN_LE && response_value[i] <= lower_warn_lim[i]) ||
  662. (eval_method[i] & WARN_EQ && response_value[i] == upper_warn_lim[i]) ||
  663. (eval_method[i] & WARN_NE && response_value[i] != upper_warn_lim[i])) {
  664. result = STATE_WARNING;
  665. }
  666. if (eval_method[i] & CRIT_GT && eval_method[i] & CRIT_LT &&
  667. lower_warn_lim[i] > upper_warn_lim[i]) {
  668. if (response_value[i] <= lower_crit_lim[i] &&
  669. response_value[i] >= upper_crit_lim[i]) {
  670. result = STATE_CRITICAL;
  671. }
  672. }
  673. else if
  674. ((eval_method[i] & CRIT_GT && response_value[i] > upper_crit_lim[i]) ||
  675. (eval_method[i] & CRIT_GE && response_value[i] >= upper_crit_lim[i]) ||
  676. (eval_method[i] & CRIT_LT && response_value[i] < lower_crit_lim[i]) ||
  677. (eval_method[i] & CRIT_LE && response_value[i] <= lower_crit_lim[i]) ||
  678. (eval_method[i] & CRIT_EQ && response_value[i] == upper_crit_lim[i]) ||
  679. (eval_method[i] & CRIT_NE && response_value[i] != upper_crit_lim[i])) {
  680. result = STATE_CRITICAL;
  681. }
  682. return result;
  683. }
  684. int
  685. lu_getll (unsigned long *ll, char *str)
  686. {
  687. char tmp[100];
  688. if (strchr (str, ':') == NULL)
  689. return 0;
  690. if (strchr (str, ',') != NULL && (strchr (str, ',') < strchr (str, ':')))
  691. return 0;
  692. if (sscanf (str, "%lu%[:]", ll, tmp) == 2)
  693. return 1;
  694. return 0;
  695. }
  696. int
  697. lu_getul (unsigned long *ul, char *str)
  698. {
  699. char tmp[100];
  700. if (sscanf (str, "%lu%[^,]", ul, tmp) == 1)
  701. return 1;
  702. if (sscanf (str, ":%lu%[^,]", ul, tmp) == 1)
  703. return 1;
  704. if (sscanf (str, "%*u:%lu%[^,]", ul, tmp) == 1)
  705. return 1;
  706. return 0;
  707. }
  708. /* trim leading whitespace
  709. if there is a leading quote, make sure it balances */
  710. char *
  711. thisarg (char *str)
  712. {
  713. str += strspn (str, " \t\r\n"); /* trim any leading whitespace */
  714. if (strstr (str, "'") == str) { /* handle SIMPLE quoted strings */
  715. if (strlen (str) == 1 || !strstr (str + 1, "'"))
  716. terminate (STATE_UNKNOWN, "Unbalanced quotes\n");
  717. }
  718. return str;
  719. }
  720. /* if there's a leading quote, advance to the trailing quote
  721. set the trailing quote to '\x0'
  722. if the string continues, advance beyond the comma */
  723. char *
  724. nextarg (char *str)
  725. {
  726. if (strstr (str, "'") == str) {
  727. if (strlen (str) > 1) {
  728. str = strstr (str + 1, "'");
  729. str[0] = 0;
  730. return (++str);
  731. }
  732. else {
  733. str[0] = 0;
  734. return NULL;
  735. }
  736. }
  737. if (strstr (str, ",") == str) {
  738. if (strlen (str) > 1) {
  739. str[0] = 0;
  740. return (++str);
  741. }
  742. else {
  743. str[0] = 0;
  744. return NULL;
  745. }
  746. }
  747. if ((str = strstr (str, ",")) && strlen (str) > 1) {
  748. str[0] = 0;
  749. return (++str);
  750. }
  751. return NULL;
  752. }