1 | | - | package com.nccgroup.loggerplusplus.exports; |
2 | | - | |
3 | | - | import com.nccgroup.loggerplusplus.*; |
4 | | - | import com.nccgroup.loggerplusplus.logentry.LogEntry; |
5 | | - | import com.nccgroup.loggerplusplus.logview.processor.LogProcessor; |
6 | | - | import com.nccgroup.loggerplusplus.util.Globals; |
7 | | - | import org.apache.http.HttpHost; |
8 | | - | import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; |
9 | | - | import org.elasticsearch.action.admin.indices.create.CreateIndexResponse; |
10 | | - | import org.elasticsearch.action.admin.indices.get.GetIndexRequest; |
11 | | - | import org.elasticsearch.action.bulk.BulkItemResponse; |
12 | | - | import org.elasticsearch.action.bulk.BulkRequest; |
13 | | - | import org.elasticsearch.action.bulk.BulkResponse; |
14 | | - | import org.elasticsearch.action.index.IndexRequest; |
15 | | - | import org.elasticsearch.action.index.IndexRequestBuilder; |
16 | | - | import org.elasticsearch.client.*; |
17 | | - | import org.elasticsearch.common.settings.Settings; |
18 | | - | |
19 | | - | import java.io.IOException; |
20 | | - | import java.net.InetAddress; |
21 | | - | import java.net.UnknownHostException; |
22 | | - | import java.util.ArrayList; |
23 | | - | import java.util.concurrent.Executors; |
24 | | - | import java.util.concurrent.ScheduledExecutorService; |
25 | | - | import java.util.concurrent.ScheduledFuture; |
26 | | - | import java.util.concurrent.TimeUnit; |
27 | | - | |
28 | | - | import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; |
29 | | - | |
30 | | - | public class ElasticSearchLogger { |
31 | | - | //TODO REIMPLEMENT |
32 | | - | // IndicesAdminClient adminClient; |
33 | | - | // Client client; |
34 | | - | // RestHighLevelClient httpClient; |
35 | | - | // ArrayList<LogEntry> pendingEntries; |
36 | | - | // private InetAddress address; |
37 | | - | // private int port; |
38 | | - | // private String clusterName; |
39 | | - | // private boolean isEnabled; |
40 | | - | // private String indexName; |
41 | | - | // private boolean includeReqResp; |
42 | | - | // |
43 | | - | // private final ScheduledExecutorService executorService; |
44 | | - | // private ScheduledFuture indexTask; |
45 | | - | // |
46 | | - | // |
47 | | - | // public ElasticSearchLogger(LogProcessor logProcessor){ |
48 | | - | // this.isEnabled = false; |
49 | | - | // this.indexName = "logger"; |
50 | | - | // |
51 | | - | // logProcessor.addLogListener(this); |
52 | | - | // executorService = Executors.newScheduledThreadPool(1); |
53 | | - | // } |
54 | | - | // |
55 | | - | // public void setEnabled(boolean isEnabled) throws UnknownHostException { |
56 | | - | // if(isEnabled){ |
57 | | - | // this.address = InetAddress.getByName(LoggerPlusPlus.preferences.getSetting(Globals.PREF_ELASTIC_ADDRESS)); |
58 | | - | // this.port = LoggerPlusPlus.preferences.getSetting(Globals.PREF_ELASTIC_PORT); |
59 | | - | // this.clusterName = LoggerPlusPlus.preferences.getSetting(Globals.PREF_ELASTIC_CLUSTER_NAME); |
60 | | - | // this.indexName = LoggerPlusPlus.preferences.getSetting(Globals.PREF_ELASTIC_INDEX); |
61 | | - | // Settings settings = Settings.builder().put("cluster.name", this.clusterName).build(); |
62 | | - | // |
63 | | - | // httpClient = new RestHighLevelClient(RestClient.builder( |
64 | | - | // new HttpHost(this.address, this.port, "http"))); |
65 | | - | // |
66 | | - | // createIndices(); |
67 | | - | // pendingEntries = new ArrayList<>(); |
68 | | - | // includeReqResp = LoggerPlusPlus.preferences.getSetting(Globals.PREF_ELASTIC_INCLUDE_REQ_RESP); |
69 | | - | // int delay = LoggerPlusPlus.preferences.getSetting(Globals.PREF_ELASTIC_DELAY); |
70 | | - | // indexTask = executorService.scheduleAtFixedRate(() -> indexPendingEntries(), delay, delay, TimeUnit.SECONDS); |
71 | | - | // }else{ |
72 | | - | // if(this.indexTask != null){ |
73 | | - | // indexTask.cancel(true); |
74 | | - | // } |
75 | | - | // this.pendingEntries = null; |
76 | | - | // this.client = null; |
77 | | - | // this.adminClient = null; |
78 | | - | // } |
79 | | - | // this.isEnabled = isEnabled; |
80 | | - | // } |
81 | | - | // |
82 | | - | // private void createIndices(){ |
83 | | - | // GetIndexRequest request = new GetIndexRequest(); |
84 | | - | // request.indices(this.indexName); |
85 | | - | // |
86 | | - | // boolean exists = false; |
87 | | - | // try { |
88 | | - | // exists = httpClient.indices().exists(request, RequestOptions.DEFAULT); |
89 | | - | // } catch (IOException e) { |
90 | | - | // e.printStackTrace(); |
91 | | - | // } |
92 | | - | // |
93 | | - | // if(!exists) { |
94 | | - | // CreateIndexRequest _request = new CreateIndexRequest(this.indexName); |
95 | | - | // |
96 | | - | // try { |
97 | | - | // CreateIndexResponse createIndexResponse = httpClient.indices().create(_request, RequestOptions.DEFAULT); |
98 | | - | // } catch (IOException e) { |
99 | | - | // e.printStackTrace(); |
100 | | - | // } |
101 | | - | // } |
102 | | - | // } |
103 | | - | // |
104 | | - | // public IndexRequest buildIndexRequest(LogEntry logEntry){ |
105 | | - | // try{ |
106 | | - | // IndexRequestBuilder requestBuilder = client.prepareIndex(this.indexName, "requestresponse") |
107 | | - | // .setSource( |
108 | | - | // jsonBuilder().startObject() |
109 | | - | // .field("protocol", logEntry.protocol) |
110 | | - | // .field("method", logEntry.method) |
111 | | - | // .field("host", logEntry.hostname) |
112 | | - | // .field("path", logEntry.url.getPath()) |
113 | | - | // .field("requesttime", logEntry.formattedRequestTime.equals("NA") ? null : logEntry.formattedRequestTime) |
114 | | - | // .field("responsetime", logEntry.formattedResponseTime.equals("NA") ? null : logEntry.formattedResponseTime) |
115 | | - | // .field("responsedelay", logEntry.requestResponseDelay) |
116 | | - | // .field("status", logEntry.responseStatus) |
117 | | - | // .field("title", logEntry.title) |
118 | | - | // .field("newcookies", logEntry.newCookies) |
119 | | - | // .field("sentcookies", logEntry.sentCookies) |
120 | | - | // .field("referrer", logEntry.referrerURL) |
121 | | - | // .field("requestcontenttype", logEntry.requestContentType) |
122 | | - | // .field("requestlength", logEntry.requestLength) |
123 | | - | // .field("responselength", logEntry.responseLength) |
124 | | - | // .field("requestbody", this.includeReqResp ? new String(logEntry.requestResponse.getRequest()) : "") |
125 | | - | // .field("responsebody", this.includeReqResp ? new String(logEntry.requestResponse.getResponse()) : "") |
126 | | - | // .endObject() |
127 | | - | // ); |
128 | | - | // return requestBuilder.request(); |
129 | | - | // } catch (IOException e) { |
130 | | - | // e.printStackTrace(); |
131 | | - | // } |
132 | | - | // return null; |
133 | | - | // } |
134 | | - | // |
135 | | - | // private void addToPending(LogEntry logEntry){ |
136 | | - | // if(!this.isEnabled) return; |
137 | | - | // synchronized (pendingEntries) { |
138 | | - | // pendingEntries.add(logEntry); |
139 | | - | // } |
140 | | - | // } |
141 | | - | // |
142 | | - | // private void indexPendingEntries(){ |
143 | | - | // if(!this.isEnabled || this.pendingEntries.size() == 0) return; |
144 | | - | // |
145 | | - | // BulkRequest httpBulkBuilder = new BulkRequest(); |
146 | | - | // |
147 | | - | // ArrayList<LogEntry> entriesInBulk; |
148 | | - | // synchronized (pendingEntries){ |
149 | | - | // entriesInBulk = (ArrayList<LogEntry>) pendingEntries.clone(); |
150 | | - | // pendingEntries.clear(); |
151 | | - | // } |
152 | | - | // |
153 | | - | // for (LogEntry logEntry : entriesInBulk) { |
154 | | - | // IndexRequest request = buildIndexRequest(logEntry); |
155 | | - | // if(request != null) { |
156 | | - | // httpBulkBuilder.add(request); |
157 | | - | // }else{ |
158 | | - | // //Could not buildPreferences index request. Ignore it? |
159 | | - | // } |
160 | | - | // } |
161 | | - | // |
162 | | - | // try { |
163 | | - | // BulkResponse bulkResponse = httpClient.bulk(httpBulkBuilder, RequestOptions.DEFAULT); |
164 | | - | // if(bulkResponse.hasFailures()){ |
165 | | - | // for (BulkItemResponse bulkItemResponse : bulkResponse.getItems()) { |
166 | | - | // System.err.println(bulkItemResponse.getFailureMessage()); |
167 | | - | // } |
168 | | - | // } |
169 | | - | // } catch (IOException e) { |
170 | | - | // e.printStackTrace(); |
171 | | - | // } |
172 | | - | // |
173 | | - | //// if(resp.hasFailures()){ |
174 | | - | //// for (BulkItemResponse bulkItemResponse : resp.getItems()) { |
175 | | - | //// System.err.println(bulkItemResponse.getFailureMessage()); |
176 | | - | //// } |
177 | | - | //// } |
178 | | - | // } |
179 | | - | // |
180 | | - | // @Override |
181 | | - | // public void onRequestAdded(int modelIndex, LogEntry logEntry, boolean hasResponse) { |
182 | | - | // if(!this.isEnabled) return; |
183 | | - | // if(hasResponse){ |
184 | | - | // addToPending(logEntry); |
185 | | - | // } |
186 | | - | // } |
187 | | - | // |
188 | | - | // @Override |
189 | | - | // public void onResponseUpdated(int modelRow, LogEntry existingEntry) { |
190 | | - | // if(!this.isEnabled) return; |
191 | | - | // addToPending(existingEntry); |
192 | | - | // } |
193 | | - | // |
194 | | - | // @Override |
195 | | - | // public void onRequestRemoved(int modelIndex, LogEntry logEntry) { |
196 | | - | // |
197 | | - | // } |
198 | | - | // |
199 | | - | // @Override |
200 | | - | // public void onLogsCleared() { |
201 | | - | // |
202 | | - | // } |
203 | | - | } |
204 | | - | |