check_snmp.c 26 KB

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