check_snmp.c 26 KB

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