Commit 4ecddf5b authored by CompileNix's avatar CompileNix

update #1: enhance customized messages

to not only control the message subject and body.
following stuff can be controled / overwritten additionally:
- everthing 'slack-node' allowes to be set on 'Slack.webhook' except autogenerated fields ('fallback' and 'text' -> is beeing set via 'expression.template' and 'ts' -> current timestamp)
parent 0da9cb4a
......@@ -47,19 +47,6 @@ class Config {
// Referer: 17,
// UserAgent: 18
// };
this.expressions = [
{
match: /status:404/,
//filter: (/** @type {string} */ line) => { return line.match(/./); },
subject: (/** @type {any} */ match) => {
return `HTTP ${match[this.defaultMatchingGroupName.StatusCode]}: `;
},
template: (/** @type {any} */ match) => {
return `${match[this.defaultMatchingGroupName.Domain]} \`"${match[this.defaultMatchingGroupName.Request]}"\``;
}
},
{ match: /status:5\d{2}/, subject: 'HTTP 5xx: ' }
];
this.enableEmail = false;
this.smtpUsername = 'smtp login username';
......@@ -75,7 +62,53 @@ class Config {
this.slackWebHookUri = 'https://hooks.slack.com/services/xxxxxx/xxxxxx/xxxxxx';
this.slackChannel = '#general';
this.slackUsername = 'webserver-access_log-bot';
this.botName = 'web-access_log2email';
this.botIcon = "https://compilenix.org/cdn/Compilenix.png";
this.debug = true;
this.expressions = [
{
match: /" 503 /,
//filter: (/** @type {string} */ line) => { return line.match(/./); },
subject: (/** @type {any} */ match) => {
return `HTTP ${match[this.defaultMatchingGroupName.StatusCode]} (something we're already fixing...)\n`;
},
template: (/** @type {any} */ match) => {
return `${match[this.defaultMatchingGroupName.Method]} ${match[this.defaultMatchingGroupName.Domain]} \`${match[this.defaultMatchingGroupName.Path]}\`\nUser-Agent: \`${match[this.defaultMatchingGroupName.UserAgent]}\``;
},
slackOptions: {
channel: this.slackChannel,
username: this.slackUsername,
attachments: [{
footer: this.botName,
footer_icon: this.botIcon,
color: "#f0d32c",
mrkdwn_in: ["text", "pretext"]
}]
}
},
{
match: /" 5(?!03)\d{2} /, // matches all 5xx status codes, except a 503 (using regex negative lookahead)
//filter: (/** @type {string} */ line) => { return line.match(/./); },
subject: (/** @type {any} */ match) => {
return `HTTP ${match[this.defaultMatchingGroupName.StatusCode]}\n`;
},
template: (/** @type {any} */ match) => {
return `${match[this.defaultMatchingGroupName.Method]} ${match[this.defaultMatchingGroupName.Domain]} \`${match[this.defaultMatchingGroupName.Path]}\`\nUser-Agent: \`${match[this.defaultMatchingGroupName.UserAgent]}\``;
},
slackOptions: {
channel: this.slackChannel,
username: this.slackUsername,
attachments: [{
footer: this.botName,
footer_icon: this.botIcon,
color: "#c4463d",
mrkdwn_in: ["text", "pretext"]
}]
}
}
];
}
};
......
......@@ -38,13 +38,15 @@ async function notificationQueueWorker() {
const oldSubject = message.expression.subject;
if (typeof (message.expression.subject) !== 'function') {
var oldSubjectValue = '';
if (message.expression.subject.toString) oldSubjectValue = message.expression.subject.toString();
if (message.expression.subject && message.expression.subject.toString) oldSubjectValue = message.expression.subject.toString();
message.expression.subject = () => oldSubjectValue;
}
const oldTemplate = message.expression.template;
if (typeof (message.expression.template) !== 'function') {
message.expression.template = () => `\`${message.message}\``;
var oldTemplateValue = `\`${message.message}\``;
if (message.expression.template && message.expression.template.toString) oldTemplateValue = message.expression.template.toString();
message.expression.template = () => oldTemplateValue;
}
if (config.enableEmail) {
......@@ -56,18 +58,20 @@ async function notificationQueueWorker() {
});
}
const oldSlackOptions = message.expression.slackOptions;
if (config.enableSlack) {
slack.webhook({
channel: config.slackChannel,
username: config.slackUserName,
text: `${message.expression.subject(match)}${message.expression.template(match)}`
}, (err, response) => {
let payload = message.expression.slackOptions;
payload.attachments[0].fallback = `${message.expression.subject(match)}${message.expression.template(match)}`;
payload.attachments[0].text = payload.attachments[0].fallback;
payload.attachments[0].ts = Date.now() / 1000;
slack.webhook(payload, (err, response) => {
if (config.debug && err) console.log(err, response);
});
}
message.expression.subject = oldSubject;
message.expression.template = oldTemplate;
message.expression.slackOptions = oldSlackOptions;
await sleep(1000);
}
......
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