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
|
/*-
* ============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.fe.monitoring;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.openecomp.sdc.common.api.Constants;
import org.openecomp.sdc.common.http.client.api.HttpRequest;
import org.openecomp.sdc.common.http.client.api.HttpResponse;
import org.openecomp.sdc.common.http.config.HttpClientConfig;
import org.openecomp.sdc.common.http.config.Timeouts;
import org.openecomp.sdc.common.monitoring.MonitoringEvent;
import org.openecomp.sdc.common.monitoring.MonitoringMetricsFetcher;
import org.openecomp.sdc.fe.config.Configuration;
import org.openecomp.sdc.fe.config.ConfigurationManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.ServletContext;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
public class FeMonitoringService {
private static final String URL = "%s://%s:%s/sdc2/rest/monitoring";
private static Logger monitoringLogger = LoggerFactory.getLogger("asdc.fe.monitoring.service");
private static Logger log = LoggerFactory.getLogger(FeMonitoringService.class.getName());
private static Gson gson = new GsonBuilder().setPrettyPrinting().create();
private class MonitoringScheduledTask implements Runnable {
@Override
public void run() {
monitoringLogger.trace("Executing FE Monitoring Task - Start");
MonitoringEvent monitoringMetrics = MonitoringMetricsFetcher.getInstance().getMonitoringMetrics();
processMonitoringEvent(monitoringMetrics);
monitoringLogger.trace("Executing FE Monitoring Task - Status = {}", monitoringMetrics.toString());
}
}
/**
* This executor will execute the Monitoring task.
*/
ScheduledExecutorService monitoringExecutor = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
return new Thread(r, "FE-Monitoring-Thread");
}
});
private ServletContext context;
public FeMonitoringService(ServletContext context) {
this.context = context;
}
public void start(int interval) {
Configuration config = ((ConfigurationManager) context.getAttribute(Constants.CONFIGURATION_MANAGER_ATTR))
.getConfiguration();
if (config.getSystemMonitoring().getEnabled()) {
log.info("FE monitoring service enabled, interval is {} seconds", interval);
this.monitoringExecutor.scheduleAtFixedRate(new MonitoringScheduledTask(), 0, interval, TimeUnit.SECONDS);
} else {
log.info("FE monitoring service is disabled");
}
}
private void processMonitoringEvent(MonitoringEvent monitoringMetrics) {
try {
Configuration config = ((ConfigurationManager) context.getAttribute(Constants.CONFIGURATION_MANAGER_ATTR))
.getConfiguration();
String redirectedUrl = String.format(URL, config.getBeProtocol(), config.getBeHost(),
Constants.HTTPS.equals(config.getBeProtocol()) ? config.getBeSslPort() : config.getBeHttpPort());
int timeout = 3000;
String monitoringMetricsJson = gson.toJson(monitoringMetrics);
HttpEntity myEntity = new StringEntity(monitoringMetricsJson, ContentType.APPLICATION_JSON);
HttpResponse<String> resposne = HttpRequest.post(redirectedUrl, myEntity, new HttpClientConfig(new Timeouts(timeout, timeout)));
int beResponseStatus = resposne.getStatusCode();
if (beResponseStatus != HttpStatus.SC_OK) {
monitoringLogger.error("Unexpected HTTP response from BE : {}", beResponseStatus);
}
} catch (Exception e) {
monitoringLogger.error("Monitoring BE failed with exception ", e);
}
}
}
|