check_snmp.c 26 KB

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