001    /*
002     * MonaLisaPCQueueInfoFinder.java
003     *
004     * Created on September 10, 2004, 12:06 PM
005     *
006     * This file is part of the STAR Scheduler.
007     * Copyright (c) 2002-2006 STAR Collaboration - Brookhaven National Laboratory
008     *
009     * STAR Scheduler is free software; you can redistribute it and/or modify
010     * it under the terms of the GNU General Public License as published by
011     * the Free Software Foundation; either version 2 of the License, or
012     * (at your option) any later version.
013     *
014     * STAR Scheduler is distributed in the hope that it will be useful,
015     * but WITHOUT ANY WARRANTY; without even the implied warranty of
016     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
017     * GNU General Public License for more details.
018     *
019     * You should have received a copy of the GNU General Public License
020     * along with STAR Scheduler; if not, write to the Free Software
021     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
022     */
023    
024    package gov.bnl.star.offline.scheduler.monitor;
025    
026    import java.util.logging.Level;
027    import java.util.logging.Logger;
028    import java.io.*;
029    import java.net.*;
030    import java.util.*;
031    
032    /**
033     *
034     * @author  stratos
035     */
036    public class MonaLisaPCQueueInfoFinder implements QueueInfoFinder {
037        
038        static private Logger log = Logger.getLogger(MonaLisaPCQueueInfoFinder.class.getName());
039        //    myHashMap is where the loadInformation method
040        //    puts the monitoring information retrieved from the pseudo client
041        private Map myHashMap = new HashMap();
042        
043        /** Creates a new instance of MonaLisaPCQueueInfoFinder */
044        public MonaLisaPCQueueInfoFinder() {
045        }
046        
047        /**
048         * @param args the command line arguments
049         */
050        public static void main(String[] args) {
051            MonaLisaPCQueueInfoFinder finder = new MonaLisaPCQueueInfoFinder();
052            // Example of getting Queue Info from the default MonALISA pseudo client
053            // host=stargrid03.rcf.bnl.gov, port=6004)
054            QueueInfo info = finder.getQueueInfo("LSF_star_cas_short");
055            displayInfo(info);
056        }
057        
058        private static void displayInfo(QueueInfo  info) {
059            System.out.println("Queue Name: " + info.getQueueName());
060            System.out.println("Total Jobs: " + info.getTotalJobs());
061            System.out.println("Running Jobs: " + info.getRunningJobs());
062            System.out.println("Waiting Jobs: " + info.getWaitingJobs());
063            System.out.println("EstimatedResponseTime: " + info.getEstimatedResponseTime());
064            System.out.println("WorstResponseTime: " + info.getWorstResponseTime());
065            System.out.println("AverageRunTime: " + info.getAverageRunTime() );
066        }
067        
068        public java.util.Collection getQueueInfo() {
069            return null;
070        }
071        
072        public java.util.Collection getQueueInfo(java.util.Collection queueNames) {
073            return null;
074        }
075        
076        public QueueInfo getQueueInfo(String queueName) {
077            
078            //        default values for the QueueInfo object
079            double estimatedResponseTime = -1.;
080            double worstResponseTime = -1.;
081            int runningJobs = -1;
082            int waitingJobs = -1;
083            int totalJobs = -1;
084            int freeCPUs  = -1;
085            String status = null;
086            double averageRunTime = -1.;
087            
088            loadInformation();
089            MonaLisaQueueInfo queueInfo = new MonaLisaQueueInfo();
090            
091            queueInfo.setQueueName(queueName);
092            
093            if (myHashMap.get("Star/QUEUES/"+queueName+"/EstimatedResponseTime") != null) {
094                estimatedResponseTime = ((Double) myHashMap.get("Star/QUEUES/"+queueName+"/EstimatedResponseTime")).doubleValue();
095            }
096            
097            if (myHashMap.get("Star/QUEUES/"+queueName+"/WorstResponseTime") != null){
098                worstResponseTime = ( (Double) myHashMap.get("Star/QUEUES/"+queueName+"/WorstResponseTime")).doubleValue();
099            }
100            
101            if (myHashMap.get("Star/QUEUES/"+queueName+"/RunningJobs") != null){
102                runningJobs = (int) ( (Double) myHashMap.get("Star/QUEUES/"+queueName+"/RunningJobs")).doubleValue();
103            }
104            
105            if (myHashMap.get("Star/QUEUES/"+queueName+"/WaitingJobs") != null){
106                waitingJobs = (int) ( (Double) myHashMap.get("Star/QUEUES/"+queueName+"/WaitingJobs")).doubleValue();
107            }
108            
109            if (myHashMap.get("Star/QUEUES/"+queueName+"/TotalJobs") != null){
110                totalJobs = (int) ( (Double) myHashMap.get("Star/QUEUES/"+queueName+"/TotalJobs")).doubleValue();
111            }
112            
113                    
114            if (myHashMap.get("Star/QUEUES/"+queueName+"/AverageRunTime") != null){
115                averageRunTime = ( (Double) myHashMap.get("Star/QUEUES/"+queueName+"/AverageRunTime")).doubleValue();
116            }
117            queueInfo.setEstimatedResponseTime(estimatedResponseTime);
118            queueInfo.setWorstResponseTime(worstResponseTime);
119            queueInfo.setRunningJobs(runningJobs);
120            queueInfo.setWaitingJobs(waitingJobs);
121            queueInfo.setTotalJobs(totalJobs);
122            queueInfo.setFreeCPUs(freeCPUs);
123            queueInfo.setStatus(status);
124            queueInfo.setAverageRunTime(averageRunTime);
125            
126            return queueInfo;
127        }
128        
129        private void loadInformation() {
130            log.info("Loading information from default MonALISA Pseudo Client");
131            String HOST = "stargrid03.rcf.bnl.gov";
132            int PORT    = 9331;
133            Socket sock = null;
134            PrintWriter out = null;
135            BufferedReader in = null;
136            
137            String fromServer = null;
138            try {
139                sock = new Socket(HOST, PORT);
140                ObjectInputStream objIn = new ObjectInputStream(new BufferedInputStream(sock.getInputStream()));
141                myHashMap = (HashMap) objIn.readObject();
142    //            System.out.println(" size of Map: "+ myHashMap.size() );
143    //            Iterator keyIter = myHashMap.keySet().iterator();
144    //            while (keyIter.hasNext()) {
145    //                String key = (String) keyIter.next();
146    //                Double Value = (Double) myHashMap.get(key);
147    //                System.out.println( key+" : "+Value );
148    //            }
149                objIn.close();
150                sock.close();
151            } catch (UnknownHostException uhe) {
152                System.out.println("UnknowHostException: "+HOST);
153                System.exit(-1);
154            } catch (IOException ioe) {
155                System.out.println("IOException: "+ioe);
156                System.exit(-1);
157            } catch (ClassNotFoundException cnfe) {
158                System.out.println("Class Not Found Exception:"+cnfe.getMessage());
159                System.exit(-1);
160            }
161            
162        }
163    }