Skip to content

Commit

Permalink
MGR-126
Browse files Browse the repository at this point in the history
  • Loading branch information
madness-inc committed Dec 6, 2021
1 parent b7c99d4 commit e48aee4
Show file tree
Hide file tree
Showing 7 changed files with 217 additions and 35 deletions.
Expand Up @@ -33,6 +33,7 @@
import javax.management.openmbean.CompositeData;

import org.apache.commons.collections.keyvalue.DefaultMapEntry;
import org.apache.commons.lang3.StringUtils;
import org.appng.api.DataContainer;
import org.appng.api.DataProvider;
import org.appng.api.FieldProcessor;
Expand All @@ -50,72 +51,69 @@
@Component("env")
public class Environment implements DataProvider {

@SuppressWarnings("unchecked")
public DataContainer getData(Site site, Application application, org.appng.api.Environment environment,
Options options, Request request, FieldProcessor fieldProcessor) {
String action = options.getOptionValue("mode", "id");
DataContainer dataContainer = new DataContainer(fieldProcessor);
private static final double HIGH_TRESHOLD = 0.85d;
private static final double MEDIUM_TRESHOLD = 0.75d;

public DataContainer getData(Site site, Application app, org.appng.api.Environment env, Options opts,
Request request, FieldProcessor fp) {
String action = opts.getOptionValue("mode", "id");
DataContainer dataContainer = new DataContainer(fp);
Map<?, ?> entryMap = null;
if ("env".equals(action)) {
entryMap = System.getenv();
} else if ("props".equals(action)) {
entryMap = System.getProperties();
} else if ("jvm".equals(action)) {
entryMap = new HashMap<String, String>();
Map<String, String> jvm = new HashMap<String, String>();
entryMap = jvm;
List<String> inputArguments = ManagementFactory.getRuntimeMXBean().getInputArguments();
for (String arg : inputArguments) {
int idx = arg.indexOf('=');
String key = arg;
String value = "";
if (idx > 0) {
key = arg.substring(0, idx);
value = arg.substring(idx + 1);
String key = idx > 0 ? arg.substring(0, idx) : arg;
String value = idx > 0 ? arg.substring(idx + 1) : StringUtils.EMPTY;
if (!jvm.containsKey(key)) {
jvm.put(key, value);
} else {
jvm.replace(key, jvm.get(key) + StringUtils.LF + value);
}
((Map<String, String>) entryMap).put(key, value);
}
} else if ("mem".equals(action)) {
entryMap = new HashMap<String, LeveledEntry>();
Unit unit = Unit.KB;
long factor = unit.getFactor();
DecimalFormat format = new DecimalFormat(" #,###,000 " + unit.name());
DecimalFormat percentFormat = new DecimalFormat("#0.00 %");

Map<String, LeveledEntry> memory = new HashMap<String, LeveledEntry>();
entryMap = memory;
MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
addUsage((Map<String, LeveledEntry>) entryMap, factor, format, percentFormat, mBeanServer, "Heap", "Memory",
null, "HeapMemoryUsage");
addUsage((Map<String, LeveledEntry>) entryMap, factor, format, percentFormat, mBeanServer, "Metaspace",
"MemoryPool", "Metaspace", "Usage");
addUsage(memory, mBeanServer, "Heap", "Memory", null, "HeapMemoryUsage");
addUsage(memory, mBeanServer, "Metaspace", "MemoryPool", "Metaspace", "Usage");
} else if ("proc".equals(action)) {
entryMap = new HashMap<String, String>();
Map<String, String> proc = new HashMap<String, String>();
entryMap = proc;
OperatingSystemMXBean osMxBean = ManagementFactory.getOperatingSystemMXBean();
int procs = osMxBean.getAvailableProcessors();
double load = osMxBean.getSystemLoadAverage();
((Map<String, String>) entryMap).put("Processors", Integer.toString(procs));
((Map<String, String>) entryMap).put("Average Load", Double.toString(load));
proc.put("Processors", Integer.toString(osMxBean.getAvailableProcessors()));
proc.put("Average Load", Double.toString(osMxBean.getSystemLoadAverage()));
}

List<Entry<String, ?>> entries = getSortedEntries(entryMap);
dataContainer.setPage(entries, fieldProcessor.getPageable());
dataContainer.setPage(getSortedEntries(entryMap), fp.getPageable());
return dataContainer;
}

protected void addUsage(Map<String, LeveledEntry> entryMap, long factor, DecimalFormat format,
DecimalFormat percentFormat, MBeanServer mBeanServer, String entryName, String type, String name,
String attributeName) {
protected void addUsage(Map<String, LeveledEntry> entryMap, MBeanServer mBeanServer, String entryName, String type,
String name, String attributeName) {
try {
DecimalFormat format = new DecimalFormat(" #,###,000 " + Unit.KB.name());
ObjectName objectName = new ObjectName("java.lang:type=" + type + (null == name ? "" : (",name=" + name)));
CompositeData attribute = (CompositeData) mBeanServer.getAttribute(objectName, attributeName);
long committed = (long) attribute.get("committed");
long max = (long) attribute.get("max");
long used = (long) attribute.get("used");
long factor = Unit.KB.getFactor();
entryMap.put(entryName + " Size", new LeveledEntry(format.format((double) committed / factor)));
entryMap.put(entryName + " Max", new LeveledEntry(max < 0 ? "?" : format.format((double) max / factor)));
entryMap.put(entryName + " Used", new LeveledEntry(format.format((double) used / factor)));
if (max > 0) {
double percentage = (double) used / (double) max;
int level = percentage < 0.75d ? LeveledEntry.LOW
: (percentage < 0.85d ? LeveledEntry.MED : LeveledEntry.HIGH);
entryMap.put(entryName + " Used (%)", new LeveledEntry(percentFormat.format(percentage), level));
int level = percentage < MEDIUM_TRESHOLD ? LeveledEntry.LOW
: (percentage < HIGH_TRESHOLD ? LeveledEntry.MED : LeveledEntry.HIGH);
entryMap.put(entryName + " Used (%)",
new LeveledEntry(new DecimalFormat("#0.00 %").format(percentage), level));
}
} catch (OperationsException | ReflectionException | MBeanException e) {
log.error("error adding memory usage", e);
Expand Down
@@ -0,0 +1,62 @@
/*
* Copyright 2011-2021 the original author or authors.
*
* 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 org.appng.application.manager.business;

import org.apache.commons.lang3.StringUtils;
import org.appng.api.support.CallableDataSource;
import org.appng.testsupport.validation.WritingXmlValidator;
import org.appng.testsupport.validation.XPathDifferenceHandler;
import org.junit.Test;

public class EnvironmentTest extends AbstractTest {

static {
WritingXmlValidator.writeXml = false;
}

@Test
public void testSystemEnv() throws Exception {
validate("systemEnv");
}

@Test
public void testSystemProps() throws Exception {
validate("systemProps");
}

@Test
public void testJvmArguments() throws Exception {
validate("jvmArguments");
}

@Test
public void testProcessor() throws Exception {
validate("processor");
}

@Test
public void testMemory() throws Exception {
validate("memory");
}

protected void validate(String dataSource) throws Exception {
CallableDataSource ds = getDataSource(dataSource).getCallableDataSource();
ds.perform("");
ds.getDatasource().getData().setResultset(null);
XPathDifferenceHandler dh = new XPathDifferenceHandler(true);
validate(ds.getDatasource(), StringUtils.capitalize(dataSource), dh);
}
}
35 changes: 35 additions & 0 deletions src/test/resources/xml/EnvironmentTest-validateJvmArguments.xml
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<datasource xmlns="http://www.appng.org/schema/platform" id="jvmArguments">
<config>
<title id="jvmArguments">JVM Arguments</title>
<permissions>
<permission ref="platform.environment" mode="set"></permission>
</permissions>
<meta-data bindClass="java.util.Map$Entry">
<field name="key" type="longtext" binding="key">
<label id="name">Name</label>
</field>
<field name="value" type="text" displayLength="1000" binding="value">
<label id="value">Value</label>
</field>
</meta-data>
<linkpanel id="links" location="top">
<link id="links[1]" mode="intern" target="/system?act=reloadPlatform">
<permissions>
<permission ref="platform.reload" mode="set"></permission>
</permissions>
<label id="platform.reload">Reload platform</label>
<icon>reload</icon>
<confirmation id="platform.reload.confirm">Do you really want to reload the platform?</confirmation>
</link>
<link id="links[2]" mode="webservice" target="/service/localhost/appng-manager/webservice/systemReport">
<permissions>
<permission ref="platform.report" mode="set"></permission>
</permissions>
<label id="systemReport">System Report</label>
<icon>goto</icon>
</link>
</linkpanel>
</config>
<data />
</datasource>
33 changes: 33 additions & 0 deletions src/test/resources/xml/EnvironmentTest-validateMemory.xml
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<datasource xmlns="http://www.appng.org/schema/platform" id="memory">
<config>
<title id="memory">Memory</title>
<permissions>
<permission ref="platform.environment" mode="set"></permission>
</permissions>
<meta-data bindClass="java.util.Map$Entry">
<field name="key" type="longtext" binding="key">
<label id="name">Name</label>
</field>
<field name="value.value" type="text" binding="value.value">
<label id="value">Value</label>
</field>
<field name="value.level" type="image" binding="value.level">
<icon condition="${current.value.level == 0}">spacer</icon>
<icon condition="${current.value.level == 1}">led_green</icon>
<icon condition="${current.value.level == 2}">led_orange</icon>
<icon condition="${current.value.level == 3}">led_red</icon>
</field>
</meta-data>
<linkpanel id="links" location="top">
<link id="links[1]" mode="webservice" target="/service/localhost/appng-manager/webservice/threadViewer">
<permissions>
<permission ref="platform.threads" mode="set"></permission>
</permissions>
<label id="threads">Threads</label>
<icon>goto</icon>
</link>
</linkpanel>
</config>
<data />
</datasource>
18 changes: 18 additions & 0 deletions src/test/resources/xml/EnvironmentTest-validateProcessor.xml
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<datasource xmlns="http://www.appng.org/schema/platform" id="processor">
<config>
<title id="Processor">Processor</title>
<permissions>
<permission ref="platform.environment" mode="set"></permission>
</permissions>
<meta-data bindClass="java.util.Map$Entry">
<field name="key" type="longtext" binding="key">
<label id="name">Name</label>
</field>
<field name="value" type="text" binding="value">
<label id="value">Value</label>
</field>
</meta-data>
</config>
<data />
</datasource>
18 changes: 18 additions & 0 deletions src/test/resources/xml/EnvironmentTest-validateSystemEnv.xml
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<datasource xmlns="http://www.appng.org/schema/platform" id="systemEnv">
<config>
<title id="systemEnvironment">System Environment</title>
<permissions>
<permission ref="platform.environment" mode="set"></permission>
</permissions>
<meta-data bindClass="java.util.Map$Entry">
<field name="key" type="text" displayLength="60" binding="key">
<label id="name">Name</label>
</field>
<field name="value" type="text" displayLength="1000" binding="value">
<label id="value">Value</label>
</field>
</meta-data>
</config>
<data />
</datasource>
18 changes: 18 additions & 0 deletions src/test/resources/xml/EnvironmentTest-validateSystemProps.xml
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<datasource xmlns="http://www.appng.org/schema/platform" id="systemProps">
<config>
<title id="systemProperties">System Properties</title>
<permissions>
<permission ref="platform.environment" mode="set"></permission>
</permissions>
<meta-data bindClass="java.util.Map$Entry">
<field name="key" type="text" displayLength="60" binding="key">
<label id="name">Name</label>
</field>
<field name="value" type="text" displayLength="1000" binding="value">
<label id="value">Value</label>
</field>
</meta-data>
</config>
<data />
</datasource>

0 comments on commit e48aee4

Please sign in to comment.