summaryrefslogtreecommitdiffstats
path: root/common-app-api/src/main/java/org/openecomp/sdc/common/monitoring/MonitoringMetricsFetcher.java
blob: 9df1aea98368012628db9b16ef5851e7d09d4ce0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * 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.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.sdc.common.monitoring;

import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.RuntimeMXBean;
import java.lang.management.ThreadMXBean;
import java.util.HashMap;
import java.util.Map;

import javax.management.MBeanServer;
import javax.management.ObjectName;

import org.hyperic.sigar.FileSystem;
import org.hyperic.sigar.Sigar;
import org.openecomp.sdc.common.impl.ExternalConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MonitoringMetricsFetcher {

	private static Logger monitoringLogger = LoggerFactory.getLogger("asdc.fe.monitoring.fetcher");

	private static volatile MonitoringMetricsFetcher instance;
	private static RuntimeMXBean runtimeMXBean;
	private static ThreadMXBean threadMXBean;
	private static MemoryMXBean memoryMXBean;
	private static MBeanServer platformMBeanServer;
	private static Sigar sigarSession;

	private static String appName;
	private static String jvmName = "Unknown";
	private final String PROCESS_CPU_TIME_ATTR = "ProcessCpuTime";

	private static String FE_JVM_NAME = "jetty-fe";
	private static String BE_JVM_NAME = "jetty-be";

	private MonitoringMetricsFetcher() {
	};

	public static MonitoringMetricsFetcher getInstance() {
		if (instance == null) {
			instance = init();
		}
		return instance;
	}

	public MonitoringEvent getMonitoringMetrics() {
		MonitoringEvent monitoringEvent = new MonitoringEvent();
		monitoringEvent.setHostid(getFQDN());
		monitoringEvent.setHostcpu(getHostCpuTime());
		monitoringEvent.setHostmem(getHostUsedMemory());
		monitoringEvent.setHostdisk(getHostUsedDisk().toString());

		monitoringEvent.setJvmid(jvmName);

		monitoringEvent.setJvmcpu(getJvmCpuTime());
		monitoringEvent.setJvmmem(getJvmUsedHeapMemory());
		monitoringEvent.setJvmtnum(getJvmThreads());

		monitoringEvent.setAppid(appName);
		// this is probably from healthcheck
		// TODO
		monitoringEvent.setAppstat("appStatus");
		return monitoringEvent;
	}

	private static synchronized MonitoringMetricsFetcher init() {
		if (instance == null) {
			instance = new MonitoringMetricsFetcher();
			threadMXBean = ManagementFactory.getThreadMXBean();
			memoryMXBean = ManagementFactory.getMemoryMXBean();
			runtimeMXBean = ManagementFactory.getRuntimeMXBean();
			platformMBeanServer = ManagementFactory.getPlatformMBeanServer();
			sigarSession = new Sigar();
			appName = ExternalConfiguration.getAppName();
			monitoringLogger.debug("appName is {}", appName);
			// Accoridng to Yaki, there is no "calculated" jvmName like it was
			// in TAS,
			// just "jetty-be" or "jetty-fe"
			if (appName.contains("fe")) {
				jvmName = FE_JVM_NAME;
			} else if (appName.contains("be")) {
				jvmName = BE_JVM_NAME;
			} else {
				monitoringLogger
						.warn("Couldn't determine jvmName, appName is expected to contain \"be\" or \"fe\" string");
			}
		}
		return instance;
	}

	/**
	 * Returns the number of live threads for this JVM
	 * 
	 * @return number of live threads
	 */
	private Integer getJvmThreads() {
		return threadMXBean.getThreadCount();
	}

	/**
	 * Returns the number of used heap memory (bytes)
	 * 
	 * @return the number of used heap memory (bytes)
	 */
	private long getJvmUsedHeapMemory() {
		return memoryMXBean.getHeapMemoryUsage().getUsed();
	}

	/**
	 * Returns the jvm cpu time (msec)
	 * 
	 * @return the jvm cpu time (msec)
	 */
	private long getJvmCpuTime() {

		long cpuTime = -1;
		try {
			cpuTime = (long) platformMBeanServer.getAttribute(
					new ObjectName(ManagementFactory.OPERATING_SYSTEM_MXBEAN_NAME), PROCESS_CPU_TIME_ATTR);
		} catch (Exception e) {
			monitoringLogger.error("Couldn't measure JVM CPU time, error: {}", e);
		}
		return cpuTime;
	}

	/**
	 * Returns the host total cpu time (msec)
	 * 
	 * @return the host total cpu time (msec)
	 */
	private long getHostCpuTime() {
		long cpuTime = -1;
		try {
			cpuTime = sigarSession.getCpu().getTotal();
		} catch (Exception e) {
			monitoringLogger.error("Couldn't measure host CPU time, error: {}", e);
		}
		return cpuTime;
	}

	/**
	 * Returns the host used memory(msec)
	 * 
	 * @return the host used memory(msec)
	 */
	private Double getHostUsedMemory() {
		Double memory = -1.0;
		try {
			memory = sigarSession.getMem().getUsedPercent();
		} catch (Exception e) {
			monitoringLogger.error("Couldn't measure host used memory, error: {}", e);
		}
		return memory;
	}

	/**
	 * Returns the percentage of all available FS
	 * 
	 * @return the host avail disk(bytes)
	 */
	private Map<String, Double> getHostUsedDisk() {
		Map<String, Double> res = new HashMap<>();
		try {
			FileSystem[] fileSystemList = sigarSession.getFileSystemList();
			for (FileSystem fileSystem : fileSystemList) {

				String dirName = fileSystem.getDirName();
				double usePercent = sigarSession.getFileSystemUsage(dirName).getUsePercent() * 100;
				res.put(dirName, usePercent);
			}
		} catch (Exception e) {
			monitoringLogger.error("Couldn't measure host used disk, error: {}", e);
		}
		return res;
	}

	/**
	 * Returns the FQDN
	 * 
	 * @return the FQDN
	 */
	private String getFQDN() {
		String fqdn = "";
		try {
			fqdn = sigarSession.getFQDN();
		} catch (Exception e) {
			monitoringLogger.error("Couldn't get FQDN, error: {}", e);
		}
		return fqdn;
	}
}