-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathone_exporter.go
107 lines (84 loc) · 2.64 KB
/
one_exporter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Prometheus exporter for OpenNebula.
package main
import (
"net/http"
"os"
"path"
"strconv"
"strings"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/spf13/viper"
"gopkg.in/alecthomas/kingpin.v2"
)
type config struct {
user string
password string
endpoint string
interval int
host string
port int
path string
}
func newConfig(fileName string, logger log.Logger) (config, error) {
viper.SetDefault("endpoint", "") // "" will be set to "http://localhost:2633/RPC2" by goca
viper.SetDefault("interval", 60)
viper.SetDefault("path", "/metrics")
viper.SetDefault("port", 9621)
viper.SetConfigType("yaml")
viper.AddConfigPath(".")
if fileName != "" {
level.Info(logger).Log("msg", "using provided configuration file", "file", fileName)
dir, file := path.Split(fileName)
viper.SetConfigName(file)
viper.AddConfigPath(dir)
}
err := viper.ReadInConfig()
if err != nil {
return config{}, err
}
return config{
user: viper.Get("user").(string),
password: viper.Get("password").(string),
endpoint: viper.Get("endpoint").(string),
interval: viper.Get("interval").(int),
host: viper.Get("host").(string),
port: viper.Get("port").(int),
path: viper.Get("path").(string),
}, nil
}
func allowedLevel(logLevel string) level.Option {
switch strings.ToLower(logLevel) {
case "error":
return level.AllowError()
case "debug":
return level.AllowDebug()
default:
return level.AllowInfo()
}
}
func main() {
logger := log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr))
logger = log.With(logger, "ts", log.DefaultTimestampUTC, "caller", log.DefaultCaller)
cfgFile := kingpin.Flag("config", "config file for one_exporter").Short('c').String()
logLevel := kingpin.Flag("loglevel", "the log level to output. options are error, info or debug. defaults to info").Short('l').Default("info").String()
kingpin.Version(Version)
kingpin.Parse()
logger = level.NewFilter(logger, allowedLevel(*logLevel))
level.Info(logger).Log("msg", "starting exporter for OpenNebula")
config, err := newConfig(*cfgFile, logger)
if err != nil {
level.Error(logger).Log("error", err)
return
}
level.Debug(logger).Log("msg", "loaded config", "user", config.user, "endpoint", config.endpoint)
initMetrics()
go recordMetrics(config, logger)
level.Info(logger).Log("msg", "starting exporter", "host", config.host, "port", config.port, "path", config.path)
http.Handle(config.path, promhttp.Handler())
err = http.ListenAndServe(config.host+":"+strconv.Itoa(config.port), nil)
if err != nil {
level.Error(logger).Log("error", err)
}
}