weizhouapache commented on a change in pull request #5588:
URL: https://github.com/apache/cloudstack/pull/5588#discussion_r752061390
##########
File path: server/src/main/java/com/cloud/server/StatsCollector.java
##########
@@ -564,11 +657,352 @@ protected Point creteInfluxDbPoint(Object metricsObject)
{
}
}
+ Gson gson;
+
+ class ManagementServerCollector extends AbstractStatsCollector {
+ @Override
+ protected void runInContext() {
+ LOGGER.debug(String.format("%s is running...",
this.getClass().getSimpleName()));
+ long msid = ManagementServerNode.getManagementServerId();
+ ManagementServerHostVO mshost = null;
+ ManagementServerHostStatsEntry hostStatsEntry = null;
+ try {
+ mshost = managementServerHostDao.findByMsid(msid);
+ // get local data
+ hostStatsEntry = getDataFrom(mshost);
+ managementServerHostStats.put(mshost.getUuid(),
hostStatsEntry);
+ // send to other hosts
+ clusterManager.publishStatus(gson.toJson(hostStatsEntry));
+ } catch (Throwable t) {
+ // pokemon catch to make sure the thread stays running
+ LOGGER.error("Error trying to retrieve host stats", t);
+ }
+ try {
+ // send to DB
+ storeStatus(hostStatsEntry, mshost);
+ } catch (Throwable t) {
+ // pokemon catch to make sure the thread stays running
+ LOGGER.error("Error trying to store host state", t);
+ }
+ }
+
+ private void storeStatus(ManagementServerHostStatsEntry
hostStatsEntry, ManagementServerHostVO mshost) {
+ if (hostStatsEntry == null || mshost == null) {
+ return;
+ }
+ ManagementServerStatusVO msStats =
managementServerStatusDao.findByMsId(hostStatsEntry.getManagementServerHostUuid());
+ if (msStats == null) {
+ LOGGER.info(String.format("creating new status info record for
host %s - %s",
+ mshost.getName(),
+ hostStatsEntry.getManagementServerHostUuid()));
+ msStats = new ManagementServerStatusVO();
+ msStats.setMsId(hostStatsEntry.getManagementServerHostUuid());
+ }
+ msStats.setOsDistribution(hostStatsEntry.getOsDistribution()); //
for now just the bunch details come later
+ msStats.setJavaName(hostStatsEntry.getJvmVendor());
+ msStats.setJavaVersion(hostStatsEntry.getJvmVersion());
+ Date startTime = new Date(hostStatsEntry.getStartTime());
+ if (LOGGER.isTraceEnabled()) {
+ LOGGER.trace(String.format("reporting starttime %s",
startTime));
+ }
+ msStats.setLastStart(startTime);
+ msStats.setUpdated(new Date());
+ managementServerStatusDao.persist(msStats);
+ }
+
+ @NotNull
+ private ManagementServerHostStatsEntry
getDataFrom(ManagementServerHostVO mshost) {
+ ManagementServerHostStatsEntry newEntry = new
ManagementServerHostStatsEntry();
+ LOGGER.debug("Metrics collection start...");
+ newEntry.setManagementServerHostId(mshost.getId());
+ newEntry.setManagementServerHostUuid(mshost.getUuid());
+ retrieveSession(newEntry);
+ getJvmDimensions(newEntry);
+ LOGGER.debug("Metrics collection extra...");
+ getRuntimeData(newEntry);
+ getCpuData(newEntry);
+ getMemoryData(newEntry);
+ // newEntry must now include a pid!
+ getProcFsData(newEntry);
+ getFsData(newEntry);
+ getDataBaseStatistics(newEntry, mshost.getMsid());
+ gatherAllMetrics(newEntry);
+ LOGGER.debug("Metrics collection end!");
+ return newEntry;
+ }
+
+ private void retrieveSession(ManagementServerHostStatsEntry newEntry) {
+ long sessions = ApiSessionListener.getSessionCount();
+ newEntry.setSessions(sessions);
+ if (LOGGER.isTraceEnabled()) {
+ LOGGER.trace(String.format("Sessions found in Api %d vs
context %d", sessions,ApiSessionListener.getNumberOfSessions()));
+ } else {
+ LOGGER.debug("Sessions active: " + sessions);
+ }
+ }
+
+ private void getDataBaseStatistics(ManagementServerHostStatsEntry
newEntry, long msid) {
+ int count = _hostDao.countByMs(msid);
+ newEntry.setAgentCount(count);
+ }
+
+ private void getMemoryData(@NotNull ManagementServerHostStatsEntry
newEntry) {
+ MemoryMXBean mxBean = ManagementFactory.getMemoryMXBean();
+ newEntry.setTotalInit(mxBean.getHeapMemoryUsage().getInit() +
mxBean.getNonHeapMemoryUsage().getInit());
+ newEntry.setTotalUsed(mxBean.getHeapMemoryUsage().getUsed() +
mxBean.getNonHeapMemoryUsage().getUsed());
+ newEntry.setTotalMax(mxBean.getHeapMemoryUsage().getMax() +
mxBean.getNonHeapMemoryUsage().getMax());
+
newEntry.setTotalCommitted(mxBean.getHeapMemoryUsage().getCommitted() +
mxBean.getNonHeapMemoryUsage().getCommitted());
+ }
+
+ private void getCpuData(@NotNull ManagementServerHostStatsEntry
newEntry) {
+ final OperatingSystemMXBean mxBean =
ManagementFactory.getOperatingSystemMXBean();
+ newEntry.setAvailableProcessors(mxBean.getAvailableProcessors());
+ newEntry.setLoadAverage(mxBean.getSystemLoadAverage());
+ if (LOGGER.isTraceEnabled()) {
+ LOGGER.trace(String.format(
+ "Metrics processors - %d , loadavg - %f ",
+ newEntry.getAvailableProcessors(),
+ newEntry.getLoadAverage()));
+ }
+ }
+
+ private void getRuntimeData(@NotNull ManagementServerHostStatsEntry
newEntry) {
+ final RuntimeMXBean mxBean = ManagementFactory.getRuntimeMXBean();
+ newEntry.setUptime(mxBean.getUptime());
+ newEntry.setStartTime(mxBean.getStartTime());
+ newEntry.setProcessId(mxBean.getPid());
+ newEntry.setJvmName(mxBean.getName());
+ newEntry.setJvmVendor(mxBean.getVmVendor());
+ newEntry.setJvmVersion(mxBean.getVmVersion());
+ if (LOGGER.isTraceEnabled()) {
+ LOGGER.trace(String.format(
+ "Metrics uptime - %d , starttime - %d",
+ newEntry.getUptime(),
+ newEntry.getStartTime()));
+ }
+ }
+
+ private void getJvmDimensions(@NotNull ManagementServerHostStatsEntry
newEntry) {
+ Runtime runtime = Runtime.getRuntime();
+ newEntry.setTotalMemoryBytes(runtime.totalMemory());
+ newEntry.setFreeMemoryBytes(runtime.freeMemory());
+ newEntry.setAvailableProcessors(runtime.availableProcessors());
+ newEntry.setTotalMax(runtime.maxMemory());
+ //long maxMem = runtime.maxMemory();
+ if (LOGGER.isTraceEnabled()) {
+ LOGGER.trace(String.format(
+ "Metrics proc - %d , maxMem - %d , totalMemory - %d ,
freeMemory - %f ",
+ newEntry.getAvailableProcessors(),
+ newEntry.getTotalMax(),
+ newEntry.getTotalMemoryBytes(),
+ newEntry.getFreeMemoryBytes()));
+ }
+ }
+
+ /**
+ * As for data from outside the JVM, we only rely on /proc/ contained
data.
+ *
+ * @param newEntry
+ */
+ private void getProcFsData(@NotNull ManagementServerHostStatsEntry
newEntry) {
+ String OS = Script.runSimpleBashScript("cat /proc/version");
+ newEntry.setOsDistribution(OS);
+ String mem = Script.runSimpleBashScript("cat /proc/meminfo | grep
MemTotal | cut -f 2 -d ':' | tr -d 'a-zA-z '").trim();
+ newEntry.setSystemMemoryTotal(Long.parseLong(mem) * 1024);
+ String free = Script.runSimpleBashScript("cat /proc/meminfo | grep
MemFree | cut -f 2 -d ':' | tr -d 'a-zA-z '").trim();
+ newEntry.setSystemMemoryFree(Long.parseLong(free) * 1024);
+ String used = Script.runSimpleBashScript(String.format("ps -o rss=
%d", newEntry.getPid()));
+ newEntry.setSystemMemoryUsed(Long.parseLong(used));
+ String maxuse = Script.runSimpleBashScript(String.format("ps -o
vsz= %d", newEntry.getPid()));
+ newEntry.setSystemMemoryVirtualSize(Long.parseLong(maxuse));
+
+ newEntry.setSystemTotalCpuCycles(getSystemCpuCyclesTotal());
+ newEntry.setSystemLoadAverages(getCpuLoads());
+ newEntry.setSystemCyclesUsage(getSystemCpuUsage(newEntry));
+ if (LOGGER.isTraceEnabled()) {
+ LOGGER.debug(
+ String.format("cpu\ncapacities: %f\n loads: %s ;
%s ; %s\n stats: %f ; %f ; %f",
+ newEntry.getSystemTotalCpuCycles(),
+ newEntry.getSystemLoadAverages()[0],
newEntry.getSystemLoadAverages()[1], newEntry.getSystemLoadAverages()[2],
+ newEntry.getSystemCyclesUsage()[0],
newEntry.getSystemCyclesUsage()[1], newEntry.getSystemCyclesUsage()[2]
+ )
+ );
+ }
+ }
+
+ @NotNull
+ private double[] getCpuLoads() {
+ String[] cpuloadString = Script.runSimpleBashScript("cat
/proc/loadavg").split(" ");
+ double[] cpuloads = {Double.parseDouble(cpuloadString[0]),
Double.parseDouble(cpuloadString[1]), Double.parseDouble(cpuloadString[2])};
+ return cpuloads;
+ }
+
+ private double [] getSystemCpuUsage(@NotNull
ManagementServerHostStatsEntry newEntry) {
+ String[] cpustats = Script.runSimpleBashScript("cat /proc/stat |
grep \"cpu \" | tr -d \"cpu\"").trim().split(" ");
+ double [] cycleUsage = {Double.parseDouble(cpustats[0]) +
Double.parseDouble(cpustats[1]), Double.parseDouble(cpustats[2]),
Double.parseDouble(cpustats[3])};
+ return cycleUsage;
+ }
+
+ private double getSystemCpuCyclesTotal() {
+ String cpucaps = Script.runSimpleBashScript("cat /proc/cpuinfo |
grep \"cpu MHz\" | grep \"cpu MHz\" | cut -f 2 -d : | tr -d ' '| tr '\\n' \"
\"");
+ double totalcpucap = 0;
+ for (String cpucap : cpucaps.split(" ")) {
+ totalcpucap += Double.parseDouble(cpucap);
+ }
+ return totalcpucap;
+ }
+
+ private void getFsData(@NotNull ManagementServerHostStatsEntry
newEntry) {
+ Set<String> logFileNames = LogUtils.getLogFileNames();
+ StringBuilder logInfoBuilder = new StringBuilder();
+ for (String fileName : logFileNames) {
+ String du = Script.runSimpleBashScript(String.format("du -sk
%s", fileName));
+ String df = Script.runSimpleBashScript(String.format("df -k %s
| grep -v Filesystem", fileName));
+ logInfoBuilder.append(fileName).append('\n').append("usage:
").append(du).append('\n').append("disk :").append(df);
Review comment:
@DaanHoogland
all are fine. what I meant is, we'd better add "KB" or "kilobytes" in the
log info so it will be more clear.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]