001 /* 002 * MDSHostInfoFinder.java 003 * 004 * Created on May 12, 2003, 2:34 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 package gov.bnl.star.offline.scheduler.monitor; 024 025 import java.util.Hashtable; 026 import javax.naming.Context; 027 import javax.naming.NamingEnumeration; 028 import javax.naming.directory.DirContext; 029 import javax.naming.directory.InitialDirContext; 030 import javax.naming.directory.Attributes; 031 import javax.naming.directory.Attribute; 032 import javax.naming.directory.SearchResult; 033 034 import org.globus.mds.gsi.common.GSIMechanism; 035 036 /** 037 * This is an example to demonstrate how to use JNDI to connect to 038 * a secure LDAP server (MDS-2 server) using the GSI SASL mechanism. 039 * This is a pretty standard example for JNDI that could work 040 * with an arbitraty SASL mechanism. 041 * For more information about JNDI and SASL authentication see the 042 * <A HREF="http://java.sun.com/products/jndi/tutorial/ldap/security/sasl.html"> 043 * JNDI Tutorial</A> 044 */ 045 public class MDSHostInfoFinder { 046 047 private String host = null; 048 private String binddn = null; 049 private String baseDN = "cl=STAR_CAS_Linux_Cluster, mds-vo-name=local, o=grid"; 050 private String qop = "auth-conf, auth"; 051 private int port = 2135; 052 private int version = 3; 053 054 private InitialDirContext Dirctx; 055 056 public MDSHostInfoFinder(String host) { 057 this.host=host; 058 Dirctx = getDirContext(); 059 // String DN="host="+qhost+", "+baseDN; 060 // System.out.println(DN); 061 // MyprintAttrs(getHostInfoAttrs(DN)); 062 // HostInfo MI = getHostInfo(qhost); 063 } 064 065 public HostInfo getHostInfo(String host){ 066 MDSHostInfo MDSHI=new MDSHostInfo(); 067 String DN="host="+host+", "+baseDN; 068 // System.out.println(DN); 069 try { 070 Attributes attrs = getHostInfoAttrs(DN); 071 // String HostName = (attrs.get("GlueHostName").get()).toString() ; 072 MDSHI.setHostName( (attrs.get("GlueHostName").get()).toString() ); 073 074 // Load 075 076 MDSHI.setLoad1Min( Double.parseDouble( (attrs.get("GlueHostProcessorLoadLast1Min").get()).toString()) ); 077 MDSHI.setLoad5Min( Double.parseDouble( (attrs.get("GlueHostProcessorLoadLast5Min").get()).toString()) ); 078 MDSHI.setLoad15Min( Double.parseDouble( (attrs.get("GlueHostProcessorLoadLast15Min").get()).toString()) ); 079 080 // SMP Load 081 082 MDSHI.setLoadSMP1Min( Double.parseDouble( (attrs.get("GlueHostSMPLoadLast1Min").get()).toString()) ); 083 MDSHI.setLoadSMP5Min( Double.parseDouble( (attrs.get("GlueHostSMPLoadLast5Min").get()).toString()) ); 084 MDSHI.setLoadSMP15Min( Double.parseDouble( (attrs.get("GlueHostSMPLoadLast15Min").get()).toString()) ); 085 086 // Memory 087 088 MDSHI.setMemorySize( Double.parseDouble( (attrs.get("GlueHostMainMemoryRAMSize").get()).toString()) ); 089 MDSHI.setMemoryAvailable( Double.parseDouble( (attrs.get("GlueHostMainMemoryRAMAvailable").get()).toString()) ); 090 091 // Processor 092 MDSHI.setProcessorCount( Integer.parseInt( (attrs.get("GlueHostArchitectureSMPSize").get()).toString()) ); 093 MDSHI.setProcessorSpeed( Double.parseDouble( (attrs.get("GlueHostProcessorClockSpeed").get()).toString()) ); 094 095 // Network Adapter 096 MDSHI.setIPAddress( (attrs.get("GlueHostNetworkAdapterIPAddress").get()).toString() ); 097 MDSHI.setAdapterName( (attrs.get("GlueHostNetworkAdapterName").get()).toString() ); 098 099 // Host Unique ID 100 MDSHI.setUniqueID( (attrs.get("GlueHostUniqueID").get()).toString() ); 101 102 103 } catch (Exception e) { 104 System.out.println(" No Such Element Found Exception."); 105 e.printStackTrace(); 106 } 107 return MDSHI; 108 } 109 110 private Attributes getHostInfoAttrs (String DN){ 111 Attributes ats = null; 112 try { 113 ats = Dirctx.getAttributes(DN); 114 } catch (Exception e) { 115 System.err.println("getHostInfo failed: " + e.getMessage()); 116 e.printStackTrace(); 117 } finally { 118 if (Dirctx != null) { 119 try { Dirctx.close(); } catch(Exception e) {} 120 } 121 } 122 return ats; 123 } 124 125 private InitialDirContext getDirContext() { 126 Hashtable env = new Hashtable(); 127 env.put(Context.INITIAL_CONTEXT_FACTORY, 128 "com.sun.jndi.ldap.LdapCtxFactory"); 129 130 env.put("java.naming.ldap.version", String.valueOf(version)); 131 132 /* Specify host and port to use for directory service */ 133 env.put(Context.PROVIDER_URL, "ldap://"+ host + ":" + port); 134 135 /* Specify the particular SASL mechanism to use 136 * Use GSIMechanism.NAME for the GSI SASL mechanism. 137 */ 138 env.put(Context.SECURITY_AUTHENTICATION, GSIMechanism.NAME); 139 140 /* This property specifies where the implementation of 141 * the GSI SASL mechanism for JNDI can be found. 142 */ 143 env.put("javax.security.sasl.client.pkgs", 144 "org.globus.mds.gsi.jndi"); 145 146 /* This property specifies the quality of protection 147 * value. It can be a comma separated list of protection 148 * values in preference order. There are three possible 149 * qop values: 150 * "auth" - authentication only, 151 * "auth-int" - authentication with integrity protection 152 * (GSI without encryption) 153 * "auth-conf" - authentication with integrity and privacy 154 * protections. (GSI with encryption) 155 * If not specified, defaults to "auth" 156 */ 157 env.put("javax.security.sasl.qop", qop); 158 159 /* This property can be used to pass a specific 160 * set of credentials for the GSI SASL mechanism 161 * to use. It must be a GSSCredential object. 162 * If not set, the defaut credential will be 163 * used. 164 */ 165 166 if (binddn != null) { 167 // perform authentication as a user. 168 env.put(Context.SECURITY_PRINCIPAL, binddn); 169 } 170 171 InitialDirContext ctx = null; 172 173 try { 174 ctx = new InitialDirContext(env); 175 } catch(Exception e) { 176 System.err.println("Unable to get InitialDirContext ctx: " + e.getMessage()); 177 e.printStackTrace(); 178 } 179 180 return ctx; 181 } 182 183 184 /* This method is here for Debugging purposes. 185 * For a given Attribute set it prints all its 186 * attributes and all their values. 187 */ 188 static void MyprintAttrs (Attributes attrs) { 189 Attribute attr=null; 190 if (attrs == null) { 191 System.out.println("No attributes"); 192 } else { 193 /* Print each attribute */ 194 try { 195 for (NamingEnumeration ae = attrs.getAll(); ae.hasMore();) { 196 attr = (Attribute)ae.next(); 197 System.out.print(attr.getID()+": "); 198 /* print each value */ 199 for (NamingEnumeration e = attr.getAll(); 200 e.hasMore(); 201 System.out.print(e.next()+" ") ) 202 ; 203 System.out.println(); 204 } 205 } catch (Exception e) { 206 System.out.println(" NoSuchElementException for Attribute: "+attr.getID()); 207 e.printStackTrace(); 208 } 209 } 210 } 211 212 }