2020-10-20 16:25:30 +02:00
package org.gcube.smartgears.handler.resourceregistry.resourcemanager ;
import java.io.BufferedReader ;
import java.io.File ;
import java.io.FileReader ;
import java.io.IOException ;
import java.lang.management.ManagementFactory ;
import java.lang.management.OperatingSystemMXBean ;
import java.net.InetAddress ;
import java.net.UnknownHostException ;
import java.nio.file.FileStore ;
import java.nio.file.Files ;
import java.nio.file.Paths ;
import java.util.ArrayList ;
2021-08-04 15:12:58 +02:00
import java.util.Calendar ;
2022-06-09 19:20:10 +02:00
import java.util.Date ;
2020-10-20 16:25:30 +02:00
import java.util.HashMap ;
2022-03-11 11:03:43 +01:00
import java.util.Iterator ;
2020-10-20 16:25:30 +02:00
import java.util.List ;
import java.util.Map ;
2022-03-10 18:14:25 +01:00
import java.util.Set ;
2022-03-11 11:03:43 +01:00
import java.util.TreeSet ;
2020-10-20 16:25:30 +02:00
import java.util.UUID ;
import java.util.regex.Matcher ;
import java.util.regex.Pattern ;
import javax.management.AttributeNotFoundException ;
import javax.management.InstanceNotFoundException ;
import javax.management.MBeanException ;
import javax.management.MBeanServer ;
import javax.management.MalformedObjectNameException ;
import javax.management.ObjectName ;
import javax.management.ReflectionException ;
2022-03-11 11:03:43 +01:00
import org.gcube.com.fasterxml.jackson.databind.JsonNode ;
2022-06-09 16:07:22 +02:00
import org.gcube.common.authorization.library.provider.SecurityTokenProvider ;
2020-10-20 16:25:30 +02:00
import org.gcube.informationsystem.model.reference.entities.Facet ;
import org.gcube.informationsystem.model.reference.entities.Resource ;
import org.gcube.informationsystem.model.reference.relations.ConsistsOf ;
import org.gcube.informationsystem.resourceregistry.api.exceptions.AvailableInAnotherContextException ;
import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException ;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException ;
2022-02-01 16:47:02 +01:00
import org.gcube.informationsystem.resourceregistry.api.exceptions.contexts.ContextNotFoundException ;
2021-10-25 11:00:27 +02:00
import org.gcube.informationsystem.resourceregistry.api.exceptions.entities.resource.ResourceNotFoundException ;
2020-10-22 12:10:37 +02:00
import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClient ;
import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClientFactory ;
2020-10-20 16:25:30 +02:00
import org.gcube.informationsystem.resourceregistry.publisher.ResourceRegistryPublisher ;
import org.gcube.informationsystem.resourceregistry.publisher.ResourceRegistryPublisherFactory ;
2023-02-10 14:03:27 +01:00
import org.gcube.informationsystem.serialization.ElementMapper ;
2020-10-20 16:25:30 +02:00
import org.gcube.resourcemanagement.model.impl.entities.facets.CPUFacetImpl ;
2021-08-04 15:12:58 +02:00
import org.gcube.resourcemanagement.model.impl.entities.facets.EventFacetImpl ;
2020-10-22 12:10:37 +02:00
import org.gcube.resourcemanagement.model.impl.entities.facets.LocationFacetImpl ;
2020-10-20 16:25:30 +02:00
import org.gcube.resourcemanagement.model.impl.entities.facets.MemoryFacetImpl ;
import org.gcube.resourcemanagement.model.impl.entities.facets.NetworkingFacetImpl ;
import org.gcube.resourcemanagement.model.impl.entities.facets.SimplePropertyFacetImpl ;
import org.gcube.resourcemanagement.model.impl.entities.facets.SoftwareFacetImpl ;
2020-12-21 23:10:14 +01:00
import org.gcube.resourcemanagement.model.impl.entities.facets.StateFacetImpl ;
2020-10-20 16:25:30 +02:00
import org.gcube.resourcemanagement.model.impl.entities.resources.HostingNodeImpl ;
import org.gcube.resourcemanagement.model.impl.relations.consistsof.HasPersistentMemoryImpl ;
import org.gcube.resourcemanagement.model.impl.relations.consistsof.HasVolatileMemoryImpl ;
import org.gcube.resourcemanagement.model.impl.relations.consistsof.IsIdentifiedByImpl ;
import org.gcube.resourcemanagement.model.reference.entities.facets.CPUFacet ;
2021-08-04 15:12:58 +02:00
import org.gcube.resourcemanagement.model.reference.entities.facets.EventFacet ;
2020-10-22 12:10:37 +02:00
import org.gcube.resourcemanagement.model.reference.entities.facets.LocationFacet ;
2020-10-20 16:25:30 +02:00
import org.gcube.resourcemanagement.model.reference.entities.facets.MemoryFacet ;
import org.gcube.resourcemanagement.model.reference.entities.facets.MemoryFacet.MemoryUnit ;
import org.gcube.resourcemanagement.model.reference.entities.facets.NetworkingFacet ;
import org.gcube.resourcemanagement.model.reference.entities.facets.SimplePropertyFacet ;
import org.gcube.resourcemanagement.model.reference.entities.facets.SoftwareFacet ;
2020-12-21 23:10:14 +01:00
import org.gcube.resourcemanagement.model.reference.entities.facets.StateFacet ;
2020-10-20 16:25:30 +02:00
import org.gcube.resourcemanagement.model.reference.entities.resources.HostingNode ;
import org.gcube.resourcemanagement.model.reference.relations.consistsof.HasPersistentMemory ;
import org.gcube.resourcemanagement.model.reference.relations.consistsof.HasVolatileMemory ;
import org.gcube.resourcemanagement.model.reference.relations.consistsof.IsIdentifiedBy ;
import org.gcube.smartgears.configuration.container.ContainerConfiguration ;
2020-10-22 12:10:37 +02:00
import org.gcube.smartgears.configuration.container.Site ;
2020-10-20 16:25:30 +02:00
import org.gcube.smartgears.configuration.library.SmartGearsConfiguration ;
import org.gcube.smartgears.context.container.ContainerContext ;
2022-05-24 11:13:39 +02:00
import org.gcube.smartgears.handler.resourceregistry.ContextUtility ;
2020-10-20 16:25:30 +02:00
import org.gcube.smartgears.provider.ProviderFactory ;
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
2022-02-07 11:39:51 +01:00
/ * *
* @author Luca Frosini ( ISTI - CNR )
* /
2020-10-20 16:25:30 +02:00
public class HostingNodeManager {
2020-10-22 12:10:37 +02:00
2020-10-20 16:25:30 +02:00
private static Logger logger = LoggerFactory . getLogger ( HostingNodeManager . class ) ;
2020-10-22 12:10:37 +02:00
2020-10-20 16:25:30 +02:00
public static final String MEMORY_TYPE = " memoryType " ;
public static final String MEMORY_TYPE_RAM = " RAM " ;
public static final String MEMORY_TYPE_JVM = " JVM " ;
public static final String JVM_MAX_MEMORY = " jvmMaxMemory " ;
public static final String MESSAGE = " message " ;
2020-10-22 12:10:37 +02:00
private ContainerContext containerContext ;
2020-10-20 16:25:30 +02:00
private ResourceRegistryPublisher resourceRegistryPublisher ;
private HostingNode hostingNode ;
2020-10-22 12:10:37 +02:00
public HostingNodeManager ( ContainerContext containerContext ) {
this . containerContext = containerContext ;
2020-10-20 16:25:30 +02:00
this . resourceRegistryPublisher = ResourceRegistryPublisherFactory . create ( ) ;
}
2022-03-10 18:14:25 +01:00
2020-10-20 16:25:30 +02:00
public HostingNode getHostingNode ( ) {
return hostingNode ;
}
2020-10-22 12:10:37 +02:00
2022-06-08 17:42:47 +02:00
public void addToCurrentContext ( ) throws ResourceNotFoundException , ContextNotFoundException , ResourceRegistryException {
2022-06-09 16:07:22 +02:00
String currentToken = SecurityTokenProvider . instance . get ( ) ;
UUID contextUUID = ContextUtility . getContextUUID ( currentToken ) ;
/* Trying to get a context which is not the current to properly invoke the addToContext without forcing the operation using addResourceToCurrentContext */
boolean anotherContextSet = false ;
List < String > startTokens = containerContext . configuration ( ) . startTokens ( ) ;
for ( String token : startTokens ) {
UUID anotherContextUUID = ContextUtility . getContextUUID ( token ) ;
if ( anotherContextUUID . compareTo ( contextUUID ) ! = 0 ) {
ContextUtility . setContextFromToken ( token ) ;
anotherContextSet = true ;
break ;
}
}
2022-05-24 12:20:55 +02:00
UUID uuid = UUID . fromString ( containerContext . id ( ) ) ;
2021-07-07 10:20:32 +02:00
try {
2022-06-09 16:07:22 +02:00
if ( anotherContextSet ) {
resourceRegistryPublisher . addToContext ( HostingNode . NAME , uuid , contextUUID , false ) ;
} else {
resourceRegistryPublisher . addResourceToCurrentContext ( HostingNode . NAME , uuid , false ) ;
}
2022-06-09 19:20:10 +02:00
logger . info ( " {} with UUID {} successfully added to context ({}) " , HostingNode . NAME , uuid , ContextUtility . getCurrentContextName ( ) ) ;
2021-07-07 10:20:32 +02:00
} catch ( Exception e ) {
2022-06-09 19:20:10 +02:00
logger . error ( " Unable to add {} with UUID {} to context ({}) " , HostingNode . NAME , uuid , ContextUtility . getCurrentContextName ( ) , e ) ;
2020-10-20 16:25:30 +02:00
}
}
2022-06-08 17:42:47 +02:00
public void addToContext ( UUID contextUUID ) throws ResourceNotFoundException , ContextNotFoundException , ResourceRegistryException {
UUID uuid = UUID . fromString ( containerContext . id ( ) ) ;
try {
resourceRegistryPublisher . addToContext ( HostingNode . NAME , uuid , contextUUID , false ) ;
2022-06-29 15:40:04 +02:00
logger . info ( " {} with UUID {} successfully added to context ({}) " , HostingNode . NAME , uuid , ContextUtility . getContextNameFromUUID ( contextUUID ) ) ;
2022-06-08 17:42:47 +02:00
} catch ( Exception e ) {
2022-06-29 15:40:04 +02:00
logger . error ( " Unable to add {} with UUID {} to context ({}) " , HostingNode . NAME , uuid , ContextUtility . getContextNameFromUUID ( contextUUID ) , e ) ;
2022-06-08 17:42:47 +02:00
}
}
2020-10-22 12:10:37 +02:00
2022-06-09 16:07:22 +02:00
public void removeFromCurrentContext ( )
2020-10-22 12:10:37 +02:00
throws ResourceNotFoundException , ContextNotFoundException , ResourceRegistryException {
2022-05-24 12:20:55 +02:00
UUID uuid = UUID . fromString ( containerContext . id ( ) ) ;
2021-07-07 10:20:32 +02:00
try {
2022-05-24 12:20:55 +02:00
resourceRegistryPublisher . removeResourceFromCurrentContext ( HostingNode . NAME , uuid , false ) ;
2022-06-09 19:20:10 +02:00
logger . info ( " {} with UUID {} successfully removed from context ({}) " , HostingNode . NAME , uuid ,
2022-05-24 11:13:39 +02:00
ContextUtility . getCurrentContextName ( ) ) ;
2021-07-07 10:20:32 +02:00
} catch ( Exception e ) {
2022-06-09 19:20:10 +02:00
logger . error ( " Unable to remove {} with UUID {} from context ({}) " , HostingNode . NAME , uuid , ContextUtility . getCurrentContextName ( ) , e ) ;
2020-10-20 16:25:30 +02:00
}
}
2020-11-11 14:19:58 +01:00
public void removeFromContext ( UUID contextUUID )
throws ResourceNotFoundException , ContextNotFoundException , ResourceRegistryException {
2022-06-29 15:40:04 +02:00
String contextFullName = ContextUtility . getContextNameFromUUID ( contextUUID ) ;
2022-05-24 12:20:55 +02:00
UUID uuid = UUID . fromString ( containerContext . id ( ) ) ;
2021-07-07 10:20:32 +02:00
try {
2022-05-24 12:20:55 +02:00
resourceRegistryPublisher . removeResourceFromContext ( HostingNode . NAME , uuid , contextUUID , false ) ;
logger . info ( " {} with UUID {} successfully removed from context ({}) " , HostingNode . NAME , uuid , contextFullName ) ;
2021-07-07 10:20:32 +02:00
} catch ( Exception e ) {
2022-06-09 19:20:10 +02:00
logger . error ( " Unable to remove {} from context ({}) " , HostingNode . NAME , uuid , contextFullName , e ) ;
2020-11-11 14:19:58 +01:00
}
}
2020-10-22 12:10:37 +02:00
public HostingNode updateFacets ( ) throws ResourceRegistryException {
logger . debug ( " Updating {} " , HostingNode . NAME ) ;
2020-10-20 16:25:30 +02:00
MemoryFacet ramFacet = null ;
MemoryFacet jvmMemoryFacet = null ;
MemoryFacet disk = null ;
2022-06-09 19:20:10 +02:00
StateFacet stateFacet = null ;
EventFacet eventFacet = null ;
2020-10-20 16:25:30 +02:00
2022-06-09 19:20:10 +02:00
Date date = Calendar . getInstance ( ) . getTime ( ) ;
2020-10-20 16:25:30 +02:00
List < ConsistsOf < ? extends Resource , ? extends Facet > > consistsOfToRemove = new ArrayList < > ( ) ;
List < ConsistsOf < ? extends Resource , ? extends Facet > > consistsOfList = hostingNode . getConsistsOf ( ) ;
for ( ConsistsOf < ? extends Resource , ? extends Facet > c : consistsOfList ) {
2020-12-21 23:10:14 +01:00
if ( c . getTarget ( ) instanceof StateFacet ) {
stateFacet = ( StateFacet ) c . getTarget ( ) ;
2022-06-09 19:20:10 +02:00
stateFacet = getStateFacet ( stateFacet , date ) ;
2020-10-20 16:25:30 +02:00
continue ;
}
if ( c instanceof HasVolatileMemory ) {
String memoryType = ( String ) c . getAdditionalProperty ( MEMORY_TYPE ) ;
if ( memoryType . compareTo ( MEMORY_TYPE_RAM ) = = 0 ) {
ramFacet = ( MemoryFacet ) c . getTarget ( ) ;
ramFacet = getRamInfo ( ramFacet ) ;
continue ;
}
if ( memoryType . compareTo ( MEMORY_TYPE_JVM ) = = 0 ) {
jvmMemoryFacet = ( MemoryFacet ) c . getTarget ( ) ;
jvmMemoryFacet = getJVMMemoryInfo ( jvmMemoryFacet ) ;
continue ;
}
}
if ( c instanceof HasPersistentMemory ) {
disk = ( MemoryFacet ) c . getTarget ( ) ;
2020-10-22 12:10:37 +02:00
disk = getDiskSpace ( disk ) ;
2020-10-20 16:25:30 +02:00
continue ;
}
2021-08-04 15:12:58 +02:00
if ( c . getTarget ( ) instanceof EventFacet ) {
2023-05-02 17:58:03 +02:00
EventFacet foundEventFacet = ( EventFacet ) c . getTarget ( ) ;
String value = foundEventFacet . getEvent ( ) ;
2022-06-09 19:20:10 +02:00
if ( value . compareTo ( getState ( ) ) = = 0 ) {
2023-05-02 17:58:03 +02:00
// This facet must be updated (the date must be updated) so it must not be removed from update
foundEventFacet . setDate ( date ) ;
eventFacet = foundEventFacet ;
2021-08-04 15:12:58 +02:00
continue ;
}
}
2020-10-22 12:10:37 +02:00
consistsOfToRemove . add ( c ) ;
2022-06-09 19:20:10 +02:00
2020-10-22 12:10:37 +02:00
}
2022-06-09 19:20:10 +02:00
// Resource Update has effect only on specified facets.
// Removing the ones that have not to be changed.
2020-10-22 12:10:37 +02:00
consistsOfList . removeAll ( consistsOfToRemove ) ;
2022-06-09 19:20:10 +02:00
2023-05-02 17:58:03 +02:00
/* The eventFacet for the event does not exists. Going to create it */
2021-08-04 15:12:58 +02:00
if ( eventFacet = = null ) {
2022-06-09 19:20:10 +02:00
eventFacet = getEventFacet ( date ) ;
2021-08-04 15:12:58 +02:00
hostingNode . addFacet ( eventFacet ) ;
}
2022-06-09 19:20:10 +02:00
2020-10-22 12:10:37 +02:00
try {
hostingNode = resourceRegistryPublisher . updateResource ( hostingNode ) ;
} catch ( ResourceRegistryException e ) {
logger . error ( " error trying to publish hosting node " , e ) ;
}
2022-06-09 19:20:10 +02:00
2020-10-22 12:10:37 +02:00
return hostingNode ;
}
2022-03-10 18:14:25 +01:00
public static SoftwareFacet getOperativeSystem ( ) {
SoftwareFacet osSoftwareFacet = new SoftwareFacetImpl ( ) ;
OperatingSystemMXBean mxbean = ManagementFactory . getOperatingSystemMXBean ( ) ;
2022-03-11 11:03:43 +01:00
String osName = mxbean . getName ( ) ;
osSoftwareFacet . setGroup ( osName ) ; // softwareFacet.setGroup(System.getProperty("os.name"));
2022-03-10 18:14:25 +01:00
osSoftwareFacet . setName ( mxbean . getArch ( ) ) ; // softwareFacet.setName(System.getProperty("os.arch"));
2022-03-11 11:03:43 +01:00
osSoftwareFacet . setVersion ( mxbean . getVersion ( ) ) ; // softwareFacet.setVersion(System.getProperty("os.version"));
2022-03-10 18:14:25 +01:00
2022-03-11 11:03:43 +01:00
JsonNode jsonNode = ElementMapper . getObjectMapper ( ) . valueToTree ( osSoftwareFacet ) ;
Set < String > fieldNames = new TreeSet < > ( ) ;
Iterator < String > it = jsonNode . fieldNames ( ) ;
while ( it . hasNext ( ) ) fieldNames . add ( it . next ( ) ) ;
if ( osName . compareTo ( " Linux " ) = = 0 ) {
2022-03-10 18:14:25 +01:00
// Adding Linux Distribution Info
LinuxDistributionInfo linuxDistributionInfo = new LinuxDistributionInfo ( ) ;
Map < String , String > map = linuxDistributionInfo . getInfo ( ) ;
Set < String > keys = map . keySet ( ) ;
for ( String key : keys ) {
2022-03-11 11:03:43 +01:00
String k = key ;
if ( fieldNames . contains ( key ) ) {
k = " linuxDistribution- " + k ;
2022-03-10 18:14:25 +01:00
}
2022-03-11 11:03:43 +01:00
osSoftwareFacet . setAdditionalProperty ( k , map . get ( key ) ) ;
2022-03-10 18:14:25 +01:00
}
}
return osSoftwareFacet ;
}
2020-10-22 12:10:37 +02:00
private HostingNode instantiateHostingNode ( ) {
2020-10-20 16:25:30 +02:00
logger . info ( " Creating {} " , HostingNode . NAME ) ;
ContainerConfiguration containerConfiguration = containerContext . configuration ( ) ;
String id = containerContext . id ( ) ;
2020-10-22 12:10:37 +02:00
2020-10-20 16:25:30 +02:00
UUID uuid = UUID . fromString ( id ) ;
HostingNode hostingNode = new HostingNodeImpl ( ) ;
2023-04-28 11:47:12 +02:00
hostingNode . setID ( uuid ) ;
2020-10-20 16:25:30 +02:00
NetworkingFacet networkingFacet = new NetworkingFacetImpl ( ) ;
try {
networkingFacet . setIPAddress ( InetAddress . getLocalHost ( ) . getHostAddress ( ) ) ;
} catch ( UnknownHostException e ) {
logger . warn ( " unable to detect the IP address of the host " ) ;
}
String hostname = containerConfiguration . hostname ( ) ;
networkingFacet . setHostName ( hostname ) ;
networkingFacet . setDomainName ( getDomain ( hostname ) ) ;
networkingFacet . setAdditionalProperty ( " Port " , containerConfiguration . port ( ) ) ;
IsIdentifiedBy < HostingNode , NetworkingFacet > isIdentifiedBy = new IsIdentifiedByImpl < > ( hostingNode ,
2023-02-16 16:15:41 +01:00
networkingFacet ) ;
2020-10-20 16:25:30 +02:00
hostingNode . addFacet ( isIdentifiedBy ) ;
List < CPUFacet > cpuFacets = getCPUFacets ( ) ;
for ( CPUFacet cpuFacet : cpuFacets ) {
hostingNode . addFacet ( cpuFacet ) ;
}
2022-03-10 18:14:25 +01:00
SoftwareFacet osSoftwareFacet = getOperativeSystem ( ) ;
2020-10-22 12:10:37 +02:00
hostingNode . addFacet ( osSoftwareFacet ) ;
2022-03-10 18:14:25 +01:00
2020-10-22 12:10:37 +02:00
SmartGearsConfiguration config = ProviderFactory . provider ( ) . smartgearsConfiguration ( ) ;
SoftwareFacet smartgearsSoftwareFacet = new SoftwareFacetImpl ( ) ;
smartgearsSoftwareFacet . setGroup ( " gCube " ) ;
smartgearsSoftwareFacet . setName ( " SmartGears " ) ;
smartgearsSoftwareFacet . setVersion ( config . version ( ) ) ;
hostingNode . addFacet ( smartgearsSoftwareFacet ) ;
SoftwareFacet smartgearsDistributionSoftwareFacet = new SoftwareFacetImpl ( ) ;
smartgearsDistributionSoftwareFacet . setGroup ( " gCube " ) ;
smartgearsDistributionSoftwareFacet . setName ( " SmartGearsDistribution " ) ;
String smartgearsDistributionVersion = containerConfiguration . properties ( ) . get ( " SmartGearsDistribution " ) ;
smartgearsDistributionSoftwareFacet . setVersion ( smartgearsDistributionVersion ) ;
smartgearsDistributionSoftwareFacet . setAdditionalProperty ( " publication-frequency " ,
String . valueOf ( containerConfiguration . publicationFrequency ( ) ) ) ;
hostingNode . addFacet ( smartgearsDistributionSoftwareFacet ) ;
SoftwareFacet javaSoftwareFacet = new SoftwareFacetImpl ( ) ;
javaSoftwareFacet . setGroup ( System . getProperty ( " java.vendor " ) ) ;
javaSoftwareFacet . setName ( " Java " ) ;
javaSoftwareFacet . setVersion ( System . getProperty ( " java.version " ) ) ;
javaSoftwareFacet . setAdditionalProperty ( " java.vendor.url " , System . getProperty ( " java.vendor.url " ) ) ;
javaSoftwareFacet . setAdditionalProperty ( " java.specification.version " , System . getProperty ( " java.specification.version " ) ) ;
hostingNode . addFacet ( javaSoftwareFacet ) ;
2020-10-20 16:25:30 +02:00
SimplePropertyFacet simplePropertyFacet = addEnvironmentVariables ( containerConfiguration ) ;
hostingNode . addFacet ( simplePropertyFacet ) ;
2022-06-09 19:20:10 +02:00
Date date = Calendar . getInstance ( ) . getTime ( ) ;
StateFacet stateFacet = getStateFacet ( null , date ) ;
2020-12-21 23:10:14 +01:00
hostingNode . addFacet ( stateFacet ) ;
2021-08-04 15:12:58 +02:00
2022-06-09 19:20:10 +02:00
EventFacet eventFacet = getEventFacet ( date ) ;
2021-08-04 15:12:58 +02:00
hostingNode . addFacet ( eventFacet ) ;
2020-10-20 16:25:30 +02:00
MemoryFacet ramFacet = getRamInfo ( null ) ;
HasVolatileMemory < HostingNode , MemoryFacet > hasVolatileRAMMemory = new HasVolatileMemoryImpl < HostingNode , MemoryFacet > (
hostingNode , ramFacet , null ) ;
hasVolatileRAMMemory . setAdditionalProperty ( MEMORY_TYPE , MEMORY_TYPE_RAM ) ;
hostingNode . addFacet ( hasVolatileRAMMemory ) ;
MemoryFacet jvmMemoryFacet = getJVMMemoryInfo ( null ) ;
HasVolatileMemory < HostingNode , MemoryFacet > hasVolatileJVMMemory = new HasVolatileMemoryImpl < HostingNode , MemoryFacet > (
hostingNode , jvmMemoryFacet , null ) ;
hasVolatileJVMMemory . setAdditionalProperty ( MEMORY_TYPE , MEMORY_TYPE_JVM ) ;
hostingNode . addFacet ( hasVolatileJVMMemory ) ;
2020-10-22 12:10:37 +02:00
MemoryFacet diskFacet = getDiskSpace ( null ) ;
2020-10-20 16:25:30 +02:00
HasPersistentMemory < HostingNode , MemoryFacet > hasPersistentMemory = new HasPersistentMemoryImpl < HostingNode , MemoryFacet > (
hostingNode , diskFacet , null ) ;
hostingNode . addFacet ( hasPersistentMemory ) ;
2020-10-22 12:10:37 +02:00
LocationFacet locationFacet = new LocationFacetImpl ( ) ;
Site site = containerConfiguration . site ( ) ;
locationFacet . setCountry ( site . country ( ) ) ;
locationFacet . setLocation ( site . location ( ) ) ;
locationFacet . setLatitude ( site . latitude ( ) ) ;
locationFacet . setLongitude ( site . longitude ( ) ) ;
hostingNode . addFacet ( locationFacet ) ;
2022-05-24 12:20:55 +02:00
logger . info ( " {} with UUID {} instantiated " , HostingNode . NAME , uuid ) ;
2020-10-20 16:25:30 +02:00
return hostingNode ;
}
2020-10-22 12:10:37 +02:00
public HostingNode createHostingNode ( ) throws ResourceRegistryException {
2020-10-26 11:48:26 +01:00
ResourceRegistryClient resourceRegistryClient = ResourceRegistryClientFactory . create ( ) ;
UUID uuid = UUID . fromString ( containerContext . id ( ) ) ;
2020-10-20 16:25:30 +02:00
try {
2020-10-26 11:48:26 +01:00
hostingNode = resourceRegistryClient . getInstance ( HostingNode . class , uuid ) ;
hostingNode = updateFacets ( ) ;
} catch ( NotFoundException e ) {
2020-10-22 12:10:37 +02:00
hostingNode = instantiateHostingNode ( ) ;
2020-10-20 16:25:30 +02:00
hostingNode = resourceRegistryPublisher . createResource ( hostingNode ) ;
2020-10-26 11:48:26 +01:00
} catch ( AvailableInAnotherContextException e ) {
2022-06-08 17:42:47 +02:00
addToCurrentContext ( ) ;
2020-10-26 11:48:26 +01:00
hostingNode = resourceRegistryClient . getInstance ( HostingNode . class , uuid ) ;
2022-06-08 17:42:47 +02:00
} catch ( ResourceRegistryException e ) {
logger . error ( " " , e ) ;
2020-10-20 16:25:30 +02:00
}
return hostingNode ;
}
2022-06-09 19:20:10 +02:00
public String getState ( ) {
return containerContext . lifecycle ( ) . state ( ) . remoteForm ( ) . toLowerCase ( ) ;
}
private StateFacet getStateFacet ( StateFacet stateFacet , Date date ) {
2020-12-21 23:10:14 +01:00
if ( stateFacet = = null ) {
stateFacet = new StateFacetImpl ( ) ;
2020-10-20 16:25:30 +02:00
}
2022-06-09 19:20:10 +02:00
String state = getState ( ) ;
2020-12-21 23:10:14 +01:00
stateFacet . setValue ( state ) ;
2022-06-09 19:20:10 +02:00
stateFacet . setAdditionalProperty ( " date " , date ) ;
2020-12-21 23:10:14 +01:00
return stateFacet ;
2020-10-20 16:25:30 +02:00
}
2021-08-04 15:12:58 +02:00
2022-06-09 19:20:10 +02:00
private EventFacet getEventFacet ( Date date ) {
2021-08-04 15:12:58 +02:00
EventFacet eventFacet = new EventFacetImpl ( ) ;
2022-06-09 19:20:10 +02:00
eventFacet . setDate ( date ) ;
String state = getState ( ) ;
2021-08-04 15:12:58 +02:00
eventFacet . setEvent ( state ) ;
return eventFacet ;
}
2020-10-22 12:10:37 +02:00
private MemoryFacet getDiskSpace ( MemoryFacet memoryFacet ) {
2020-10-20 16:25:30 +02:00
if ( memoryFacet = = null ) {
memoryFacet = new MemoryFacetImpl ( ) ;
}
long free = 0 ;
long total = 0 ;
try {
FileStore fileStore = Files
2020-10-22 12:10:37 +02:00
. getFileStore ( Paths . get ( containerContext . configuration ( ) . persistence ( ) . location ( ) ) ) ;
2020-10-20 16:25:30 +02:00
free = fileStore . getUsableSpace ( ) / 1048576 ; // 1048576 = 1024*1024
// user to convert
// bytes in MByte
total = fileStore . getTotalSpace ( ) / 1048576 ; // 1048576 = 1024*1024
// user to convert
// bytes in MByte
} catch ( IOException ioe ) {
logger . warn ( " Unable to detect disk space information " , ioe ) ;
memoryFacet . setAdditionalProperty ( MESSAGE , " Unable to detect disk space information. " ) ;
}
memoryFacet . setUnit ( MemoryUnit . MB ) ;
memoryFacet . setSize ( total ) ;
memoryFacet . setUsed ( total - free ) ;
return memoryFacet ;
}
private static final long BYTE_TO_MB = 1024 * 1024 ;
2020-10-22 12:10:37 +02:00
2020-10-20 16:25:30 +02:00
private MemoryFacet getRamInfo ( MemoryFacet memoryFacet ) {
if ( memoryFacet = = null ) {
memoryFacet = new MemoryFacetImpl ( ) ;
}
/ *
* OperatingSystemMXBean mxbean = ManagementFactory . getOperatingSystemMXBean ( ) ;
* com . sun . management . OperatingSystemMXBean sunmxbean =
* ( com . sun . management . OperatingSystemMXBean ) mxbean ; long freeMemory =
* sunmxbean . getFreePhysicalMemorySize ( ) / 1048576 ; // in MB long totalMemory =
* sunmxbean . getTotalPhysicalMemorySize ( ) / 1048576 ; // in MB
* /
MBeanServer mBeanServer = ManagementFactory . getPlatformMBeanServer ( ) ;
long freeMemory ;
try {
freeMemory = Long . parseLong ( mBeanServer
. getAttribute ( new ObjectName ( " java.lang " , " type " , " OperatingSystem " ) , " FreePhysicalMemorySize " )
. toString ( ) ) / BYTE_TO_MB ;
} catch ( NumberFormatException | InstanceNotFoundException | AttributeNotFoundException
| MalformedObjectNameException | ReflectionException | MBeanException e ) {
logger . warn (
" Unable to get free memory from Operating System. Going to get JVM Memory. Better than nothing " ) ;
long allocatedMemory = ( Runtime . getRuntime ( ) . totalMemory ( ) - Runtime . getRuntime ( ) . freeMemory ( ) ) ;
freeMemory = Runtime . getRuntime ( ) . maxMemory ( ) - allocatedMemory ;
}
long totalMemory ;
try {
totalMemory = Long . parseLong ( mBeanServer
. getAttribute ( new ObjectName ( " java.lang " , " type " , " OperatingSystem " ) , " TotalPhysicalMemorySize " )
. toString ( ) ) / BYTE_TO_MB ;
} catch ( NumberFormatException | InstanceNotFoundException | AttributeNotFoundException
| MalformedObjectNameException | ReflectionException | MBeanException e ) {
logger . warn ( " Unable to total memory from Operating System. Going to get JVM Memory. Better than nothing " ) ;
totalMemory = Runtime . getRuntime ( ) . maxMemory ( ) ;
}
memoryFacet . setUnit ( MemoryUnit . MB ) ;
memoryFacet . setSize ( totalMemory ) ;
memoryFacet . setUsed ( totalMemory - freeMemory ) ;
return memoryFacet ;
}
private MemoryFacet getJVMMemoryInfo ( MemoryFacet memoryFacet ) {
if ( memoryFacet = = null ) {
memoryFacet = new MemoryFacetImpl ( ) ;
}
/* 1048576 = 1024*1024 used to convert bytes in MByte */
long jvmFreeMemory = Runtime . getRuntime ( ) . freeMemory ( ) / 1048576 ;
long jvmTotalMemory = Runtime . getRuntime ( ) . totalMemory ( ) / 1048576 ;
long jvmMaxMemory = Runtime . getRuntime ( ) . maxMemory ( ) / 1048576 ;
memoryFacet . setUnit ( MemoryUnit . MB ) ;
memoryFacet . setSize ( jvmTotalMemory ) ;
memoryFacet . setUsed ( jvmTotalMemory - jvmFreeMemory ) ;
memoryFacet . setAdditionalProperty ( JVM_MAX_MEMORY , jvmMaxMemory ) ;
return memoryFacet ;
}
private static String sanitizeKey ( String key ) {
return key . trim ( ) . replace ( " " , " _ " ) ;
}
2020-10-22 12:10:37 +02:00
private SimplePropertyFacet addEnvironmentVariables ( ContainerConfiguration containerConfiguration ) {
2020-10-20 16:25:30 +02:00
Map < String , String > map = new HashMap < String , String > ( ) ;
map . putAll ( containerConfiguration . properties ( ) ) ;
map . putAll ( System . getenv ( ) ) ;
SimplePropertyFacet simplePropertyFacet = new SimplePropertyFacetImpl ( ) ;
simplePropertyFacet . setName ( " ENVIRONMENT_VARIABLES " ) ;
simplePropertyFacet . setValue ( " " ) ;
for ( Map . Entry < String , String > entry : map . entrySet ( ) ) {
String varname = entry . getKey ( ) ;
if ( ( varname . compareToIgnoreCase ( " CLASSPATH " ) = = 0 ) | | ( varname . compareToIgnoreCase ( " PATH " ) = = 0 )
| | ( varname . contains ( " SSH " ) ) | | ( varname . contains ( " MAIL " ) )
| | ( varname . compareToIgnoreCase ( " LS_COLORS " ) = = 0 ) ) {
continue ;
}
simplePropertyFacet . setAdditionalProperty ( sanitizeKey ( entry . getKey ( ) ) , entry . getValue ( ) ) ;
}
return simplePropertyFacet ;
}
private static String getDomain ( String hostname ) {
try {
Pattern pattern = Pattern . compile ( " ([0-9]{1,3} \\ .[0-9]{1,3} \\ .[0-9]{1,3} \\ .[0-9]{1,3}) " ) ;
Matcher regexMatcher = pattern . matcher ( hostname ) ;
if ( regexMatcher . matches ( ) ) { // it's an IP address, nothing to trim
return hostname ;
}
return hostname . substring ( hostname . indexOf ( " . " ) + 1 ) ;
} catch ( Exception e ) {
logger . warn ( " Error while getting domain from hostname " ) ;
return hostname ;
}
}
2020-10-22 12:10:37 +02:00
2020-10-20 16:25:30 +02:00
public static final String CPU_PROCESSOR = " processor " ;
public static final String CPU_VENDOR_ID = " vendor_id " ;
public static final String CPU_MODEL_NAME = " model name " ;
public static final String CPU_CPU_MHZ = " cpu MHz " ;
public static final String CPU_MODEL_T = " model \ t " ;
public static final String CPU_MODEL_B = " model \ b " ;
public static final String CPU_MODEL_NUMBER = " modelNumber " ;
public static List < CPUFacet > getCPUFacets ( ) {
List < CPUFacet > cpuFacets = new ArrayList < > ( ) ;
File file = new File ( " /proc/cpuinfo " ) ;
if ( ! file . exists ( ) ) {
logger . warn ( " cannot acquire CPU info (no /proc/cpuinfo) " ) ;
return cpuFacets ;
}
BufferedReader input = null ;
try {
input = new BufferedReader ( new FileReader ( file ) ) ;
String line = null ;
CPUFacet cpuFacet = null ;
while ( ( line = input . readLine ( ) ) ! = null ) {
if ( ( line . startsWith ( CPU_PROCESSOR ) ) ) { // add the current
// processor to the map
cpuFacet = new CPUFacetImpl ( ) ;
cpuFacets . add ( cpuFacet ) ;
}
try {
if ( line . contains ( CPU_VENDOR_ID ) ) {
cpuFacet . setVendor ( line . split ( " : " ) [ 1 ] . trim ( ) ) ;
continue ;
}
} catch ( Exception e ) {
continue ;
}
try {
if ( line . contains ( CPU_MODEL_NAME ) ) {
cpuFacet . setModel ( line . split ( " : " ) [ 1 ] . trim ( ) ) ;
continue ;
}
} catch ( Exception e ) {
continue ;
}
try {
if ( line . contains ( CPU_CPU_MHZ ) ) {
cpuFacet . setClockSpeed ( line . split ( " : " ) [ 1 ] . trim ( ) ) ;
continue ;
}
} catch ( Exception e ) {
continue ;
}
try {
if ( ( line . contains ( CPU_MODEL_T ) ) | | ( line . contains ( CPU_MODEL_B ) ) ) {
cpuFacet . setAdditionalProperty ( CPU_MODEL_NUMBER , line . split ( " : " ) [ 1 ] . trim ( ) ) ;
continue ;
}
} catch ( Exception e ) {
continue ;
}
try {
String [ ] nameValue = line . split ( " : " ) ;
cpuFacet . setAdditionalProperty ( sanitizeKey ( nameValue [ 0 ] ) , line . split ( " : " ) [ 1 ] . trim ( ) ) ;
} catch ( Exception e ) {
}
}
} catch ( Exception e ) {
logger . warn ( " unable to acquire CPU info " , e ) ;
} finally {
if ( input ! = null ) {
try {
input . close ( ) ;
} catch ( IOException e ) {
logger . warn ( " unable to close stream " , e ) ;
}
}
}
return cpuFacets ;
}
2021-10-28 11:21:14 +02:00
public Map < UUID , String > getContextsUUID ( ) throws Exception {
2020-11-11 14:19:58 +01:00
return resourceRegistryPublisher . getResourceContexts ( hostingNode ) ;
}
2020-10-20 16:25:30 +02:00
}