├── .gitignore ├── Dockerfile ├── go.mod ├── Makefile ├── CHANGELOG.md ├── README.md ├── go.sum ├── internal ├── puppetdb │ └── puppetdb.go └── exporter │ └── exporter.go ├── main.go └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | prometheus-puppetdb-exporter 2 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM golang:1.12 as builder 2 | WORKDIR /go/src/github.com/camptocamp/prometheus-puppetdb-exporter 3 | COPY . . 4 | RUN make prometheus-puppetdb-exporter 5 | 6 | FROM scratch 7 | COPY --from=builder /go/src/github.com/camptocamp/prometheus-puppetdb-exporter/prometheus-puppetdb-exporter / 8 | ENTRYPOINT ["/prometheus-puppetdb-exporter"] 9 | CMD [""] 10 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/camptocamp/prometheus-puppetdb-exporter 2 | 3 | go 1.15 4 | 5 | require ( 6 | github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973 // indirect 7 | github.com/golang/protobuf v1.2.0 // indirect 8 | github.com/jessevdk/go-flags v1.4.0 9 | github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect 10 | github.com/prometheus/client_golang v0.8.0 11 | github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910 // indirect 12 | github.com/prometheus/common v0.0.0-20180801064454-c7de2306084e // indirect 13 | github.com/prometheus/procfs v0.0.0-20180725123919-05ee40e3a273 // indirect 14 | github.com/sirupsen/logrus v1.3.0 15 | golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 // indirect 16 | golang.org/x/sync v0.0.0-20190423024810-112230192c58 // indirect 17 | ) 18 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | DEPS = $(wildcard */*.go) 2 | VERSION = $(shell git describe --always --dirty) 3 | COMMIT_SHA1 = $(shell git rev-parse HEAD) 4 | BUILD_DATE = $(shell date +%Y-%m-%d) 5 | GOOS = linux 6 | ARCH = amd64 7 | 8 | all: lint vet prometheus-puppetdb-exporter 9 | 10 | prometheus-puppetdb-exporter: main.go $(DEPS) 11 | GO111MODULE=on CGO_ENABLED=0 GOOS=$(GOOS) \ 12 | go build -a \ 13 | -ldflags="-X main.version=$(VERSION) -X main.commitSha1=$(COMMIT_SHA1) -X main.buildDate=$(BUILD_DATE)" \ 14 | -installsuffix cgo -o $@ $< 15 | strip $@ 16 | 17 | release: prometheus-puppetdb-exporter-$(VERSION).$(GOOS)-$(ARCH).tar.gz 18 | 19 | %.tar.gz: prometheus-puppetdb-exporter LICENSE 20 | tar cvzf $@ --transform 's,^,$*/,' $^ 21 | 22 | clean: 23 | rm -f prometheus-puppetdb-exporter 24 | 25 | lint: 26 | @GO111MODULE=off go get -v golang.org/x/lint/golint 27 | @for file in $$(git ls-files '*.go' | grep -v '_workspace/'); do \ 28 | export output="$$(golint $${file} | grep -v 'type name will be used as docker.DockerInfo')"; \ 29 | [ -n "$${output}" ] && echo "$${output}" && export status=1; \ 30 | done; \ 31 | exit $${status:-0} 32 | 33 | vet: main.go 34 | go vet $< 35 | 36 | .PHONY: all lint vet clean 37 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ## [1.1.0](https://github.com/camptocamp/prometheus-puppetdb-exporter/tree/1.1.0) (2020-12-03) 4 | 5 | [Full Changelog](https://github.com/camptocamp/prometheus-puppetdb-exporter/compare/1.0.0...1.1.0) 6 | 7 | **Closed issues:** 8 | 9 | - Deactivated nodes still appear [\#8](https://github.com/camptocamp/prometheus-puppetdb-exporter/issues/8) 10 | 11 | **Improvements:** 12 | 13 | - Remove legacy `vendor/` directory 14 | 15 | ## [1.0.0](https://github.com/camptocamp/prometheus-puppetdb-exporter/tree/1.0.0) (2020-06-09) 16 | 17 | [Full Changelog](https://github.com/camptocamp/prometheus-puppetdb-exporter/compare/0.1.0...1.0.0) 18 | 19 | **Breaking changes:** 20 | 21 | - Switch port to 9635 [\#5](https://github.com/camptocamp/prometheus-puppetdb-exporter/pull/5) ([bastelfreak](https://github.com/bastelfreak)) 22 | 23 | ## [0.1.0](https://github.com/camptocamp/prometheus-puppetdb-exporter/tree/0.1.0) (2020-06-08) 24 | 25 | [Full Changelog](https://github.com/camptocamp/prometheus-puppetdb-exporter/compare/8499b362f2f346f1ce58a60d5299d0de628556aa...0.1.0) 26 | 27 | **Closed issues:** 28 | 29 | - No nodes exist? [\#3](https://github.com/camptocamp/prometheus-puppetdb-exporter/issues/3) 30 | - Report nodes as "unreported" if last report is more that 2 hours old [\#1](https://github.com/camptocamp/prometheus-puppetdb-exporter/issues/1) 31 | 32 | **Merged pull requests:** 33 | 34 | - Allow to select report metrics categories to scrape [\#2](https://github.com/camptocamp/prometheus-puppetdb-exporter/pull/2) ([mcanevet](https://github.com/mcanevet)) 35 | 36 | 37 | 38 | \* *This Changelog was automatically generated by [github_changelog_generator](https://github.com/github-changelog-generator/github-changelog-generator)* 39 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Prometheus PuppetDB exporter 2 | ============================ 3 | 4 | ## Usage 5 | 6 | ``` 7 | Usage: 8 | prometheus-puppetdb-exporter [OPTIONS] 9 | 10 | Application Options: 11 | --version Show version. 12 | -u, --puppetdb-url= PuppetDB base URL. (default: https://puppetdb:8081/pdb/query) [$PUPPETDB_URL] 13 | --cert-file= A PEM encoded certificate file. [$PUPPETDB_CERT_FILE] 14 | --key-file= A PEM encoded private key file. [$PUPPETDB_KEY_FILE] 15 | --ca-file= A PEM encoded CA's certificate. [$PUPPETDB_CA_FILE] 16 | --ssl-skip-verify Skip SSL verification. [$PUPPETDB_SSL_SKIP_VERIFY] 17 | --scrape-interval= Duration between two scrapes. (default: 5s) [$PUPPETDB_SCRAPE_INTERVAL] 18 | --listen-address= Address to listen on for web interface and telemetry. (default: 0.0.0.0:9635) 19 | [$PUPPETDB_LISTEN_ADDRESS] 20 | --metric-path= Path under which to expose metrics. (default: /metrics) [$PUPPETDB_METRIC_PATH] 21 | --verbose Enable debug mode [$PUPPETDB_VERBOSE] 22 | --unreported-node= Tag nodes as unreported if the latest report is older than the defined duration. 23 | (default: 2h) [$PUPPETDB_UNREPORTED_NODE] 24 | --categories= Report metrics categories to scrape. (default: resources,time,changes,events) 25 | [$REPORT_METRICS_CATEGORIES] 26 | 27 | Help Options: 28 | -h, --help Show this help message 29 | ``` 30 | 31 | ## Metrics 32 | 33 | ``` 34 | # HELP puppetdb_exporter_build_info puppetdb exporter build informations 35 | # TYPE puppetdb_exporter_build_info gauge 36 | puppetdb_exporter_build_info{build_date="2019-02-18",commit_sha="XXXXXXXXXX",golang_version="go1.11.4",version="1.0.0"} 1 37 | # HELP puppetdb_node_report_status_count Total count of reports status by type 38 | # TYPE puppetdb_node_report_status_count gauge 39 | puppetdb_node_report_status_count{status="changed"} 1 40 | puppetdb_node_report_status_count{status="failed"} 1 41 | puppetdb_node_report_status_count{status="unchanged"} 1 42 | ``` 43 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973 h1:xJ4a3vCFaGF/jqvzLMYoU8P317H5OQ+Via4RmuPwCS0= 2 | github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= 3 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 4 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 5 | github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM= 6 | github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= 7 | github.com/jessevdk/go-flags v1.4.0 h1:4IU2WS7AumrZ/40jfhf4QVDMsQwqA7VEHozFRrGARJA= 8 | github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= 9 | github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk= 10 | github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= 11 | github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU= 12 | github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= 13 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 14 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 15 | github.com/prometheus/client_golang v0.8.0 h1:1921Yw9Gc3iSc4VQh3PIoOqgPCZS7G/4xQNVUp8Mda8= 16 | github.com/prometheus/client_golang v0.8.0/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= 17 | github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910 h1:idejC8f05m9MGOsuEi1ATq9shN03HrxNkD/luQvxCv8= 18 | github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= 19 | github.com/prometheus/common v0.0.0-20180801064454-c7de2306084e h1:n/3MEhJQjQxrOUCzh1Y3Re6aJUUWRp2M9+Oc3eVn/54= 20 | github.com/prometheus/common v0.0.0-20180801064454-c7de2306084e/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro= 21 | github.com/prometheus/procfs v0.0.0-20180725123919-05ee40e3a273 h1:agujYaXJSxSo18YNX3jzl+4G6Bstwt+kqv47GS12uL0= 22 | github.com/prometheus/procfs v0.0.0-20180725123919-05ee40e3a273/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= 23 | github.com/sirupsen/logrus v1.3.0 h1:hI/7Q+DtNZ2kINb6qt/lS+IyXnHQe9e90POfeewL/ME= 24 | github.com/sirupsen/logrus v1.3.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= 25 | github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 26 | github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= 27 | github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= 28 | golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= 29 | golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= 30 | golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 h1:ObdrDkeb4kJdCP557AjRjq69pTHfNouLtWZG7j9rPN8= 31 | golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= 32 | golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= 33 | golang.org/x/sync v0.0.0-20190423024810-112230192c58 h1:8gQV6CLnAEikrhgkHFbMAEhagSSnXWGV915qUMm9mrU= 34 | golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 35 | golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 36 | golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 37 | golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI= 38 | golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 39 | golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= 40 | -------------------------------------------------------------------------------- /internal/puppetdb/puppetdb.go: -------------------------------------------------------------------------------- 1 | package puppetdb 2 | 3 | import ( 4 | "crypto/tls" 5 | "crypto/x509" 6 | "encoding/json" 7 | "fmt" 8 | "io/ioutil" 9 | "net/http" 10 | "net/url" 11 | "strings" 12 | ) 13 | 14 | // PuppetDB stores informations used to connect to a PuppetDB 15 | type PuppetDB struct { 16 | options *Options 17 | client *http.Client 18 | } 19 | 20 | // Options contains the options used to connect to a PuppetDB 21 | type Options struct { 22 | URL string 23 | CertPath string 24 | CACertPath string 25 | KeyPath string 26 | SSLVerify bool 27 | } 28 | 29 | // Node is a structure returned by a PuppetDB 30 | type Node struct { 31 | Certname string `json:"certname"` 32 | Deactivated string `json:"deactivated"` 33 | LatestReportStatus string `json:"latest_report_status"` 34 | ReportEnvironment string `json:"report_environment"` 35 | ReportTimestamp string `json:"report_timestamp"` 36 | LatestReportHash string `json:"latest_report_hash"` 37 | } 38 | 39 | // ReportMetric is a structure returned by a PuppetDB 40 | type ReportMetric struct { 41 | Name string `json:"name"` 42 | Value float64 `json:"value"` 43 | Category string `json:"category"` 44 | } 45 | 46 | // NewClient creates a new PuppetDB client 47 | func NewClient(options *Options) (p *PuppetDB, err error) { 48 | var transport *http.Transport 49 | 50 | puppetdbURL, err := url.Parse(options.URL) 51 | if err != nil { 52 | err = fmt.Errorf("failed to parse PuppetDB URL: %v", err) 53 | return 54 | } 55 | 56 | if puppetdbURL.Scheme != "http" && puppetdbURL.Scheme != "https" { 57 | err = fmt.Errorf("%s is not a valid http scheme", puppetdbURL.Scheme) 58 | return 59 | } 60 | 61 | if puppetdbURL.Scheme == "https" { 62 | // Load client cert 63 | cert, err := tls.LoadX509KeyPair(options.CertPath, options.KeyPath) 64 | if err != nil { 65 | err = fmt.Errorf("failed to load keypair: %s", err) 66 | return nil, err 67 | } 68 | 69 | // Load CA cert 70 | caCert, err := ioutil.ReadFile(options.CACertPath) 71 | if err != nil { 72 | err = fmt.Errorf("failed to load ca certificate: %s", err) 73 | return nil, err 74 | } 75 | caCertPool := x509.NewCertPool() 76 | caCertPool.AppendCertsFromPEM(caCert) 77 | 78 | // Setup HTTPS client 79 | tlsConfig := &tls.Config{ 80 | Certificates: []tls.Certificate{cert}, 81 | RootCAs: caCertPool, 82 | InsecureSkipVerify: !options.SSLVerify, 83 | } 84 | tlsConfig.BuildNameToCertificate() 85 | transport = &http.Transport{TLSClientConfig: tlsConfig} 86 | } else { 87 | transport = &http.Transport{} 88 | } 89 | 90 | p = &PuppetDB{ 91 | client: &http.Client{Transport: transport}, 92 | options: options, 93 | } 94 | return 95 | } 96 | 97 | // Nodes returns the list of nodes 98 | func (p *PuppetDB) Nodes() (nodes []Node, err error) { 99 | err = p.get("nodes", "[\"or\", [\"=\", [\"node\", \"active\"], false], [\"=\", [\"node\", \"active\"], true]]", &nodes) 100 | if err != nil { 101 | err = fmt.Errorf("failed to get nodes: %s", err) 102 | return 103 | } 104 | return 105 | } 106 | 107 | // ReportMetrics returns the list of reportMetrics 108 | func (p *PuppetDB) ReportMetrics(reportHash string) (reportMetrics []ReportMetric, err error) { 109 | err = p.get(fmt.Sprintf("reports/%s/metrics", reportHash), "", &reportMetrics) 110 | if err != nil { 111 | err = fmt.Errorf("failed to get reports: %s", err) 112 | return 113 | } 114 | return 115 | } 116 | 117 | func (p *PuppetDB) get(endpoint string, query string, object interface{}) (err error) { 118 | base := strings.TrimRight(p.options.URL, "/") 119 | var myurl string 120 | if query == "" { 121 | myurl = fmt.Sprintf("%s/v4/%s", base, endpoint) 122 | } else { 123 | myurl = fmt.Sprintf("%s/v4/%s?query=%s", base, endpoint, url.QueryEscape(query)) 124 | } 125 | req, err := http.NewRequest("GET", myurl, strings.NewReader("")) 126 | if err != nil { 127 | err = fmt.Errorf("failed to build request: %s", err) 128 | return 129 | } 130 | resp, err := p.client.Do(req) 131 | if err != nil { 132 | err = fmt.Errorf("failed to call API: %s", err) 133 | return 134 | } 135 | defer resp.Body.Close() 136 | 137 | body, err := ioutil.ReadAll(resp.Body) 138 | if err != nil { 139 | err = fmt.Errorf("failed to read response: %s", err) 140 | return 141 | } 142 | err = json.Unmarshal(body, object) 143 | if err != nil { 144 | err = fmt.Errorf("failed to unmarshal: %s", err) 145 | return 146 | } 147 | return 148 | } 149 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "net/http" 5 | "os" 6 | "runtime" 7 | "strings" 8 | "time" 9 | 10 | "github.com/jessevdk/go-flags" 11 | "github.com/prometheus/client_golang/prometheus" 12 | "github.com/prometheus/client_golang/prometheus/promhttp" 13 | log "github.com/sirupsen/logrus" 14 | 15 | "github.com/camptocamp/prometheus-puppetdb-exporter/internal/exporter" 16 | ) 17 | 18 | // Config stores handler's configuration 19 | type Config struct { 20 | Version bool `long:"version" description:"Show version."` 21 | PuppetDBUrl string `short:"u" long:"puppetdb-url" description:"PuppetDB base URL." env:"PUPPETDB_URL" required:"true" default:"https://puppetdb:8081/pdb/query"` 22 | CertFile string `long:"cert-file" description:"A PEM encoded certificate file." env:"PUPPETDB_CERT_FILE"` 23 | KeyFile string `long:"key-file" description:"A PEM encoded private key file." env:"PUPPETDB_KEY_FILE"` 24 | CACertFile string `long:"ca-file" description:"A PEM encoded CA's certificate." env:"PUPPETDB_CA_FILE"` 25 | SSLSkipVerify bool `long:"ssl-skip-verify" description:"Skip SSL verification." env:"PUPPETDB_SSL_SKIP_VERIFY"` 26 | ScrapeInterval string `long:"scrape-interval" description:"Duration between two scrapes." env:"PUPPETDB_SCRAPE_INTERVAL" default:"5s"` 27 | ListenAddress string `long:"listen-address" description:"Address to listen on for web interface and telemetry." env:"PUPPETDB_LISTEN_ADDRESS" default:"0.0.0.0:9635"` 28 | MetricPath string `long:"metric-path" description:"Path under which to expose metrics." env:"PUPPETDB_METRIC_PATH" default:"/metrics"` 29 | Verbose bool `long:"verbose" description:"Enable debug mode" env:"PUPPETDB_VERBOSE"` 30 | UnreportedNode string `long:"unreported-node" description:"Tag nodes as unreported if the latest report is older than the defined duration." env:"PUPPETDB_UNREPORTED_NODE" default:"2h"` 31 | Categories string `long:"categories" description:"Report metrics categories to scrape." env:"REPORT_METRICS_CATEGORIES" default:"resources,time,changes,events"` 32 | } 33 | 34 | var ( 35 | // VERSION, BUILD_DATE, GIT_COMMIT are filled in by the build script 36 | version = "<<< filled in by build >>>" 37 | buildDate = "<<< filled in by build >>>" 38 | commitSha1 = "<<< filled in by build >>>" 39 | ) 40 | 41 | func main() { 42 | var c Config 43 | parser := flags.NewParser(&c, flags.Default) 44 | if _, err := parser.Parse(); err != nil { 45 | if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp { 46 | os.Exit(0) 47 | } else { 48 | os.Exit(1) 49 | } 50 | } 51 | 52 | log.Printf("PuppetDB Metrics Exporter %s build date: %s sha1: %s Go: %s", 53 | version, buildDate, commitSha1, 54 | runtime.Version(), 55 | ) 56 | if c.Verbose { 57 | log.SetLevel(log.DebugLevel) 58 | log.Debugln("Enabling debug output") 59 | } else { 60 | log.SetLevel(log.InfoLevel) 61 | } 62 | 63 | if c.Version { 64 | return 65 | } 66 | 67 | interval, err := time.ParseDuration(c.ScrapeInterval) 68 | if err != nil { 69 | log.Fatalf("failed to parse scrape interval duration: %s", err) 70 | } 71 | 72 | // Create a map[string]struct{} of categories to provide an efficient way to 73 | // find if a category exists in the list of categories. 74 | cats := strings.Split(c.Categories, ",") 75 | categories := make(map[string]struct{}, len(cats)) 76 | for _, category := range cats { 77 | categories[category] = struct{}{} 78 | } 79 | exp, err := exporter.NewPuppetDBExporter(c.PuppetDBUrl, c.CertFile, c.CACertFile, c.KeyFile, c.SSLSkipVerify, categories) 80 | if err != nil { 81 | log.Fatalf("failed to initialize exporter: %s", err) 82 | } 83 | 84 | go exp.Scrape(interval, c.UnreportedNode, categories) 85 | 86 | buildInfo := prometheus.NewGaugeVec(prometheus.GaugeOpts{ 87 | Name: "puppetdb_exporter_build_info", 88 | Help: "puppetdb exporter build informations", 89 | }, []string{"version", "commit_sha", "build_date", "golang_version"}) 90 | buildInfo.WithLabelValues(version, commitSha1, buildDate, runtime.Version()).Set(1) 91 | prometheus.MustRegister(buildInfo) 92 | 93 | http.Handle(c.MetricPath, promhttp.Handler()) 94 | http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { 95 | w.Write([]byte(` 96 | 97 |