Last active
January 11, 2017 16:10
-
-
Save raphink/f6ef081f70755e2ac60317cc122869b7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM scratch | |
ADD puppetdb-prometheus / | |
ENTRYPOINT ["/puppetdb-prometheus"] | |
CMD [""] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"io/ioutil" | |
"net/http" | |
"strings" | |
"time" | |
"gopkg.in/yaml.v2" | |
) | |
type Node struct { | |
Certname string `json:"certname"` | |
Ipaddress string `json:"value"` | |
} | |
type Targets struct { | |
Targets []string `yaml:"targets"` | |
Labels map[string]string `yaml:"labels"` | |
} | |
const ( | |
query = "facts { name='ipaddress' and nodes { facts { name='collectd_version' and value ~ '^5\\\\.7' } and resources { type='Class' and title='Collectd' } } }" | |
port = "9103" | |
file = "/etc/prometheus-config/prometheus-targets.yml" | |
sleep = 5 * time.Second | |
) | |
var labels = map[string]string{ | |
"job": "puppet", | |
} | |
func main() { | |
client := &http.Client{} | |
for { | |
nodes, err := getNodes(client) | |
if err != nil { | |
fmt.Println(err) | |
break | |
} | |
err = writeNodes(nodes) | |
if err != nil { | |
fmt.Println(err) | |
break | |
} | |
fmt.Printf("Sleeping for %v\n", sleep) | |
time.Sleep(sleep) | |
} | |
} | |
func getNodes(client *http.Client) (nodes []Node, err error) { | |
form := strings.NewReader(fmt.Sprintf("{\"query\":\"%s\"}", query)) | |
req, err := http.NewRequest("POST", "http://puppetdb:8080/pdb/query/v4", form) | |
if err != nil { | |
return | |
} | |
req.Header.Add("Content-Type", "application/json") | |
resp, err := client.Do(req) | |
if err != nil { | |
return | |
} | |
body, err := ioutil.ReadAll(resp.Body) | |
if err != nil { | |
return | |
} | |
err = json.Unmarshal(body, &nodes) | |
return | |
} | |
func writeNodes(nodes []Node) (err error) { | |
allTargets := []Targets{} | |
for _, node := range nodes { | |
targets := Targets{} | |
target := fmt.Sprintf("%s:%s", node.Ipaddress, port) | |
targets.Targets = append(targets.Targets, target) | |
targets.Labels = labels | |
targets.Labels["certname"] = node.Certname | |
allTargets = append(allTargets, targets) | |
} | |
d, err := yaml.Marshal(&allTargets) | |
err = ioutil.WriteFile(file, d, 0644) | |
return nil | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DEPS = $(wildcard */*.go) | |
VERSION = $(shell git describe --always --dirty) | |
all: puppetdb-prometheus | |
puppetdb-prometheus: main.go $(DEPS) | |
CGO_ENABLED=0 GOOS=linux \ | |
go build -a \ | |
-ldflags="-X main.version=$(VERSION)" \ | |
-installsuffix cgo -o $@ $< | |
strip $@ | |
clean: | |
rm -f puppetdb-prometheus | |
.PHONY: all clean |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
docker run --add-host puppetdb:172.17.0.1 -v $PWD/etc:/etc -d puppetdb_prometheus |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment