diff options
author | Simon Rettberg | 2018-03-12 14:20:13 +0100 |
---|---|---|
committer | Simon Rettberg | 2018-03-12 14:20:13 +0100 |
commit | 54472b7e6762d88658e9a26f5f6c3ed789e1e7d1 (patch) | |
tree | a5360bf24df8acf85c1a0b988abe60adc5de7fc1 | |
parent | Add clustering to nodes based on address (diff) | |
download | dnbd3-status-54472b7e6762d88658e9a26f5f6c3ed789e1e7d1.tar.gz dnbd3-status-54472b7e6762d88658e9a26f5f6c3ed789e1e7d1.tar.xz dnbd3-status-54472b7e6762d88658e9a26f5f6c3ed789e1e7d1.zip |
Serverlist on command line; support new dnbd3 rpc format
6 files changed, 102 insertions, 20 deletions
diff --git a/src/main/java/org/openslx/dnbd3/status/App.java b/src/main/java/org/openslx/dnbd3/status/App.java index 65ff524..5c51e7a 100644 --- a/src/main/java/org/openslx/dnbd3/status/App.java +++ b/src/main/java/org/openslx/dnbd3/status/App.java @@ -1,15 +1,22 @@ package org.openslx.dnbd3.status; import java.io.IOException; +import java.util.Arrays; public class App { public static void main( String[] args ) throws IOException { + if ( args.length < 1 ) { + System.err.println( "Missing arguments\nUsage: tool <port> <server1> <server2> ... <serverN>" ); + System.exit( 1 ); + } + int port = Integer.parseInt( args[0] ); + String[] servers = Arrays.copyOfRange( args, 0, args.length ); System.setProperty( "com.example.jndi.dns.timeout.initial", "400" ); System.setProperty( "com.example.jndi.dns.timeout.retries", "2" ); - WebServer ws = new WebServer( 8888 ); + WebServer ws = new WebServer( port, servers ); ws.run(); } diff --git a/src/main/java/org/openslx/dnbd3/status/StatisticsGenerator.java b/src/main/java/org/openslx/dnbd3/status/StatisticsGenerator.java index fb94275..4556378 100644 --- a/src/main/java/org/openslx/dnbd3/status/StatisticsGenerator.java +++ b/src/main/java/org/openslx/dnbd3/status/StatisticsGenerator.java @@ -31,11 +31,13 @@ public class StatisticsGenerator private ExecutorService threadPool = new ThreadPoolExecutor( 3, 8, 1, TimeUnit.MINUTES, new SynchronousQueue<Runnable>() ); private List<Future<Status>> futureStatusList = new ArrayList<>(); private List<Status> statusList = new ArrayList<>(); - private final Gson jsonBuilder; + private final Gson exposedJsonBuilder; + private final Gson defaultJsonBuilder; private final Graph graph = new Graph( "DNBD 3 Status" ); private byte[] imgData = null; private final OutputMain output = new OutputMain(); + private final OutputMain newOutput = new OutputMain(); public StatisticsGenerator( List<ServerPoller> pollers ) { @@ -49,10 +51,13 @@ public class StatisticsGenerator final GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.excludeFieldsWithoutExposeAnnotation(); gsonBuilder.registerTypeHierarchyAdapter( Edge.class, new EdgeSerializer() ); - jsonBuilder = gsonBuilder.create(); + exposedJsonBuilder = gsonBuilder.create(); + defaultJsonBuilder = new GsonBuilder().create(); output.graph = graph; output.servers = new ArrayList<>(); output.timestamp = 0; + newOutput.servers = new ArrayList<>(); + newOutput.timestamp = 0; } private synchronized void updateAll() @@ -74,6 +79,7 @@ public class StatisticsGenerator } statusList.clear(); output.servers.clear(); + newOutput.servers.clear(); for ( Future<Status> future : futureStatusList ) { Status status; try { @@ -92,8 +98,12 @@ public class StatisticsGenerator srv.uptime = status.getUptime(); srv.timestamp = status.getTimestamp(); output.servers.add( srv ); + srv = srv.clone(); + srv.images = status.getImages(); + srv.clients = status.getClients(); + newOutput.servers.add( srv ); } - output.timestamp = System.currentTimeMillis(); + newOutput.timestamp = output.timestamp = System.currentTimeMillis(); synchronized ( graph ) { graph.decay(); for ( Status status : statusList ) { @@ -137,11 +147,17 @@ public class StatisticsGenerator return imgData; } - public String getJson() + public String getJson( boolean newFormat ) { ensureUpToDate(); - synchronized ( output ) { - return jsonBuilder.toJson( output ); + if ( newFormat ) { + synchronized ( newOutput ) { + return defaultJsonBuilder.toJson( newOutput ); + } + } else { + synchronized ( output ) { + return exposedJsonBuilder.toJson( output ); + } } } diff --git a/src/main/java/org/openslx/dnbd3/status/WebServer.java b/src/main/java/org/openslx/dnbd3/status/WebServer.java index 3e1a65c..f652f6a 100644 --- a/src/main/java/org/openslx/dnbd3/status/WebServer.java +++ b/src/main/java/org/openslx/dnbd3/status/WebServer.java @@ -18,15 +18,24 @@ public class WebServer extends NanoHTTPD private final StatisticsGenerator imageGenerator; - public WebServer( int port ) + public WebServer( int port, String... servers ) { super( port ); List<ServerPoller> pollers = new ArrayList<>(); - pollers.add( new ServerPoller( "132.230.4.2", 5003 ) ); - pollers.add( new ServerPoller( "132.230.8.113", 5003 ) ); - pollers.add( new ServerPoller( "132.230.4.60", 5003 ) ); - pollers.add( new ServerPoller( "10.4.128.240", 5003 ) ); - pollers.add( new ServerPoller( "10.3.56.43", 5003 ) ); + for ( String server : servers ) { + String[] parts = server.split( ":" ); + if (parts.length > 2) { + System.out.println( "Ignoring invalid line " + server ); + } else if (parts.length == 2) { + try { + pollers.add( new ServerPoller( parts[0], Integer.parseInt( parts[1] ) ) ); + } catch (NumberFormatException e) { + System.out.println( "Ignoring invalid port from " + server ); + } + } else { + pollers.add( new ServerPoller( server, 5003 ) ); + } + } imageGenerator = new StatisticsGenerator( pollers ); } @@ -39,7 +48,9 @@ public class WebServer extends NanoHTTPD if ( uri.equals( "/image.png" ) ) return serveImage(); if ( uri.equals( "/data.json" ) ) - return serveJson(); + return serveJson(false); + if ( uri.equals( "/data2.json" ) ) + return serveJson(true); // Static files if ( uri.equals( "/" ) ) @@ -89,13 +100,15 @@ public class WebServer extends NanoHTTPD } } - private NanoHTTPD.Response serveJson() + private NanoHTTPD.Response serveJson(boolean newFormat) { - String data = imageGenerator.getJson(); + String data = imageGenerator.getJson(newFormat); if ( data == null ) { return new NanoHTTPD.Response( NanoHTTPD.Response.Status.INTERNAL_ERROR, "text/plain", "Internal Server Error" ); } else { - return new NanoHTTPD.Response( NanoHTTPD.Response.Status.OK, "application/json", data ); + NanoHTTPD.Response response = new NanoHTTPD.Response( NanoHTTPD.Response.Status.OK, "application/json", data ); + response.addHeader( "Access-Control-Allow-Origin", "*" ); + return response; } } diff --git a/src/main/java/org/openslx/dnbd3/status/output/ServerStats.java b/src/main/java/org/openslx/dnbd3/status/output/ServerStats.java index b915484..67dda59 100644 --- a/src/main/java/org/openslx/dnbd3/status/output/ServerStats.java +++ b/src/main/java/org/openslx/dnbd3/status/output/ServerStats.java @@ -1,8 +1,14 @@ package org.openslx.dnbd3.status.output; +import java.util.ArrayList; +import java.util.List; + +import org.openslx.dnbd3.status.rpc.Client; +import org.openslx.dnbd3.status.rpc.Image; + import com.google.gson.annotations.Expose; -public class ServerStats +public class ServerStats implements Cloneable { @Expose @@ -18,4 +24,27 @@ public class ServerStats @Expose public long timestamp; + public List<Image> images; + + public List<Client> clients; + + @Override + public ServerStats clone() + { + ServerStats n = new ServerStats(); + n.address = this.address; + n.clientCount = this.clientCount; + n.uptime = this.uptime; + n.bytesSent = this.bytesSent; + n.bytesReceived = this.bytesReceived; + n.timestamp = this.timestamp; + if ( this.images != null ) { + n.images = new ArrayList<>( this.images ); + } + if ( this.clients != null ) { + n.clients = new ArrayList<>( this.clients ); + } + return n; + } + } diff --git a/src/main/java/org/openslx/dnbd3/status/poller/ServerPoller.java b/src/main/java/org/openslx/dnbd3/status/poller/ServerPoller.java index 446ab2a..97b5f5a 100644 --- a/src/main/java/org/openslx/dnbd3/status/poller/ServerPoller.java +++ b/src/main/java/org/openslx/dnbd3/status/poller/ServerPoller.java @@ -9,6 +9,8 @@ import org.openslx.dnbd3.status.rpc.Status; import com.google.gson.Gson; +import fi.iki.elonen.NanoHTTPD; + /** * Polling a dnbd3 server for its status. * @@ -19,11 +21,13 @@ public class ServerPoller private final String address; private final String server; private final Gson parseGson = new Gson(); + + private byte[] buffer = new byte[5000]; public ServerPoller( String host, int port ) { this.address = host; - this.server = "http://" + host + ":" + port + "/"; + this.server = "http://" + host + ":" + port + "/query?q=stats&q=clients"; } public Status update() @@ -50,7 +54,14 @@ public class ServerPoller // Now read data Status status; try { - status = parseGson.fromJson( new InputStreamReader( is ), Status.class ); + InputStreamReader isr = new InputStreamReader( is ); + status = parseGson.fromJson( isr, Status.class ); + while ( is.read( buffer ) > 0 ) { + // Nothing + } + NanoHTTPD.safeClose( isr ); + NanoHTTPD.safeClose( is ); + NanoHTTPD.safeClose( con.getErrorStream() ); status.setAddress( address ); status.setTimestamp( System.currentTimeMillis() ); } catch ( Exception e ) { diff --git a/src/main/java/org/openslx/dnbd3/status/rpc/Image.java b/src/main/java/org/openslx/dnbd3/status/rpc/Image.java index 910db82..6e2cde9 100644 --- a/src/main/java/org/openslx/dnbd3/status/rpc/Image.java +++ b/src/main/java/org/openslx/dnbd3/status/rpc/Image.java @@ -3,10 +3,16 @@ package org.openslx.dnbd3.status.rpc; public class Image { + private int id = -1; private String name = null; private int rid = -1; private int complete = -1; private int users = -1; + + public int getId() + { + return id; + } public String getName() { |