-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Grigory Ignatyev
committed
Jul 6, 2018
1 parent
983620c
commit 2b405c4
Showing
3 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
||
} |