1
0
Fork 0
mirror of https://github.com/Luzifer/elb-instance-status.git synced 2024-10-18 04:44:25 +00:00

Expose metrics about checks for prometheus

This commit is contained in:
Knut Ahlers 2016-06-06 16:33:45 +02:00
parent f398ff8eda
commit eaa816da4e
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E
3 changed files with 90 additions and 23 deletions

View file

@ -1,16 +1,21 @@
--- ---
- name: Ensure there are at least 30% free inodes on / root_free_inodes:
name: Ensure there are at least 30% free inodes on /
command: test $(df -i | grep "/$" | xargs | cut -d ' ' -f 5 | sed "s/%//") -lt 70 command: test $(df -i | grep "/$" | xargs | cut -d ' ' -f 5 | sed "s/%//") -lt 70
- name: Ensure there are at least 30% free inodes on /var/lib/docker docker_free_inodes:
name: Ensure there are at least 30% free inodes on /var/lib/docker
command: test $(df -i | grep "/var/lib/docker$" | xargs | cut -d ' ' -f 5 | sed "s/%//") -lt 70 command: test $(df -i | grep "/var/lib/docker$" | xargs | cut -d ' ' -f 5 | sed "s/%//") -lt 70
- name: Ensure there is at least 30% free disk space on /var/lib/docker docker_free_diskspace:
name: Ensure there is at least 30% free disk space on /var/lib/docker
command: test $(df | grep "/var/lib/docker$" | xargs | cut -d ' ' -f 5 | sed "s/%//") -lt 70 command: test $(df | grep "/var/lib/docker$" | xargs | cut -d ' ' -f 5 | sed "s/%//") -lt 70
- name: Ensure volume on /var/lib/docker is mounted docker_mounted:
name: Ensure volume on /var/lib/docker is mounted
command: mount | grep -q /var/lib/docker command: mount | grep -q /var/lib/docker
- name: Ensure docker can start a small container docker_start_container:
command: docker run --rm alpine /bin/sh -c "echo testing123" | grep -q testing123 name: Ensure docker can start a small container
command: docker run --rm alpine /bin/sh -c "echo testing123" | grep -q testing123

38
main.go
View file

