Commit f3a4bdac authored by CompileNix's avatar CompileNix

add slack

parent 22a7a6db
class Config {
constructor() {
this.enableEmail = false;
this.smtpUsername = 'smtp login username';
this.smtpPassword = 'password';
this.smtpHost = 'your.email.server';
......@@ -15,7 +16,9 @@ class Config {
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 }
],
];
this.enableSlack = true;
this.slackWebHookUri = 'https://hooks.slack.com/services/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'
this.debug = false // show additional console output
}
};
......
......@@ -4,9 +4,10 @@ const fs = require("fs-extra");
const nodemailer = require('nodemailer');
const config = require("./Config.js");
const Tail = require('tail').Tail;
const Slack = require('slack-node');
let transporter;
let slack = new Slack();
let mailOptions = {};
let lineCounter = 0;
const fileWatchers = {};
......@@ -30,6 +31,10 @@ function setupSmtp() {
};
}
function setupSlack() {
slack.setWebhook(config.slackWebHookUri);
}
async function sendMail(mailOptions) {
return new Promise((resolve, reject) => {
transporter.sendMail(mailOptions, (error, info) => {
......@@ -55,7 +60,7 @@ function optimizeExpressionCollectionOrder() {
}
async function filterLog( /** @type {string} */ line) {
if (lineCounter % 10 === 0) optimizeExpressionCollectionOrder();
if (lineCounter % 100 === 0) optimizeExpressionCollectionOrder();
for (let index = 0; index < config.expressions.length; index++) {
const expression = config.expressions[index];
......@@ -63,12 +68,24 @@ async function filterLog( /** @type {string} */ line) {
if (expression.match.test(line.toString())) {
config.expressions[index].matchCounter++;
await sendMail({
from: config.mailfrom,
to: config.mailto,
subject: `${config.subjectPrefix} - ${expression.subject}`,
text: line
});
if (config.enableEmail) {
await sendMail({
from: config.mailfrom,
to: config.mailto,
subject: `${config.subjectPrefix} - ${expression.subject}`,
text: line
});
}
if (config.enableSlack) {
slack.webhook({
channel: "#general",
username: "web-access_log2slack",
text: `${expression.subject}: ${line}`
}, (err, response) => {
console.log(response);
});
}
}
}
}
......@@ -102,6 +119,6 @@ async function setupTail( /** @type {string[]} */ filesToWatch) {
(async() => {
setupSmtp();
setupSlack();
await setupTail(config.filesToWatch);
//setInterval(emailQueueWorker, 2000);
})();
\ No newline at end of file
This diff is collapsed.
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