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