Giter VIP home page Giter VIP logo

graylog-content-pack-nginx-docker's Introduction

Graylog content pack for nginx using JSON logging

This is partially based on the nginx json content pack.

It is designed for people using nginx in a docker container, and will only work with nginx version 1.11.8 onwards (you can remove the escape=json from the nginx setup if you want to use an earlier version).

The advantage of using docker's GELF driver is that you get a LOT of extra information you'll otherwise (e.g. syslog) won't get. List of additional metadata fields you're getting when using docker's GELF driver (source):

  • Hostname – Name of the Docker host
  • Container ID – Full ID of the container
  • Container Name – Human readable name of the container
  • Image ID – ID of the image used to create this container
  • Image Name – Human readable image name
  • Command – Command or entrypoint that is executed inside of the container
  • Tag – A tag that was given on creation time to identify containers easily
  • Creation time – A timestamp when this container was started
  • Log level – Was the message send to STDOUT or STDERR?

The core advantage of using json is that you can add arbitrary fields to the nginx logging and they will just appear magically in graylog rather than having to delve into complex regex expressions to do things.

This content pack will create one UDP input for both of nginx's logs (error_log and access_log).

Extractors are applied to effectively read the most important data into message fields. You will be able to do searches for all requests of a given remote IP, all requests that were answered with a HTTP 400 or just all requests that were slow.

The pack comes with a default dashboard to build upon and several streams that pre-group your HTTP requests into interesting categories. The additional log information described below (see Configuring nginx) will also add timing information to the requests handled by nginx.

See screenshots at the bottom of this page.

Configuring nginx

You need to run at least nginx version 1.11.8, escaped JSON support.

Add this to your nginx configuration and restart the service:

log_format graylog2_json escape=json '{ "timestamp": "$msec", '
             '"remote_addr": "$remote_addr", '
             '"body_bytes_sent": $body_bytes_sent, '
             '"request_time": $request_time, '
             '"response_status": $status, '
             '"request": "$request", '
             '"request_method": "$request_method", '
             '"host": "$host",'
             '"upstream_cache_status": "$upstream_cache_status",'
             '"upstream_addr": "$upstream_addr",'
             '"http_x_forwarded_for": "$http_x_forwarded_for",'
             '"http_referrer": "$http_referer", '
             '"http_user_agent": "$http_user_agent", '
             '"http_version": "$server_protocol", '
             '"nginx_access": true }';

access_log /var/log/nginx/access.log graylog2_json;
error_log /var/log/nginx/error.log warn;

This configuration will send various NGINX variables to Graylog. You can log other useful information for each request by adding any other NGINX variables into the JSON.

Building (your) docker and running it

Build

I recommend softlinking your log files to stdout/stderr, as done in nginx's Dockerfile, by adding this directive to your Dockerfile:

# forward request and error logs to docker log collector
RUN ln -sf /dev/stdout /var/log/nginx/access.log && ln -sf /dev/stderr /var/log/nginx/error.log

If you don't want to do that, you can simple change the two settings (access_log and error_log) to:

access_log /dev/stdout graylog2_json;
error_log stderr warn;

Run

Now, when your logs are collected by docker from stdout & stderr, you can run your docker using this command:

docker run --log-driver=gelf --log-opt gelf-address=udp://<GraylogIP>:12401 <ImageName> <Command>

for example:

docker run --log-driver=gelf --log-opt gelf-address=udp://<GraylogIP>:12401 busybox echo Hello Graylog

Screenshots

Screenshots

Screenshots

graylog-content-pack-nginx-docker's People

Contributors

ericandrewlewis avatar phillipjohnson avatar ronlut avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

graylog-content-pack-nginx-docker's Issues

Track URL path

Would it be possible to track the URL path as a separate field?

JsonParseException: Unexpected character ('<' (code 60))

This content pack does not work in graylog 4.1.5 with nginx 1.19. I get the following error:

com.fasterxml.jackson.core.JsonParseException: Unexpected character ('<' (code 60)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
  at [Source: (String)"<190>Sep 21 12:53:47 1d9e42d48ece nginx: { .... }

Even if I add an extractor to remove any non-JSON before the JSON extract, it still fails with the same error.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.