001    /*
002     * MonaLisaQueueInfoFinder.java
003     *
004     * Created on July 21, 2004, 9:02 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.Hashtable;
027    import java.util.Map;
028    import java.util.logging.Level;
029    import org.apache.log4j.Logger;
030    import lia.ws.*;
031    import ws.lia.*;
032    
033    /**
034     *
035     * @author  stratos
036     */
037    public class MonaLisaQueueInfoFinder implements QueueInfoFinder {
038        static private Logger log = Logger.getLogger(MonaLisaClusterInfoFinder.class.getName());
039        private Map queueInfos = new Hashtable();
040        
041        /** Creates a new instance of MonaLisaQueueInfoFinder */
042        public MonaLisaQueueInfoFinder() {
043        }
044        
045        /**
046         * @param args the command line arguments
047         */
048        public static void main(String[] args) {
049            MonaLisaQueueInfoFinder finder = new MonaLisaQueueInfoFinder();
050    // Example of getting Queue Info from the default Web Service (http://stargrid03.rcf.bnl.gov:6004)
051            QueueInfo info = finder.getQueueInfo("LSF_star_cas_dd");
052            displayInfo(info);
053    // Example of getting Queue Info from a specific Web Service
054            System.out.println("Example of getting Queue Info from a specific Web Service");
055    //        String MLWebService_address = "http://stargrid03.rcf.bnl.gov:6004/axis/services/MLWebService";
056            String MLWebService_address ="http://monalisa-starlight.cern.ch:8888/axis/services/MLWebService";
057            info = finder.getQueueInfofromWS("LSF_star_cas_short",MLWebService_address);
058            displayInfo(info);
059    // Example of getting Queue Info from a Set of Web Services
060    //  .... under Construction .... use default for now...
061            String[] MLWebService_addresses = {
062                "http://stargrid03.rcf.bnl.gov:6004/axis/services/MLWebService",
063                "http://monalisa-starlight.cern.ch:8888/axis/services/MLWebService"
064            };
065            info = finder.getQueueInfo("LSF_star_cas_big");
066            displayInfo(info);
067        }
068        
069        public java.util.Collection getQueueInfo() {
070            return null;
071        }
072        
073        // returns QueueInfo for a specific queue from a
074        // specific Web Service
075        public QueueInfo loadInformationFromWS(String WS){
076            log.info("Loading information from Monalisa");
077            try {
078                MLWebServiceService service = new ws.lia.MLWebServiceServiceLocator();
079            //    service.setMLWebServiceAddress(WS);
080                log.debug("Using Web Service: "+service.getMLWebService());
081                MLWebService port = service.getMLWebService();
082                Result[] result=port.getValues("Star", "QUEUES", "*", "*", -90000, 0);
083                for (int i=0; i<result.length; i++) {
084                    MonaLisaQueueInfo queueInfo = new MonaLisaQueueInfo();
085                    queueInfo.setQueueName(result[i].getNodeName());
086                    for (int j=0; j<result[i].getParam_name().length; j++) {
087                        String paramName = result[i].getParam_name(j);
088                        double paramValue = result[i].getParam(j);
089                        if ("TotalJobs".equals(paramName)) {
090                            queueInfo.setTotalJobs((int)paramValue);
091                        }
092                        if ("RunningJobs".equals(paramName)) {
093                            queueInfo.setRunningJobs((int)paramValue);
094                        }
095                        if ("WaitingJobs".equals(paramName)) {
096                            queueInfo.setWaitingJobs((int)paramValue);
097                        }
098                        if ("FreeCPUS".equals(paramName)) {
099                            queueInfo.setFreeCPUs((int)paramValue);
100                        }
101                        if ("WorstResponseTime".equals(paramName)) {
102                            queueInfo.setWorstResponseTime((double)paramValue);
103                        }
104                        if ("EstimatedResponseTime".equals(paramName)) {
105                            queueInfo.setEstimatedResponseTime((double)paramValue);
106                        }
107                        //                    if ("Status".equals(paramName)) {
108                        //                        queueInfo.setStatus(paramValue);
109                        //                    }
110                        
111                    }
112                    String queueName = queueInfo.getQueueName();
113                    if (queueName == null) queueName = queueInfo.getQueueName();
114                    log.info("Buffering monitoring information for queue: " + queueInfo.getQueueName());
115                    queueInfos.put(queueName, queueInfo);
116                }
117            } catch (Exception e) {
118                log.error("Couldn't load queue information from Monalisa", e);
119            }
120            return null;
121        }
122        
123        
124        // returns QueueInfo for a specific queue from a set of WSs
125        public QueueInfo getQueueInfofromWS(String[] WS) {
126            return null;
127        }
128        
129        public QueueInfo getQueueInfo(String queueName) {
130            // All the Result objects returned from the Web Service
131            // get into the Hashtable queueInfos.
132            // We return only the Hashtable element for the queueName.
133            if (queueInfos.size() == 0) loadInformation();
134            return (QueueInfo) queueInfos.get(queueName);
135        }
136        
137        public QueueInfo getQueueInfofromWS(String queueName, String WS) {
138            if (queueInfos.size() == 0) loadInformationFromWS(WS);
139            return (QueueInfo) queueInfos.get(queueName);
140        }
141        public java.util.Collection getQueueInfo(java.util.Collection queueNames) {
142            return null;
143        }
144        
145        
146        private static void displayInfo(QueueInfo  info) {
147            System.out.println("Queue Name: " + info.getQueueName());
148            System.out.println("Total Jobs: " + info.getTotalJobs());
149            System.out.println("Running Jobs: " + info.getRunningJobs());
150            System.out.println("Waiting Jobs: " + info.getWaitingJobs());
151            System.out.println("EstimatedResponseTime: " + info.getEstimatedResponseTime());
152            System.out.println("WorstResponseTime: " + info.getWorstResponseTime());
153        }
154        
155        private void loadInformation() {
156            log.info("Loading information from Monalisa");
157            try {
158                MLWebServiceService service = new ws.lia.MLWebServiceServiceLocator();
159                MLWebService port = service.getMLWebService();
160                Result[] result=port.getValues("Star", "QUEUES", "*", "*", -90000, 0);
161                for (int i=0; i<result.length; i++) {
162                    MonaLisaQueueInfo queueInfo = new MonaLisaQueueInfo();
163                    queueInfo.setQueueName(result[i].getNodeName());
164                    for (int j=0; j<result[i].getParam_name().length; j++) {
165                        String paramName = result[i].getParam_name(j);
166                        double paramValue = result[i].getParam(j);
167                        if ("TotalJobs".equals(paramName)) {
168                            queueInfo.setTotalJobs((int)paramValue);
169                        }
170                        if ("RunningJobs".equals(paramName)) {
171                            queueInfo.setRunningJobs((int)paramValue);
172                        }
173                        if ("WaitingJobs".equals(paramName)) {
174                            queueInfo.setWaitingJobs((int)paramValue);
175                        }
176                        if ("FreeCPUS".equals(paramName)) {
177                            queueInfo.setFreeCPUs((int)paramValue);
178                        }
179                        if ("WorstResponseTime".equals(paramName)) {
180                            queueInfo.setWorstResponseTime((double)paramValue);
181                        }
182                        if ("EstimatedResponseTime".equals(paramName)) {
183                            queueInfo.setEstimatedResponseTime((double)paramValue);
184                        }
185                        //                    if ("Status".equals(paramName)) {
186                        //                        queueInfo.setStatus(paramValue);
187                        //                    }
188                        
189                    }
190                    String queueName = queueInfo.getQueueName();
191                    if (queueName == null) queueName = queueInfo.getQueueName();
192                    log.debug("Buffering monitoring information for queue: " + queueInfo.getQueueName());
193                    queueInfos.put(queueName, queueInfo);
194                }
195            } catch (Exception e) {
196                log.error("Couldn't load queue information from Monalisa", e);
197            }
198        }
199        
200    }