001/* 002 * Copyright (C) 2015-present The Prometheus jmx_exporter Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package io.prometheus.jmx; 018 019import io.prometheus.jmx.common.http.ConfigurationException; 020import io.prometheus.jmx.common.http.HTTPServerFactory; 021import io.prometheus.metrics.exporter.httpserver.HTTPServer; 022import io.prometheus.metrics.model.registry.PrometheusRegistry; 023import java.io.File; 024import java.net.InetAddress; 025import java.text.SimpleDateFormat; 026import java.util.Date; 027import java.util.Locale; 028 029public class WebServer { 030 031 private static final SimpleDateFormat SIMPLE_DATE_FORMAT = 032 new SimpleDateFormat("yyyy-MM-dd | HH:mm:ss.SSS", Locale.getDefault()); 033 034 public static void main(String[] args) throws Exception { 035 if (args.length < 2) { 036 System.err.println("Usage: WebServer <[hostname:]port> <yaml configuration file>"); 037 System.exit(1); 038 } 039 040 String host = "0.0.0.0"; 041 int port; 042 int colonIndex = args[0].lastIndexOf(':'); 043 044 if (colonIndex < 0) { 045 port = Integer.parseInt(args[0]); 046 } else { 047 port = Integer.parseInt(args[0].substring(colonIndex + 1)); 048 host = args[0].substring(0, colonIndex); 049 } 050 051 new BuildInfoMetrics().register(PrometheusRegistry.defaultRegistry); 052 new JmxCollector(new File(args[1]), JmxCollector.Mode.STANDALONE) 053 .register(PrometheusRegistry.defaultRegistry); 054 055 HTTPServer httpServer = null; 056 057 try { 058 httpServer = 059 new HTTPServerFactory() 060 .createHTTPServer( 061 InetAddress.getByName(host), 062 port, 063 PrometheusRegistry.defaultRegistry, 064 new File(args[1])); 065 066 System.out.println( 067 String.format( 068 "%s | %s | INFO | %s | %s", 069 SIMPLE_DATE_FORMAT.format(new Date()), 070 Thread.currentThread().getName(), 071 WebServer.class.getName(), 072 "Running")); 073 074 Thread.currentThread().join(); 075 } catch (ConfigurationException e) { 076 System.err.println("Configuration Exception : " + e.getMessage()); 077 System.exit(1); 078 } catch (Throwable t) { 079 System.err.println("Exception starting"); 080 t.printStackTrace(); 081 } finally { 082 if (httpServer != null) { 083 httpServer.close(); 084 } 085 } 086 } 087}