WIP - refactoring.

This commit is contained in:
Mark Nellemann 2020-08-10 15:44:14 +02:00
parent 504058ae7a
commit 2b95adcb23
6 changed files with 89 additions and 46 deletions

View File

@ -24,6 +24,7 @@ dependencies {
// Use the latest Groovy version for building this library
implementation 'org.codehaus.groovy:groovy-all:3.0.5'
implementation 'com.squareup.okhttp3:okhttp:4.8.0'
implementation 'org.influxdb:influxdb-java:2.19'
implementation 'org.slf4j:slf4j-api:1.7.+'
runtimeOnly 'ch.qos.logback:logback-classic:1.+'
@ -31,7 +32,8 @@ dependencies {
testImplementation('org.spockframework:spock-core:2.0-M3-groovy-3.0')
testImplementation("org.slf4j:slf4j-simple:1.7.+")
testImplementation('com.squareup.okhttp3:mockwebserver:4.8.0')
//implementation platform('org.testcontainers:testcontainers-bom:1.14.3') //import bom
//testCompile "org.testcontainers:influxdb:1.14.3"
}
application {

View File

@ -8,8 +8,48 @@ import groovy.util.logging.Slf4j
@Slf4j
class App {
App(String... args) {
println("App()")
Map<String,ManagedSystem> systems = new HashMap<String, ManagedSystem>()
Map<String, LogicalPartition> partitions = new HashMap<String, LogicalPartition>()
HmcClient hmc
try {
hmc = new HmcClient("https://10.32.64.39:12443", "hmci", "hmcihmci")
hmc.login()
hmc.getManagedSystems().each { systemKey, system ->
// Add to list of known systems
systems.putIfAbsent(systemKey, system)
// Get and process metrics for this system
String json = hmc.getPcmForManagedSystemWithId(system)
system.processPcmJson(json)
/*hmc.getLogicalPartitionsForManagedSystem(systemValue).each { lparKey, lpar ->
partitions.putIfAbsent(lparKey, lpar)
//hmc.get
}*/
}
hmc.logoff()
} catch(Exception e) {
log.error(e.message)
}
}
static void main(String... args) {
def cli = new CliBuilder()
cli.h(longOpt: 'help', 'display usage')
cli.v(longOpt: 'version', 'display version')
@ -23,20 +63,10 @@ class App {
//println("TODO: Use configuration file: " + options.config)
}
Hmc hmc
try {
hmc = new Hmc("https://10.32.64.39:12443", "hmci", "hmcihmci")
hmc.login()
hmc.getManagedSystems()
hmc.getLogicalPartitions()
hmc.getProcessedMetrics()
hmc.logoff()
} catch(Exception e) {
log.error(e.message)
}
// TODO: Read configuration file or create new empty file,
// pass the properties or configuration bean to App.
new App(args)
System.exit(0);
}

View File

@ -21,7 +21,7 @@ import java.security.cert.CertificateException
import java.security.cert.X509Certificate;
@Slf4j
class Hmc {
class HmcClient {
private final MediaType MEDIA_TYPE_IBM_XML_LOGIN = MediaType.parse("application/vnd.ibm.powervm.web+xml; type=LogonRequest");
@ -29,12 +29,12 @@ class Hmc {
private final String username
private final String password
protected Map<String,ManagedSystem> managedSystems = new HashMap<String, ManagedSystem>()
//protected Map<String,ManagedSystem> managedSystems = new HashMap<String, ManagedSystem>()
protected String authToken
private final OkHttpClient client
Hmc(String baseUrl, String username, String password) {
HmcClient(String baseUrl, String username, String password) {
this.baseUrl = baseUrl
this.username = username
this.password = password
@ -93,30 +93,31 @@ class Hmc {
}
void getManagedSystems() {
Map<String, ManagedSystem> getManagedSystems() {
log.debug("getManagedSystems()")
URL url = new URL(String.format("%s/rest/api/uom/ManagedSystem", baseUrl))
Response response = getResponse(url)
String responseBody = response.body.string()
Map<String,ManagedSystem> managedSystemsMap = new HashMap<String, ManagedSystem>()
def feed = new XmlSlurper().parseText(responseBody)
feed?.entry?.each { entry ->
//log.debug("Entry")
entry.content.each { content ->
//log.debug("Content")
content.ManagedSystem.each { system ->
ManagedSystem managedSystem = new ManagedSystem(entry.id as String)
managedSystem.name = system.SystemName
managedSystem.model = system.MachineTypeModelAndSerialNumber.Model
managedSystem.type = system.MachineTypeModelAndSerialNumber.MachineType
managedSystem.serialNumber = system.MachineTypeModelAndSerialNumber.SerialNumber
managedSystems.put(managedSystem.id, managedSystem)
managedSystemsMap.put(managedSystem.id, managedSystem)
log.debug("getManagedSystems() " + managedSystem.toString())
}
}
}
return managedSystemsMap
}
@ -128,7 +129,7 @@ class Hmc {
}
void getLogicalPartitionsForManagedSystem(ManagedSystem system) {
Map<String, LogicalPartition> getLogicalPartitionsForManagedSystem(ManagedSystem system) {
log.debug("getLogicalPartitionsForManagedSystem() - " + system.name)
URL url = new URL(String.format("%s/rest/api/uom/ManagedSystem/%s/LogicalPartition", baseUrl, system.id))
@ -136,6 +137,7 @@ class Hmc {
String responseBody = response.body.string()
//log.debug(responseBody)
Map<String, LogicalPartition> partitionMap = new HashMap<String, LogicalPartition>() {}
def feed = new XmlSlurper().parseText(responseBody)
feed?.entry?.each { entry ->
//log.debug("Entry")
@ -145,35 +147,35 @@ class Hmc {
LogicalPartition logicalPartition = new LogicalPartition(partition.PartitionUUID as String)
logicalPartition.name = partition.PartitionName
logicalPartition.type = partition.PartitionType
system.partitions.put(logicalPartition.id, logicalPartition)
partitionMap.put(logicalPartition.id, logicalPartition)
log.debug("getLogicalPartitionsForManagedSystem() " + logicalPartition.toString())
}
}
}
return partitionMap
}
void getProcessedMetrics() {
managedSystems.each {
getProcessedMetricsForManagedSystem(it.getValue())
getPcmForManagedSystemWithId(it.getValue())
}
}
void getProcessedMetricsForManagedSystem(ManagedSystem system) {
log.debug("getProcessedMetricsForManagedSystem() " - system.name)
URL url = new URL(String.format("%s/rest/api/pcm/ManagedSystem/%s/ProcessedMetrics?NoOfSamples=1", baseUrl, system.id))
String getPcmForManagedSystemWithId(String systemId) {
log.debug("getProcessedMetricsForManagedSystem() " - systemId)
URL url = new URL(String.format("%s/rest/api/pcm/ManagedSystem/%s/ProcessedMetrics?NoOfSamples=1", baseUrl, systemId))
Response response = getResponse(url)
String responseBody = response.body.string()
//log.debug(responseBody)
def feed = new XmlSlurper().parseText(responseBody)
feed?.entry?.each { entry ->
String link = entry.link["@href"]
//linksList.add(link)
switch (entry.category["@term"]) {
case "ManagedSystem":
processPcmJsonForManagedSystem(getPcmJsonForManagedSystem(link))
return getPcmJsonForManagedSystem(link)
break
case "LogicalPartition":
//processPcmJsonForLogicalPartition(getPcmJsonForLogicalPartition(getProcessedMetricsForLogicalPartition(link)))
@ -206,14 +208,15 @@ class Hmc {
}
String getPcmJsonForManagedSystem(String jsonUrl) {
private String getPcmJsonForManagedSystem(String jsonUrl) {
log.debug("getPcmJsonForManagedSystem() - " + jsonUrl)
URL url = new URL(jsonUrl)
Response response = getResponse(url)
return response.body.string()
}
String getPcmJsonForLogicalPartition(String jsonUrl) {
private String getPcmJsonForLogicalPartition(String jsonUrl) {
log.debug("getPcmJsonForLogicalPartition() - " + jsonUrl)
URL url = new URL(jsonUrl)
Response response = getResponse(url)
@ -221,16 +224,6 @@ class Hmc {
}
void processPcmJsonForManagedSystem(String json) {
log.debug("processPcmJsonForManagedSystem()")
def jsonObject = new JsonSlurper().parseText(json)
String systemUuid = jsonObject?.systemUtil?.utilInfo?.uuid as String
if(systemUuid && managedSystems.containsKey(systemUuid)) {
log.debug("processPcmJsonForManagedSystem() - Found UUID for ManagedSystem: " + systemUuid)
ManagedSystem system = managedSystems.get(systemUuid)
system.processMetrics(json)
}
}
void processPcmJsonForLogicalPartition(String json) {
log.debug("processPcmJsonForLogicalPartition()")

View File

@ -0,0 +1,5 @@
package biz.nellemann.hmci
class InfluxClient {
}

View File

@ -26,7 +26,20 @@ class ManagedSystem {
return "[${id}] ${name} (${type}-${model} ${serialNumber})"
}
void processMetrics(String json) {
void processPcmJson(String json) {
log.debug("processPcmJson()")
def jsonObject = new JsonSlurper().parseText(json)
String systemUuid = jsonObject?.systemUtil?.utilInfo?.uuid as String
if(systemUuid && this.id == systemUuid) {
log.debug("processPcmJson() - Found UUID for this ManagedSystem: " + systemUuid)
processMetrics(json)
}
}
private void processMetrics(String json) {
//metrics = new JsonSlurper().parseText(json) as PcmData
def pcmMap = new JsonSlurper().parseText(json)
metrics = new PcmData(pcmMap as Map)

View File

@ -5,15 +5,15 @@ import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
import spock.lang.Specification
class HmcTest extends Specification {
class HmcClientTest extends Specification {
Hmc hmc
HmcClient hmc
MockWebServer mockServer = new MockWebServer();
def setup() {
mockServer.start();
hmc = new Hmc(mockServer.url("/").toString(), "testUser", "testPassword")
hmc = new HmcClient(mockServer.url("/").toString(), "testUser", "testPassword")
hmc.authToken = "blaBla"
}