From f03116a5cac1904d4fb4e8a730572c45b3967ea6 Mon Sep 17 00:00:00 2001 From: James Kassemi Date: Mon, 14 Nov 2016 22:58:09 -0700 Subject: [PATCH] Initial commit --- .dockerignore | 4 + .promu.yml | 31 ++++ Dockerfile | 9 + LICENSE | 8 + Makefile | 68 ++++++++ README.md | 136 +++++++++++++++ VERSION | 1 + script-exporter.yml | 10 ++ script_exporter.go | 159 ++++++++++++++++++ script_exporter_test.go | 45 +++++ vendor/github.com/Sirupsen/logrus | 1 + vendor/github.com/beorn7/perks | 1 + vendor/github.com/golang/protobuf | 1 + .../matttproud/golang_protobuf_extensions | 1 + vendor/github.com/prometheus/client_golang | 1 + vendor/github.com/prometheus/client_model | 1 + vendor/github.com/prometheus/common | 1 + vendor/github.com/prometheus/procfs | 1 + vendor/github.com/prometheus/promu | 1 + vendor/golang.org/x/sys | 1 + vendor/gopkg.in/yaml.v2 | 1 + 21 files changed, 482 insertions(+) create mode 100644 .dockerignore create mode 100644 .promu.yml create mode 100644 Dockerfile create mode 100644 LICENSE create mode 100644 Makefile create mode 100644 README.md create mode 100644 VERSION create mode 100644 script-exporter.yml create mode 100644 script_exporter.go create mode 100644 script_exporter_test.go create mode 160000 vendor/github.com/Sirupsen/logrus create mode 160000 vendor/github.com/beorn7/perks create mode 160000 vendor/github.com/golang/protobuf create mode 160000 vendor/github.com/matttproud/golang_protobuf_extensions create mode 160000 vendor/github.com/prometheus/client_golang create mode 160000 vendor/github.com/prometheus/client_model create mode 160000 vendor/github.com/prometheus/common create mode 160000 vendor/github.com/prometheus/procfs create mode 160000 vendor/github.com/prometheus/promu create mode 160000 vendor/golang.org/x/sys create mode 160000 vendor/gopkg.in/yaml.v2 diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..1694cb0 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,4 @@ +.build/ +.tarballs/ + +!.build/linux-amd64 diff --git a/.promu.yml b/.promu.yml new file mode 100644 index 0000000..528e7fb --- /dev/null +++ b/.promu.yml @@ -0,0 +1,31 @@ +go: + cgo: true +repository: + path: github.com/adhocteam/script_exporter +build: + flags: -a -tags 'netgo static_build' + ldflags: | + -linkmode external -extldflags -static + -X {{repoPath}}/vendor/github.com/prometheus/common/version.Version={{.Version}} + -X {{repoPath}}/vendor/github.com/prometheus/common/version.Revision={{.Revision}} + -X {{repoPath}}/vendor/github.com/prometheus/common/version.Branch={{.Branch}} + -X {{repoPath}}/vendor/github.com/prometheus/common/version.BuildUser={{user}}@{{host}} + -X {{repoPath}}/vendor/github.com/prometheus/common/version.BuildDate={{date "20060102-15:04:05"}} +tarball: + files: + - LICENSE +crossbuild: + platforms: + - linux/amd64 +# - linux/386 +# - darwin/amd64 +# - darwin/386 +# - windows/amd64 +# - windows/386 +# - netbsd/amd64 +# - netbsd/386 +# - linux/arm +# - linux/arm64 +# - netbsd/arm +# - linux/ppc64 +# - linux/ppc64le diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..d885ee8 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,9 @@ +FROM quay.io/prometheus/busybox:latest +MAINTAINER James Kassemi (Ad Hoc, LLC) + +COPY script-exporter /bin/script-exporter +COPY script-exporter.yml /etc/script-exporter/config.yml + +EXPOSE 9172 +ENTRYPOINT [ "/bin/script-exporter" ] +CMD ["-config.file=/etc/script-exporter/config.yml"] diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..37fd0a0 --- /dev/null +++ b/LICENSE @@ -0,0 +1,8 @@ +The MIT License (MIT) +Copyright (c) 2016 Ad Hoc, LLC + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..32e94b0 --- /dev/null +++ b/Makefile @@ -0,0 +1,68 @@ +# Copyright 2015 The Prometheus Authors +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +GO := GO15VENDOREXPERIMENT=1 go +PROMU := $(GOPATH)/bin/promu +pkgs = $(shell $(GO) list ./... | grep -v /vendor/) + +PREFIX ?= $(shell pwd) +BIN_DIR ?= $(shell pwd) +DOCKER_IMAGE_NAME ?= adhocteam/script-exporter +DOCKER_IMAGE_TAG ?= $(subst /,-,$(shell git rev-parse --abbrev-ref HEAD)) + + +all: format build test + +style: + @echo ">> checking code style" + @! gofmt -d $(shell find . -path ./vendor -prune -o -name '*.go' -print) | grep '^' + +test: + @echo ">> running tests" + @$(GO) test -short $(pkgs) + +format: + @echo ">> formatting code" + @$(GO) fmt $(pkgs) + +vet: + @echo ">> vetting code" + @$(GO) vet $(pkgs) + +build: promu + @echo ">> building binaries" + @$(PROMU) build --prefix $(PREFIX) + +tarball: promu + @echo ">> building release tarball" + @$(PROMU) tarball --prefix $(PREFIX) $(BIN_DIR) + +crossbuild: promu + @echo ">> building" + @$(PROMU) crossbuild + +docker: + @echo ">> building docker image" + @docker build -t "$(DOCKER_IMAGE_NAME):$(DOCKER_IMAGE_TAG)" . + +release: + @$(PROMU) crossbuild tarballs + @$(PROMU) release .tarballs + +promu: + @GOOS=$(shell uname -s | tr A-Z a-z) \ + GOARCH=$(subst x86_64,amd64,$(patsubst i%86,386,$(shell uname -m))) \ + $(GO) get -u github.com/prometheus/promu + + +.PHONY: all style format build crossbuild test vet tarball docker promu diff --git a/README.md b/README.md new file mode 100644 index 0000000..39eb4cd --- /dev/null +++ b/README.md @@ -0,0 +1,136 @@ +# Script Exporter + +GitHub: https://github.com/adhocteam/script_exporter + +Prometheus exporter written to execute and collect metrics on script exit status +and duration. Designed to allow the execution of probes where support for the +probe type wasn't easily configured with the Prometheus blackbox exporter. + +## Sample Configuration + +```yaml +scripts: + - name: success + script: sleep 5 + + - name: failure + script: sleep 2 && exit 1 + + - name: timeout + script: sleep 5 + timeout: 1 +``` + +## Running + +You can run via docker with: + +``` +docker run -d -p 9172:9172 --name script-exporter \ + -v `pwd`/config.yml:/etc/script-exporter/config.yml:ro \ + -config.file=/etc/script-exporter/config.yml + -web.listen-address=":9172" \ + -web.telemetry-path="/metrics" \ + -config.shell="/bin/sh" \ + adhocteam/script-exporter:master +``` + +You'll need to customize the docker image or use the binary on the host system +to install tools such as curl for certain scenarios. + +## Output + +`$ curl http://localhost:9172/metrics` + +``` +... +# HELP script_duration_seconds Duration for configured scripts with zero exit status +# TYPE script_duration_seconds histogram +script_duration_seconds_bucket{script="failure",le="0.005"} 0 +script_duration_seconds_bucket{script="failure",le="0.01"} 0 +script_duration_seconds_bucket{script="failure",le="0.025"} 0 +script_duration_seconds_bucket{script="failure",le="0.05"} 0 +script_duration_seconds_bucket{script="failure",le="0.1"} 0 +script_duration_seconds_bucket{script="failure",le="0.25"} 0 +script_duration_seconds_bucket{script="failure",le="0.5"} 0 +script_duration_seconds_bucket{script="failure",le="1"} 0 +script_duration_seconds_bucket{script="failure",le="2.5"} 1 +script_duration_seconds_bucket{script="failure",le="5"} 1 +script_duration_seconds_bucket{script="failure",le="10"} 1 +script_duration_seconds_bucket{script="failure",le="+Inf"} 1 +script_duration_seconds_sum{script="failure"} 2.014404206 +script_duration_seconds_count{script="failure"} 1 +script_duration_seconds_bucket{script="success",le="0.005"} 0 +script_duration_seconds_bucket{script="success",le="0.01"} 0 +script_duration_seconds_bucket{script="success",le="0.025"} 0 +script_duration_seconds_bucket{script="success",le="0.05"} 0 +script_duration_seconds_bucket{script="success",le="0.1"} 0 +script_duration_seconds_bucket{script="success",le="0.25"} 0 +script_duration_seconds_bucket{script="success",le="0.5"} 0 +script_duration_seconds_bucket{script="success",le="1"} 0 +script_duration_seconds_bucket{script="success",le="2.5"} 0 +script_duration_seconds_bucket{script="success",le="5"} 0 +script_duration_seconds_bucket{script="success",le="10"} 1 +script_duration_seconds_bucket{script="success",le="+Inf"} 1 +script_duration_seconds_sum{script="success"} 5.015765115 +script_duration_seconds_count{script="success"} 1 +script_duration_seconds_bucket{script="timeout",le="0.005"} 0 +script_duration_seconds_bucket{script="timeout",le="0.01"} 0 +script_duration_seconds_bucket{script="timeout",le="0.025"} 0 +script_duration_seconds_bucket{script="timeout",le="0.05"} 0 +script_duration_seconds_bucket{script="timeout",le="0.1"} 0 +script_duration_seconds_bucket{script="timeout",le="0.25"} 0 +script_duration_seconds_bucket{script="timeout",le="0.5"} 0 +script_duration_seconds_bucket{script="timeout",le="1"} 0 +script_duration_seconds_bucket{script="timeout",le="2.5"} 1 +script_duration_seconds_bucket{script="timeout",le="5"} 1 +script_duration_seconds_bucket{script="timeout",le="10"} 1 +script_duration_seconds_bucket{script="timeout",le="+Inf"} 1 +script_duration_seconds_sum{script="timeout"} 1.001585331 +script_duration_seconds_count{script="timeout"} 1 +# HELP script_exporter_build_info A metric with a constant '1' value labeled by version, revision, branch, and goversion from which script_exporter was built. +# TYPE script_exporter_build_info gauge +script_exporter_build_info{branch="",goversion="go1.7.3",revision="",version=""} 1 +# HELP script_failure_duration_seconds Duration for configured scripts with non-zero exit status +# TYPE script_failure_duration_seconds histogram +script_failure_duration_seconds_bucket{script="failure",le="0.005"} 0 +script_failure_duration_seconds_bucket{script="failure",le="0.01"} 0 +script_failure_duration_seconds_bucket{script="failure",le="0.025"} 0 +script_failure_duration_seconds_bucket{script="failure",le="0.05"} 0 +script_failure_duration_seconds_bucket{script="failure",le="0.1"} 0 +script_failure_duration_seconds_bucket{script="failure",le="0.25"} 0 +script_failure_duration_seconds_bucket{script="failure",le="0.5"} 0 +script_failure_duration_seconds_bucket{script="failure",le="1"} 0 +script_failure_duration_seconds_bucket{script="failure",le="2.5"} 1 +script_failure_duration_seconds_bucket{script="failure",le="5"} 1 +script_failure_duration_seconds_bucket{script="failure",le="10"} 1 +script_failure_duration_seconds_bucket{script="failure",le="+Inf"} 1 +script_failure_duration_seconds_sum{script="failure"} 2.014404206 +script_failure_duration_seconds_count{script="failure"} 1 +script_failure_duration_seconds_bucket{script="timeout",le="0.005"} 0 +script_failure_duration_seconds_bucket{script="timeout",le="0.01"} 0 +script_failure_duration_seconds_bucket{script="timeout",le="0.025"} 0 +script_failure_duration_seconds_bucket{script="timeout",le="0.05"} 0 +script_failure_duration_seconds_bucket{script="timeout",le="0.1"} 0 +script_failure_duration_seconds_bucket{script="timeout",le="0.25"} 0 +script_failure_duration_seconds_bucket{script="timeout",le="0.5"} 0 +script_failure_duration_seconds_bucket{script="timeout",le="1"} 0 +script_failure_duration_seconds_bucket{script="timeout",le="2.5"} 1 +script_failure_duration_seconds_bucket{script="timeout",le="5"} 1 +script_failure_duration_seconds_bucket{script="timeout",le="10"} 1 +script_failure_duration_seconds_bucket{script="timeout",le="+Inf"} 1 +script_failure_duration_seconds_sum{script="timeout"} 1.001585331 +script_failure_duration_seconds_count{script="timeout"} 1 +``` + +## Design + +The script exporter adds `script_failure_duration_seconds` and +`script_duration_seconds` histograms configured with the default buckets +to the default prometheus handler metrics available at `/metrics`. When Prometheus +scrapes the target all scripts are executed with sh and the observations are +included in the output. + +YMMV if you're attempting to execute a large number of scripts, and you'd be +better off creating an exporter that can handle your protocol without launching +shell processes for each scrape. diff --git a/VERSION b/VERSION new file mode 100644 index 0000000..8acdd82 --- /dev/null +++ b/VERSION @@ -0,0 +1 @@ +0.0.1 diff --git a/script-exporter.yml b/script-exporter.yml new file mode 100644 index 0000000..1227ff8 --- /dev/null +++ b/script-exporter.yml @@ -0,0 +1,10 @@ +scripts: + - name: 'success' + script: sleep 5 + + - name: 'failure' + script: sleep 2 && exit 1 + + - name: 'timeout' + script: sleep 5 + timeout: 1 diff --git a/script_exporter.go b/script_exporter.go new file mode 100644 index 0000000..4a3688d --- /dev/null +++ b/script_exporter.go @@ -0,0 +1,159 @@ +package main + +import ( + "context" + "flag" + "fmt" + "gopkg.in/yaml.v2" + "io/ioutil" + "net/http" + "os" + "os/exec" + "time" + + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/common/log" + "github.com/prometheus/common/version" +) + +var ( + showVersion = flag.Bool("version", false, "Print version information.") + configFile = flag.String("config.file", "script-exporter.yml", "Script exporter configuration file.") + listenAddress = flag.String("web.listen-address", ":9172", "The address to listen on for HTTP requests.") + metricsPath = flag.String("web.telemetry-path", "/metrics", "Path under which to expose metrics.") + shell = flag.String("config.shell", "/bin/sh", "Shell to execute script") + + histogram = prometheus.NewHistogramVec(prometheus.HistogramOpts{ + Name: "script_duration_seconds", + Help: "Duration for configured scripts with zero exit status", + }, []string{"script"}) + + failureHistogram = prometheus.NewHistogramVec(prometheus.HistogramOpts{ + Name: "script_failure_duration_seconds", + Help: "Duration for configured scripts with non-zero exit status", + }, []string{"script"}) +) + +type Config struct { + Scripts []*Script `yaml:"scripts"` +} + +type Script struct { + Name string `yaml:"name"` + Content string `yaml:"script"` + Timeout int64 `yaml:"timeout"` +} + +func runScript(script *Script) error { + ctx, cancel := context.WithTimeout(context.Background(), time.Duration(script.Timeout)*time.Second) + defer cancel() + + bashCmd := exec.CommandContext(ctx, *shell) + + bashIn, err := bashCmd.StdinPipe() + + if err != nil { + return err + } + + if err = bashCmd.Start(); err != nil { + return err + } + + if _, err = bashIn.Write([]byte(script.Content)); err != nil { + return err + } + + bashIn.Close() + + return bashCmd.Wait() +} + +func runScripts(config *Config) { + ch := make(chan bool) + + for _, script := range config.Scripts { + go func(script *Script) { + start := time.Now() + err := runScript(script) + duration := time.Since(start).Seconds() + + if err == nil { + log.Debugf("OK: %s (after %fs).", script.Name, duration) + } else { + log.Errorf("ERROR: %s: %s (failed after %fs).", script.Name, err, duration) + failureHistogram.WithLabelValues(script.Name).Observe(duration) + } + + histogram.WithLabelValues(script.Name).Observe(duration) + + ch <- true + }(script) + } + + for i := 0; i < len(config.Scripts); i++ { + <-ch + } +} + +func init() { + prometheus.MustRegister(version.NewCollector("script_exporter")) + prometheus.MustRegister(histogram) + prometheus.MustRegister(failureHistogram) +} + +func main() { + flag.Parse() + + if *showVersion { + fmt.Fprintln(os.Stdout, version.Print("script_exporter")) + os.Exit(0) + } + + log.Infoln("Starting script_exporter", version.Info()) + + yamlFile, err := ioutil.ReadFile(*configFile) + + if err != nil { + log.Fatalf("Error reading config file: %s", err) + } + + config := Config{} + + err = yaml.Unmarshal(yamlFile, &config) + + if err != nil { + log.Fatalf("Error parsing config file: %s", err) + } + + log.Infof("Loaded %d script configurations", len(config.Scripts)) + + for _, script := range config.Scripts { + if script.Timeout == 0 { + script.Timeout = 15 + } + } + + promHandler := prometheus.Handler() + + http.HandleFunc("/metrics", func(w http.ResponseWriter, r *http.Request) { + runScripts(&config) + promHandler.ServeHTTP(w, r) + }) + + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + w.Write([]byte(` + Script Exporter + +

