001    /*
002     * MonaLisaPCHostInfoFinder.java
003     *
004     * Created on September 15, 2004, 10:53 AM
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.Collection;
027    import java.util.HashMap;
028    import java.util.Map;
029    import java.util.logging.Level;
030    import java.util.logging.Logger;
031    import java.net.*;
032    import java.io.*;
033    
034    /**
035     * @author  stratos
036     */
037    public class MonaLisaPCHostInfoFinder {
038        
039        static private Logger log = Logger.getLogger(MonaLisaPCQueueInfoFinder.class.getName());
040        private Map myHashMap = new HashMap();
041        
042        /** Creates a new instance of MonaLisaPCHostInfoFinder */
043        public MonaLisaPCHostInfoFinder() {
044        }
045        
046        public static void main(String[] args) {
047            MonaLisaPCHostInfoFinder finder = new MonaLisaPCHostInfoFinder();
048            MonaLisaHostInfo info = finder.getHostInfo("rcas6134.rcf.bnl.gov");
049            displayInfo(info);
050        }
051        
052        public MonaLisaHostInfo getHostInfo(String hostName) {
053            double load1m = -1.;
054            double load5m = -1.;
055            double load15m = -1.;
056            double noCPUs = -1;
057            long MEM_free = -1;
058            
059            loadInformation();
060            MonaLisaHostInfo Info = new MonaLisaHostInfo();
061            
062            if (myHashMap.get("Star/PN/"+hostName+"/Load5") != null) {
063                load5m = ((Double) myHashMap.get("Star/PN/"+hostName+"/Load5")).doubleValue();
064            }
065            
066            if (myHashMap.get("Star/PN/"+hostName+"/Load1") != null) {
067                load1m = ((Double) myHashMap.get("Star/PN/"+hostName+"/Load1")).doubleValue();
068            }
069            
070            if (myHashMap.get("Star/PN/"+hostName+"/Load15") != null) {
071                load15m = ((Double) myHashMap.get("Star/PN/"+hostName+"/Load15")).doubleValue();
072            }
073            
074            if (myHashMap.get("Star/PN/"+hostName+"/NoCPUs") != null) {
075                noCPUs = ((Double) myHashMap.get("Star/PN/"+hostName+"/NoCPUs")).doubleValue();
076            }
077            
078            Info.setHostName(hostName);
079            Info.setLoad5m(load5m);
080            Info.setLoad1m(load1m);
081            Info.setLoad15m(load15m);
082            Info.setNoCPUs(noCPUs);
083            Info.setMEM_free(MEM_free);
084            
085            return Info;
086        }
087        
088        private static void displayInfo(MonaLisaHostInfo  info){
089            System.out.println("Hostname: "+info.getHostName() );
090            System.out.println("Load1m: "+info.getLoad1m() );
091            System.out.println("Load5m: "+info.getLoad5m() );
092            System.out.println("Load15m: "+info.getLoad15m() );
093            System.out.println("NoCPUs: "+info.getNoCPUs() );
094            System.out.println("MEM_free: "+info.getMEM_free() );
095        }
096        
097        private void loadInformation() {
098            log.info("Loading information from default MonALISA Pseudo Client");
099            String HOST = "stargrid03.rcf.bnl.gov";
100            int PORT    = 9331;
101            Socket sock = null;
102            PrintWriter out = null;
103            BufferedReader in = null;
104            
105            String fromServer = null;
106            try {
107                sock = new Socket(HOST, PORT);
108                ObjectInputStream objIn = new ObjectInputStream(new BufferedInputStream(sock.getInputStream()));
109                myHashMap = (HashMap) objIn.readObject();
110                //            System.out.println(" size of Map: "+ myHashMap.size() );
111                //            Iterator keyIter = myHashMap.keySet().iterator();
112                //            while (keyIter.hasNext()) {
113                //                String key = (String) keyIter.next();
114                //                Double Value = (Double) myHashMap.get(key);
115                //                System.out.println( key+" : "+Value );
116                //            }
117                objIn.close();
118                sock.close();
119            } catch (UnknownHostException uhe) {
120                System.out.println("UnknowHostException: "+HOST);
121                System.exit(-1);
122            } catch (IOException ioe) {
123                System.out.println("IOException: "+ioe);
124                System.exit(-1);
125            } catch (ClassNotFoundException cnfe) {
126                System.out.println("Class Not Found Exception:"+cnfe.getMessage());
127                System.exit(-1);
128            }
129            
130        }
131    }