001package io.prometheus.jmx;
002
003import io.prometheus.client.CollectorRegistry;
004import io.prometheus.jmx.common.http.ConfigurationException;
005import io.prometheus.jmx.common.http.HTTPServerFactory;
006
007import java.io.File;
008import java.net.InetSocketAddress;
009import java.text.SimpleDateFormat;
010import java.util.Date;
011import java.util.Locale;
012
013public class WebServer {
014
015    private static final SimpleDateFormat SIMPLE_DATE_FORMAT =
016            new SimpleDateFormat("yyyy-MM-dd | HH:mm:ss.SSS", Locale.getDefault());
017
018    public static void main(String[] args) throws Exception {
019        if (args.length < 2) {
020            System.err.println("Usage: WebServer <[hostname:]port> <yaml configuration file>");
021            System.exit(1);
022        }
023
024        InetSocketAddress socket;
025        int colonIndex = args[0].lastIndexOf(':');
026
027        if (colonIndex < 0) {
028            int port = Integer.parseInt(args[0]);
029            socket = new InetSocketAddress(port);
030        } else {
031            int port = Integer.parseInt(args[0].substring(colonIndex + 1));
032            String host = args[0].substring(0, colonIndex);
033            socket = new InetSocketAddress(host, port);
034        }
035
036        new BuildInfoCollector().register();
037        new JmxCollector(new File(args[1]), JmxCollector.Mode.STANDALONE).register();
038
039        try {
040            new HTTPServerFactory().createHTTPServer(socket, CollectorRegistry.defaultRegistry, false, new File(args[1]));
041        } catch (ConfigurationException e) {
042            System.err.println("Configuration Exception : " + e.getMessage());
043            System.exit(1);
044        }
045
046        System.out.println(
047                String.format("%s | %s | INFO | %s | %s",
048                        SIMPLE_DATE_FORMAT.format(new Date()),
049                        Thread.currentThread().getName(),
050                        WebServer.class.getName(),
051                        "Running"));
052    }
053}