forked from iamseth/azure_sql_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
azure_sql_exporter.go
179 lines (160 loc) · 5.37 KB
/
azure_sql_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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package main
import (
"database/sql"
"flag"
"fmt"
"io/ioutil"
"net/http"
"sync"
yaml "gopkg.in/yaml.v2"
_ "github.com/denisenkom/go-mssqldb"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/log"
)
var (
// Version of azure_sql_exporter. Set at build time.
Version = "0.0.0.dev"
listenAddress = flag.String("web.listen-address", ":9139", "Address to listen on for web interface and telemetry.")
metricsPath = flag.String("web.telemetry-path", "/metrics", "Path under which to expose metrics.")
configFile = flag.String("config.file", "./config.yaml", "Specify the config file with the database credentials.")
)
const namespace = "azure_sql"
// Exporter implements prometheus.Collector.
type Exporter struct {
dbs []Database
mutex sync.RWMutex
up prometheus.Gauge
uerecalcAge *prometheus.GaugeVec
ncrecalcAge *prometheus.GaugeVec
dbUp *prometheus.GaugeVec
}
// NewExporter returns an initialized MS SQL Exporter.
func NewExporter(dbs []Database) *Exporter {
return &Exporter{
dbs: dbs,
up: newGuage("up", "Was the last scrape of Azure SQL successful."),
uerecalcAge: newGuageVec("ue_recalc_age", "Number of hours since the last sas recalc restore has been created"),
ncrecalcAge: newGuageVec("nc_recalc_age", "Number of hours since the last sas recalc restore has been created"),
dbUp: newGuageVec("db_up", "Is the database is accessible."),
}
}
// Describe describes all the metrics exported by the MS SQL exporter.
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
e.uerecalcAge.Describe(ch)
e.ncrecalcAge.Describe(ch)
e.dbUp.Describe(ch)
e.up.Describe(ch)
}
// Collect fetches the stats from MS SQL and delivers them as Prometheus metrics. It implements prometheus.Collector.
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
for _, db := range e.dbs {
log.Debugf("Scraping %s", db.String())
go e.scrapeDatabase(db)
}
e.mutex.Lock()
defer e.mutex.Unlock()
e.uerecalcAge.Collect(ch)
e.ncrecalcAge.Collect(ch)
e.dbUp.Collect(ch)
e.up.Set(1)
}
func (e *Exporter) scrapeDatabase(d Database) {
conn, err := sql.Open("mssql", d.DSN())
if err != nil {
e.mutex.Lock()
defer e.mutex.Unlock()
log.Errorf("Failed to access database %s: %s", d, err)
e.dbUp.WithLabelValues(d.Server, d.Name).Set(0)
return
}
defer conn.Close()
query := "SELECT TOP 1 (SELECT DATEDIFF(HOUR, create_date, GETDATE()) FROM sys.databases WHERE name = 'DBname'), (SELECT DATEDIFF(HOUR, create_date, GETDATE()) FROM sys.databases WHERE name = 'DBName')"
var uerecalcage, ncrecalcage float64
err = conn.QueryRow(query).Scan(&uerecalcage, &ncrecalcage)
if err != nil {
e.mutex.Lock()
defer e.mutex.Unlock()
log.Errorf("Failed to query database %s: %s", d, err)
e.dbUp.WithLabelValues(d.Server, d.Name).Set(0)
return
}
e.mutex.Lock()
defer e.mutex.Unlock()
e.uerecalcAge.WithLabelValues(d.Server, d.Name).Set(uerecalcage)
e.ncrecalcAge.WithLabelValues(d.Server, d.Name).Set(ncrecalcage)
e.dbUp.WithLabelValues(d.Server, d.Name).Set(1)
}
// Database represents a MS SQL database connection.
type Database struct {
Name string
Server string
User string
Password string
Port uint
}
// DSN returns the data source name as a string for the DB connection.
func (d Database) DSN() string {
return fmt.Sprintf("server=%s;user id=%s;password=%s;port=%d;database=%s", d.Server, d.User, d.Password, d.Port, d.Name)
}
// DSN returns the data source name as a string for the DB connection with the password hidden for safe log output.
func (d Database) String() string {
return fmt.Sprintf("server=%s;user id=%s;password=******;port=%d;database=%s", d.Server, d.User, d.Port, d.Name)
}
// Config contains all the required information for connecting to the databases.
type Config struct {
Databases []Database
}
// NewConfig creates an instance of Config from a local YAML file.
func NewConfig(path string) (Config, error) {
fh, err := ioutil.ReadFile(path)
if err != nil {
return Config{}, fmt.Errorf("unable to read file %s: %s", path, err)
}
var config Config
err = yaml.Unmarshal(fh, &config)
if err != nil {
return Config{}, fmt.Errorf("unable to unmarshal file %s: %s", path, err)
}
return config, nil
}
func newGuageVec(metricsName, docString string) *prometheus.GaugeVec {
return prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: metricsName,
Help: docString,
},
[]string{"server", "database"},
)
}
func newGuage(metricsName, docString string) prometheus.Gauge {
return prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: namespace,
Name: metricsName,
Help: docString,
},
)
}
func main() {
flag.Parse()
config, err := NewConfig(*configFile)
if err != nil {
log.Fatalf("Cannot open config file %s: %s", *configFile, err)
}
exporter := NewExporter(config.Databases)
prometheus.MustRegister(exporter)
http.Handle(*metricsPath, prometheus.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>Azure SQL Exporter</title></head>
<body>
<h1>Azure SQL Exporter</h1>
<p><a href='` + *metricsPath + `'>Metrics</a></p>
</body>
</html>
`))
})
log.Infof("Starting Server: %s", *listenAddress)
log.Fatal(http.ListenAndServe(*listenAddress, nil))
}