|
| 1 | +# Syslog-ng log forwarding to Parseable |
| 2 | + |
| 3 | + |
| 4 | +Syslog-ng implement [syslog](https://en.wikipedia.org/wiki/Syslog) protocol for Unix and Unix-like systems, |
| 5 | +to forward syslog-ng log to Parseable you can follow the guide below. |
| 6 | + |
| 7 | +## Syslog-ng setup |
| 8 | + |
| 9 | +For this documentation we use syslog-ng in form of docker image, first setup docker-compose file: |
| 10 | + |
| 11 | +``` |
| 12 | +--- |
| 13 | +version: "2.1" |
| 14 | +services: |
| 15 | + syslog-ng: |
| 16 | + image: lscr.io/linuxserver/syslog-ng:latest |
| 17 | + container_name: syslog-ng |
| 18 | + environment: |
| 19 | + - PUID=1000 |
| 20 | + - PGID=1000 |
| 21 | + - TZ=Europe/London |
| 22 | + volumes: |
| 23 | + - /your/local/path/syslog-ng/config:/config |
| 24 | + - /your/local/path/syslog-ng/log:/var/log #optional |
| 25 | + ports: |
| 26 | + - 514:5514/udp |
| 27 | + - 601:6601/tcp |
| 28 | + - 6514:6514/tcp |
| 29 | + restart: unless-stopped |
| 30 | + parseable: |
| 31 | + image: parseable/parseable:latest |
| 32 | + entrypoint: /bin/parseable |
| 33 | + command: server |
| 34 | + ports: |
| 35 | + - "8000:8000" |
| 36 | + environment: |
| 37 | + - P_S3_URL=https://minio.parseable.io:9000 |
| 38 | + - P_S3_ACCESS_KEY=minioadmin |
| 39 | + - P_S3_SECRET_KEY=minioadmin |
| 40 | + - P_S3_REGION=us-east-1 |
| 41 | + - P_S3_BUCKET=parseable |
| 42 | + - P_LOCAL_STORAGE=/tmp/data |
| 43 | + - P_USERNAME=parseable |
| 44 | + - P_PASSWORD=parseable |
| 45 | +``` |
| 46 | + |
| 47 | +Run `docker-compose up` and syslog-ng will listen on port UDP `514` and in `/your/local/path/syslog-ng/config` |
| 48 | +path will contain default configuration file, we only need to customize destination log in main config file, |
| 49 | +`syslog-ng.conf`. |
| 50 | + |
| 51 | +``` |
| 52 | +$ cat syslog-ng.conf |
| 53 | +############################################################################ |
| 54 | +# Default syslog-ng.conf file which collects all local logs into a |
| 55 | +# single file called /var/log/messages tailored to container usage. |
| 56 | +
|
| 57 | +@version: 3.35 |
| 58 | +@include "scl.conf" |
| 59 | +
|
| 60 | +source s_local { |
| 61 | + internal(); |
| 62 | +}; |
| 63 | +
|
| 64 | +source s_network_tcp { |
| 65 | + syslog(transport(tcp) port(6601)); |
| 66 | +}; |
| 67 | +
|
| 68 | +source s_network_udp { |
| 69 | + syslog(transport(udp) port(5514)); |
| 70 | +}; |
| 71 | +
|
| 72 | +destination d_local { |
| 73 | + file("/var/log/messages"); |
| 74 | + file("/var/log/messages-kv.log" template("$ISODATE $HOST $(format-welf --scope all-nv-pairs)\n") frac-digits(3)); |
| 75 | +}; |
| 76 | +
|
| 77 | +log { |
| 78 | + source(s_local); |
| 79 | + source(s_network_tcp); |
| 80 | + source(s_network_udp); |
| 81 | + destination(d_local); |
| 82 | +}; |
| 83 | +``` |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | +## Sending syslog log |
| 88 | + |
| 89 | +First ensure log stream in Parseable already created. |
| 90 | + |
| 91 | +``` |
| 92 | +$ curl --request PUT \ |
| 93 | + --url http://localhost:8000/api/v1/logstream/{logstream-name} \ |
| 94 | + --header 'Authorization: Basic cGFyc2VhYmxlOnBhcnNlYWJsZQ==' |
| 95 | +``` |
| 96 | + |
| 97 | +Add following section in `syslog-ng.conf` file: |
| 98 | + |
| 99 | +``` |
| 100 | +destination d_http { |
| 101 | + http( |
| 102 | + url("http://parseable:8000/api/v1/logstream/logstream-name") |
| 103 | + method("POST") |
| 104 | + user-agent("syslog-ng User Agent") |
| 105 | + user("parseable") |
| 106 | + password("parseable") |
| 107 | + headers("X-P-META-Host: 192.168.1.10", "X-P-TAGS-Language: syslog") |
| 108 | + headers("Content-Type: application/json") |
| 109 | + body-suffix("\n") |
| 110 | + body('$(format-json --scope rfc5424 --key ISODATE)') |
| 111 | + ); |
| 112 | +}; |
| 113 | +``` |
| 114 | + |
| 115 | +and then in `log` section add `d_http` as destination: |
| 116 | + |
| 117 | +``` |
| 118 | +log { |
| 119 | + source(s_local); |
| 120 | + source(s_network_tcp); |
| 121 | + source(s_network_udp); |
| 122 | + destination(d_local); |
| 123 | + destination(d_http); |
| 124 | +}; |
| 125 | +``` |
| 126 | + |
| 127 | +`url()` define the address of parseable server, use `parseable` if you following |
| 128 | +docker compose step. `user(), password()` to define HTTP Basic Auth, required by Parseable to authenticate |
| 129 | +sending log request. `headers()` to customize custom header and set content-type of the payload. And |
| 130 | +finally `body('$(format-json --scope rfc5424 --key ISODATE)')` to format syslog as JSON. |
| 131 | + |
| 132 | +After editing `syslog-ng.conf` restart the docker-compose instance. |
| 133 | + |
| 134 | + |
| 135 | +## Testing |
| 136 | + |
| 137 | +To test you can use following python script. |
| 138 | + |
| 139 | +```python |
| 140 | +import logging |
| 141 | +import logging.handlers |
| 142 | + |
| 143 | +my_logger = logging.getLogger('MyLogger') |
| 144 | +my_logger.setLevel(logging.DEBUG) |
| 145 | + |
| 146 | +handler = logging.handlers.SysLogHandler(address=('127.0.0.1', 514)) |
| 147 | + |
| 148 | +my_logger.addHandler(handler) |
| 149 | + |
| 150 | +my_logger.debug('test-app this is debug') |
| 151 | +my_logger.critical('test-app this is critical') |
| 152 | +``` |
| 153 | + |
| 154 | +Execute those code and then you can query Parseable using |
| 155 | + |
| 156 | +``` |
| 157 | +$ curl --request POST \ |
| 158 | + --url http://localhost:8000/api/v1/query \ |
| 159 | + --header 'Authorization: Basic cGFyc2VhYmxlOnBhcnNlYWJsZQ==' \ |
| 160 | + --header 'Content-Type: application/json' \ |
| 161 | + --data '{ |
| 162 | + "startTime": "2022-10-16T13:11:00+00:00", |
| 163 | + "query": "select * from logstream-name limit 10", |
| 164 | + "endTime": "2022-10-16T13:21:00+00:00" |
| 165 | +}' |
| 166 | +``` |
| 167 | + |
| 168 | +Or view it in Parseable dashboard. |
0 commit comments