Script Exporter

+

Metrics

+ + `)) + }) + + log.Infoln("Listening on", *listenAddress) + + if err := http.ListenAndServe(*listenAddress, nil); err != nil { + log.Fatalf("Error starting HTTP server: %s", err) + } +} diff --git a/script_exporter_test.go b/script_exporter_test.go new file mode 100644 index 0000000..3ea94eb --- /dev/null +++ b/script_exporter_test.go @@ -0,0 +1,45 @@ +package main + +import ( + "testing" + + dto "github.com/prometheus/client_model/go" +) + +var config = &Config{ + Scripts: []*Script{ + {"success", "exit 0", 1}, + {"failure", "exit 1", 1}, + {"timeout", "sleep 5", 1}, + }, +} + +func TestRunScripts(t *testing.T) { + runScripts(config) + + results := []struct { + name string + total int + failure int + }{ + {"success", 1, 0}, + {"failure", 1, 1}, + {"timeout", 1, 1}, + } + + for _, result := range results { + s := &dto.Metric{} + histogram.WithLabelValues(result.name).Write(s) + + if samples := int(*s.Histogram.SampleCount); samples != result.total { + t.Errorf("Expecting 1 total sample, received %d", samples) + } + + f := &dto.Metric{} + failureHistogram.WithLabelValues(result.name).Write(f) + + if samples := int(*f.Histogram.SampleCount); samples != result.failure { + t.Errorf("Expecting 1 failed sample, received %d", samples) + } + } +} diff --git a/vendor/github.com/Sirupsen/logrus b/vendor/github.com/Sirupsen/logrus new file mode 160000 index 0000000..abc6f20 --- /dev/null +++ b/vendor/github.com/Sirupsen/logrus @@ -0,0 +1 @@ +Subproject commit abc6f20dabf4b10195f233ad21ea6c5ba33acae0 diff --git a/vendor/github.com/beorn7/perks b/vendor/github.com/beorn7/perks new file mode 160000 index 0000000..4c0e845 --- /dev/null +++ b/vendor/github.com/beorn7/perks @@ -0,0 +1 @@ +Subproject commit 4c0e84591b9aa9e6dcfdf3e020114cd81f89d5f9 diff --git a/vendor/github.com/golang/protobuf b/vendor/github.com/golang/protobuf new file mode 160000 index 0000000..4bd1920 --- /dev/null +++ b/vendor/github.com/golang/protobuf @@ -0,0 +1 @@ +Subproject commit 4bd1920723d7b7c925de087aa32e2187708897f7 diff --git a/vendor/github.com/matttproud/golang_protobuf_extensions b/vendor/github.com/matttproud/golang_protobuf_extensions new file mode 160000 index 0000000..c12348c --- /dev/null +++ b/vendor/github.com/matttproud/golang_protobuf_extensions @@ -0,0 +1 @@ +Subproject commit c12348ce28de40eed0136aa2b644d0ee0650e56c diff --git a/vendor/github.com/prometheus/client_golang b/vendor/github.com/prometheus/client_golang new file mode 160000 index 0000000..7664178 --- /dev/null +++ b/vendor/github.com/prometheus/client_golang @@ -0,0 +1 @@ +Subproject commit 7664178cb2d46b7b36a28d7b0af688ab436a3fa0 diff --git a/vendor/github.com/prometheus/client_model b/vendor/github.com/prometheus/client_model new file mode 160000 index 0000000..fa8ad6f --- /dev/null +++ b/vendor/github.com/prometheus/client_model @@ -0,0 +1 @@ +Subproject commit fa8ad6fec33561be4280a8f0514318c79d7f6cb6 diff --git a/vendor/github.com/prometheus/common b/vendor/github.com/prometheus/common new file mode 160000 index 0000000..0d5de9d --- /dev/null +++ b/vendor/github.com/prometheus/common @@ -0,0 +1 @@ +Subproject commit 0d5de9d6d8629cb8bee6d4674da4127cd8b615a3 diff --git a/vendor/github.com/prometheus/procfs b/vendor/github.com/prometheus/procfs new file mode 160000 index 0000000..abf152e --- /dev/null +++ b/vendor/github.com/prometheus/procfs @@ -0,0 +1 @@ +Subproject commit abf152e5f3e97f2fafac028d2cc06c1feb87ffa5 diff --git a/vendor/github.com/prometheus/promu b/vendor/github.com/prometheus/promu new file mode 160000 index 0000000..9f4d6bd --- /dev/null +++ b/vendor/github.com/prometheus/promu @@ -0,0 +1 @@ +Subproject commit 9f4d6bd7f2229244ed87917c7f1a641c777e21be diff --git a/vendor/golang.org/x/sys b/vendor/golang.org/x/sys new file mode 160000 index 0000000..b699b70 --- /dev/null +++ b/vendor/golang.org/x/sys @@ -0,0 +1 @@ +Subproject commit b699b7032584f0953262cb2788a0ca19bb494703 diff --git a/vendor/gopkg.in/yaml.v2 b/vendor/gopkg.in/yaml.v2 new file mode 160000 index 0000000..a5b47d3 --- /dev/null +++ b/vendor/gopkg.in/yaml.v2 @@ -0,0 +1 @@ +Subproject commit a5b47d31c556af34a302ce5d659e6fea44d90de0