2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.openecomp.sdc.common.monitoring;
22 import java.lang.management.ManagementFactory;
23 import java.lang.management.MemoryMXBean;
24 import java.lang.management.RuntimeMXBean;
25 import java.lang.management.ThreadMXBean;
26 import java.util.HashMap;
28 import javax.management.MBeanServer;
29 import javax.management.ObjectName;
30 import org.hyperic.sigar.FileSystem;
31 import org.hyperic.sigar.Sigar;
32 import org.openecomp.sdc.common.impl.ExternalConfiguration;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 public class MonitoringMetricsFetcher {
38 private static Logger monitoringLogger = LoggerFactory.getLogger("asdc.fe.monitoring.fetcher");
39 private static volatile MonitoringMetricsFetcher instance;
40 private static RuntimeMXBean runtimeMXBean;
41 private static ThreadMXBean threadMXBean;
42 private static MemoryMXBean memoryMXBean;
43 private static MBeanServer platformMBeanServer;
44 private static Sigar sigarSession;
45 private static String appName;
46 private static String jvmName = "Unknown";
47 private final String PROCESS_CPU_TIME_ATTR = "ProcessCpuTime";
48 private static String FE_JVM_NAME = "jetty-fe";
49 private static String BE_JVM_NAME = "jetty-be";
51 private MonitoringMetricsFetcher() {
54 public static MonitoringMetricsFetcher getInstance() {
55 if (instance == null) {
61 public MonitoringEvent getMonitoringMetrics() {
62 MonitoringEvent monitoringEvent = new MonitoringEvent();
63 monitoringEvent.setHostid(getFQDN());
64 monitoringEvent.setHostcpu(getHostCpuTime());
65 monitoringEvent.setHostmem(getHostUsedMemory());
66 monitoringEvent.setHostdisk(getHostUsedDisk().toString());
67 monitoringEvent.setJvmid(jvmName);
68 monitoringEvent.setJvmcpu(getJvmCpuTime());
69 monitoringEvent.setJvmmem(getJvmUsedHeapMemory());
70 monitoringEvent.setJvmtnum(getJvmThreads());
71 monitoringEvent.setAppid(appName);
72 // this is probably from healthcheck
73 monitoringEvent.setAppstat("appStatus");
74 return monitoringEvent;
77 private static synchronized MonitoringMetricsFetcher init() {
78 if (instance == null) {
79 instance = new MonitoringMetricsFetcher();
80 threadMXBean = ManagementFactory.getThreadMXBean();
81 memoryMXBean = ManagementFactory.getMemoryMXBean();
82 runtimeMXBean = ManagementFactory.getRuntimeMXBean();
83 platformMBeanServer = ManagementFactory.getPlatformMBeanServer();
84 sigarSession = new Sigar();
85 appName = ExternalConfiguration.getAppName();
86 monitoringLogger.debug("appName is {}", appName);
87 // Accoridng to Yaki, there is no "calculated" jvmName like it was
91 // just "jetty-be" or "jetty-fe"
92 if (appName.contains("fe")) {
93 jvmName = FE_JVM_NAME;
94 } else if (appName.contains("be")) {
95 jvmName = BE_JVM_NAME;
97 monitoringLogger.warn("Couldn't determine jvmName, appName is expected to contain \"be\" or \"fe\" string");
104 * Returns the number of live threads for this JVM
106 * @return number of live threads
108 private Integer getJvmThreads() {
109 return threadMXBean.getThreadCount();
113 * Returns the number of used heap memory (bytes)
115 * @return the number of used heap memory (bytes)
117 private long getJvmUsedHeapMemory() {
118 return memoryMXBean.getHeapMemoryUsage().getUsed();
122 * Returns the jvm cpu time (msec)
124 * @return the jvm cpu time (msec)
126 private long getJvmCpuTime() {
129 cpuTime = (long) platformMBeanServer.getAttribute(new ObjectName(ManagementFactory.OPERATING_SYSTEM_MXBEAN_NAME), PROCESS_CPU_TIME_ATTR);
130 } catch (Exception e) {
131 monitoringLogger.error("Couldn't measure JVM CPU time", e);
137 * Returns the host total cpu time (msec)
139 * @return the host total cpu time (msec)
141 private long getHostCpuTime() {
144 cpuTime = sigarSession.getCpu().getTotal();
145 } catch (Exception e) {
146 monitoringLogger.error("Couldn't measure host CPU time", e);
152 * Returns the host used memory(msec)
154 * @return the host used memory(msec)
156 private Double getHostUsedMemory() {
157 Double memory = -1.0;
159 memory = sigarSession.getMem().getUsedPercent();
160 } catch (Exception e) {
161 monitoringLogger.error("Couldn't measure host used memory", e);
167 * Returns the percentage of all available FS
169 * @return the host avail disk(bytes)
171 private Map<String, Double> getHostUsedDisk() {
172 Map<String, Double> res = new HashMap<>();
174 FileSystem[] fileSystemList = sigarSession.getFileSystemList();
175 for (FileSystem fileSystem : fileSystemList) {
176 String dirName = fileSystem.getDirName();
177 double usePercent = sigarSession.getFileSystemUsage(dirName).getUsePercent() * 100;
178 res.put(dirName, usePercent);
180 } catch (Exception e) {
181 monitoringLogger.error("Couldn't measure host used disk", e);
191 private String getFQDN() {
194 fqdn = sigarSession.getFQDN();
195 } catch (Exception e) {
196 monitoringLogger.error("Couldn't get FQDN", e);