forked from louisfelix/azure-health-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
114 lines (91 loc) · 3.03 KB
/
main.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
108
109
110
111
112
113
114
package main
import (
"io/ioutil"
"net/http"
"os"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/log"
"github.com/prometheus/common/version"
"gopkg.in/alecthomas/kingpin.v2"
"gopkg.in/yaml.v2"
)
var (
configFile = kingpin.Flag("config.file", "Exporter configuration file.").Default("config/config.yml").String()
listenAddress = kingpin.Flag("web.listen-address", "The address to listen on for HTTP requests.").Default(":9613").String()
metricsPath = kingpin.Flag("web.telemetry-path", "Path under which to expose metrics.").Default("/metrics").String()
config Config
azureErrorDesc = prometheus.NewDesc("azure_error", "Error collecting metrics", nil, nil)
)
// Config of the exporter
type Config struct {
ResourceConfigurations []ResourceConfiguration `yaml:"resource_configurations"`
ExposeAzureTagInfo bool `yaml:"expose_azure_tag_info"`
}
// ResourceConfiguration specify resources to monitor (by types and tags)
type ResourceConfiguration struct {
ResourceTags map[string]string `yaml:"resource_tags"`
ResourceTypes []string `yaml:"resource_types"`
}
func init() {
prometheus.MustRegister(version.NewCollector("azure_health_exporter"))
}
func main() {
kingpin.Version(version.Print("azure-health-exporter"))
kingpin.HelpFlag.Short('h')
kingpin.Parse()
log.Info("Starting exporter", version.Info())
log.Info("Build context", version.BuildContext())
_, err := loadConfig(*configFile)
if err != nil {
log.Fatalf("Error loading config file: %v", err)
}
session, err := NewAzureSession(os.Getenv("AZURE_SUBSCRIPTION_ID"))
if err != nil {
log.Fatalf("Error creating Azure session: %v", err)
}
resourceHealthCollector := NewResourceHealthCollector(session)
prometheus.MustRegister(resourceHealthCollector)
http.Handle(*metricsPath, promhttp.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>azure-health-exporter</title></head>
<body>
<h1>azure-health-exporter</h1>
<p><a href="` + *metricsPath + `">Metrics</a></p>
</body>
</html>`))
})
log.Info("Beginning to serve on address ", *listenAddress)
log.Fatal(http.ListenAndServe(*listenAddress, nil))
}
func loadConfig(configFile string) (Config, error) {
if fileExists(configFile) {
log.Infof("Loading config file %v", configFile)
// Load config from file
configData, err := ioutil.ReadFile(configFile)
if err != nil {
return Config{}, err
}
return loadConfigContent(configData)
}
log.Infof("Config file %v does not exist, using default values", configFile)
return Config{}, nil
}
func loadConfigContent(configData []byte) (Config, error) {
config = Config{}
var err error
err = yaml.Unmarshal(configData, &config)
if err != nil {
return config, err
}
log.Info("Config loaded")
return config, nil
}
func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}