check_snmp.c 26 KB

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