check_snmp.c 26 KB

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