Search in sources :

Example 1 with WavefrontAPI

use of com.wavefront.api.WavefrontAPI in project java by wavefrontHQ.

the class AbstractAgent method start.

/**
 * Entry-point for the application.
 *
 * @param args Command-line parameters passed on to JCommander to configure the daemon.
 */
public void start(String[] args) throws IOException {
    try {
        // read build information and print version.
        props = ResourceBundle.getBundle("build");
        logger.info("Starting proxy version " + props.getString("build.version"));
        logger.info("Arguments: " + Joiner.on(", ").join(args));
        JCommander jCommander = new JCommander(this, args);
        if (help) {
            jCommander.setProgramName(this.getClass().getCanonicalName());
            jCommander.usage();
            System.exit(0);
        }
        if (unparsed_params != null) {
            logger.info("Unparsed arguments: " + Joiner.on(", ").join(unparsed_params));
        }
        /* ------------------------------------------------------------------------------------
       * Configuration Setup.
       * ------------------------------------------------------------------------------------ */
        // 1. Load the listener configurations.
        loadListenerConfigurationFile();
        loadLogsIngestionConfig();
        managedExecutors.add(agentConfigurationExecutor);
        // Conditionally enter an interactive debugging session for logsIngestionConfig.yaml
        if (testLogs) {
            InteractiveLogsTester interactiveLogsTester = new InteractiveLogsTester(this::loadLogsIngestionConfig, prefix);
            logger.info("Reading line-by-line sample log messages from STDIN");
            while (interactiveLogsTester.interactiveTest()) {
            // empty
            }
            System.exit(0);
        }
        // 2. Read or create the unique Id for the daemon running on this machine.
        readOrCreateDaemonId();
        if (proxyHost != null) {
            System.setProperty("http.proxyHost", proxyHost);
            System.setProperty("https.proxyHost", proxyHost);
            System.setProperty("http.proxyPort", String.valueOf(proxyPort));
            System.setProperty("https.proxyPort", String.valueOf(proxyPort));
        }
        if (proxyUser != null && proxyPassword != null) {
            Authenticator.setDefault(new Authenticator() {

                @Override
                public PasswordAuthentication getPasswordAuthentication() {
                    if (getRequestorType() == RequestorType.PROXY) {
                        return new PasswordAuthentication(proxyUser, proxyPassword.toCharArray());
                    } else {
                        return null;
                    }
                }
            });
        }
        // create List of custom tags from the configuration string
        String[] tags = customSourceTagsProperty.split(",");
        for (String tag : tags) {
            tag = tag.trim();
            if (!customSourceTags.contains(tag)) {
                customSourceTags.add(tag);
            } else {
                logger.warning("Custom source tag: " + tag + " was repeated. Check the customSourceTags property in " + "wavefront.conf");
            }
        }
        // 3. Setup proxies.
        WavefrontAPI service = createAgentService();
        try {
            setupQueueing(service);
        } catch (IOException e) {
            logger.log(Level.SEVERE, "Cannot setup local file for queueing due to IO error", e);
            throw e;
        }
        // 4. Start the (push) listening endpoints
        startListeners();
        // set up OoM memory guard
        if (memGuardFlushThreshold > 0) {
            setupMemoryGuard((float) memGuardFlushThreshold / 100);
        }
        new Timer().schedule(new TimerTask() {

            @Override
            public void run() {
                try {
                    // exit if no active listeners
                    if (activeListeners.count() == 0) {
                        logger.severe("**** All listener threads failed to start - there is already a running instance " + "listening on configured ports, or no listening ports configured!");
                        logger.severe("Aborting start-up");
                        System.exit(1);
                    }
                    // 5. Poll or read the configuration file to use.
                    AgentConfiguration config;
                    if (configFile != null) {
                        logger.info("Loading configuration file from: " + configFile);
                        try {
                            config = GSON.fromJson(new FileReader(configFile), AgentConfiguration.class);
                        } catch (FileNotFoundException e) {
                            throw new RuntimeException("Cannot read config file: " + configFile);
                        }
                        try {
                            config.validate(localAgent);
                        } catch (RuntimeException ex) {
                            logger.log(Level.SEVERE, "cannot parse config file", ex);
                            throw new RuntimeException("cannot parse config file", ex);
                        }
                        agentId = null;
                    } else {
                        updateAgentMetrics.run();
                        config = fetchConfig();
                        logger.info("scheduling regular configuration polls");
                        agentConfigurationExecutor.scheduleAtFixedRate(updateAgentMetrics, 10, 60, TimeUnit.SECONDS);
                        agentConfigurationExecutor.scheduleWithFixedDelay(updateConfiguration, 0, 1, TimeUnit.SECONDS);
                    }
                    // 6. Setup work units and targets based on the configuration.
                    if (config != null) {
                        logger.info("initial configuration is available, setting up proxy");
                        processConfiguration(config);
                    }
                    Runtime.getRuntime().addShutdownHook(new Thread("proxy-shutdown-hook") {

                        @Override
                        public void run() {
                            shutdown();
                        }
                    });
                    logger.info("setup complete");
                } catch (Throwable t) {
                    logger.log(Level.SEVERE, "Aborting start-up", t);
                    System.exit(1);
                }
            }
        }, 5000);
    } catch (Throwable t) {
        logger.log(Level.SEVERE, "Aborting start-up", t);
        System.exit(1);
    }
}
Also used : WavefrontAPI(com.wavefront.api.WavefrontAPI) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) InteractiveLogsTester(com.wavefront.agent.logsharvesting.InteractiveLogsTester) Timer(java.util.Timer) TimerTask(java.util.TimerTask) JCommander(com.beust.jcommander.JCommander) AgentConfiguration(com.wavefront.api.agent.AgentConfiguration) FileReader(java.io.FileReader) Authenticator(java.net.Authenticator) PasswordAuthentication(java.net.PasswordAuthentication)

Aggregations

JCommander (com.beust.jcommander.JCommander)1 InteractiveLogsTester (com.wavefront.agent.logsharvesting.InteractiveLogsTester)1 WavefrontAPI (com.wavefront.api.WavefrontAPI)1 AgentConfiguration (com.wavefront.api.agent.AgentConfiguration)1 FileNotFoundException (java.io.FileNotFoundException)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 Authenticator (java.net.Authenticator)1 PasswordAuthentication (java.net.PasswordAuthentication)1 Timer (java.util.Timer)1 TimerTask (java.util.TimerTask)1