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 ******************************************************************************/
23 package org.onap.dmaap.datarouter.node;
25 import java.io.BufferedReader;
27 import java.io.FileReader;
28 import java.io.FileWriter;
29 import java.io.Writer;
30 import java.nio.file.Files;
31 import java.nio.file.Paths;
32 import java.util.Arrays;
33 import java.util.TimerTask;
34 import java.util.regex.Matcher;
35 import java.util.regex.Pattern;
38 * Cleanup of old log files.
40 * Periodically scan the log directory for log files that are older than the log file retention interval, and delete
41 * them. In a future release, This class will also be responsible for uploading events logs to the log server to
42 * support the log query APIs.
45 public class LogManager extends TimerTask {
47 private NodeConfigManager config;
48 private Matcher isnodelog;
49 private Matcher iseventlog;
50 private Uploader worker;
51 private String uploaddir;
52 private String logdir;
54 private class Uploader extends Thread implements DeliveryQueueHelper {
56 public long getInitFailureTimer() {
60 public long getWaitForFileProcessFailureTimer() {
64 public double getFailureBackoff() {
68 public long getMaxFailureTimer() {
72 public long getExpirationTimer() {
76 public int getFairFileLimit() {
80 public long getFairTimeLimit() {
84 public String getDestURL(DestInfo destinationInfo, String fileid) {
85 return (config.getEventLogUrl());
88 public void handleUnreachable(DestInfo destinationInfo) {
91 public boolean handleRedirection(DestInfo destinationInfo, String location, String fileid) {
95 public boolean isFollowRedirects() {
99 public String getFeedId(String subid) {
103 private DeliveryQueue dq;
106 dq = new DeliveryQueue(this,
107 new DestInfo("LogUpload", uploaddir, null, null, null, config.getMyName(), config.getMyAuth(), false,
108 false, false, false));
110 setName("Log Uploader");
114 private synchronized void snooze() {
117 } catch (Exception e) {
121 private synchronized void poke() {
133 private void scan() {
134 long threshold = System.currentTimeMillis() - config.getLogRetention();
135 File dir = new File(logdir);
136 String[] fns = dir.list();
138 String lastqueued = "events-000000000000.log";
139 String curlog = StatusLog.getCurLogFile();
140 curlog = curlog.substring(curlog.lastIndexOf('/') + 1);
142 Writer w = new FileWriter(uploaddir + "/.meta");
143 w.write("POST\tlogdata\nContent-Type\ttext/plain\n");
145 BufferedReader br = new BufferedReader(new FileReader(uploaddir + "/.lastqueued"));
146 lastqueued = br.readLine();
148 } catch (Exception e) {
150 for (String fn : fns) {
151 if (!isnodelog.reset(fn).matches()) {
152 if (!iseventlog.reset(fn).matches()) {
155 if (lastqueued.compareTo(fn) < 0 && curlog.compareTo(fn) > 0) {
158 String pid = config.getPublishId();
159 Files.createLink(Paths.get(uploaddir + "/" + pid), Paths.get(logdir + "/" + fn));
160 Files.createLink(Paths.get(uploaddir + "/" + pid + ".M"), Paths.get(uploaddir + "/.meta"));
161 } catch (Exception e) {
165 File f = new File(dir, fn);
166 if (f.lastModified() < threshold) {
170 try (Writer w = new FileWriter(uploaddir + "/.lastqueued")) {
171 (new File(uploaddir + "/.meta")).delete();
172 w.write(lastqueued + "\n");
173 } catch (Exception e) {
179 * Construct a log manager
181 * The log manager will check for expired log files every 5 minutes at 20 seconds after the 5 minute boundary.
182 * (Actually, the interval is the event log rollover interval, which defaults to 5 minutes).
184 public LogManager(NodeConfigManager config) {
185 this.config = config;
187 isnodelog = Pattern.compile("node\\.log\\.\\d{8}").matcher("");
188 iseventlog = Pattern.compile("events-\\d{12}\\.log").matcher("");
189 } catch (Exception e) {
191 logdir = config.getLogDir();
192 uploaddir = logdir + "/.spool";
193 (new File(uploaddir)).mkdirs();
194 long now = System.currentTimeMillis();
195 long intvl = StatusLog.parseInterval(config.getEventLogInterval(), 30000);
196 long when = now - now % intvl + intvl + 20000L;
197 config.getTimer().scheduleAtFixedRate(this, when - now, intvl);
198 worker = new Uploader();
202 * Trigger check for expired log files and log files to upload