More work on reading and updating PCM preferences to enable energymonitoring.

This commit is contained in:
Mark Nellemann 2022-11-28 09:12:33 +01:00
parent bcd2b84e9f
commit 2d13bddde1
7 changed files with 70 additions and 30 deletions

View file

@ -32,7 +32,7 @@ import java.util.concurrent.Callable;
defaultValueProvider = biz.nellemann.hmci.DefaultProvider.class) defaultValueProvider = biz.nellemann.hmci.DefaultProvider.class)
public class Application implements Callable<Integer> { public class Application implements Callable<Integer> {
@Option(names = { "-c", "--conf" }, description = "Configuration file [default: ${DEFAULT-VALUE}].", paramLabel = "<file>", defaultValue = "/etc/hmci.toml") @Option(names = { "-c", "--conf" }, description = "Configuration file [default: ${DEFAULT-VALUE}].", paramLabel = "<file>")
private File configurationFile; private File configurationFile;
@Option(names = { "-d", "--debug" }, description = "Enable debugging [default: false].") @Option(names = { "-d", "--debug" }, description = "Enable debugging [default: false].")

View file

@ -17,6 +17,7 @@ package biz.nellemann.hmci;
import biz.nellemann.hmci.dto.xml.*; import biz.nellemann.hmci.dto.xml.*;
import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.dataformat.xml.XmlMapper; import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -37,8 +38,6 @@ class ManagedSystem extends Resource {
private List<String> excludePartitions = new ArrayList<>(); private List<String> excludePartitions = new ArrayList<>();
private List<String> includePartitions = new ArrayList<>(); private List<String> includePartitions = new ArrayList<>();
private Boolean doEnergy = true;
private final RestClient restClient; private final RestClient restClient;
protected ManagedSystemEntry entry; protected ManagedSystemEntry entry;
@ -46,6 +45,8 @@ class ManagedSystem extends Resource {
protected ManagedSystemPcmPreference pcmPreference; protected ManagedSystemPcmPreference pcmPreference;
protected SystemEnergy systemEnergy; protected SystemEnergy systemEnergy;
protected boolean enableEnergyMonitoring = false;
private String uriPath; private String uriPath;
public String name; public String name;
public String id; public String id;
@ -84,17 +85,17 @@ class ManagedSystem extends Resource {
return; return;
} }
if(pcmPreference.energyMonitoringCapable && !pcmPreference.energyMonitorEnabled) { if(doEnergy && pcmPreference.energyMonitoringCapable && !pcmPreference.energyMonitorEnabled) {
// TODO: Try to enable setPcmPreference();
} }
if(pcmPreference.energyMonitorEnabled) { if(pcmPreference.energyMonitorEnabled) {
this.doEnergy = doEnergy;
systemEnergy = new SystemEnergy(restClient, this); systemEnergy = new SystemEnergy(restClient, this);
} }
} }
public void discover() { public void discover() {
try { try {
@ -191,10 +192,31 @@ class ManagedSystem extends Resource {
} }
public void setPcmPreference() {
log.info("getPcmPreferences()");
try {
String urlPath = String.format("/rest/api/pcm/ManagedSystem/%s/preferences", id);
XmlMapper xmlMapper = new XmlMapper();
if(pcmPreference.energyMonitoringCapable && !pcmPreference.energyMonitorEnabled) {
//log.warn("getPcmPreferences() - TODO: Enabling energyMonitor");
pcmPreference.metadata.atom = null;
pcmPreference.energyMonitorEnabled = true;
//xmlMapper.enable(SerializationFeature.INDENT_OUTPUT);
String updateXml = xmlMapper.writeValueAsString(pcmPreference);
//log.warn(updateXml);
restClient.postRequest(urlPath, updateXml);
}
} catch (IOException e) {
log.warn("setPcmPreferences() - Error: {}", e.getMessage());
}
}
public void getPcmPreferences() { public void getPcmPreferences() {
log.info("getPcmPreferences()"); log.debug("getPcmPreferences()");
try { try {
String urlPath = String.format("/rest/api/pcm/ManagedSystem/%s/preferences", id); String urlPath = String.format("/rest/api/pcm/ManagedSystem/%s/preferences", id);
@ -217,19 +239,13 @@ class ManagedSystem extends Resource {
if(xmlFeed.getEntry().getContent().isManagedSystemPcmPreference()) { if(xmlFeed.getEntry().getContent().isManagedSystemPcmPreference()) {
pcmPreference = xmlFeed.getEntry().getContent().getManagedSystemPcmPreference(); pcmPreference = xmlFeed.getEntry().getContent().getManagedSystemPcmPreference();
if(pcmPreference.energyMonitoringCapable && !pcmPreference.energyMonitorEnabled) { enableEnergyMonitoring = pcmPreference.energyMonitorEnabled;
log.warn("getPcmPreferences() - TODO: Enable energyMonitor");
//pcmPreference.energyMonitorEnabled = true;
//xmlMapper.configure(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED,true);
//String updateXml = xmlMapper.writeValueAsString(pcmPreference);
//restClient.postRequest(urlPath, updateXml);
}
} else { } else {
throw new UnsupportedOperationException("Failed to deserialize ManagedSystemPcmPreference"); throw new UnsupportedOperationException("Failed to deserialize ManagedSystemPcmPreference");
} }
} catch (Exception e) { } catch (Exception e) {
log.debug("getPcmPreferences() - Error: {}", e.getMessage()); log.warn("getPcmPreferences() - Error: {}", e.getMessage());
} }
} }

View file

@ -168,8 +168,10 @@ class ManagementConsole implements Runnable {
// Only continue for powered-on operating systems // Only continue for powered-on operating systems
if(managedSystem.entry != null && Objects.equals(managedSystem.entry.state, "operating")) { if(managedSystem.entry != null && Objects.equals(managedSystem.entry.state, "operating")) {
if(doEnergy) {
managedSystem.getPcmPreferences(); managedSystem.getPcmPreferences();
managedSystem.setDoEnergy(doEnergy); managedSystem.setDoEnergy(doEnergy);
}
// Check exclude / include // Check exclude / include
if (!excludeSystems.contains(managedSystem.name) && includeSystems.isEmpty()) { if (!excludeSystems.contains(managedSystem.name) && includeSystems.isEmpty()) {

View file

@ -23,6 +23,7 @@ public class RestClient {
private final static Logger log = LoggerFactory.getLogger(RestClient.class); private final static Logger log = LoggerFactory.getLogger(RestClient.class);
private final MediaType MEDIA_TYPE_IBM_XML_LOGIN = MediaType.parse("application/vnd.ibm.powervm.web+xml; type=LogonRequest"); private final MediaType MEDIA_TYPE_IBM_XML_LOGIN = MediaType.parse("application/vnd.ibm.powervm.web+xml; type=LogonRequest");
private final MediaType MEDIA_TYPE_IBM_XML_POST = MediaType.parse("application/xml, application/vnd.ibm.powervm.pcm.dita");
protected OkHttpClient httpClient; protected OkHttpClient httpClient;
@ -211,17 +212,16 @@ public class RestClient {
*/ */
public synchronized String postRequest(URL url, String payload) throws IOException { public synchronized String postRequest(URL url, String payload) throws IOException {
log.info("sendPostRequest() - URL: {}", url.toString()); log.debug("sendPostRequest() - URL: {}", url.toString());
RequestBody requestBody; RequestBody requestBody;
if(payload != null) { if(payload != null) {
requestBody = RequestBody.create(payload, MediaType.get("application/xml")); requestBody = RequestBody.create(payload, MEDIA_TYPE_IBM_XML_POST);
} else { } else {
requestBody = RequestBody.create("", null); requestBody = RequestBody.create("", null);
} }
Request request = new Request.Builder() Request request = new Request.Builder()
.url(url) .url(url)
//.addHeader("Content-Type", "application/xml")
.addHeader("content-type", "application/xml") .addHeader("content-type", "application/xml")
.addHeader("X-API-Session", (authToken == null ? "" : authToken) ) .addHeader("X-API-Session", (authToken == null ? "" : authToken) )
.post(requestBody).build(); .post(requestBody).build();
@ -232,7 +232,7 @@ public class RestClient {
if (!response.isSuccessful()) { if (!response.isSuccessful()) {
response.close(); response.close();
log.warn(responseBody); //log.warn(responseBody);
log.error("sendPostRequest() - Unexpected response: {}", response.code()); log.error("sendPostRequest() - Unexpected response: {}", response.code());
throw new IOException("sendPostRequest() - Unexpected response: " + response.code()); throw new IOException("sendPostRequest() - Unexpected response: " + response.code());
} }

View file

@ -1,25 +1,31 @@
package biz.nellemann.hmci.dto.xml; package biz.nellemann.hmci.dto.xml;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import java.io.Serializable; import java.io.Serializable;
@JsonIgnoreProperties(ignoreUnknown = true)
public class Link implements Serializable { public class Link implements Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@JacksonXmlProperty(isAttribute = true)
public String rel; public String rel;
public String getRel() { public String getRel() {
return rel; return rel;
} }
@JacksonXmlProperty(isAttribute = true)
public String type; public String type;
public String getType() { public String getType() {
return type; return type;
} }
@JacksonXmlProperty(isAttribute = true)
public String href; public String href;
public String getHref() { public String getHref() {

View file

@ -2,14 +2,18 @@ package biz.nellemann.hmci.dto.xml;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import java.io.Serializable; import java.io.Serializable;
@JsonIgnoreProperties({ "kb", "kxe", "schemaVersion", "Metadata" }) @JsonIgnoreProperties({ "kb", "kxe", "Metadata" })
public class MachineTypeModelAndSerialNumber implements Serializable { public class MachineTypeModelAndSerialNumber implements Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@JacksonXmlProperty(isAttribute = true)
private final String schemaVersion = "V1_0";
@JsonProperty("MachineType") @JsonProperty("MachineType")
public String machineType; public String machineType;

View file

@ -12,34 +12,46 @@ public class ManagedSystemPcmPreference {
@JacksonXmlProperty(isAttribute = true) @JacksonXmlProperty(isAttribute = true)
private final String schemaVersion = "V1_0"; private final String schemaVersion = "V1_0";
@JacksonXmlProperty(isAttribute = true, localName = "xmlns:ManagedSystemPcmPreference") @JacksonXmlProperty(isAttribute = true, localName = "xmlns")
private final String xmlns = "http://www.ibm.com/xmlns/systems/power/firmware/pcm/mc/2012_10/"; private final String xmlns = "http://www.ibm.com/xmlns/systems/power/firmware/pcm/mc/2012_10/";
@JacksonXmlProperty(isAttribute = true, localName = "xmlns:ManagedSystemPcmPreference")
private final String ns1 = "http://www.ibm.com/xmlns/systems/power/firmware/pcm/mc/2012_10/";
@JacksonXmlProperty(isAttribute = true, localName = "xmlns:ns2")
private final String ns2 = "http://www.w3.org/XML/1998/namespace/k2";
@JsonProperty("Metadata") @JsonProperty("Metadata")
public Metadata metadata; public Metadata metadata;
@JsonProperty("SystemName") @JsonProperty("SystemName")
public String systemName; public String systemName;
//public MachineTypeModelAndSerialNumber machineTypeModelSerialNumber; @JsonProperty("MachineTypeModelSerialNumber")
public MachineTypeModelAndSerialNumber machineTypeModelSerialNumber;
@JsonProperty("EnergyMonitoringCapable") @JsonProperty("EnergyMonitoringCapable")
public Boolean energyMonitoringCapable = false; public Boolean energyMonitoringCapable = false;
@JsonProperty("LongTermMonitorEnabled") @JsonProperty("LongTermMonitorEnabled")
public Boolean longTermMonitorEnabled = false; public Boolean longTermMonitorEnabled;
@JsonProperty("AggregationEnabled") @JsonProperty("AggregationEnabled")
public Boolean aggregationEnabled = false; public Boolean aggregationEnabled;
@JsonProperty("ShortTermMonitorEnabled") @JsonProperty("ShortTermMonitorEnabled")
public Boolean shortTermMonitorEnabled; public Boolean shortTermMonitorEnabled;
@JsonProperty("ComputeLTMEnabled") // ksv ksv="V1_1_0"
public Boolean computeLTMEnabled; //@JacksonXmlProperty(isAttribute = true)
//@JsonProperty("ComputeLTMEnabled")
//public Boolean computeLTMEnabled;
@JsonProperty("EnergyMonitorEnabled") @JsonProperty("EnergyMonitorEnabled")
public Boolean energyMonitorEnabled; public Boolean energyMonitorEnabled = false;
@JsonProperty("AssociatedManagedSystem")
public Link associatedManagedSystem;
} }