import java.io.IOException;
import java.io.PrintWriter;
import java.security.KeyStore;
import java.security.Provider;
import java.security.Security;
import java.security.cert.X509Certificate;
import java.util.Formatter;
import java.util.HashMap;
import java.util.Locale;
import javax.net.ssl.ManagerFactoryParameters;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactorySpi;
import javax.net.ssl.X509TrustManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.axis.types.UnsignedInt;
import com.zeus.ZXTMServlet.ZXTMHttpServletRequest;
import com.zeus.soap.zxtm._1_0.DiagnoseConfigError;
import com.zeus.soap.zxtm._1_0.DiagnoseErrLevel;
import com.zeus.soap.zxtm._1_0.DiagnoseErrorInfo;
import com.zeus.soap.zxtm._1_0.DiagnoseFailedNode;
import com.zeus.soap.zxtm._1_0.DiagnoseFlipperError;
import com.zeus.soap.zxtm._1_0.DiagnoseLocator;
import com.zeus.soap.zxtm._1_0.DiagnosePort;
import com.zeus.soap.zxtm._1_0.DiagnoseSystemStatus;
import com.zeus.soap.zxtm._1_0.SystemConnectionsConnection;
import com.zeus.soap.zxtm._1_0.SystemConnectionsLocator;
import com.zeus.soap.zxtm._1_0.SystemConnectionsPort;
import com.zeus.soap.zxtm._1_0.SystemMachineInfoLocator;
import com.zeus.soap.zxtm._1_0.SystemMachineInfoPort;
import com.zeus.soap.zxtm._1_0.SystemStatsLocator;
import com.zeus.soap.zxtm._1_0.SystemStatsPort;
import com.zeus.soap.zxtm._1_0.VirtualServerLocator;
import com.zeus.soap.zxtm._1_0.VirtualServerPort;
import com.zeus.soap.zxtm._1_0.VirtualServerProtocol;
public class ServerStatus extends HttpServlet {
private static final long serialVersionUID = 1L;
public void init() throws ServletException {
super.init();
// Install the all-trusting trust manager
Security.addProvider( new MyProvider() );
Security.setProperty( "ssl.TrustManagerFactory.algorithm",
"TrustAllCertificates" );
}
public void doGet( HttpServletRequest req, HttpServletResponse res )
throws ServletException, IOException
{
try {
ZXTMHttpServletRequest zreq = (ZXTMHttpServletRequest)req;
String[] userPass = zreq.getRemoteUserAndPassword();
if( userPass == null ) throw new Exception( "No Authentication details" );
// Username is userPass[0], password is userPass[1]
String admin = "https://"+userPass[0]+":"+userPass[1]+"@localhost:9090/soap";
SystemMachineInfoLocator smil = new SystemMachineInfoLocator();
smil.setSystemMachineInfoPortEndpointAddress( admin );
SystemMachineInfoPort smip = smil.getSystemMachineInfoPort();
// will throw an exception if admin username and passwd are incorrect
smip.getProductVersion();
// No exceptions thrown... must have been successful ;-)
String result = generateReport( admin );
res.setContentType( "text/html" );
PrintWriter out = res.getWriter();
out.println( "
System Status
" );
out.println( result );
} catch( Exception e ) {
res.setHeader( "WWW-Authenticate", "Basic realm=\"ZXTM Server Status\"" );
res.setHeader( "Content-Type", "text/html" );
res.setStatus( 401 );
String message =
"" +
"Unauthorized" +
"" +
"Unauthorized - please log in
" +
"Please log in with the Admin username and password
" +
"Error: " + e.toString() + "
" +
"" +
"";
PrintWriter out = res.getWriter();
out.println( message );
}
}
public void doPost( HttpServletRequest req, HttpServletResponse res )
throws ServletException, IOException
{
doGet( req, res );
}
public String joinArray( String[] a ) {
StringBuilder ret = new StringBuilder();
ret.append( '[' );
for( int i = 0; i < a.length; i++ ) {
if( i > 0 ) ret.append( ", " );
else ret.append( ' ' );
ret.append( a[i] );
}
ret.append( " ]" );
return ret.toString();
}
public String generateReport( String admin ) {
HashMap data = new HashMap();
// Get the system name
try {
data.put( "name", java.net.InetAddress.getLocalHost().getHostName() );
} catch( Exception e ) {
// ReverseDNS probably failed... where's `uname -n` when you need it?
int i;
if( (i = e.toString().lastIndexOf( ": " )) != -1 ) {
data.put( "name", e.toString().substring( i+2 ));
} else {
data.put( "name_error", "Error: Could not retrieve system name: " + e.toString() );
}
}
// Get the essential statistics
try {
SystemMachineInfoLocator smil = new SystemMachineInfoLocator();
smil.setSystemMachineInfoPortEndpointAddress( admin );
SystemMachineInfoPort smip = smil.getSystemMachineInfoPort();
data.put( "version", smip.getProductVersion() );
data.put( "ips", joinArray( smip.getIPAddresses() ) );
SystemStatsLocator ssl = new SystemStatsLocator();
ssl.setSystemStatsPortEndpointAddress( admin );
SystemStatsPort ssp = ssl.getSystemStatsPort();
int u = ssp.getUpTime()/100;
data.put( "uptime",
u/(24*3600) + " days, " +
(u%(24*3600))/3600 + " hours, " +
(u%(3600)/60) + " minutes, " +
u%60+" seconds" );
data.put( "cpu", ssp.getSysCPUBusyPercent() + "%" );
data.put( "memory",
ssp.getSysMemInUse() + " Mb of " + ssp.getSysMemTotal() + " Mb total");
data.put( "freefds", ssp.getSysFDsFree() + " fds" );
} catch( Exception e ) {
data.put( "stats_error", "Error: Could not retrieve system status: " + e.toString() );
}
// Virtual Servers
try {
VirtualServerLocator vsl = new VirtualServerLocator();
vsl.setVirtualServerPortEndpointAddress( admin );
VirtualServerPort vsp = vsl.getVirtualServerPort();
String[] vsnames = vsp.getVirtualServerNames();
boolean[] vsenabled = vsp.getEnabled( vsnames );
UnsignedInt[] vsports = vsp.getPort( vsnames );
VirtualServerProtocol[] vsprotocols = vsp.getProtocol( vsnames );
boolean[] vssecure = vsp.getSSLDecrypt( vsnames );
String[] vs = new String[ vsnames.length ];
for( int i = 0; i < vsnames.length; i++ ){
vs[i] =
"\n " + vsnames[i] + " (" +
vsprotocols[i].toString() + ", port " + vsports[i] +
(vssecure[i]?", secure":"") +
(vsenabled[i]?", running":", stopped") +
")";
}
data.put( "vs", joinArray( vs ) );
} catch( Exception e ) {
data.put( "vs_error", "Error: Could not retrieve running virtual servers: " + e.toString() );
}
// System Diagnosis
try {
DiagnoseLocator dl = new DiagnoseLocator();
dl.setDiagnosePortEndpointAddress( admin );
DiagnosePort dp = dl.getDiagnosePort();
DiagnoseErrorInfo ei = dp.diagnoseSystem( DiagnoseErrLevel.ERR_WARN );
data.put( "deadzxtms", joinArray( ei.getNotReachableTrafficManagers() ) );
DiagnoseConfigError[] dce = ei.getConfigErrors();
String[] dces = new String[ dce.length ];
for( int i = 0; i < dce.length; i++ ) {
StringBuilder v = new StringBuilder();
v.append( "\n " );
v.append( dce[i].getFilename() );
if( !dce[i].getConfigKey().equals( "-" ) )
v.append( " (key: " ).append( dce[i].getConfigKey() ).append( ')' );
v.append( ": " ).append( dce[i].getDescription() );
dces[i] = v.toString();
}
data.put( "configerrors", joinArray( dces ) );
DiagnoseFlipperError[] dfe = ei.getFlipperErrors();
String[] dfes = new String[ dfe.length ];
for( int i = 0; i < dfe.length; i++ ) {
StringBuilder v = new StringBuilder();
v.append( "\n " );
v.append( dfe[i].getMachine() )
.append( " (" ).append( dfe[i].getIpAddress() ).append( "): ")
.append( dfe[i].getErrors().toString() );
dfes[i] = v.toString();
}
data.put( "flippererrors", joinArray( dfes ) );
DiagnoseFailedNode[] dfn = ei.getFailedNodes();
String[] dfns = new String[ dfn.length ];
for( int i = 0; i < dfn.length; i++ ) {
StringBuilder v = new StringBuilder();
v.append( "\n " );
v.append( dfn[i].getNode() ).append( " (" )
.append( dfn[i].getIpAddress() ).append( ':' ).append(dfn[i].getPort() )
.append( "): " ).append( dfn[i].getErrorMessage() );
dfns[i] = v.toString();
}
data.put( "nodeerrors", joinArray( dfns ) );
DiagnoseSystemStatus[] dss = ei.getSystemStatuses();
String[] dsss = new String[ dss.length ];
for( int i = 0; i < dss.length; i++ ) {
StringBuilder v = new StringBuilder();
v.append( "\n " );
v.append( dss[i].getComponent() ).append( ": " ).append( dss[i].getMessage() );
dsss[i] = v.toString();
}
data.put( "systemstatus", joinArray( dsss ) );
} catch( Exception e ) {
data.put( "diagnose_error", "Error: Could not retrieve system diagnosis: " + e.toString() );
}
// Connections list
try {
SystemConnectionsLocator scl = new SystemConnectionsLocator();
scl.setSystemConnectionsPortEndpointAddress( admin );
SystemConnectionsPort scp = scl.getSystemConnectionsPort();
SystemConnectionsConnection[] scc = scp.getAllConnections();
String[] sccs = new String[ scc.length ];
for( int i = 0; i < scc.length; i++ ) {
StringBuilder v = new StringBuilder();
v.append( "\n " );
Formatter f = new Formatter( v, Locale.US );
f.format( "%-21s -> %-12s -> %-21s: %4d bytes in, %5d out;",
scc[i].getFrom(),
scc[i].getVserver(),
scc[i].getTo().equals("")?"(no node)":scc[i].getTo(),
scc[i].getBytes_in(),
scc[i].getBytes_out() );
if( scc[i].getTime_server() < 0 ) {
f.format( " in progress" );
} else {
f.format( " %4d secs ago", scc[i].getTime_server() );
}
if( scc[i].getRequest() != null ) {
f.format( " (Request %s; returned status %s)",
scc[i].getRequest(),
scc[i].getCode().equals("")?"unknown":scc[i].getCode() );
}
sccs[i] = v.toString();
}
data.put( "connections", joinArray( sccs ) );
} catch( Exception e ) {
data.put( "connections_error", "Error: Could not retrieve connections list: " + e.toString() );
}
String name;
if( data.get( "name_error" ) != null ) {
name = data.get( "name_error" );
} else {
name =
"Hostname: " + data.get( "name" );
}
String status;
if( data.get( "stats_error" ) != null ) {
status = data.get( "stats_error" );
} else {
status =
"Software version: " + data.get( "version" ) + "\n" +
"IP Addresses: " + data.get( "ips" ) + "\n" +
"Software uptime: " + data.get( "uptime" ) + "\n" +
"CPU Utilization: " + data.get( "cpu" ) + "\n" +
"System Memory in use: " + data.get( "memory" ) + "\n" +
"Free file descriptors: " + data.get( "freefds" ) + "\n";
}
String vservers;
if( data.get( "vs_error" ) != null ) {
vservers = data.get( "vs_error" );
} else {
vservers =
"Virtual servers: " + data.get( "vs" ) + "\n";
}
String errors;
if( data.get( "diagnose_error" ) != null ) {
errors = data.get( "diagnose_error" );
} else {
errors =
"Unreachable ZXTMs: " + data.get( "deadzxtms" ) + "\n" +
"Configuration errors: " + data.get( "configerrors" ) + "\n" +
"Traffic IP errors: " + data.get( "flippererrors" ) + "\n" +
"Dead nodes: " + data.get( "nodeerrors" ) + "\n" +
"Status errors: " + data.get( "systemstatus" ) + "\n";
}
String connections;
if( data.get( "connections_error" ) != null ) {
connections = data.get( "connections_error" );
} else {
connections =
"Connections: " + data.get( "connections" ) + "\n";
}
StringBuilder ret = new StringBuilder();
Formatter f = new Formatter( ret, Locale.US );
f.format(
"System Name
\n" +
"\n%s\n
\n" +
"Statistics
\n" +
"\n%s\n
\n" +
"Virtual Servers
\n" +
"\n%s\n
\n" +
"System Errors
\n" +
"\n%s\n
\n" +
"Connections
\n" +
"\n%s\n
\n",
name, status, vservers, errors, connections );
return ret.toString();
}
/* The following code disables certificate checking.
* Use the Security.addProvider and Security.setProperty
* calls to enable it */
public static class MyProvider extends Provider {
private static final long serialVersionUID = 1L;
public MyProvider() {
super( "MyProvider", 1.0, "Trust certificates" );
put( "TrustManagerFactory.TrustAllCertificates",
MyTrustManagerFactory.class.getName() );
}
protected static class MyTrustManagerFactory
extends TrustManagerFactorySpi {
public MyTrustManagerFactory() {}
protected void engineInit( KeyStore keystore ) {}
protected void engineInit(
ManagerFactoryParameters mgrparams ) {}
protected TrustManager[] engineGetTrustManagers() {
return new TrustManager[] {
new MyX509TrustManager()
};
}
}
protected static class MyX509TrustManager
implements X509TrustManager {
public void checkClientTrusted(
X509Certificate[] chain, String authType) {}
public void checkServerTrusted(
X509Certificate[] chain, String authType) {}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
}
}
}