@ -17,6 +17,7 @@ import (
"github.com/Luzifer/rconfig" "github.com/Luzifer/rconfig"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus"
"github.com/robfig/cron" "github.com/robfig/cron"
) )
@ -30,7 +31,7 @@ var (
version = "dev" version = "dev"
checks = []checkCommand{} checks = map[string]checkCommand{}
checkResults = map[string]*checkResult{} checkResults = map[string]*checkResult{}
checkResultsLock sync.RWMutex checkResultsLock sync.RWMutex
lastResultRegistered time.Time lastResultRegistered time.Time
@ -78,17 +79,19 @@ func main() {
r := mux.NewRouter() r := mux.NewRouter()
r.HandleFunc("/status", handleELBHealthCheck) r.HandleFunc("/status", handleELBHealthCheck)
r.Handle("/metrics", prometheus.Handler())
http.ListenAndServe(cfg.Listen, r) http.ListenAndServe(cfg.Listen, r)
} }
func spawnChecks() { func spawnChecks() {
for i := range checks { for id := range checks {
go executeAndRegisterCheck(i) go executeAndRegisterCheck(id)
} }
} }
func executeAndRegisterCheck(checkIndex int) { func executeAndRegisterCheck(checkID string) {
check := checks[checkIndex] check := checks[checkID]
start := time.Now()
cmd := exec.Command("/bin/bash", "-c", check.Command) cmd := exec.Command("/bin/bash", "-c", check.Command)
err := cmd.Run() err := cmd.Run()
@ -97,21 +100,28 @@ func executeAndRegisterCheck(checkIndex int) {
checkResultsLock.Lock() checkResultsLock.Lock()
if _, ok := checkResults[check.Name]; !ok { if _, ok := checkResults[checkID]; !ok {
checkResults[check.Name] = &checkResult{ checkResults[checkID] = &checkResult{
Check: check, Check: check,
} }
} }
if success == checkResults[check.Name].IsSuccess { if success == checkResults[checkID].IsSuccess {
checkResults[check.Name].Streak++ checkResults[checkID].Streak++
} else { } else {
checkResults[check.Name].IsSuccess = success checkResults[checkID].IsSuccess = success
checkResults[check.Name].Streak = 1 checkResults[checkID].Streak = 1
} }
lastResultRegistered = time.Now() lastResultRegistered = time.Now()
if success {
checkPassing.WithLabelValues(checkID).Set(1)
} else {
checkPassing.WithLabelValues(checkID).Set(0)
}
checkExecutionTime.WithLabelValues(checkID).Observe(float64(time.Since(start).Nanoseconds()) / float64(time.Microsecond))
checkResultsLock.Unlock() checkResultsLock.Unlock()
} }
@ -121,7 +131,7 @@ func handleELBHealthCheck(res http.ResponseWriter, r *http.Request) {
buf := bytes.NewBuffer([]byte{}) buf := bytes.NewBuffer([]byte{})
checkResultsLock.RLock() checkResultsLock.RLock()
for cn, cr := range checkResults { for _, cr := range checkResults {
state := "" state := ""
switch { switch {
case cr.IsSuccess: case cr.IsSuccess:
@ -134,15 +144,17 @@ func handleELBHealthCheck(res http.ResponseWriter, r *http.Request) {
state = "CRIT" state = "CRIT"
healthy = false healthy = false
} }
fmt.Fprintf(buf, "[%s] %s\n", state, cn) fmt.Fprintf(buf, "[%s] %s\n", state, cr.Check.Name)
} }
checkResultsLock.RUnlock() checkResultsLock.RUnlock()
res.Header().Set("X-Collection-Parsed-In", strconv.FormatInt(time.Since(start).Nanoseconds()/int64(time.Microsecond), 10)+"ms") res.Header().Set("X-Collection-Parsed-In", strconv.FormatInt(time.Since(start).Nanoseconds()/int64(time.Microsecond), 10)+"ms")
res.Header().Set("X-Last-Result-Registered-At", lastResultRegistered.Format(time.RFC1123)) res.Header().Set("X-Last-Result-Registered-At", lastResultRegistered.Format(time.RFC1123))
if healthy { if healthy {
currentStatusCode.Set(http.StatusOK)
res.WriteHeader(http.StatusOK) res.WriteHeader(http.StatusOK)
} else { } else {
currentStatusCode.Set(http.StatusInternalServerError)
res.WriteHeader(http.StatusInternalServerError) res.WriteHeader(http.StatusInternalServerError)
} }

50
metrics.go Normal file
View file

@ -0,0 +1,50 @@
package main
import (
"log"
"os"
"github.com/prometheus/client_golang/prometheus"
)
var (
checkPassing *prometheus.GaugeVec
checkExecutionTime *prometheus.SummaryVec
currentStatusCode prometheus.Gauge
dynamicLabels = []string{"check_id"}
)
func init() {
hostname, err := os.Hostname()
if err != nil {
log.Fatalf("Unable to determine own hostname: %s", err)
}
co := prometheus.GaugeOpts{
Subsystem: "elb_instance_status",
ConstLabels: prometheus.Labels{"hostname": hostname},
}
co.Name = "check_passing"
co.Help = "Bit showing whether the check PASSed (=1) or FAILed (=0), WARNs are also reported as FAILs"
cp := prometheus.NewGaugeVec(co, dynamicLabels)
co.Name = "status_code"
co.Help = "Contains the current HTTP status code the ELB is seeing"
csc := prometheus.NewGauge(co)
cet := prometheus.NewSummaryVec(prometheus.SummaryOpts{
Namespace: co.Namespace,
Subsystem: co.Subsystem,
ConstLabels: co.ConstLabels,
Name: "check_execution_time",
Help: "Timespan in µs the execution of the check took",
}, dynamicLabels)
checkPassing = prometheus.MustRegisterOrGet(cp).(*prometheus.GaugeVec)
currentStatusCode = prometheus.MustRegisterOrGet(csc).(prometheus.Gauge)
checkExecutionTime = prometheus.MustRegisterOrGet(cet).(*prometheus.SummaryVec)
}