diff --git a/cmd/bash-exporter/bash-exporter.go b/cmd/bash-exporter/bash-exporter.go index b2b2589..d98b362 100644 --- a/cmd/bash-exporter/bash-exporter.go +++ b/cmd/bash-exporter/bash-exporter.go @@ -23,16 +23,23 @@ func main() { addr := flag.String("web.listen-address", ":9300", "Address on which to expose metrics") interval := flag.Int("interval", 300, "Interval for metrics collection in seconds") path := flag.String("path", "/scripts", "path to directory with bash scripts") + labels := flag.String("labels", "hostname,env", "additioanal labels") prefix := flag.String("prefix", "bash", "Prefix for metrics") debug := flag.Bool("debug", false, "Debug log level") flag.Parse() + var labelsArr []string + + labelsArr = strings.Split(*labels, ",") + labelsArr = append(labelsArr, "verb", "job") + verbMetrics = prometheus.NewGaugeVec( prometheus.GaugeOpts{ Name: fmt.Sprintf("%s", *prefix), Help: "bash exporter metrics", }, - []string{"verb", "job"}, + // []string{"verb", "job"}, + labelsArr, ) prometheus.MustRegister(verbMetrics) @@ -49,11 +56,11 @@ func main() { } http.Handle("/metrics", prometheus.Handler()) - go Run(int(*interval), *path, names, *debug) + go Run(int(*interval), *path, names, labelsArr, *debug) log.Fatal(http.ListenAndServe(*addr, nil)) } -func Run(interval int, path string, names []string, debug bool) { +func Run(interval int, path string, names []string, labelsArr []string, debug bool) { for { var wg sync.WaitGroup oArr := []*run.Output{} @@ -76,8 +83,17 @@ func Run(interval int, path string, names []string, debug bool) { // } verbMetrics.Reset() for _, o := range oArr { - for metric, value := range o.Result { - verbMetrics.With(prometheus.Labels{"verb": metric, "job": o.Job}).Set(float64(value)) + + for metric, value := range o.Schema.Results { + for _, label := range labelsArr { + if _, ok := o.Schema.Labels[label]; !ok { + o.Schema.Labels[label] = "" + } + } + o.Schema.Labels["verb"] = metric + o.Schema.Labels["job"] = o.Job + fmt.Println(o.Schema.Labels) + verbMetrics.With(prometheus.Labels(o.Schema.Labels)).Set(float64(value)) } } time.Sleep(time.Duration(interval) * time.Second) diff --git a/examples/job-1.sh b/examples/job-1.sh index 669a087..c67d491 100755 --- a/examples/job-1.sh +++ b/examples/job-1.sh @@ -1,5 +1,5 @@ #!/bin/sh -echo "{\"items\": 21}" +echo '{"labels": {"env": "dev"}, "results": {"items": 21} }' exit 0 diff --git a/examples/job-2.sh b/examples/job-2.sh index 004a094..5946691 100755 --- a/examples/job-2.sh +++ b/examples/job-2.sh @@ -1,5 +1,5 @@ #!/bin/sh -echo "{\"get\": 0.003, \"put\": 0.13, \"time\": 0.5}" +echo '{"labels": {"hostname": "node-1"}, "results": {"get": 0.003, "put": 0.13, "time": 0.5} }' exit 0 diff --git a/examples/job-3.sh b/examples/job-3.sh new file mode 100755 index 0000000..3f77ce1 --- /dev/null +++ b/examples/job-3.sh @@ -0,0 +1,19 @@ +#!/bin/bash + +hnm=`hostname` + +if [ true ] +then + result=1 +else + result=0 +fi + +if [ $result=1 ] +then + echo "{\"labels\": {\"host\": \"$hnm\"}, \"results\": {\"my_system_health\": 1} }" +else + echo "{\"labels\": {\"host\": \"$hnm\"}, \"results\": {\"my_system_health\": 0} }" +fi + +exit 0 diff --git a/pkg/run/run.go b/pkg/run/run.go index 493645b..b6a347b 100644 --- a/pkg/run/run.go +++ b/pkg/run/run.go @@ -7,8 +7,13 @@ import ( ) type Output struct { - Result map[string]float64 `json:""` - Job string `json:""` + Schema Schema `json:""` + Job string `json:""` +} + +type Schema struct { + Results map[string]float64 `json:"results"` + Labels map[string]string `json:"labels"` } func (o *Output) RunJob(p *Params) { @@ -25,7 +30,7 @@ func (o *Output) RunExec(path *string) { log.Fatal(err) } - err = json.Unmarshal(out, &o.Result) + err = json.Unmarshal(out, &o.Schema) if err != nil { log.Fatal(err) }