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 final int TO_PERCENT_MULTIPLIER = 100;
41 private static Logger monitoringLogger = LoggerFactory.getLogger("asdc.fe.monitoring.fetcher");
43 private static volatile MonitoringMetricsFetcher instance;
44 private static RuntimeMXBean runtimeMXBean;
45 private static ThreadMXBean threadMXBean;
46 private static MemoryMXBean memoryMXBean;
47 private static MBeanServer platformMBeanServer;
48 private static Sigar sigarSession;
50 private static String appName;
51 private static String jvmName = "Unknown";
52 private static final String PROCESS_CPU_TIME_ATTR = "ProcessCpuTime";
54 private static final String FE_JVM_NAME = "jetty-fe";
55 private static final String BE_JVM_NAME = "jetty-be";
57 private MonitoringMetricsFetcher() {
60 public static MonitoringMetricsFetcher getInstance() {
61 if (instance == null) {
67 public MonitoringEvent getMonitoringMetrics() {
68 MonitoringEvent monitoringEvent = new MonitoringEvent();
69 monitoringEvent.setHostid(getFQDN());
70 monitoringEvent.setHostcpu(getHostCpuTime());
71 monitoringEvent.setHostmem(getHostUsedMemory());
72 monitoringEvent.setHostdisk(getHostUsedDisk().toString());
74 monitoringEvent.setJvmid(jvmName);
76 monitoringEvent.setJvmcpu(getJvmCpuTime());
77 monitoringEvent.setJvmmem(getJvmUsedHeapMemory());
78 monitoringEvent.setJvmtnum(getJvmThreads());
80 monitoringEvent.setAppid(appName);
81 // this is probably from healthcheck
82 monitoringEvent.setAppstat("appStatus");
83 return monitoringEvent;
86 private static synchronized MonitoringMetricsFetcher init() {
87 if (instance == null) {
88 instance = new MonitoringMetricsFetcher();
89 threadMXBean = ManagementFactory.getThreadMXBean();
90 memoryMXBean = ManagementFactory.getMemoryMXBean();
91 runtimeMXBean = ManagementFactory.getRuntimeMXBean();
92 platformMBeanServer = ManagementFactory.getPlatformMBeanServer();
93 sigarSession = new Sigar();
94 appName = ExternalConfiguration.getAppName();
95 monitoringLogger.debug("appName is {}", appName);
96 // Accoridng to Yaki, there is no "calculated" jvmName like it was
98 // just "jetty-be" or "jetty-fe"
99 if (appName.contains("fe")) {
100 jvmName = FE_JVM_NAME;
101 } else if (appName.contains("be")) {
102 jvmName = BE_JVM_NAME;
105 .warn("Couldn't determine jvmName, appName is expected to contain \"be\" or \"fe\" string");
112 * Returns the number of live threads for this JVM
114 * @return number of live threads
116 private Integer getJvmThreads() {
117 return threadMXBean.getThreadCount();
121 * Returns the number of used heap memory (bytes)
123 * @return the number of used heap memory (bytes)
125 private long getJvmUsedHeapMemory() {
126 return memoryMXBean.getHeapMemoryUsage().getUsed();
130 * Returns the jvm cpu time (msec)
132 * @return the jvm cpu time (msec)
134 private long getJvmCpuTime() {
138 cpuTime = (long) platformMBeanServer.getAttribute(
139 new ObjectName(ManagementFactory.OPERATING_SYSTEM_MXBEAN_NAME), PROCESS_CPU_TIME_ATTR);
140 } catch (Exception e) {
141 monitoringLogger.error("Couldn't measure JVM CPU time, error: {}", e);
147 * Returns the host total cpu time (msec)
149 * @return the host total cpu time (msec)
151 private long getHostCpuTime() {
154 cpuTime = sigarSession.getCpu().getTotal();
155 } catch (Exception e) {
156 monitoringLogger.error("Couldn't measure host CPU time, error: {}", e);
162 * Returns the host used memory(msec)
164 * @return the host used memory(msec)
166 private Double getHostUsedMemory() {
167 Double memory = -1.0;
169 memory = sigarSession.getMem().getUsedPercent();
170 } catch (Exception e) {
171 monitoringLogger.error("Couldn't measure host used memory, error: {}", e);
177 * Returns the percentage of all available FS
179 * @return the host avail disk(bytes)
181 private Map<String, Double> getHostUsedDisk() {
182 Map<String, Double> res = new HashMap<>();
184 FileSystem[] fileSystemList = sigarSession.getFileSystemList();
185 for (FileSystem fileSystem : fileSystemList) {
187 String dirName = fileSystem.getDirName();
188 double usePercent = sigarSession.getFileSystemUsage(dirName).getUsePercent() * TO_PERCENT_MULTIPLIER;
189 res.put(dirName, usePercent);
191 } catch (Exception e) {
192 monitoringLogger.error("Couldn't measure host used disk, error: {}", e);
202 private String getFQDN() {
205 fqdn = sigarSession.getFQDN();
206 } catch (Exception e) {
207 monitoringLogger.error("Couldn't get FQDN, error: {}", e);