check_snmp.c 23 KB

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