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.be.monitoring;
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25 import org.apache.http.HttpEntity;
26 import org.apache.http.HttpStatus;
27 import org.apache.http.entity.ContentType;
28 import org.apache.http.entity.StringEntity;
29 import org.openecomp.sdc.be.config.Configuration;
30 import org.openecomp.sdc.be.config.ConfigurationManager;
31 import org.openecomp.sdc.common.api.Constants;
32 import org.openecomp.sdc.common.http.client.api.HttpRequest;
33 import org.openecomp.sdc.common.http.client.api.HttpResponse;
34 import org.openecomp.sdc.common.http.config.HttpClientConfig;
35 import org.openecomp.sdc.common.http.config.Timeouts;
36 import org.openecomp.sdc.common.log.wrappers.Logger;
37 import org.openecomp.sdc.common.monitoring.MonitoringEvent;
38 import org.openecomp.sdc.common.monitoring.MonitoringMetricsFetcher;
40 import javax.servlet.ServletContext;
41 import java.util.concurrent.Executors;
42 import java.util.concurrent.ScheduledExecutorService;
43 import java.util.concurrent.ThreadFactory;
44 import java.util.concurrent.TimeUnit;
46 public class BeMonitoringService {
48 private static final String URL = "%s://%s:%s/sdc2/rest/monitoring";
49 private static Logger monitoringLogger = Logger.getLogger("asdc.be.monitoring.service");
50 private static Logger log = Logger.getLogger(BeMonitoringService.class.getName());
51 private static Gson gson = new GsonBuilder().setPrettyPrinting().create();
53 private class MonitoringScheduledTask implements Runnable {
56 monitoringLogger.trace("Executing BE Monitoring Task - Start");
57 MonitoringEvent monitoringMetrics = MonitoringMetricsFetcher.getInstance().getMonitoringMetrics();
58 processMonitoringEvent(monitoringMetrics);
59 monitoringLogger.trace("Executing BE Monitoring Task - Status = {}", monitoringMetrics.toString());
64 * This executor will execute the Monitoring task.
66 ScheduledExecutorService monitoringExecutor = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
68 public Thread newThread(Runnable r) {
69 return new Thread(r, "BE-Monitoring-Thread");
72 private ServletContext context;
74 public BeMonitoringService(ServletContext context) {
75 this.context = context;
78 public void start(int interval) {
79 Configuration config = ((ConfigurationManager) context.getAttribute(Constants.CONFIGURATION_MANAGER_ATTR))
81 if (config.getSystemMonitoring().getEnabled()) {
82 log.info("BE monitoring service is enabled, interval is {} seconds", interval);
83 this.monitoringExecutor.scheduleAtFixedRate(new MonitoringScheduledTask(), 0, interval, TimeUnit.SECONDS);
85 log.info("BE monitoring service is disabled");
89 private void processMonitoringEvent(MonitoringEvent monitoringMetrics) {
91 Configuration config = ((ConfigurationManager) context.getAttribute(Constants.CONFIGURATION_MANAGER_ATTR))
93 String redirectedUrl = String.format(URL, config.getBeProtocol(), config.getBeFqdn(),
94 config.getBeHttpPort());
96 final int timeout = 3000;
97 String monitoringMetricsJson = gson.toJson(monitoringMetrics);
98 HttpEntity myEntity = new StringEntity(monitoringMetricsJson, ContentType.APPLICATION_JSON);
99 HttpResponse<String> httpResponse = HttpRequest.post(redirectedUrl, myEntity, new HttpClientConfig(new Timeouts(timeout, timeout)));
100 int beResponseStatus = httpResponse.getStatusCode();
101 if (beResponseStatus != HttpStatus.SC_OK) {
102 monitoringLogger.error("Unexpected HTTP response from BE : {}", beResponseStatus);
104 } catch (Exception e) {
105 monitoringLogger.error("Monitoring BE failed with exception ", e);