forked from zhangjianweibj/prometheus-libvirt-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
46 lines (41 loc) · 1.53 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
package main
import (
"flag"
"fmt"
"github.com/digitalocean/go-libvirt"
exp "github.com/onmetal/prometheus-libvirt-exporter/pkg/prometheus-libvirt-exporter"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.uber.org/zap"
"net/http"
)
func main() {
logger, _ := zap.NewProduction()
defer logger.Sync()
var (
listenAddress = flag.String("web.listen-address", ":9000", "Address to listen on for web interface and telemetry.")
metricsPath = flag.String("web.telemetry-path", "/metrics", "Path under which to expose metrics.")
libvirtURI = flag.String("libvirt.uri", "/var/run/libvirt/libvirt-sock-ro", "Libvirt URI from which to extract metrics.")
driver = flag.String("libvirt.driver", string(libvirt.QEMUSystem), fmt.Sprintf("Available drivers: %s (Default), %s, %s and %s ", libvirt.QEMUSystem, libvirt.QEMUSession, libvirt.XenSystem, libvirt.TestDefault))
)
flag.Parse()
exporter, err := exp.NewLibvirtExporter(*libvirtURI, libvirt.ConnectURI(*driver))
if err != nil {
panic(err)
}
prometheus.MustRegister(exporter)
http.Handle(*metricsPath, promhttp.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`
<html>
<head><title>Libvirt Exporter</title></head>
<body>
<h1>Libvirt Exporter</h1>
<p><a href='` + *metricsPath + `'>Metrics</a></p>
</body>
</html>`))
})
if err = http.ListenAndServe(*listenAddress, nil); err != nil {
logger.Error("unexpected server shutdown", zap.Error(err))
}
}