001    /*
002     * $RCSfile: MonaLisaClusterInfoFinder.java,v $
003     * JUnit based test
004     *
005     * Created on July 22, 2003, 4:16 PM
006     *
007     * This file is part of the STAR Scheduler.
008     * Copyright (c) 2002-2003 STAR Collaboration - Brookhaven National Laboratory
009     *
010     * STAR Scheduler is free software; you can redistribute it and/or modify
011     * it under the terms of the GNU General Public License as published by
012     * the Free Software Foundation; either version 2 of the License, or
013     * (at your option) any later version.
014     *
015     * STAR Scheduler is distributed in the hope that it will be useful,
016     * but WITHOUT ANY WARRANTY; without even the implied warranty of
017     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
018     * GNU General Public License for more details.
019     *
020     * You should have received a copy of the GNU General Public License
021     * along with STAR Scheduler; if not, write to the Free Software
022     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
023    
024    /** Retrieves cluster monitoring information from MonaLisa.
025     *
026     * @author Efstratios Efstathiadis
027     * @author Gabriele Carcassi
028     * @version $Revision: 1.5 $ $Date: 2006/04/26 15:31:02 $
029     */
030    
031    package gov.bnl.star.offline.scheduler.monitor;
032    
033    import java.util.ArrayList;
034    import java.util.Hashtable;
035    import java.util.List;
036    import java.util.Map;
037    import java.util.logging.Level;
038    import org.apache.log4j.Logger;
039    import lia.ws.*;
040    import ws.lia.*;
041    
042    public class MonaLisaClusterInfoFinder implements ClusterInfoFinder {
043        static private Logger log = Logger.getLogger(MonaLisaClusterInfoFinder.class.getName());
044        private Map clusterInfos = new Hashtable();
045        
046        
047        /** Creates a new instance of MonaLisaClient */
048        public MonaLisaClusterInfoFinder() {
049        }
050        
051        /**
052         * @param args the command line arguments
053         */
054        public static void main(String[] args) {
055            MonaLisaClusterInfoFinder finder = new MonaLisaClusterInfoFinder();
056            ClusterInfo info = finder.getClusterInfo("rcas.rcf.bnl.gov");
057            displayInfo(info);
058            info = finder.getClusterInfo("rcrs.rcf.bnl.gov");
059            displayInfo(info);
060        }
061        
062        private static void displayInfo(ClusterInfo  info) {
063            System.out.println("Cluster name: " + info.getClusterName());
064            System.out.println("Total load 1m: " + info.getLoad1m());
065            System.out.println("Total load 5m: " + info.getLoad5m());
066            System.out.println("Average load 1m: " + info.getAverageLoad1m());
067            System.out.println("Average load 5m: " + info.getAverageLoad5m());
068            System.out.println("CPU number: " + info.getCPUCount());
069        }
070        
071        public ClusterInfo getClusterInfo(String clusterName) {
072            if (clusterInfos.size() == 0) loadInformation();
073            return (ClusterInfo) clusterInfos.get(clusterName);
074        }
075        
076        private static Map getClusterNameMap() {
077            Map clusterNameMap = new Hashtable();
078            clusterNameMap.put("STAR_CAS_Linux_Cluster", "rcas.rcf.bnl.gov");
079            clusterNameMap.put("STAR_CRS_Linux_Cluster", "rcrs.rcf.bnl.gov");
080            return clusterNameMap;
081        }
082        
083        private void loadInformation() {
084            log.info("Loading load information from Monalisa");
085            try {
086                MLWebServiceService service = new ws.lia.MLWebServiceServiceLocator(); 
087                MLWebService port = service.getMLWebService();
088                Result[] result=port.getValues("Star", "CLUSTERS", "*", "*", -30000, 0);            
089                for (int i=0; i<result.length; i++) {
090                    MonaLisaClusterInfo clusterInfo = new MonaLisaClusterInfo();
091                    clusterInfo.setClusterName(result[i].getNodeName());
092                    for (int j=0; j<result[i].getParam_name().length; j++) {
093                        String paramName = result[i].getParam_name(j);
094                        double paramValue = result[i].getParam(j);
095                        if ("NCPUS".equals(paramName)) {
096                            clusterInfo.setCPUCount((int)paramValue);
097                        }
098                        if ("Load1".equals(paramName)) {
099                            clusterInfo.setLoad1m(paramValue);
100                        }
101                        if ("Load5".equals(paramName)) {
102                            clusterInfo.setLoad5m(paramValue);
103                        }
104                        if ("Average1".equals(paramName)) {
105                            clusterInfo.setAverageLoad1m(paramValue);
106                        }
107                        if ("Average5".equals(paramName)) {
108                            clusterInfo.setAverageLoad5m(paramValue);
109                        }
110                    }
111                    String clusterName = (String) getClusterNameMap().get(clusterInfo.getClusterName());
112                    if (clusterName == null) clusterName = clusterInfo.getClusterName();
113                    log.debug("Buffering monitoring information for cluster: " + clusterInfo.getClusterName());
114                    clusterInfos.put(clusterName, clusterInfo);
115                }
116            } catch (Exception e) {
117                log.debug("Couldn't load cluster information from Monalisa", e);
118            }
119        }
120        
121        private class MonaLisaClusterInfo implements ClusterInfo {
122            private double averageLoad1m;
123            private double averageLoad5m;
124            private int CPUCount;
125            private String clusterName;
126            private double load1m;
127            private double load5m;
128            
129            public double getAverageLoad1m() {
130                return averageLoad1m;
131            }
132            
133            public void setAverageLoad1m(double averageLoad1m) {
134                this.averageLoad1m = averageLoad1m;
135            }
136            
137            public double getAverageLoad5m() {
138                return averageLoad5m;
139            }
140            
141            public void setAverageLoad5m(double averageLoad5m) {
142                this.averageLoad5m = averageLoad5m;
143            }
144            
145            public int getCPUCount() {
146                return CPUCount;
147            }
148            
149            public void setCPUCount(int CPUCount) {
150                this.CPUCount = CPUCount;
151            }
152            
153            public String getClusterName() {
154                return clusterName;
155            }
156            
157            public void setClusterName(String clusterName) {
158                this.clusterName = clusterName;
159            }
160            
161            public double getLoad1m() {
162                return load1m;
163            }
164            
165            public void setLoad1m(double load1m) {
166                this.load1m = load1m;
167            }
168            
169            public double getLoad5m() {
170                return load5m;
171            }
172            
173            public void setLoad5m(double load5m) {
174                this.load5m = load5m;
175            }
176            
177        }
178        
179    }