Skip to content

Commit 95b284a

Browse files
Merge pull request #329 from chris7444/master
take the hostname from /etc/host_hostname if the file is there. This …
2 parents 876f7dc + 2f1edd1 commit 95b284a

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

README.md

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,17 @@ You can tell logspout to only include certain containers by setting filter param
5959
--volume=/var/run/docker.sock:/var/run/docker.sock \
6060
gliderlabs/logspout \
6161
raw://192.168.10.10:5000?filter.name=*_db
62-
62+
6363
$ docker run \
6464
--volume=/var/run/docker.sock:/var/run/docker.sock \
6565
gliderlabs/logspout \
6666
raw://192.168.10.10:5000?filter.id=3b6ba57db54a
67-
67+
6868
$ docker run \
6969
--volume=/var/run/docker.sock:/var/run/docker.sock \
7070
gliderlabs/logspout \
7171
raw://192.168.10.10:5000?filter.sources=stdout%2Cstderr
72-
72+
7373
# Forward logs from containers with both label 'a' starting with 'x', and label 'b' ending in 'y'.
7474
$ docker run \
7575
--volume=/var/run/docker.sock:/var/run/docker.sock \
@@ -156,6 +156,44 @@ Logspout relies on the Docker API to retrieve container logs. A failure in the A
156156
* `SYSLOG_TAG` - datum for tag field (default `{{.ContainerName}}+route.Options["append_tag"]`)
157157
* `SYSLOG_TIMESTAMP` - datum for timestamp field (default `{{.Timestamp}}`)
158158

159+
#### Using Logspout in a swarm
160+
161+
In a swarm, logspout is best deployed as a global service. When running logspout with 'docker run', you can change the value of the hostname field using the `SYSLOG_HOSTNAME` environment variable as explained above. However, this does not work in a compose file because the value for `SYSLOG_HOSTNAME` will be the same for all logspout "tasks", regardless of the docker host on which they run. To support this mode of deployment, the syslog adapter will look for the file `/etc/host_hostname` and, if the file exists and it is not empty, will configure the hostname field with the content of this file. You can then use a volume mount to map a file on the docker hosts with the file `/etc/host_hostname` in the container. The sample compose file below illustrates how this can be done
162+
163+
```
164+
version: "3"
165+
networks:
166+
logging:
167+
services:
168+
logspout:
169+
image: gliderlabs/logspout:latest
170+
networks:
171+
- logging
172+
volumes:
173+
- /etc/hostname:/etc/host_hostname:ro
174+
- /var/run/docker.sock:/var/run/docker.sock
175+
command:
176+
syslog://svt2-logger.am2.cloudra.local:514
177+
deploy:
178+
mode: global
179+
resources:
180+
limits:
181+
cpus: '0.20'
182+
memory: 256M
183+
reservations:
184+
cpus: '0.10'
185+
memory: 128M
186+
```
187+
188+
logspout can then be deployed as a global service in the swam with the following command
189+
190+
```bash
191+
docker stack deploy --compose-file <name of your compose file>
192+
```
193+
194+
More information about services and their mode of deployment can be found here:
195+
https://docs.docker.com/engine/swarm/how-swarm-mode-works/services/
196+
159197
## Modules
160198

161199
The standard distribution of logspout comes with all modules defined in this repository. You can remove or add new modules with custom builds of logspout. In the `custom` dir, edit the `modules.go` file and do a `docker build`.

adapters/syslog/syslog.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"errors"
66
"fmt"
7+
"io/ioutil"
78
"log"
89
"log/syslog"
910
"net"
@@ -67,8 +68,14 @@ func NewSyslogAdapter(route *router.Route) (router.LogAdapter, error) {
6768

6869
format := getopt("SYSLOG_FORMAT", "rfc5424")
6970
priority := getopt("SYSLOG_PRIORITY", "{{.Priority}}")
70-
hostname := getopt("SYSLOG_HOSTNAME", "{{.Container.Config.Hostname}}")
7171
pid := getopt("SYSLOG_PID", "{{.Container.State.Pid}}")
72+
73+
content, err := ioutil.ReadFile("/etc/host_hostname") // just pass the file name
74+
if err == nil && len(content) > 0{
75+
hostname = string(content) // convert content to a 'string'
76+
} else {
77+
hostname = getopt("SYSLOG_HOSTNAME", "{{.Container.Config.Hostname}}")
78+
}
7279
tag := getopt("SYSLOG_TAG", "{{.ContainerName}}"+route.Options["append_tag"])
7380
structuredData := getopt("SYSLOG_STRUCTURED_DATA", "")
7481
if route.Options["structured_data"] != "" {

0 commit comments

Comments
 (0)