check_snmp.c 26 KB

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