Commit c94b4ecc authored by CompileNix's avatar CompileNix

Initial

parents
/node_modules
*.txt
/Config.js
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}/Index.js"
}
]
}
\ No newline at end of file
class Config {
constructor() {
this.smtpUsername = 'smtp login username';
this.smtpPassword = 'password';
this.smtpHost = 'your.email.server';
this.smtpPort = 25;
this.smtps = false;
this.mailfrom = 'foo@baar.local';
this.mailto = 'foo@baar.local';
this.subjectPrefix = 'access_log mailer: ';
this.filesToWatch = [
'test.txt',
'test1.txt'
];
this.expressions = [
{ match: /HTTP\/\d\.\d\" 404/g, subject: 'HTTP 404', matchCounter: 0 },
{ match: /HTTP\/\d\.\d\" 5\d{2}/g, subject: 'HTTP 5xx', matchCounter: 0 }
]
}
};
module.exports = new Config();
'use-strict';
const fs = require("fs-extra");
const nodemailer = require('nodemailer');
const config = require("./Config.js");
const Tail = require('tail').Tail;
let transporter;
let mailOptions = {};
let lineCounter = 0;
const fileWatchers = {};
function setupSmtp() {
transporter = nodemailer.createTransport({
host: config.smtpHost,
port: config.smtpPort,
secure: config.smtps,
auth: {
user: config.smtpUsername,
pass: config.smtpPassword
}
});
mailOptions = {
from: config.mailfrom,
to: config.mailto,
subject: `${config.subjectPrefix} - `,
text: ''
};
}
function sendMail(subject, message) {
mailOptions.subject += subject;
mailOptions.text = message;
transporter.sendMail(mailOptions, (error, info) => {
mailOptions = {
subject: `${config.subjectPrefix} - `,
text: ''
};
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
}
// sort by expression.matchCounter DESC
function optimizeExpressionCollectionOrder() {
config.expressions.sort((first, seconds) => {
if (first.matchCounter > seconds.matchCounter) return -1;
if (first.matchCounter < seconds.matchCounter) return 1;
return 0;
});
}
function filterLog( /** @type {string} */ line) {
if (lineCounter % 10 === 0) optimizeExpressionCollectionOrder();
for (let index = 0; index < config.expressions.length; index++) {
const expression = config.expressions[index];
if (expression.match.test(line)) {
config.expressions[index].matchCounter++;
sendMail(expression.subject, line);
return;
}
}
}
async function setupTail(filesToWatch) {
for (const fileName of filesToWatch) {
if (!(await fs.exists(fileName))) {
console.log(`File not found, not watching: ${fileName}`);
continue;
}
const tail = new Tail(fileName, {
separator: /[\r]{0,1}\n/,
fromBeginning: false,
fsWatchOptions: {},
follow: true
});
tail.on('line', data => {
lineCounter++;
console.log(`Watcher (${fileName}): ${data}`);
filterLog(data);
});
tail.on('error', error => {
console.log(`Watcher ERROR (${fileName}): `, error);
});
fileWatchers[fileName] = tail;
}
}
return (async() => {
setupSmtp();
await setupTail(config.filesToWatch);
})();
\ No newline at end of file
# Installation
```sh
git clone https://git.compilenix.org/Compilenix/web-access_log2email.git
cd web-access_log2email
npm install
cp Config.example.js Config.js
$EDITOR Config.js # make your changes
node Index.js
```
## Profit!
This diff is collapsed.
{
"name": "web-access_log2email",
"description": "monitor a webserver's access log and e-mail defined matches",
"authors": "Compilenix",
"version": "1.0.0",
"main": "Index.js",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://git.compilenix.org/Compilenix/web-access_log2email"
},
"dependencies": {
"fs-extra": "^4.0.2",
"node-tail": "0.0.4",
"nodemailer": "4.4.0",
"tail": "^1.2.3"
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment