2020-10-12 08:15:53 +00:00
|
|
|
/*
|
2020-10-11 10:25:55 +00:00
|
|
|
* Copyright 2020 Mark Nellemann <mark.nellemann@gmail.com>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
package biz.nellemann.hmci;
|
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2021-01-13 07:31:07 +00:00
|
|
|
|
|
|
|
import java.time.Duration;
|
|
|
|
import java.time.Instant;
|
2020-10-11 10:25:55 +00:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
2020-10-12 08:15:53 +00:00
|
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
|
|
|
|
|
import static java.lang.Thread.*;
|
2020-10-11 10:25:55 +00:00
|
|
|
|
|
|
|
class Insights {
|
|
|
|
|
|
|
|
private final static Logger log = LoggerFactory.getLogger(Insights.class);
|
|
|
|
|
|
|
|
final Configuration configuration;
|
|
|
|
|
|
|
|
InfluxClient influxClient;
|
2020-10-12 08:15:53 +00:00
|
|
|
final Map<String, HmcClient> hmcClients = new HashMap<>();
|
|
|
|
final Map<String,ManagedSystem> systems = new HashMap<>();
|
|
|
|
final Map<String, LogicalPartition> partitions = new HashMap<>();
|
2020-10-11 10:25:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
Insights(Configuration configuration) {
|
|
|
|
this.configuration = configuration;
|
|
|
|
try {
|
|
|
|
influxClient = new InfluxClient(configuration.influx);
|
|
|
|
influxClient.login();
|
2020-12-23 12:02:48 +00:00
|
|
|
} catch (Exception e) {
|
2020-10-11 10:25:55 +00:00
|
|
|
System.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initial scan
|
|
|
|
discover();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void discover() {
|
|
|
|
|
|
|
|
configuration.hmc.forEach( configHmc -> {
|
2020-10-14 08:41:31 +00:00
|
|
|
if(!hmcClients.containsKey(configHmc.name)) {
|
2020-10-11 10:25:55 +00:00
|
|
|
HmcClient hmcClient = new HmcClient(configHmc);
|
|
|
|
hmcClients.put(configHmc.name, hmcClient);
|
2020-10-14 08:41:31 +00:00
|
|
|
log.info("discover() - Adding HMC: " + hmcClient);
|
2020-10-11 10:25:55 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
hmcClients.forEach(( hmcId, hmcClient) -> {
|
|
|
|
|
|
|
|
try {
|
2020-10-15 13:23:16 +00:00
|
|
|
hmcClient.logoff();
|
2020-10-11 10:25:55 +00:00
|
|
|
hmcClient.login();
|
|
|
|
hmcClient.getManagedSystems().forEach((systemId, system) -> {
|
|
|
|
|
|
|
|
// Add to list of known systems
|
2020-10-14 08:41:31 +00:00
|
|
|
if(!systems.containsKey(systemId)) {
|
|
|
|
systems.put(systemId, system);
|
2021-01-13 07:31:07 +00:00
|
|
|
log.info(hmcId + " discover() - Found ManagedSystem: " + system);
|
2020-10-14 08:41:31 +00:00
|
|
|
}
|
2020-10-11 10:25:55 +00:00
|
|
|
|
|
|
|
// Get LPAR's for this system
|
|
|
|
try {
|
|
|
|
hmcClient.getLogicalPartitionsForManagedSystem(system).forEach((partitionId, partition) -> {
|
|
|
|
|
|
|
|
// Add to list of known partitions
|
2020-10-14 08:41:31 +00:00
|
|
|
if(!partitions.containsKey(partitionId)) {
|
|
|
|
partitions.put(partitionId, partition);
|
2021-01-13 07:31:07 +00:00
|
|
|
log.info(hmcId + " discover() - Found LogicalPartition: " + partition);
|
2020-10-14 08:41:31 +00:00
|
|
|
}
|
|
|
|
|
2020-10-11 10:25:55 +00:00
|
|
|
});
|
|
|
|
} catch (Exception e) {
|
2021-01-13 07:31:07 +00:00
|
|
|
log.error(hmcId + " discover() - getLogicalPartitions", e);
|
2020-10-11 10:25:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
} catch(Exception e) {
|
2021-01-13 07:31:07 +00:00
|
|
|
log.error(hmcId + " discover() - getManagedSystems: " + e.getMessage());
|
2020-10-11 10:25:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void getMetricsForSystems() {
|
|
|
|
|
|
|
|
systems.forEach((systemId, system) -> {
|
|
|
|
|
|
|
|
HmcClient hmcClient = hmcClients.get(system.hmcId);
|
|
|
|
|
|
|
|
// Get and process metrics for this system
|
|
|
|
String tmpJsonString = null;
|
|
|
|
try {
|
|
|
|
tmpJsonString = hmcClient.getPcmDataForManagedSystem(system);
|
|
|
|
} catch (Exception e) {
|
|
|
|
log.error("getMetricsForSystems()", e);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(tmpJsonString != null && !tmpJsonString.isEmpty()) {
|
|
|
|
system.processMetrics(tmpJsonString);
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void getMetricsForPartitions() {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
// Get LPAR's for this system
|
|
|
|
partitions.forEach((partitionId, partition) -> {
|
|
|
|
|
|
|
|
HmcClient hmcClient = hmcClients.get(partition.system.hmcId);
|
|
|
|
|
|
|
|
// Get and process metrics for this partition
|
|
|
|
String tmpJsonString2 = null;
|
|
|
|
try {
|
|
|
|
tmpJsonString2 = hmcClient.getPcmDataForLogicalPartition(partition);
|
|
|
|
} catch (Exception e) {
|
2020-10-12 08:15:53 +00:00
|
|
|
log.error("getMetricsForPartitions() - getPcmDataForLogicalPartition", e);
|
2020-10-11 10:25:55 +00:00
|
|
|
}
|
|
|
|
if(tmpJsonString2 != null && !tmpJsonString2.isEmpty()) {
|
|
|
|
partition.processMetrics(tmpJsonString2);
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
} catch(Exception e) {
|
|
|
|
log.error("getMetricsForPartitions()", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-12-11 07:39:19 +00:00
|
|
|
void getMetricsForEnergy() {
|
|
|
|
|
|
|
|
systems.forEach((systemId, system) -> {
|
|
|
|
|
|
|
|
HmcClient hmcClient = hmcClients.get(system.hmcId);
|
|
|
|
|
|
|
|
// Get and process metrics for this system
|
|
|
|
String tmpJsonString = null;
|
|
|
|
try {
|
|
|
|
tmpJsonString = hmcClient.getPcmDataForEnergy(system.energy);
|
|
|
|
} catch (Exception e) {
|
|
|
|
log.error("getMetricsForEnergy()", e);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(tmpJsonString != null && !tmpJsonString.isEmpty()) {
|
|
|
|
system.energy.processMetrics(tmpJsonString);
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-11 10:25:55 +00:00
|
|
|
void writeMetricsForManagedSystems() {
|
2020-10-19 10:00:03 +00:00
|
|
|
try {
|
|
|
|
systems.forEach((systemId, system) -> influxClient.writeManagedSystem(system));
|
|
|
|
} catch (NullPointerException npe) {
|
2021-01-13 07:31:07 +00:00
|
|
|
log.warn("writeMetricsForManagedSystems() - NPE: " + npe.toString(), npe);
|
2020-10-19 10:00:03 +00:00
|
|
|
}
|
2020-10-11 10:25:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void writeMetricsForLogicalPartitions() {
|
2020-10-19 10:00:03 +00:00
|
|
|
try {
|
|
|
|
partitions.forEach((partitionId, partition) -> influxClient.writeLogicalPartition(partition));
|
|
|
|
} catch (NullPointerException npe) {
|
2021-01-13 07:31:07 +00:00
|
|
|
log.warn("writeMetricsForLogicalPartitions() - NPE: " + npe.toString(), npe);
|
2020-10-19 10:00:03 +00:00
|
|
|
}
|
2020-10-11 10:25:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-12-11 07:39:19 +00:00
|
|
|
void writeMetricsForSystemEnergy() {
|
|
|
|
try {
|
|
|
|
systems.forEach((systemId, system) -> influxClient.writeSystemEnergy(system.energy));
|
|
|
|
} catch (NullPointerException npe) {
|
2021-01-13 07:31:07 +00:00
|
|
|
log.warn("writeMetricsForSystemEnergy() - NPE: " + npe.toString(), npe);
|
2020-12-11 07:39:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-11 10:25:55 +00:00
|
|
|
void run() throws InterruptedException {
|
|
|
|
|
|
|
|
log.debug("run()");
|
|
|
|
int executions = 0;
|
2020-10-12 08:15:53 +00:00
|
|
|
AtomicBoolean keepRunning = new AtomicBoolean(true);
|
|
|
|
|
2020-10-14 08:41:31 +00:00
|
|
|
Thread shutdownHook = new Thread(() -> {
|
|
|
|
keepRunning.set(false);
|
|
|
|
System.out.println("Stopping HMCi, please wait ...");
|
|
|
|
});
|
2020-10-12 08:15:53 +00:00
|
|
|
Runtime.getRuntime().addShutdownHook(shutdownHook);
|
2020-10-11 10:25:55 +00:00
|
|
|
|
2020-10-12 08:15:53 +00:00
|
|
|
do {
|
2021-01-13 07:31:07 +00:00
|
|
|
Instant start = Instant.now();
|
2020-10-11 10:25:55 +00:00
|
|
|
try {
|
|
|
|
getMetricsForSystems();
|
|
|
|
getMetricsForPartitions();
|
2020-12-11 07:39:19 +00:00
|
|
|
getMetricsForEnergy();
|
2020-10-11 10:25:55 +00:00
|
|
|
|
|
|
|
writeMetricsForManagedSystems();
|
|
|
|
writeMetricsForLogicalPartitions();
|
2020-12-11 07:39:19 +00:00
|
|
|
writeMetricsForSystemEnergy();
|
2020-10-11 10:25:55 +00:00
|
|
|
influxClient.writeBatchPoints();
|
|
|
|
|
2020-10-12 08:15:53 +00:00
|
|
|
} catch (Exception e) {
|
|
|
|
log.error("run()", e);
|
2020-10-11 10:25:55 +00:00
|
|
|
}
|
|
|
|
|
2021-01-13 07:31:07 +00:00
|
|
|
// Refresh
|
|
|
|
if (++executions > configuration.rescan) {
|
|
|
|
executions = 0;
|
|
|
|
discover();
|
|
|
|
}
|
|
|
|
|
|
|
|
Instant end = Instant.now();
|
|
|
|
long timeSpend = Duration.between(start, end).getSeconds();
|
|
|
|
log.debug("run() - duration sec: " + timeSpend);
|
|
|
|
if(timeSpend < configuration.refresh) {
|
|
|
|
//noinspection BusyWait
|
|
|
|
sleep((configuration.refresh - timeSpend) * 1000);
|
|
|
|
}
|
2020-10-12 08:15:53 +00:00
|
|
|
|
|
|
|
} while (keepRunning.get());
|
2020-10-11 10:25:55 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|