Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for http hostname in nginx filebeat module #14505

Merged
merged 5 commits into from
Nov 20, 2019

Conversation

jsoriano
Copy link
Member

@jsoriano jsoriano commented Nov 13, 2019

It is common to modify the default log format in nginx to include
the http host used by the client to make the logged request.

This continues with #13223 to add support for that, and is
similar to what we already did for Apache in #12778.

Fixes #13223.

It also fixes an issue found on source address parsing when
source addresses are not IPs. For that, the pipeline works
with addresses as strings instead of IPs, and it only tries to
convert to IP to finally store the it in source.ip if it parses
as an IP.

Co-authored-by: poma Semenov.Roman@mail.ru

@jsoriano jsoriano requested a review from a team as a code owner November 13, 2019 16:10
@jsoriano jsoriano self-assigned this Nov 13, 2019
"service.type": "nginx",
"source.address": "localhost, localhost",
"source.address": "localhost",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was being incorrectly parsed, this is also fixed as part of this PR.

"lang": "painless",
"source": "boolean isPrivate(def dot, def ip) { try { StringTokenizer tok = new StringTokenizer(ip, dot); int firstByte = Integer.parseInt(tok.nextToken()); int secondByte = Integer.parseInt(tok.nextToken()); if (firstByte == 10) { return true; } if (firstByte == 192 && secondByte == 168) { return true; } if (firstByte == 172 && secondByte >= 16 && secondByte <= 31) { return true; } if (firstByte == 127) { return true; } return false; } catch (Exception e) { return false; } } try { ctx.source.ip = null; if (ctx.nginx.access.remote_ip_list == null) { return; } def found = false; for (def item : ctx.nginx.access.remote_ip_list) { if (!isPrivate(params.dot, item)) { ctx.source.ip = item; found = true; break; } } if (!found) { ctx.source.ip = ctx.nginx.access.remote_ip_list[0]; }} catch (Exception e) { ctx.source.ip = null; }",
"source": "boolean isPrivate(def dot, def ip) { try { StringTokenizer tok = new StringTokenizer(ip, dot); int firstByte = Integer.parseInt(tok.nextToken()); int secondByte = Integer.parseInt(tok.nextToken()); if (firstByte == 10) { return true; } if (firstByte == 192 && secondByte == 168) { return true; } if (firstByte == 172 && secondByte >= 16 && secondByte <= 31) { return true; } if (firstByte == 127) { return true; } return false; } catch (Exception e) { return false; } } try { ctx.source.address = null; if (ctx.nginx.access.remote_ip_list == null) { return; } def found = false; for (def item : ctx.nginx.access.remote_ip_list) { if (!isPrivate(params.dot, item)) { ctx.source.address = item; found = true; break; } } if (!found) { ctx.source.address = ctx.nginx.access.remote_ip_list[0]; }} catch (Exception e) { ctx.source.address = null; }",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change in this script is a replacement of source.ip with source.address.

@jsoriano jsoriano added needs_backport PR is waiting to be backported to other branches. v7.6.0 labels Nov 13, 2019
@poma
Copy link
Contributor

poma commented Nov 13, 2019

LGTM

@@ -286,8 +289,12 @@
"http.version": "1.1",
"input.type": "log",
"log.offset": 1398,
"nginx.access.remote_ip_list": [
"localhost",
"localhost"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is localhost repeated here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It appears twice in the example log line:

localhost, localhost - - [29/May/2017:19:02:48 +0000] "GET /test2 HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 6.1; rv:15.0) Gecko/20120716 Firefox/15.0a2" "-"

Not sure how this log was generated.

@jsoriano jsoriano merged commit 1967bc9 into elastic:master Nov 20, 2019
@jsoriano jsoriano deleted the nginx-filebeat-hostname branch November 20, 2019 16:05
jsoriano added a commit to jsoriano/beats that referenced this pull request Nov 20, 2019
It is common to modify the default log format in nginx to include
the http host used by the client to make the logged request.

This is similar to what we already did for Apache in elastic#12778.

It also fixes an issue found on source address parsing when
source addresses are not IPs. For that, the pipeline works
with addresses as strings instead of IPs, and it only tries to
convert to IP to finally store the it in `source.ip` if it parses
as an IP.

Co-authored-by: poma <Semenov.Roman@mail.ru>
(cherry picked from commit 1967bc9)
@jsoriano jsoriano added test-plan Add this PR to be manual test plan and removed needs_backport PR is waiting to be backported to other branches. labels Nov 20, 2019
jsoriano added a commit that referenced this pull request Nov 26, 2019
…ebeat module (#14654)

It is common to modify the default log format in nginx to include
the http host used by the client to make the logged request.

This is similar to what we already did for Apache in #12778.

It also fixes an issue found on source address parsing when
source addresses are not IPs. For that, the pipeline works
with addresses as strings instead of IPs, and it only tries to
convert to IP to finally store the it in `source.ip` if it parses
as an IP.

(cherry picked from commit 1967bc9)

Co-authored-by: poma <Semenov.Roman@mail.ru>
@sayden sayden self-assigned this Jan 16, 2020
@sayden sayden added the test-plan-ok This PR passed manual testing label Jan 31, 2020
@vaisakhtr
Copy link

is it available on 7.x build?

@jsoriano
Copy link
Member Author

jsoriano commented Apr 3, 2020

is it available on 7.x build?

It is available starting on 7.6.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Filebeat Filebeat module needs testing notes review Team:Integrations Label for the Integrations team test-plan Add this PR to be manual test plan test-plan-ok This PR passed manual testing v7.6.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants