check_snmp.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  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 if (strstr (response, "INTEGER: "))
  272. show = strstr (response, "INTEGER: ") + 9;
  273. else
  274. show = response;
  275. p2 = show;
  276. iresult = STATE_DEPENDENT;
  277. if (eval_method[i] & CRIT_PRESENT) {
  278. iresult = STATE_CRITICAL;
  279. } else if (eval_method[i] & WARN_PRESENT) {
  280. iresult = STATE_WARNING;
  281. }
  282. if (eval_method[i] & CRIT_GT ||
  283. eval_method[i] & CRIT_LT ||
  284. eval_method[i] & CRIT_GE ||
  285. eval_method[i] & CRIT_LE ||
  286. eval_method[i] & CRIT_EQ ||
  287. eval_method[i] & CRIT_NE ||
  288. eval_method[i] & WARN_GT ||
  289. eval_method[i] & WARN_LT ||
  290. eval_method[i] & WARN_GE ||
  291. eval_method[i] & WARN_LE ||
  292. eval_method[i] & WARN_EQ ||
  293. eval_method[i] & WARN_NE) {
  294. p2 = strpbrk (p2, "0123456789");
  295. response_value[i] = strtoul (p2, NULL, 10);
  296. iresult = check_num (i);
  297. asprintf (&show, "%lu", response_value[i]);
  298. /*asprintf (&show, "%s", response); */
  299. }
  300. else if (eval_method[i] & CRIT_STRING) {
  301. if (strcmp (response, string_value))
  302. iresult = STATE_CRITICAL;
  303. else
  304. iresult = STATE_OK;
  305. }
  306. else if (eval_method[i] & CRIT_REGEX) {
  307. #ifdef HAVE_REGEX_H
  308. excode = regexec (&preg, response, 10, pmatch, eflags);
  309. if (excode == 0) {
  310. iresult = STATE_OK;
  311. }
  312. else if (excode != REG_NOMATCH) {
  313. regerror (excode, &preg, errbuf, MAX_INPUT_BUFFER);
  314. printf ("Execute Error: %s\n", errbuf);
  315. exit (STATE_CRITICAL);
  316. }
  317. else {
  318. iresult = STATE_CRITICAL;
  319. }
  320. #else
  321. printf ("SNMP UNKNOWN: call for regex which was not a compiled option");
  322. exit (STATE_UNKNOWN);
  323. #endif
  324. }
  325. if (response && iresult == STATE_DEPENDENT)
  326. iresult = STATE_OK;
  327. else if (eval_method[i] & CRIT_PRESENT)
  328. iresult = STATE_CRITICAL;
  329. else if (eval_method[i] & WARN_PRESENT)
  330. iresult = STATE_WARNING;
  331. result = max_state (result, iresult);
  332. if (nlabels > 1 && i < nlabels && labels[i] != NULL)
  333. asprintf (&outbuff, "%s%s%s %s%s%s", outbuff,
  334. (i == 0) ? " " : output_delim,
  335. labels[i], mark (iresult), show, mark (iresult));
  336. else
  337. asprintf (&outbuff, "%s%s%s%s%s", outbuff, (i == 0) ? " " : output_delim,
  338. mark (iresult), show, mark (iresult));
  339. if (nunits > 0 && i < nunits)
  340. asprintf (&outbuff, "%s %s", outbuff, unitv[i]);
  341. i++;
  342. } /* end while (ptr) */
  343. if (found == 0)
  344. terminate
  345. (STATE_UNKNOWN,
  346. "%s problem - No data recieved from host\nCMD: %s\n",
  347. label, command_line);
  348. /* WARNING if output found on stderr */
  349. if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
  350. result = max_state (result, STATE_WARNING);
  351. /* close stderr */
  352. (void) fclose (child_stderr);
  353. /* close the pipe */
  354. if (spclose (child_process))
  355. result = max_state (result, STATE_WARNING);
  356. /* if (nunits == 1 || i == 1) */
  357. /* printf ("%s %s -%s %s\n", label, state_text (result), outbuff, units); */
  358. /* else */
  359. printf ("%s %s -%s\n", label, state_text (result), outbuff);
  360. return result;
  361. }
  362. /* process command-line arguments */
  363. int
  364. process_arguments (int argc, char **argv)
  365. {
  366. char *ptr;
  367. int c = 1;
  368. int j = 0, jj = 0;
  369. #ifdef HAVE_GETOPT_H
  370. int option_index = 0;
  371. static struct option long_options[] = {
  372. STD_LONG_OPTS,
  373. {"community", required_argument, 0, 'C'},
  374. {"oid", required_argument, 0, 'o'},
  375. {"object", required_argument, 0, 'o'},
  376. {"delimiter", required_argument, 0, 'd'},
  377. {"output-delimiter", required_argument, 0, 'D'},
  378. {"string", required_argument, 0, 's'},
  379. {"regex", required_argument, 0, 'r'},
  380. {"ereg", required_argument, 0, 'r'},
  381. {"eregi", required_argument, 0, 'R'},
  382. {"label", required_argument, 0, 'l'},
  383. {"units", required_argument, 0, 'u'},
  384. {"port", required_argument, 0, 'p'},
  385. {"miblist", required_argument, 0, 'm'},
  386. {"protocol", required_argument, 0, 'P'},
  387. {"seclevel", required_argument, 0, 'L'},
  388. {"secname", required_argument, 0, 'U'},
  389. {"authproto", required_argument, 0, 'a'},
  390. {"authpasswd", required_argument, 0, 'A'},
  391. {"privpasswd", required_argument, 0, 'X'},
  392. {0, 0, 0, 0}
  393. };
  394. #endif
  395. if (argc < 2)
  396. return ERROR;
  397. /* reverse compatibility for very old non-POSIX usage forms */
  398. for (c = 1; c < argc; c++) {
  399. if (strcmp ("-to", argv[c]) == 0)
  400. strcpy (argv[c], "-t");
  401. if (strcmp ("-wv", argv[c]) == 0)
  402. strcpy (argv[c], "-w");
  403. if (strcmp ("-cv", argv[c]) == 0)
  404. strcpy (argv[c], "-c");
  405. }
  406. while (1) {
  407. #ifdef HAVE_GETOPT_H
  408. c =
  409. 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:",
  410. long_options, &option_index);
  411. #else
  412. 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:");
  413. #endif
  414. if (c == -1 || c == EOF)
  415. break;
  416. switch (c) {
  417. case '?': /* usage */
  418. usage3 ("Unknown argument", optopt);
  419. case 'h': /* help */
  420. print_help ();
  421. exit (STATE_OK);
  422. case 'V': /* version */
  423. print_revision (PROGNAME, REVISION);
  424. exit (STATE_OK);
  425. case 'v': /* verbose */
  426. verbose = TRUE;
  427. break;
  428. case 't': /* timeout period */
  429. if (!is_integer (optarg))
  430. usage2 ("Timeout Interval must be an integer", optarg);
  431. timeout_interval = atoi (optarg);
  432. break;
  433. case 'e': /* PRELIMINARY - may change */
  434. eval_method[j] |= WARN_PRESENT;
  435. for (ptr = optarg; (ptr = index (ptr, ',')); ptr++)
  436. ptr[0] = ' '; /* relpace comma with space */
  437. for (ptr = optarg; (ptr = index (ptr, ' ')); ptr++)
  438. eval_method[++j] |= WARN_PRESENT;
  439. asprintf (&oid, "%s %s", (oid?oid:""), optarg);
  440. break;
  441. case 'E': /* PRELIMINARY - may change */
  442. eval_method[j] |= WARN_PRESENT;
  443. for (ptr = optarg; (ptr = index (ptr, ',')); ptr++)
  444. ptr[0] = ' '; /* relpace comma with space */
  445. for (ptr = optarg; (ptr = index (ptr, ' ')); ptr++)
  446. eval_method[++j] |= CRIT_PRESENT;
  447. asprintf (&oid, "%s %s", (oid?oid:""), optarg);
  448. break;
  449. case 'c': /* critical time threshold */
  450. if (strspn (optarg, "0123456789:,") < strlen (optarg)) {
  451. printf ("Invalid critical threshold: %s\n", optarg);
  452. print_usage ();
  453. exit (STATE_UNKNOWN);
  454. }
  455. for (ptr = optarg, jj = 0; ptr && jj < MAX_OIDS; jj++) {
  456. if (lu_getll (&lower_crit_lim[jj], ptr) == 1)
  457. eval_method[jj] |= CRIT_LT;
  458. if (lu_getul (&upper_crit_lim[jj], ptr) == 1)
  459. eval_method[jj] |= CRIT_GT;
  460. (ptr = index (ptr, ',')) ? ptr++ : ptr;
  461. }
  462. break;
  463. case 'w': /* warning time threshold */
  464. if (strspn (optarg, "0123456789:,") < strlen (optarg)) {
  465. printf ("Invalid warning threshold: %s\n", optarg);
  466. print_usage ();
  467. exit (STATE_UNKNOWN);
  468. }
  469. for (ptr = optarg, jj = 0; ptr && jj < MAX_OIDS; jj++) {
  470. if (lu_getll (&lower_warn_lim[jj], ptr) == 1)
  471. eval_method[jj] |= WARN_LT;
  472. if (lu_getul (&upper_warn_lim[jj], ptr) == 1)
  473. eval_method[jj] |= WARN_GT;
  474. (ptr = index (ptr, ',')) ? ptr++ : ptr;
  475. }
  476. break;
  477. case 'H': /* Host or server */
  478. server_address = strscpy (server_address, optarg);
  479. break;
  480. case 'C': /* group or community */
  481. community = strscpy (community, optarg);
  482. break;
  483. case 'o': /* object identifier */
  484. for (ptr = optarg; (ptr = index (ptr, ',')); ptr++)
  485. ptr[0] = ' '; /* relpace comma with space */
  486. for (ptr = optarg; (ptr = index (ptr, ' ')); ptr++)
  487. j++; /* count OIDs */
  488. asprintf (&oid, "%s %s", (oid?oid:""), optarg);
  489. break;
  490. case 'd': /* delimiter */
  491. delimiter = strscpy (delimiter, optarg);
  492. break;
  493. case 'D': /* output-delimiter */
  494. output_delim = strscpy (output_delim, optarg);
  495. break;
  496. case 's': /* string or substring */
  497. strncpy (string_value, optarg, sizeof (string_value) - 1);
  498. string_value[sizeof (string_value) - 1] = 0;
  499. eval_method[jj++] = CRIT_STRING;
  500. break;
  501. case 'R': /* regex */
  502. #ifdef HAVE_REGEX_H
  503. cflags = REG_ICASE;
  504. #endif
  505. case 'r': /* regex */
  506. #ifdef HAVE_REGEX_H
  507. cflags |= REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
  508. strncpy (regex_expect, optarg, sizeof (regex_expect) - 1);
  509. regex_expect[sizeof (regex_expect) - 1] = 0;
  510. errcode = regcomp (&preg, regex_expect, cflags);
  511. if (errcode != 0) {
  512. regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER);
  513. printf ("Could Not Compile Regular Expression");
  514. return ERROR;
  515. }
  516. eval_method[jj++] = CRIT_REGEX;
  517. #else
  518. printf ("SNMP UNKNOWN: call for regex which was not a compiled option");
  519. exit (STATE_UNKNOWN);
  520. #endif
  521. break;
  522. case 'l': /* label */
  523. label = optarg;
  524. nlabels++;
  525. if (nlabels >= labels_size) {
  526. labels_size += 8;
  527. labels = realloc (labels, labels_size);
  528. if (labels == NULL)
  529. terminate (STATE_UNKNOWN,
  530. "Could not realloc() labels[%d]", nlabels);
  531. }
  532. labels[nlabels - 1] = optarg;
  533. ptr = thisarg (optarg);
  534. if (strstr (ptr, "'") == ptr)
  535. labels[nlabels - 1] = ptr + 1;
  536. else
  537. labels[nlabels - 1] = ptr;
  538. while (ptr && (ptr = nextarg (ptr))) {
  539. if (nlabels >= labels_size) {
  540. labels_size += 8;
  541. labels = realloc (labels, labels_size);
  542. if (labels == NULL)
  543. terminate (STATE_UNKNOWN, "Could not realloc() labels\n");
  544. }
  545. labels++;
  546. ptr = thisarg (ptr);
  547. if (strstr (ptr, "'") == ptr)
  548. labels[nlabels - 1] = ptr + 1;
  549. else
  550. labels[nlabels - 1] = ptr;
  551. }
  552. break;
  553. case 'u': /* units */
  554. units = optarg;
  555. nunits++;
  556. if (nunits >= unitv_size) {
  557. unitv_size += 8;
  558. unitv = realloc (unitv, unitv_size);
  559. if (unitv == NULL)
  560. terminate (STATE_UNKNOWN,
  561. "Could not realloc() units [%d]\n", nunits);
  562. }
  563. unitv[nunits - 1] = optarg;
  564. ptr = thisarg (optarg);
  565. if (strstr (ptr, "'") == ptr)
  566. unitv[nunits - 1] = ptr + 1;
  567. else
  568. unitv[nunits - 1] = ptr;
  569. while (ptr && (ptr = nextarg (ptr))) {
  570. if (nunits >= unitv_size) {
  571. unitv_size += 8;
  572. unitv = realloc (unitv, unitv_size);
  573. if (units == NULL)
  574. terminate (STATE_UNKNOWN, "Could not realloc() units\n");
  575. }
  576. nunits++;
  577. ptr = thisarg (ptr);
  578. if (strstr (ptr, "'") == ptr)
  579. unitv[nunits - 1] = ptr + 1;
  580. else
  581. unitv[nunits - 1] = ptr;
  582. }
  583. break;
  584. case 'p': /* TCP port number */
  585. port = strscpy(port, optarg);
  586. break;
  587. case 'm': /* List of MIBS */
  588. miblist = strscpy(miblist, optarg);
  589. break;
  590. case 'P': /* SNMP protocol version */
  591. proto = strscpy(proto, optarg);
  592. break;
  593. case 'L': /* security level */
  594. seclevel = strscpy(seclevel,optarg);
  595. break;
  596. case 'U': /* security username */
  597. secname = strscpy(secname, optarg);
  598. break;
  599. case 'a': /* auth protocol */
  600. asprintf (&authproto, optarg);
  601. break;
  602. case 'A': /* auth passwd */
  603. authpasswd = strscpy(authpasswd, optarg);
  604. break;
  605. case 'X': /* priv passwd */
  606. privpasswd = strscpy(privpasswd, optarg);
  607. break;
  608. }
  609. }
  610. if (server_address == NULL)
  611. asprintf (&server_address, argv[optind]);
  612. return validate_arguments ();
  613. }
  614. /******************************************************************************
  615. @@-
  616. <sect3>
  617. <title>validate_arguments</title>
  618. <para>&PROTO_validate_arguments;</para>
  619. <para>Given a database name, this function returns TRUE if the string
  620. is a valid PostgreSQL database name, and returns false if it is
  621. not.</para>
  622. <para>Valid PostgreSQL database names are less than &NAMEDATALEN;
  623. characters long and consist of letters, numbers, and underscores. The
  624. first character cannot be a number, however.</para>
  625. </sect3>
  626. -@@
  627. ******************************************************************************/
  628. int
  629. validate_arguments ()
  630. {
  631. if (community == NULL)
  632. asprintf (&community, DEFAULT_COMMUNITY);
  633. if (delimiter == NULL)
  634. asprintf (&delimiter, DEFAULT_DELIMITER);
  635. if (output_delim == NULL)
  636. asprintf (&output_delim, DEFAULT_OUTPUT_DELIMITER);
  637. if (miblist == NULL)
  638. asprintf (&miblist, DEFAULT_MIBLIST);
  639. if (label == NULL)
  640. asprintf (&label, "SNMP");
  641. if (units == NULL)
  642. asprintf (&units, "");
  643. /* Need better checks to verify seclevel and authproto choices */
  644. if (seclevel == NULL)
  645. asprintf (&seclevel, "noAuthNoPriv");
  646. if (authproto == NULL )
  647. asprintf(&authproto, DEFAULT_AUTH_PROTOCOL);
  648. if (proto == NULL || (strcmp(proto,DEFAULT_PROTOCOL) == 0) ) { /* default protocol version */
  649. asprintf(&proto, DEFAULT_PROTOCOL);
  650. asprintf(&authpriv, "%s%s", "-c ", community);
  651. }
  652. else if ( strcmp (proto, "3") == 0 ) { /* snmpv3 args */
  653. asprintf(&proto, "%s", "3");
  654. if ( (strcmp(seclevel, "noAuthNoPriv") == 0) || seclevel == NULL ) {
  655. asprintf(&authpriv, "%s", "-l noAuthNoPriv" );
  656. }
  657. else if ( strcmp(seclevel, "authNoPriv") == 0 ) {
  658. if ( secname == NULL || authpasswd == NULL) {
  659. printf ("Missing secname (%s) or authpassword (%s) ! \n",secname, authpasswd );
  660. print_usage ();
  661. exit (STATE_UNKNOWN);
  662. }
  663. asprintf(&authpriv, "-l authNoPriv -a %s -u %s -A %s ", authproto, secname, authpasswd);
  664. }
  665. else if ( strcmp(seclevel, "authPriv") == 0 ) {
  666. if ( secname == NULL || authpasswd == NULL || privpasswd == NULL ) {
  667. printf ("Missing secname (%s), authpassword (%s), or privpasswd (%s)! \n",secname, authpasswd,privpasswd );
  668. print_usage ();
  669. exit (STATE_UNKNOWN);
  670. }
  671. asprintf(&authpriv, "-l authPriv -a %s -u %s -A %s -x DES -X %s ", authproto, secname, authpasswd, privpasswd);
  672. }
  673. }
  674. else {
  675. printf ("Invalid SNMP version: %s\n", proto);
  676. print_usage ();
  677. exit (STATE_UNKNOWN);
  678. }
  679. return OK;
  680. }
  681. void
  682. print_help (void)
  683. {
  684. print_revision (PROGNAME, REVISION);
  685. printf
  686. ("Copyright (c) %s %s <%s>\n\n%s\n",
  687. COPYRIGHT, AUTHOR, EMAIL, SUMMARY);
  688. print_usage ();
  689. printf
  690. ("\nOptions:\n" LONGOPTIONS "\n" DESCRIPTION "\n" NOTES "\n",
  691. DEFAULT_COMMUNITY, DEFAULT_PORT, DEFAULT_DELIMITER, DEFAULT_TIMEOUT);
  692. support ();
  693. }
  694. void
  695. print_usage (void)
  696. {
  697. printf
  698. ("Usage:\n" " %s %s\n"
  699. " %s (-h | --help) for detailed help\n"
  700. " %s (-V | --version) for version information\n",
  701. PROGNAME, OPTIONS, PROGNAME, PROGNAME);
  702. }
  703. char *
  704. clarify_message (char *msg)
  705. {
  706. int i = 0;
  707. int foo;
  708. char tmpmsg_c[MAX_INPUT_BUFFER];
  709. char *tmpmsg = (char *) &tmpmsg_c;
  710. tmpmsg = strcpy (tmpmsg, msg);
  711. if (!strncmp (tmpmsg, " Hex:", 5)) {
  712. tmpmsg = strtok (tmpmsg, ":");
  713. while ((tmpmsg = strtok (NULL, " "))) {
  714. foo = strtol (tmpmsg, NULL, 16);
  715. /* Translate chars that are not the same value in the printers
  716. * character set.
  717. */
  718. switch (foo) {
  719. case 208:
  720. {
  721. foo = 197;
  722. break;
  723. }
  724. case 216:
  725. {
  726. foo = 196;
  727. break;
  728. }
  729. }
  730. msg[i] = foo;
  731. i++;
  732. }
  733. msg[i] = 0;
  734. }
  735. return (msg);
  736. }
  737. int
  738. check_num (int i)
  739. {
  740. int result;
  741. result = STATE_OK;
  742. if (eval_method[i] & WARN_GT && eval_method[i] & WARN_LT &&
  743. lower_warn_lim[i] > upper_warn_lim[i]) {
  744. if (response_value[i] <= lower_warn_lim[i] &&
  745. response_value[i] >= upper_warn_lim[i]) {
  746. result = STATE_WARNING;
  747. }
  748. }
  749. else if
  750. ((eval_method[i] & WARN_GT && response_value[i] > upper_warn_lim[i]) ||
  751. (eval_method[i] & WARN_GE && response_value[i] >= upper_warn_lim[i]) ||
  752. (eval_method[i] & WARN_LT && response_value[i] < lower_warn_lim[i]) ||
  753. (eval_method[i] & WARN_LE && response_value[i] <= lower_warn_lim[i]) ||
  754. (eval_method[i] & WARN_EQ && response_value[i] == upper_warn_lim[i]) ||
  755. (eval_method[i] & WARN_NE && response_value[i] != upper_warn_lim[i])) {
  756. result = STATE_WARNING;
  757. }
  758. if (eval_method[i] & CRIT_GT && eval_method[i] & CRIT_LT &&
  759. lower_warn_lim[i] > upper_warn_lim[i]) {
  760. if (response_value[i] <= lower_crit_lim[i] &&
  761. response_value[i] >= upper_crit_lim[i]) {
  762. result = STATE_CRITICAL;
  763. }
  764. }
  765. else if
  766. ((eval_method[i] & CRIT_GT && response_value[i] > upper_crit_lim[i]) ||
  767. (eval_method[i] & CRIT_GE && response_value[i] >= upper_crit_lim[i]) ||
  768. (eval_method[i] & CRIT_LT && response_value[i] < lower_crit_lim[i]) ||
  769. (eval_method[i] & CRIT_LE && response_value[i] <= lower_crit_lim[i]) ||
  770. (eval_method[i] & CRIT_EQ && response_value[i] == upper_crit_lim[i]) ||
  771. (eval_method[i] & CRIT_NE && response_value[i] != upper_crit_lim[i])) {
  772. result = STATE_CRITICAL;
  773. }
  774. return result;
  775. }
  776. int
  777. lu_getll (unsigned long *ll, char *str)
  778. {
  779. char tmp[100];
  780. if (strchr (str, ':') == NULL)
  781. return 0;
  782. if (strchr (str, ',') != NULL && (strchr (str, ',') < strchr (str, ':')))
  783. return 0;
  784. if (sscanf (str, "%lu%[:]", ll, tmp) == 2)
  785. return 1;
  786. return 0;
  787. }
  788. int
  789. lu_getul (unsigned long *ul, char *str)
  790. {
  791. char tmp[100];
  792. if (sscanf (str, "%lu%[^,]", ul, tmp) == 1)
  793. return 1;
  794. if (sscanf (str, ":%lu%[^,]", ul, tmp) == 1)
  795. return 1;
  796. if (sscanf (str, "%*u:%lu%[^,]", ul, tmp) == 1)
  797. return 1;
  798. return 0;
  799. }
  800. /* trim leading whitespace
  801. if there is a leading quote, make sure it balances */
  802. char *
  803. thisarg (char *str)
  804. {
  805. str += strspn (str, " \t\r\n"); /* trim any leading whitespace */
  806. if (strstr (str, "'") == str) { /* handle SIMPLE quoted strings */
  807. if (strlen (str) == 1 || !strstr (str + 1, "'"))
  808. terminate (STATE_UNKNOWN, "Unbalanced quotes\n");
  809. }
  810. return str;
  811. }
  812. /* if there's a leading quote, advance to the trailing quote
  813. set the trailing quote to '\x0'
  814. if the string continues, advance beyond the comma */
  815. char *
  816. nextarg (char *str)
  817. {
  818. if (strstr (str, "'") == str) {
  819. if (strlen (str) > 1) {
  820. str = strstr (str + 1, "'");
  821. str[0] = 0;
  822. return (++str);
  823. }
  824. else {
  825. str[0] = 0;
  826. return NULL;
  827. }
  828. }
  829. if (strstr (str, ",") == str) {
  830. if (strlen (str) > 1) {
  831. str[0] = 0;
  832. return (++str);
  833. }
  834. else {
  835. str[0] = 0;
  836. return NULL;
  837. }
  838. }
  839. if ((str = strstr (str, ",")) && strlen (str) > 1) {
  840. str[0] = 0;
  841. return (++str);
  842. }
  843. return NULL;
  844. }