View Javadoc

1   /*
2    * $Id: Monitor.java,v 1.1 2006/06/16 14:50:06 kea Exp $
3    */
4   
5   package org.astrogrid.monitor;
6   
7   import java.io.IOException;
8   import java.io.InputStream;
9   import java.io.OutputStreamWriter;
10  import java.io.PrintWriter;
11  import java.net.MalformedURLException;
12  import java.net.URL;
13  import java.rmi.RemoteException;
14  import java.util.Enumeration;
15  import java.util.Vector;
16  import javax.xml.rpc.ServiceException;
17  import javax.xml.rpc.encoding.XMLType;
18  import org.apache.axis.client.Call;
19  import org.apache.axis.client.Service;
20  import org.astrogrid.status.ServiceStatus;
21  import org.astrogrid.util.TimeStamp;
22  
23  /***
24   * Servlet for monitoring the statuses of other services and displaying them
25   * in HTML
26   *
27   * @author mch
28   */
29  public class Monitor  {
30  
31     Vector datacenters = null;
32     Vector ceaServices = null;
33  
34     final static String ERR_COLOUR = "#FF0000";
35     final static String OK_COLOUR = "#00FF00";
36     final static String WARN_COLOUR = "#FFFF00";
37     
38     /*** Set up with default list of datacenters */
39     protected void initialise() {
40        datacenters = new Vector();
41        datacenters.add("http://grendel12.roe.ac.uk:8080/pal-6df");
42        datacenters.add("http://grendel12.roe.ac.uk:8080/pal-sec");
43        datacenters.add("http://grendel12.roe.ac.uk:8080/pal-vizier");
44        datacenters.add("http://astrogrid.roe.ac.uk:8080/pal-ssa");
45        datacenters.add("http://astrogrid.roe.ac.uk:8080/pal-usnob");
46        datacenters.add("http://astrogrid.roe.ac.uk:8080/pal-twomass");
47        datacenters.add("http://zhumulangma.star.le.ac.uk:8080/astrogrid-pal-SNAPSHOT");
48        datacenters.add("http://msslxy.mssl.ucl.ac.uk:8080/astrogrid-pal-fits-SNAPSHOT");
49        datacenters.add("http://ag01.ast.cam.ac.uk:8080/astrogrid-pal-Itn05_release");
50        datacenters.add("http://twmbarlwm.star.le.ac.uk:8888/astrogrid-pal-SNAPSHOT");
51        datacenters.add("http://twmbarlwm.star.le.ac.uk:8888/astrogrid-pal-fits-SNAPSHOT");
52        datacenters.add("http://twmbarlwm.star.le.ac.uk:8888/astrogrid-pal-cds-SNAPSHOT");
53        datacenters.add("http://twmbarlwm.star.le.ac.uk:8888/astrogrid-pal-sec-SNAPSHOT");
54           
55        ceaServices = new Vector();
56        ceaServices.add("http://grendel12.roe.ac.uk:8080/pal-6df");
57        ceaServices.add("http://grendel12.roe.ac.uk:8080/pal-sec");
58        ceaServices.add("http://grendel12.roe.ac.uk:8080/pal-vizier");
59        ceaServices.add("http://astrogrid.roe.ac.uk:8080/pal-ssa");
60        ceaServices.add("http://astrogrid.roe.ac.uk:8080/pal-usnob");
61        ceaServices.add("http://astrogrid.roe.ac.uk:8080/pal-twomass");
62        ceaServices.add("http://zhumulangma.star.le.ac.uk:8080/astrogrid-pal-SNAPSHOT");
63     }
64     
65     public Monitor() {
66        initialise();
67     }
68     
69     /***
70      * Finds out statuses of all services and makes an Html table to the given
71      * Writer.  Does it this way so we can flush as we write, in case some servers
72      * timeout, etc
73      */
74     public void writeHtmlTables(PrintWriter out)  throws IOException {
75  
76        out.print("<h3>Datacenters</h3>");
77        out.print("<table>"+
78                     "<tr>"+
79                     "<th>Endpoint</th><th>JSP</th><th>SOAP</th>"+
80                     "</tr>");
81        
82        Enumeration datacenter = datacenters.elements();
83        while (datacenter.hasMoreElements()) {
84           out.flush();
85           String endpoint = (String) datacenter.nextElement();
86           writeDatacenterStatusRow(endpoint, out);
87        }
88        out.print("</table>");
89        out.flush();
90     }
91     
92     
93     
94     protected void writeDatacenterStatusRow(String endpoint, PrintWriter out) {
95        out.println("<tr>");
96        out.println("<td><a href='"+endpoint+"'>"+endpoint+"</a></td>");
97        try {
98           //test JSP connection
99           TimeStamp timestamp = new TimeStamp();
100          URL url = new URL(endpoint+"/serverStatus.jsp");
101          InputStream in = url.openStream();
102          String bgcolor=OK_COLOUR;
103          if (timestamp.getSecsSince()>5) { bgcolor=WARN_COLOUR; }
104          out.println("<td bgcolor='"+bgcolor+"'>"+timestamp.getSecsSince()+"s</td>");
105       }
106       catch (MalformedURLException mue) {
107          out.println("<td bgcolor='"+ERR_COLOUR+"'>"+mue.getMessage()+"</td>");
108       }
109       catch (IOException ioe) {
110          out.println("<td bgcolor='"+ERR_COLOUR+"'>"+ioe.getMessage()+"</td>");
111       }
112          
113       //test SOAP connection
114       try {
115          TimeStamp timestamp = new TimeStamp();
116          ServiceStatus status = getDatacenterStatus(endpoint);
117          out.println("<td bgcolor='"+OK_COLOUR+"'>"+timestamp.getSecsSince()+"s</td>");
118       }
119       catch (ServiceException e) {
120          out.println("<td bgcolor='"+ERR_COLOUR+"'>"+e+"</td>");
121       }
122       catch (RemoteException e) {
123          out.println("<td bgcolor='"+ERR_COLOUR+"'>"+e+"</td>");
124       }
125          
126       out.println("</tr>\n");
127       
128    }
129    
130    public static ServiceStatus getDatacenterStatus(String endpoint) throws ServiceException, RemoteException {
131       Service  service = new Service();
132       Call     call    = (Call) service.createCall();
133       
134       call.setTargetEndpointAddress( endpoint+"/services/AxisDataService06");
135       call.setOperationName("getServiceStatus");
136       
137       //call.setReturnType( XMLType.XSD_STRING );
138       
139       Object response = call.invoke( new Object[] {  } );
140       
141       return null;
142    }
143    
144    public static String getSimpleDatacenterStatus(String endpoint) throws ServiceException, RemoteException {
145       Service  service = new Service();
146       Call     call    = (Call) service.createCall();
147       
148       call.setTargetEndpointAddress( endpoint+"/services/AxisDataService06");
149       call.setOperationName("getSimpleServiceStatus");
150       
151       call.setReturnType( XMLType.XSD_STRING );
152       
153       return (String) call.invoke( new Object[] {  } );
154    }
155    /*
156    public String getCeaStatus(String endpoint) {
157       Call call = getCall();
158       SOAPBodyElement sbeRequest =
159                                  new SOAPBodyElement();
160       sbeRequest.setName("submitQuery");
161       try {
162          Vector result = (Vector) call.invoke (new Object[] {sbeRequest});
163          SOAPBodyElement sbe = (SOAPBodyElement) result.get(0);
164          Document resultDoc = sbe.getAsDocument();
165       }catch(RemoteException re) {
166          log.debug(re);
167       } catch (Exception e) {
168          resultDoc = null;
169          e.printStackTrace();
170          log.debug(e);
171    }
172 
173    private Call getCall() {
174       log.debug("entered getCall()");
175       if(DEBUG_FLAG) log.info("entered getCall()");
176       Call _call = null;
177       try {
178          Service  service = new Service();
179          _call = (Call) service.createCall();
180          _call.setTargetEndpointAddress(this.endPoint);
181          _call.setSOAPActionURI("");
182          _call.setOperationStyle(org.apache.axis.enum.Style.MESSAGE);
183          _call.setOperationUse(org.apache.axis.enum.Use.LITERAL);
184          _call.setEncodingStyle(null);
185       } catch(ServiceException se) {
186          se.printStackTrace();
187          log.debug(se);
188          _call = null;
189       }finally {
190          log.debug("exiting getCall()");
191          if(DEBUG_FLAG) log.info("exiting getCall()");
192          return _call;
193       }
194    }
195     */
196    
197    /***
198     *
199     */
200    public static void main(String[] args) throws IOException {
201       Monitor m = new Monitor();
202       PrintWriter out =new PrintWriter(new OutputStreamWriter(System.out));
203       out.write("<html><body>");
204       m.writeHtmlTables(out);
205       out.write("</body></html>");
206    }
207 }
208