check_snmp.c 26 KB

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