-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
88 lines (72 loc) · 2.04 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
package main
import (
"database/sql"
"fmt"
"github.com/gofiber/adaptor/v2"
"github.com/gofiber/fiber/v2"
"github.com/namsral/flag"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/sirupsen/logrus"
"github.com/umutcomlekci/mssql_exporter/collector"
_ "github.com/denisenkom/go-mssqldb"
)
var (
log = logrus.New()
)
type config struct {
sqlServer string
sqlServerPort int
sqlUserName string
sqlPassword string
}
func readAndValidateConfig() config {
var result config
flag.StringVar(&result.sqlServer, "sqlserver", "", "Sql Server")
flag.IntVar(&result.sqlServerPort, "sqlport", 1433, "Sql Server Port")
flag.StringVar(&result.sqlUserName, "sqlusername", "", "Sql Username")
flag.StringVar(&result.sqlPassword, "sqlpassword", "", "Sql Password")
flag.Parse()
return result
}
func configureRoutes(app *fiber.App) {
var landingPage = []byte(`<html>
<head><title>MSSQL exporter for Prometheus</title></head>
<body>
<h1>MSSQL exporter for Prometheus</h1>
<p><a href='/metrics'>Metrics</a></p>
</body>
</html>
`)
app.Get("/", func(c *fiber.Ctx) error {
_, err := c.Write(landingPage)
return err
})
app.Get("/metrics", adaptor.HTTPHandler(promhttp.Handler()))
}
func main() {
config := readAndValidateConfig()
if config.sqlServer == "" {
log.Fatal("Missing SERVER information")
return
} else if config.sqlUserName == "" {
log.Fatal("Missing USERNAME information")
return
} else if config.sqlPassword == "" {
log.Fatal("Missing PASSWORD information")
return
}
log.Infof("Connecting to server %s", config.sqlServer)
connString := fmt.Sprintf("server=%s;user id=%s;password=%s;port=%d;", config.sqlServer, config.sqlUserName, config.sqlPassword, config.sqlServerPort)
conn, err := sql.Open("mssql", connString)
if err != nil {
log.Fatal("Sql connection failed")
return
}
defer conn.Close()
coll := collector.New(conn, log)
prometheus.MustRegister(coll)
app := fiber.New()
configureRoutes(app)
log.Fatal(app.Listen(":8080"))
}