Skip to content

Commit

Permalink
add bash-exporter core
Browse files Browse the repository at this point in the history
  • Loading branch information
Grigory Ignatyev committed Jul 6, 2018
1 parent 983620c commit 2b405c4
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
64 changes: 64 additions & 0 deletions cmd/bash-exporter/bash-exporter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package main

import (
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"strings"
"sync"
"time"

"github.com/gree-gorey/bash-exporter/pkg/run"
"github.com/prometheus/client_golang/prometheus"
)

var (
webapiMetrics *prometheus.GaugeVec
)

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", "/usr/local/bash-exporter/run.sh", "path to script")
prefix := flag.String("prefix", "bash", "Prefix for metrics")
debug := flag.Bool("debug", false, "Debug log level")
flag.Parse()
pathArray := strings.Split(*path, "/")
name := strings.Split(pathArray[len(pathArray)-1], ".")[0]
webapiMetrics = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: fmt.Sprintf("%s_%s", *prefix, name),
Help: "tests",
},
[]string{"kind"},
)
prometheus.MustRegister(webapiMetrics)
http.Handle("/metrics", prometheus.Handler())
go Run(int(*interval), *path, *debug)
log.Fatal(http.ListenAndServe(*addr, nil))
}

func Run(interval int, path string, debug bool) {
for {
var wg sync.WaitGroup
o := run.Output{}
wg.Add(1)
p := run.Params{UseWg: true, Wg: &wg, Path: &path}
go o.RunJob(&p)
wg.Wait()
if debug == true {
ser, err := json.Marshal(o)
if err != nil {
log.Println(err)
}
log.Println(string(ser))
}
webapiMetrics.Reset()
for metric, value := range o.Result {
webapiMetrics.With(prometheus.Labels{"kind": metric}).Set(float64(value))
}
time.Sleep(time.Duration(interval) * time.Second)
}
}
10 changes: 10 additions & 0 deletions pkg/run/params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package run

import "sync"

// Type Params stores parameters.
type Params struct {
Path *string
UseWg bool
Wg *sync.WaitGroup
}
32 changes: 32 additions & 0 deletions pkg/run/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package run

import (
"encoding/json"
"log"
"os/exec"
)

type Output struct {
Result map[string]float64 `json:""`
}

func (o *Output) RunJob(p *Params) {
if p.UseWg {
defer p.Wg.Done()
}
o.RunExec(p.Path)
}

func (o *Output) RunExec(path *string) {

out, err := exec.Command(*path).Output()
if err != nil {
log.Fatal(err)
}

err = json.Unmarshal(out, &o.Result)
if err != nil {
log.Fatal(err)
}

}

0 comments on commit 2b405c4

Please sign in to comment.