/** Shut down the metric catcher */ public void shutdown() { logger.info("shutting down..."); // Pass shutdown to the metricCatcher if (metricCatcher != null) metricCatcher.shutdown(); try { // Wait for it shutdown metricCatcher.join(1000); } catch (InterruptedException e) { logger.info("interrupted waiting for thread to shutdown, exiting..."); } }
/** * Load properties, build a MetricCatcher, start catching * * @param propertiesFile The config file * @throws IOException if the properties file cannot be read */ public Loader(File propertiesFile) throws IOException { logger.info("Starting metriccatcher"); logger.info("Loading configuration from: " + propertiesFile.getAbsolutePath()); Properties properties = new Properties(); try { properties.load(new FileReader(propertiesFile)); for (Object key : properties.keySet()) { // copy properties into system properties System.setProperty((String) key, (String) properties.get(key)); } } catch (IOException e) { logger.error("error reading properties file: " + e); System.exit(1); } int reportingInterval = 60; String intervalProperty = properties.getProperty(METRICCATCHER_INTERVAL); if (intervalProperty != null) { try { reportingInterval = Integer.parseInt(intervalProperty); } catch (NumberFormatException e) { logger.warn("Couldn't parse " + METRICCATCHER_INTERVAL + " setting", e); } } boolean disableJvmMetrics = false; String disableJvmProperty = properties.getProperty(METRICCATCHER_DISABLE_JVM_METRICS); if (disableJvmProperty != null) { disableJvmMetrics = BooleanUtils.toBoolean(disableJvmProperty); if (disableJvmMetrics) { logger.info("Disabling JVM metric reporting"); } } boolean reportingEnabled = false; // Start a Ganglia reporter if specified in the config String gangliaHost = properties.getProperty(METRICCATCHER_GANGLIA_HOST); String gangliaPort = properties.getProperty(METRICCATCHER_GANGLIA_PORT); if (gangliaHost != null && gangliaPort != null) { logger.info("Creating Ganglia reporter pointed at " + gangliaHost + ":" + gangliaPort); GangliaReporter gangliaReporter = new GangliaReporter(gangliaHost, Integer.parseInt(gangliaPort)); gangliaReporter.printVMMetrics = !disableJvmMetrics; gangliaReporter.start(reportingInterval, TimeUnit.SECONDS); reportingEnabled = true; } // Start a Graphite reporter if specified in the config String graphiteHost = properties.getProperty(METRICCATCHER_GRAPHITE_HOST); String graphitePort = properties.getProperty(METRICCATCHER_GRAPHITE_PORT); if (graphiteHost != null && graphitePort != null) { String graphitePrefix = properties.getProperty(METRICCATCHER_GRAPHITE_PREFIX); if (graphitePrefix == null) { graphitePrefix = InetAddress.getLocalHost().getHostName(); } logger.info( "Creating Graphite reporter pointed at " + graphiteHost + ":" + graphitePort + " with prefix '" + graphitePrefix + "'"); GraphiteReporter graphiteReporter = new GraphiteReporter( graphiteHost, Integer.parseInt(graphitePort), StringUtils.trimToNull(graphitePrefix)); graphiteReporter.printVMMetrics = !disableJvmMetrics; graphiteReporter.start(reportingInterval, TimeUnit.SECONDS); reportingEnabled = true; } String reporterConfigFile = properties.getProperty(METRICCATCHER_REPORTER_CONFIG); if (reporterConfigFile != null) { logger.info("Trying to load reporterConfig from file: {}", reporterConfigFile); try { ReporterConfig.loadFromFileAndValidate(reporterConfigFile).enableAll(); } catch (Exception e) { logger.error( "Failed to load metrics-reporter-config, metric sinks will not be activated", e); } reportingEnabled = true; } if (!reportingEnabled) { logger.error("No reporters enabled. MetricCatcher can not do it's job"); throw new RuntimeException("No reporters enabled"); } int maxMetrics = Integer.parseInt(properties.getProperty(METRICCATCHER_MAX_METRICS, "500")); logger.info("Max metrics: " + maxMetrics); Map<String, Metric> lruMap = new LRUMap<String, Metric>(10, maxMetrics); int port = Integer.parseInt(properties.getProperty(METRICCATCHER_UDP_PORT, "1420")); logger.info("Listening on UDP port " + port); DatagramSocket socket = new DatagramSocket(port); metricCatcher = new MetricCatcher(socket, lruMap); metricCatcher.start(); // Register a shutdown hook and wait for termination Runtime.getRuntime() .addShutdownHook( new Thread() { @Override public void run() { shutdown(); }; }); }