Catalog alignment
[sdc.git] / common-app-api / src / main / java / org / openecomp / sdc / common / monitoring / MonitoringMetricsFetcher.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.common.monitoring;
22
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;
28
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;
36 import java.util.Map;
37
38 public class MonitoringMetricsFetcher {
39
40         private static Logger monitoringLogger = LoggerFactory.getLogger("asdc.fe.monitoring.fetcher");
41
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;
48
49         private static String appName;
50         private static String jvmName = "Unknown";
51         private final String PROCESS_CPU_TIME_ATTR = "ProcessCpuTime";
52
53         private static String FE_JVM_NAME = "jetty-fe";
54         private static String BE_JVM_NAME = "jetty-be";
55
56         private MonitoringMetricsFetcher() {
57         };
58
59         public static MonitoringMetricsFetcher getInstance() {
60                 if (instance == null) {
61                         instance = init();
62                 }
63                 return instance;
64         }
65
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());
72
73                 monitoringEvent.setJvmid(jvmName);
74
75                 monitoringEvent.setJvmcpu(getJvmCpuTime());
76                 monitoringEvent.setJvmmem(getJvmUsedHeapMemory());
77                 monitoringEvent.setJvmtnum(getJvmThreads());
78
79                 monitoringEvent.setAppid(appName);
80                 // this is probably from healthcheck
81                 monitoringEvent.setAppstat("appStatus");
82                 return monitoringEvent;
83         }
84
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
96                         // in TAS,
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;
102                         } else {
103                                 monitoringLogger
104                                                 .warn("Couldn't determine jvmName, appName is expected to contain \"be\" or \"fe\" string");
105                         }
106                 }
107                 return instance;
108         }
109
110         /**
111          * Returns the number of live threads for this JVM
112          * 
113          * @return number of live threads
114          */
115         private Integer getJvmThreads() {
116                 return threadMXBean.getThreadCount();
117         }
118
119         /**
120          * Returns the number of used heap memory (bytes)
121          * 
122          * @return the number of used heap memory (bytes)
123          */
124         private long getJvmUsedHeapMemory() {
125                 return memoryMXBean.getHeapMemoryUsage().getUsed();
126         }
127
128         /**
129          * Returns the jvm cpu time (msec)
130          * 
131          * @return the jvm cpu time (msec)
132          */
133         private long getJvmCpuTime() {
134
135                 long cpuTime = -1;
136                 try {
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);
141                 }
142                 return cpuTime;
143         }
144
145         /**
146          * Returns the host total cpu time (msec)
147          * 
148          * @return the host total cpu time (msec)
149          */
150         private long getHostCpuTime() {
151                 long cpuTime = -1;
152                 try {
153                         cpuTime = sigarSession.getCpu().getTotal();
154                 } catch (Exception e) {
155                         monitoringLogger.error("Couldn't measure host CPU time, error: {}", e);
156                 }
157                 return cpuTime;
158         }
159
160         /**
161          * Returns the host used memory(msec)
162          * 
163          * @return the host used memory(msec)
164          */
165         private Double getHostUsedMemory() {
166                 Double memory = -1.0;
167                 try {
168                         memory = sigarSession.getMem().getUsedPercent();
169                 } catch (Exception e) {
170                         monitoringLogger.error("Couldn't measure host used memory, error: {}", e);
171                 }
172                 return memory;
173         }
174
175         /**
176          * Returns the percentage of all available FS
177          * 
178          * @return the host avail disk(bytes)
179          */
180         private Map<String, Double> getHostUsedDisk() {
181                 Map<String, Double> res = new HashMap<>();
182                 try {
183                         FileSystem[] fileSystemList = sigarSession.getFileSystemList();
184                         for (FileSystem fileSystem : fileSystemList) {
185
186                                 String dirName = fileSystem.getDirName();
187                                 double usePercent = sigarSession.getFileSystemUsage(dirName).getUsePercent() * 100;
188                                 res.put(dirName, usePercent);
189                         }
190                 } catch (Exception e) {
191                         monitoringLogger.error("Couldn't measure host used disk, error: {}", e);
192                 }
193                 return res;
194         }
195
196         /**
197          * Returns the FQDN
198          * 
199          * @return the FQDN
200          */
201         private String getFQDN() {
202                 String fqdn = "";
203                 try {
204                         fqdn = sigarSession.getFQDN();
205                 } catch (Exception e) {
206                         monitoringLogger.error("Couldn't get FQDN, error: {}", e);
207                 }
208                 return fqdn;
209         }
210 }