1 /*******************************************************************************
2 * ============LICENSE_START==================================================
4 * * ===========================================================================
5 * * Copyright © 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====================================================
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 ******************************************************************************/
24 package org.onap.dmaap.datarouter.node;
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import java.io.BufferedReader;
30 import java.io.FileReader;
31 import java.io.FileWriter;
32 import java.io.Writer;
33 import java.nio.file.Files;
34 import java.nio.file.Paths;
35 import java.util.Arrays;
36 import java.util.TimerTask;
37 import java.util.regex.Matcher;
38 import java.util.regex.Pattern;
39 import org.jetbrains.annotations.NotNull;
42 * Cleanup of old log files.
44 * <p>Periodically scan the log directory for log files that are older than the log file retention interval, and delete
45 * them. In a future release, This class will also be responsible for uploading events logs to the log server to
46 * support the log query APIs.
49 public class LogManager extends TimerTask {
51 private static final String EXCEPTION = "Exception";
52 private EELFLogger logger = EELFManager.getInstance().getLogger(LogManager.class);
53 private NodeConfigManager config;
54 private Matcher isnodelog;
55 private Matcher iseventlog;
56 private Uploader worker;
57 private String uploaddir;
58 private String logdir;
61 * Construct a log manager
63 * <p>The log manager will check for expired log files every 5 minutes at 20 seconds after the 5 minute boundary.
64 * (Actually, the interval is the event log rollover interval, which defaults to 5 minutes).
66 public LogManager(NodeConfigManager config) {
69 isnodelog = Pattern.compile("node\\.log\\.\\d{8}").matcher("");
70 iseventlog = Pattern.compile("events-\\d{12}\\.log").matcher("");
71 } catch (Exception e) {
72 logger.error(EXCEPTION, e);
74 logdir = config.getLogDir();
75 uploaddir = logdir + "/.spool";
76 (new File(uploaddir)).mkdirs();
77 long now = System.currentTimeMillis();
78 long intvl = StatusLog.parseInterval(config.getEventLogInterval(), 30000);
79 long when = now - now % intvl + intvl + 20000L;
80 config.getTimer().scheduleAtFixedRate(this, when - now, intvl);
81 worker = new Uploader();
85 * Trigger check for expired log files and log files to upload.
91 public Uploader getWorker() {
95 class Uploader extends Thread implements DeliveryQueueHelper {
97 private static final String META = "/.meta";
98 private EELFLogger logger = EELFManager.getInstance().getLogger(Uploader.class);
99 private DeliveryQueue dq;
102 dq = new DeliveryQueue(this,
103 new DestInfoBuilder().setName("LogUpload").setSpool(uploaddir).setSubid(null).setLogdata(null)
104 .setUrl(null).setAuthuser(config.getMyName()).setAuthentication(config.getMyAuth())
105 .setMetaonly(false).setUse100(false).setPrivilegedSubscriber(false)
106 .setFollowRedirects(false)
107 .setDecompress(false).createDestInfo());
109 setName("Log Uploader");
113 public long getInitFailureTimer() {
117 public long getWaitForFileProcessFailureTimer() {
121 public double getFailureBackoff() {
125 public long getMaxFailureTimer() {
129 public long getExpirationTimer() {
133 public int getFairFileLimit() {
137 public long getFairTimeLimit() {
141 public String getDestURL(DestInfo destinationInfo, String fileid) {
142 return (config.getEventLogUrl());
145 public void handleUnreachable(DestInfo destinationInfo) {
146 throw new UnsupportedOperationException();
149 public boolean handleRedirection(DestInfo destinationInfo, String location, String fileid) {
153 public boolean isFollowRedirects() {
157 public String getFeedId(String subid) {
161 private synchronized void snooze() {
164 } catch (Exception e) {
165 logger.error(EXCEPTION, e);
169 private synchronized void poke() {
182 private void scan() {
183 long threshold = System.currentTimeMillis() - config.getLogRetention();
184 File dir = new File(logdir);
185 String[] fns = dir.list();
187 String lastqueued = "events-000000000000.log";
188 String curlog = StatusLog.getCurLogFile();
189 curlog = curlog.substring(curlog.lastIndexOf('/') + 1);
191 Writer writer = new FileWriter(uploaddir + META);
192 writer.write("POST\tlogdata\nContent-Type\ttext/plain\n");
194 BufferedReader br = new BufferedReader(new FileReader(uploaddir + "/.lastqueued"));
195 lastqueued = br.readLine();
197 } catch (Exception e) {
198 logger.error(EXCEPTION, e);
200 for (String fn : fns) {
201 if (!isnodelog.reset(fn).matches()) {
202 if (!iseventlog.reset(fn).matches()) {
205 lastqueued = setLastQueued(lastqueued, curlog, fn);
207 File file = new File(dir, fn);
208 if (file.lastModified() < threshold) {
212 try (Writer w = new FileWriter(uploaddir + "/.lastqueued")) {
213 (new File(uploaddir + META)).delete();
214 w.write(lastqueued + "\n");
215 } catch (Exception e) {
216 logger.error(EXCEPTION, e);
221 private String setLastQueued(String lastqueued, String curlog, String fn) {
222 if (lastqueued.compareTo(fn) < 0 && curlog.compareTo(fn) > 0) {
225 String pid = config.getPublishId();
226 Files.createLink(Paths.get(uploaddir + "/" + pid), Paths.get(logdir + "/" + fn));
227 Files.createLink(Paths.get(uploaddir + "/" + pid + ".M"), Paths.get(uploaddir + META));
228 } catch (Exception e) {
229 logger.error(EXCEPTION, e);