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 EELFLogger logger = EELFManager.getInstance().getLogger(LogManager.class);
52 private NodeConfigManager config;
53 private Matcher isnodelog;
54 private Matcher iseventlog;
55 private Uploader worker;
56 private String uploaddir;
57 private String logdir;
60 * Construct a log manager
62 * <p>The log manager will check for expired log files every 5 minutes at 20 seconds after the 5 minute boundary.
63 * (Actually, the interval is the event log rollover interval, which defaults to 5 minutes).
65 public LogManager(NodeConfigManager config) {
68 isnodelog = Pattern.compile("node\\.log\\.\\d{8}").matcher("");
69 iseventlog = Pattern.compile("events-\\d{12}\\.log").matcher("");
70 } catch (Exception e) {
71 logger.error("Exception", e);
73 logdir = config.getLogDir();
74 uploaddir = logdir + "/.spool";
75 (new File(uploaddir)).mkdirs();
76 long now = System.currentTimeMillis();
77 long intvl = StatusLog.parseInterval(config.getEventLogInterval(), 30000);
78 long when = now - now % intvl + intvl + 20000L;
79 config.getTimer().scheduleAtFixedRate(this, when - now, intvl);
80 worker = new Uploader();
84 * Trigger check for expired log files and log files to upload.
90 private class Uploader extends Thread implements DeliveryQueueHelper {
92 private static final String EXCEPTION = "Exception";
93 private static final String META = "/.meta";
94 private EELFLogger logger = EELFManager.getInstance().getLogger(Uploader.class);
95 private DeliveryQueue dq;
98 dq = new DeliveryQueue(this,
99 new DestInfoBuilder().setName("LogUpload").setSpool(uploaddir).setSubid(null).setLogdata(null)
100 .setUrl(null).setAuthuser(config.getMyName()).setAuthentication(config.getMyAuth())
101 .setMetaonly(false).setUse100(false).setPrivilegedSubscriber(false)
102 .setFollowRedirects(false)
103 .setDecompress(false).createDestInfo());
105 setName("Log Uploader");
109 public long getInitFailureTimer() {
113 public long getWaitForFileProcessFailureTimer() {
117 public double getFailureBackoff() {
121 public long getMaxFailureTimer() {
125 public long getExpirationTimer() {
129 public int getFairFileLimit() {
133 public long getFairTimeLimit() {
137 public String getDestURL(DestInfo destinationInfo, String fileid) {
138 return (config.getEventLogUrl());
141 public void handleUnreachable(DestInfo destinationInfo) {
142 throw new UnsupportedOperationException();
145 public boolean handleRedirection(DestInfo destinationInfo, String location, String fileid) {
149 public boolean isFollowRedirects() {
153 public String getFeedId(String subid) {
157 private synchronized void snooze() {
160 } catch (Exception e) {
161 logger.error(EXCEPTION, e);
165 private synchronized void poke() {
177 private void scan() {
178 long threshold = System.currentTimeMillis() - config.getLogRetention();
179 File dir = new File(logdir);
180 String[] fns = dir.list();
182 String lastqueued = "events-000000000000.log";
183 String curlog = StatusLog.getCurLogFile();
184 curlog = curlog.substring(curlog.lastIndexOf('/') + 1);
186 Writer writer = new FileWriter(uploaddir + META);
187 writer.write("POST\tlogdata\nContent-Type\ttext/plain\n");
189 BufferedReader br = new BufferedReader(new FileReader(uploaddir + "/.lastqueued"));
190 lastqueued = br.readLine();
192 } catch (Exception e) {
193 logger.error(EXCEPTION, e);
195 for (String fn : fns) {
196 if (!isnodelog.reset(fn).matches()) {
197 if (!iseventlog.reset(fn).matches()) {
200 lastqueued = setLastQueued(lastqueued, curlog, fn);
202 File file = new File(dir, fn);
203 if (file.lastModified() < threshold) {
207 try (Writer w = new FileWriter(uploaddir + "/.lastqueued")) {
208 (new File(uploaddir + META)).delete();
209 w.write(lastqueued + "\n");
210 } catch (Exception e) {
211 logger.error(EXCEPTION, e);
216 private String setLastQueued(String lastqueued, String curlog, String fn) {
217 if (lastqueued.compareTo(fn) < 0 && curlog.compareTo(fn) > 0) {
220 String pid = config.getPublishId();
221 Files.createLink(Paths.get(uploaddir + "/" + pid), Paths.get(logdir + "/" + fn));
222 Files.createLink(Paths.get(uploaddir + "/" + pid + ".M"), Paths.get(uploaddir + META));
223 } catch (Exception e) {
224 logger.error(EXCEPTION, e);