Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jwilder committed Mar 14, 2014
0 parents commit 9fa26ff
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
docker-gen
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.SILENT :
.PHONY : docker-gen clean fmt

all: docker-gen

docker-gen:
echo "Building docker-gen"
go build

18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
docker-gen
=====

Config file generator using running docker container meta-data.

This is mostly a proof of concept to generate config files for:

* fluentd, logstash or other centralized logging tools that tail the containers JSON log file.
* logrotate files to rotate container JSON log files

`go get github.com/jwilder/docker-gen`

`docker-gen template.file`

TODO:

* Restart command hooks for when files are regenerated
* Tail docker event stream to detect when containers are started and stopped automatically
45 changes: 45 additions & 0 deletions docker-gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import (
"github.com/fsouza/go-dockerclient"
"os"
"path/filepath"
"text/template"
)

func usage() {
println("Usage: docker-log template.file")
}

func generateFile(templatePath string, containers []docker.APIContainers) {
tmpl, err := template.ParseFiles(templatePath)
if err != nil {
panic(err)
}

err = tmpl.ExecuteTemplate(os.Stdout, filepath.Base(templatePath), containers)
}

func main() {

if len(os.Args) != 2 {
usage()
os.Exit(1)
}

endpoint := "unix:///var/run/docker.sock"
client, err := docker.NewClient(endpoint)

if err != nil {
panic(err)
}

containers, err := client.ListContainers(docker.ListContainersOptions{
All: false,
})
if err != nil {
panic(err)
}

generateFile(os.Args[1], containers)
}
20 changes: 20 additions & 0 deletions templates/fluentd.conf.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

## File input
## read docker logs with tag=docker.container

{{range $key, $value := .}}
<source>
type tail
format json
time_key time
path /var/lib/docker/containers/{{ $value.ID }}/{{ $value.ID }}-json.log
pos_file /var/lib/docker/containers/{{ $value.ID }}/{{ $value.ID }}-json.log.pos
tag docker.container.{{printf "%.*s" 12 $value.ID}}
rotate_wait 5
</source>
{{end}}

<match docker.**>
type stdout
</match>

0 comments on commit 9fa26ff

Please sign in to comment.