Skip to main content

This project aims to simplify log shipping to your graylog server quite simply

Project description

Project description

Graylog Http Gelf with Python

Description

This library aims to simplify the process of sending logs to your graylog server using the python language.

To use this lib is necessary before have create input using GELF HTTP in input area on your Graylog GUI (web page) in port 9000 on your browser.

First step

The first step is to install the gray-py-gelf library by managing python packages using pip:

pip install gray-py-gelf

Second step

Some extra libs used in this example to compose own log.

from datetime import date
from time import gmtime, strftime
today = date.today()

Import gray-py-gelf in your python code using:

from graylog_lib.graylog_http_gelf import Graylog

Third step

Instantiating the graylog object from its constructor pass by parameter:

graylog = Graylog(url="http://localhost:12201/gelf", host="Rafael's Notebook", short_message="my_indice_name", level=6)

Creating your log to send to graylog GELF

Remember that your GELF input must already be created before sending any logs to Graylog to manage:

log = '"id":{},"name":"{}","age":{},"date":"{}","hour":"{}"'.format(1, "Rafael Sanches", 28, today.strftime("%d/%m/%Y"), strftime("%H:%M:%S", gmtime()))
log = '{'+log+'}'

resp = graylog.sender(log)

print(resp)

You must import this extractor before you can start uploading your logs to Graylog.

Extractors to safe your life and transform your log in separated keys and values:

Import this extractor in your input GELF HTTP:

{
  "extractors": [
    {
      "title": "extractor_json",
      "extractor_type": "json",
      "converters": [],
      "order": 0,
      "cursor_strategy": "copy",
      "source_field": "json",
      "target_field": "",
      "extractor_config": {
        "list_separator": ", ",
        "kv_separator": "=",
        "key_prefix": "",
        "key_separator": "_",
        "replace_key_whitespace": true,
        "key_whitespace_replacement": "_"
      },
      "condition_type": "none",
      "condition_value": ""
    }
  ],
  "version": "3.0.2"
}

Some tips:

  • No mark 'flatten' option field when you will go to create your extractor;
  • Mark 'key_whitespace_replacement' and set some thing type _

Means about atributes graylog object:

  •       url: Your graylog server and port used to GELF
    
  •      host: Who send this log? e.g ip the machine for example
    
  •     level: A integer number that represents a level about your log content
    
  • short_message: Use this field to represent your index
  • sender: Is a method to send a string type a json to lib python and there is a pre-processing and send all infos to your graylog server in a json format. This method give return to you a http 202 code if all that's OK.

Example full code:

from datetime import date
from time import gmtime, strftime
today = date.today()

from graylog_lib.graylog_http_gelf import Graylog

graylog = Graylog(url="http://localhost:12201/gelf", host="Rafael's Notebook", short_message="rafael_test", level=6)
for i in range(50):
    log = '"id":{},"name":"{}","age":{},"date":"{}","hour":"{}"'.format(i, "Rafael Sanches", 28, today.strftime("%d/%m/%Y"), strftime("%H:%M:%S", gmtime()))
    log = '{'+log+'}'
    print(type(log))
    print(log)
    resp = graylog.sender(log)
    print(resp)

Any questions feel free to send me an email: rafaelsanches123@gmail.com If you want to see cool things in python visit my personal blog!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

gray_py_gelf-1.2.0-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file gray_py_gelf-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: gray_py_gelf-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.8

File hashes

Hashes for gray_py_gelf-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d68b0eef9fbbd6da7351eb7de55346bc31d6704f4fa124bc19e814d99ea5167a
MD5 ba65fa51e27cdf1b47feb9a623cd8ced
BLAKE2b-256 bb65163472c7beded0488f5741e34587d765ecd19cf552cb9afced9771fa2d99

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page