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=========================================================
21 package org.openecomp.sdc.common.monitoring;
23 import org.hyperic.sigar.FileSystem;
24 import org.hyperic.sigar.Sigar;
25 import org.openecomp.sdc.common.impl.ExternalConfiguration;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 import javax.management.MBeanServer;
30 import javax.management.ObjectName;
31 import java.lang.management.ManagementFactory;
32 import java.lang.management.MemoryMXBean;
33 import java.lang.management.RuntimeMXBean;
34 import java.lang.management.ThreadMXBean;
35 import java.util.HashMap;
38 public class MonitoringMetricsFetcher {
40 private static Logger monitoringLogger = LoggerFactory.getLogger("asdc.fe.monitoring.fetcher");
42 private static volatile MonitoringMetricsFetcher instance;
43 private static RuntimeMXBean runtimeMXBean;
44 private static ThreadMXBean threadMXBean;
45 private static MemoryMXBean memoryMXBean;
46 private static MBeanServer platformMBeanServer;
47 private static Sigar sigarSession;
49 private static String appName;
50 private static String jvmName = "Unknown";
51 private final String PROCESS_CPU_TIME_ATTR = "ProcessCpuTime";
53 private static String FE_JVM_NAME = "jetty-fe";
54 private static String BE_JVM_NAME = "jetty-be";
56 private MonitoringMetricsFetcher() {
59 public static MonitoringMetricsFetcher getInstance() {
60 if (instance == null) {
66 public MonitoringEvent getMonitoringMetrics() {
67 MonitoringEvent monitoringEvent = new MonitoringEvent();
68 monitoringEvent.setHostid(getFQDN());
69 monitoringEvent.setHostcpu(getHostCpuTime());
70 monitoringEvent.setHostmem(getHostUsedMemory());
71 monitoringEvent.setHostdisk(getHostUsedDisk().toString());
73 monitoringEvent.setJvmid(jvmName);
75 monitoringEvent.setJvmcpu(getJvmCpuTime());
76 monitoringEvent.setJvmmem(getJvmUsedHeapMemory());
77 monitoringEvent.setJvmtnum(getJvmThreads());
79 monitoringEvent.setAppid(appName);
80 // this is probably from healthcheck
81 monitoringEvent.setAppstat("appStatus");
82 return monitoringEvent;
85 private static synchronized MonitoringMetricsFetcher init() {
86 if (instance == null) {
87 instance = new MonitoringMetricsFetcher();
88 threadMXBean = ManagementFactory.getThreadMXBean();
89 memoryMXBean = ManagementFactory.getMemoryMXBean();
90 runtimeMXBean = ManagementFactory.getRuntimeMXBean();
91 platformMBeanServer = ManagementFactory.getPlatformMBeanServer();
92 sigarSession = new Sigar();
93 appName = ExternalConfiguration.getAppName();
94 monitoringLogger.debug("appName is {}", appName);
95 // Accoridng to Yaki, there is no "calculated" jvmName like it was
97 // just "jetty-be" or "jetty-fe"
98 if (appName.contains("fe")) {
99 jvmName = FE_JVM_NAME;
100 } else if (appName.contains("be")) {
101 jvmName = BE_JVM_NAME;
104 .warn("Couldn't determine jvmName, appName is expected to contain \"be\" or \"fe\" string");
111 * Returns the number of live threads for this JVM
113 * @return number of live threads
115 private Integer getJvmThreads() {
116 return threadMXBean.getThreadCount();
120 * Returns the number of used heap memory (bytes)
122 * @return the number of used heap memory (bytes)
124 private long getJvmUsedHeapMemory() {
125 return memoryMXBean.getHeapMemoryUsage().getUsed();
129 * Returns the jvm cpu time (msec)
131 * @return the jvm cpu time (msec)
133 private long getJvmCpuTime() {
137 cpuTime = (long) platformMBeanServer.getAttribute(
138 new ObjectName(ManagementFactory.OPERATING_SYSTEM_MXBEAN_NAME), PROCESS_CPU_TIME_ATTR);
139 } catch (Exception e) {
140 monitoringLogger.error("Couldn't measure JVM CPU time, error: {}", e);
146 * Returns the host total cpu time (msec)
148 * @return the host total cpu time (msec)
150 private long getHostCpuTime() {
153 cpuTime = sigarSession.getCpu().getTotal();
154 } catch (Exception e) {
155 monitoringLogger.error("Couldn't measure host CPU time, error: {}", e);
161 * Returns the host used memory(msec)
163 * @return the host used memory(msec)
165 private Double getHostUsedMemory() {
166 Double memory = -1.0;
168 memory = sigarSession.getMem().getUsedPercent();
169 } catch (Exception e) {
170 monitoringLogger.error("Couldn't measure host used memory, error: {}", e);
176 * Returns the percentage of all available FS
178 * @return the host avail disk(bytes)
180 private Map<String, Double> getHostUsedDisk() {
181 Map<String, Double> res = new HashMap<>();
183 FileSystem[] fileSystemList = sigarSession.getFileSystemList();
184 for (FileSystem fileSystem : fileSystemList) {
186 String dirName = fileSystem.getDirName();
187 double usePercent = sigarSession.getFileSystemUsage(dirName).getUsePercent() * 100;
188 res.put(dirName, usePercent);
190 } catch (Exception e) {
191 monitoringLogger.error("Couldn't measure host used disk, error: {}", e);
201 private String getFQDN() {
204 fqdn = sigarSession.getFQDN();
205 } catch (Exception e) {
206 monitoringLogger.error("Couldn't get FQDN, error: {}", e);