Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
W
web-access_log2email
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
CompileNix
web-access_log2email
Commits
683558ad
Commit
683558ad
authored
Dec 11, 2017
by
CompileNix
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
complete
#1
: add expression based message filter / templating for customized messages
parent
2d5f0144
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
35 additions
and
17 deletions
+35
-17
Config.example.js
Config.example.js
+11
-6
Index.js
Index.js
+24
-11
No files found.
Config.example.js
View file @
683558ad
...
...
@@ -4,8 +4,8 @@ class Config {
'
test.txt
'
,
'
test1.txt
'
];
this
.
defaultMessageTemplateFilter
=
/^
\[((\d{1,2}\/\w{3}\/\d{4})
:
(\d{2}
:
\d{2}
:
\d{2})
([
+-
]\d{4}))\]
status:
(\d{3})
request_time:
(\d{1,}
.
\d{3})
upstream_response_time:
(\d{1,}
.
\d{3})
bytes_sent:
(\d{1,})
client_ip:
(\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b
|
(
:
{0,2}[\d
a-f
]{1,4}){1,8})
domain:
([\w
.-
]
*
)
request:"
((\w{3,7})
(\/[\w
-.,_~:<>
\\/
[
\]
%@!$'()*+;?=&#
]
*
)
HTTP
\/(\d\.\d))
" referer:"
([\w
-.,_~:<>
\\/
[
\]
%@!$'()*+;?=&#
]
*
)
" user_agent:"
([\w
-.,_~:
\/
[
\]
%@!$'()*+;?=&#
]
*
)
"$/
g
this
.
defaultM
essageTemplateFilterMatchingGroupNames
=
{
this
.
defaultMessageTemplateFilter
=
/^
\[((\d{1,2}\/\w{3}\/\d{4})
:
(\d{2}
:
\d{2}
:
\d{2})
([
+-
]\d{4}))\]
status:
(\d{3})
request_time:
(\d{1,}
.
\d{3})
upstream_response_time:
(\d{1,}
.
\d{3})
bytes_sent:
(\d{1,})
client_ip:
(\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b
|
(
:
{0,2}[\d
a-f
]{1,4}){1,8})
domain:
([\w
.-
]
*
)
request:"
((\w{3,7})
(\/[\w
-.,_~:<>
\\/
[
\]
%@!$'()*+;?=&#
]
*
)
HTTP
\/(\d\.\d))
" referer:"
([\w
-.,_~:<>
\\/
[
\]
%@!$'()*+;?=&#
]
*
)
" user_agent:"
([\w
-.,_~:
\/
[
\]
%@!$'()*+;?=&#
]
*
)
"$/
;
this
.
defaultM
atchingGroupName
=
{
All
:
0
,
DateTime
:
1
,
Date
:
2
,
...
...
@@ -26,11 +26,16 @@ class Config {
};
this
.
expressions
=
[
{
match
:
/HTTP
\/\d\.\d\"
404/g
,
subject
:
'
HTTP 404:
'
,
//messageTemplateFilter: /()/g
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
:
/
HTTP
\/\d\.\d\"
5
\d{2}
/g
,
subject
:
'
HTTP 5xx:
'
}
{
match
:
/
status:5
\d{2}
/
,
subject
:
'
HTTP 5xx:
'
}
];
this
.
enableEmail
=
false
;
...
...
Index.js
View file @
683558ad
...
...
@@ -9,7 +9,6 @@ const Slack = require('slack-node');
let
transporter
;
let
slack
=
new
Slack
();
let
mailOptions
=
{};
let
lineCounter
=
0
;
const
fileWatchers
=
{};
let
messages
=
new
Array
();
let
queueWorkerRunning
=
false
;
...
...
@@ -26,12 +25,31 @@ async function notificationQueueWorker() {
continue
;
}
let
filterExpression
=
config
.
defaultMessageTemplateFilter
;
if
(
message
.
expression
.
filter
)
{
filterExpression
=
message
.
expression
.
filter
;
}
let
match
=
message
.
message
.
match
(
filterExpression
);
if
(
match
===
null
)
match
=
message
.
message
;
const
oldSubject
=
message
.
expression
.
subject
;
if
(
typeof
(
message
.
expression
.
subject
)
!==
'
function
'
)
{
var
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
}
\`
`
;
}
if
(
config
.
enableEmail
)
{
await
sendMail
({
from
:
config
.
mailfrom
,
to
:
config
.
mailto
,
subject
:
`
${
config
.
subjectPrefix
}
`
,
text
:
`
${
message
.
expression
.
subject
}${
message
.
message
}
`
subject
:
`
${
config
.
subjectPrefix
}
${
message
.
expression
.
subject
(
match
)}
`
,
text
:
message
.
expression
.
template
(
match
)
});
}
...
...
@@ -39,12 +57,14 @@ async function notificationQueueWorker() {
slack
.
webhook
({
channel
:
config
.
slackChannel
,
username
:
config
.
slackUserName
,
text
:
`
${
message
.
expression
.
subject
}${
message
.
message
}
`
text
:
`
${
message
.
expression
.
subject
(
match
)}${
message
.
expression
.
template
(
match
)
}
`
},
(
err
,
response
)
=>
{
if
(
config
.
debug
&&
err
)
console
.
log
(
err
,
response
);
});
}
message
.
expression
.
subject
=
oldSubjectValue
;
message
.
expression
.
template
=
oldTemplate
;
await
sleep
(
1000
);
}
...
...
@@ -57,7 +77,6 @@ async function filterLog( /** @type {string} */ line) {
expression
.
match
.
lastIndex
=
0
;
if
(
expression
.
match
.
test
(
line
))
{
config
.
expressions
[
index
].
matchCounter
++
;
messages
.
push
({
expression
:
expression
,
message
:
line
...
...
@@ -86,11 +105,6 @@ async function setupTail( /** @type {string[]} */ filesToWatch) {
process
.
exit
(
1
);
}
for
(
let
index
=
0
;
index
<
config
.
expressions
.
length
;
index
++
)
{
config
.
expressions
[
index
].
matchCounter
=
0
;
config
.
expressions
[
index
].
lastMatchContent
=
''
;
}
for
(
const
fileName
of
filesToWatch
)
{
if
(
!
(
await
fs
.
exists
(
fileName
)))
{
console
.
log
(
`File not found, not watching:
${
fileName
}
`
);
...
...
@@ -105,7 +119,6 @@ async function setupTail( /** @type {string[]} */ filesToWatch) {
});
tail
.
on
(
'
line
'
,
(
/** @type {string} */
data
)
=>
{
lineCounter
++
;
filterLog
(
data
);
});
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment