-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
129 lines (103 loc) · 2.85 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"flag"
"fmt"
"net/http"
"os"
"strings"
"log"
"github.com/mnadeem/volume_exporter/exporter"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/version"
)
type volumeNamePathFlag []string
func (v *volumeNamePathFlag) Set(value string) error {
*v = append(*v, value)
return nil
}
func (v *volumeNamePathFlag) String() string {
return fmt.Sprint(*v)
}
type volumeFlags struct {
volumeNamePath volumeNamePathFlag
}
func main() {
var (
listenAddress = flag.String("web.listen-address", ":9888", "Address to listen on for web interface and telemetry.")
metricPath = flag.String("web.telemetry-path", "/metrics", "Path under which to expose metrics.")
volFlags = volumeFlags{}
)
log.Println("Starting volume_exporter")
flag.Var(&volFlags.volumeNamePath, "volume-dir", "Volumes to report, the format is volumeName:VolumeDir;\n For example ==> logs:/app/logs; can be used multiple times to provide more than one value")
flag.Parse()
if len(volFlags.volumeNamePath) < 1 {
log.Println("Missing volume-dir")
log.Println()
flag.Usage()
os.Exit(1)
}
volOpts := exporter.VolumeOpts{}
for _, np := range volFlags.volumeNamePath {
name, path := splitFlag(np)
log.Printf("Directory Name : %s, Path : %s", name, path)
volOpts.Options = append(volOpts.Options, exporter.VolumeOpt{Name: name, Path: path})
}
exporter.Register(&volOpts)
log.Println("Starting volume_exporter", version.Info())
log.Println("Build context", version.BuildContext())
log.Fatal(serverMetrics(*listenAddress, *metricPath))
}
// Split colon separated flag into two fields
func splitFlag(s string) (string, string) {
if len(s) == 0 {
log.Fatalln("Nothing to Monitor")
os.Exit(1)
}
slice := strings.SplitN(s, ":", 2)
if len(slice) == 1 {
log.Fatalf("Invalid option %s", s)
os.Exit(1)
}
name := strings.TrimSpace(slice[0])
path := strings.TrimSpace(slice[1])
if len(name) == 0 {
log.Fatalf("Invalid Name on %s", s)
os.Exit(1)
}
exists, err := exists(path)
if err != nil {
log.Fatalf("Error validating if path exists %s , error %v", path, err)
os.Exit(1)
}
if !exists {
log.Fatalf("Invalid Path %s", path)
os.Exit(1)
}
return name, path
}
func exists(path string) (bool, error) {
stat, err := os.Stat(path)
if err == nil {
return true, nil
}
if stat.IsDir() {
return true, nil
}
return false, err
}
func serverMetrics(listenAddress, metricsPath string) error {
http.Handle(metricsPath, promhttp.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`
<html>
<head><title>Volume Exporter Metrics</title></head>
<body>
<h1>Volume Exporter Metrics</h1>
<p><a href='` + metricsPath + `'>Metrics</a></p>
</body>
</html>
`))
})
log.Printf("Starting Server: %s", listenAddress)
return http.ListenAndServe(listenAddress, nil)
}