Reformat common-app-api
[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 package org.openecomp.sdc.common.monitoring;
21
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;
27 import java.util.Map;
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;
35
36 public class MonitoringMetricsFetcher {
37
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";
50
51     private MonitoringMetricsFetcher() {
52     }
53
54     public static MonitoringMetricsFetcher getInstance() {
55         if (instance == null) {
56             instance = init();
57         }
58         return instance;
59     }
60
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;
75     }
76
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
88
89             // in TAS,
90
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;
96             } else {
97                 monitoringLogger.warn("Couldn't determine jvmName, appName is expected to contain \"be\" or \"fe\" string");
98             }
99         }
100         return instance;
101     }
102
103     /**
104      * Returns the number of live threads for this JVM
105      *
106      * @return number of live threads
107      */
108     private Integer getJvmThreads() {
109         return threadMXBean.getThreadCount();
110     }
111
112     /**
113      * Returns the number of used heap memory (bytes)
114      *
115      * @return the number of used heap memory (bytes)
116      */
117     private long getJvmUsedHeapMemory() {
118         return memoryMXBean.getHeapMemoryUsage().getUsed();
119     }
120
121     /**
122      * Returns the jvm cpu time (msec)
123      *
124      * @return the jvm cpu time (msec)
125      */
126     private long getJvmCpuTime() {
127         long cpuTime = -1;
128         try {
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);
132         }
133         return cpuTime;
134     }
135
136     /**
137      * Returns the host total cpu time (msec)
138      *
139      * @return the host total cpu time (msec)
140      */
141     private long getHostCpuTime() {
142         long cpuTime = -1;
143         try {
144             cpuTime = sigarSession.getCpu().getTotal();
145         } catch (Exception e) {
146             monitoringLogger.error("Couldn't measure host CPU time", e);
147         }
148         return cpuTime;
149     }
150
151     /**
152      * Returns the host used memory(msec)
153      *
154      * @return the host used memory(msec)
155      */
156     private Double getHostUsedMemory() {
157         Double memory = -1.0;
158         try {
159             memory = sigarSession.getMem().getUsedPercent();
160         } catch (Exception e) {
161             monitoringLogger.error("Couldn't measure host used memory", e);
162         }
163         return memory;
164     }
165
166     /**
167      * Returns the percentage of all available FS
168      *
169      * @return the host avail disk(bytes)
170      */
171     private Map<String, Double> getHostUsedDisk() {
172         Map<String, Double> res = new HashMap<>();
173         try {
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);
179             }
180         } catch (Exception e) {
181             monitoringLogger.error("Couldn't measure host used disk", e);
182         }
183         return res;
184     }
185
186     /**
187      * Returns the FQDN
188      *
189      * @return the FQDN
190      */
191     private String getFQDN() {
192         String fqdn = "";
193         try {
194             fqdn = sigarSession.getFQDN();
195         } catch (Exception e) {
196             monitoringLogger.error("Couldn't get FQDN", e);
197         }
198         return fqdn;
199     }
200 }