/
options.go
64 lines (53 loc) · 1.44 KB
/
options.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
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
yaml "gopkg.in/yaml.v2"
)
var (
exporterVersion = "0.5"
)
// NFTOptions is a inner representation of a options
type NFTOptions struct {
BindTo string `yaml:"bind_to"`
URLPath string `yaml:"url_path"`
FakeNftJSON string `yaml:"fake_nft_json"`
NFTLocation string `yaml:"nft_location"`
}
// Options is a representation of a options
type Options struct {
Nft NFTOptions `yaml:"nftables_exporter"`
}
// Parse options from yaml config file
func loadOptions() Options {
configFile := flag.String("config", "/etc/nftables_exporter.yaml", "Path to nftables_exporter config file")
verbose := flag.Bool("verbose", false, "Verbose log output")
debug := flag.Bool("debug", false, "Debug log output")
version := flag.Bool("version", false, "Show application version and exit")
flag.Parse()
if *version {
fmt.Printf("nftables_exporter version: %s\n", exporterVersion)
os.Exit(0)
}
logger = newLogger(*verbose, *debug)
logger.Verbose("Read options from %s\n", *configFile)
yamlFile, err := ioutil.ReadFile(*configFile)
if err != nil {
logger.Panic("Failed read %s: %s", configFile, err)
}
opts := Options{
NFTOptions{
BindTo: "9630",
URLPath: "/metrics",
FakeNftJSON: "",
NFTLocation: "/sbin/nft",
},
}
if yaml.Unmarshal(yamlFile, &opts) != nil {
logger.Panic("Failed parse %s: %s", configFile, err)
}
logger.Debug("Parsed options: %s", opts)
return opts
}