-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
69 lines (59 loc) · 1.71 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
package main
import (
"fmt"
"net"
"net/http"
"os"
"os/signal"
"path/filepath"
"syscall"
"github.com/openebs/scope-plugin/metrics"
log "github.com/sirupsen/logrus"
)
// setupSocket will create a unix socket at the specified socket path
func setupSocket(socketPath string) (net.Listener, error) {
os.RemoveAll(filepath.Dir(socketPath))
if err := os.MkdirAll(filepath.Dir(socketPath), 0700); err != nil {
return nil, fmt.Errorf("failed to create directory %q: %v", filepath.Dir(socketPath), err)
}
listener, err := net.Listen("unix", socketPath)
if err != nil {
return nil, fmt.Errorf("failed to listen on %q: %v", socketPath, err)
}
log.Printf("Listening on: unix://%s", socketPath)
return listener, nil
}
func setupSignals(socketPath string) {
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM)
go func() {
<-interrupt
os.RemoveAll(filepath.Dir(socketPath))
os.Exit(0)
}()
}
func main() {
// Put socket in sub-directory to have more control on permissions
const socketPath = "/var/run/scope/plugins/openebs/openebs.sock"
// Handle the exit signal
setupSignals(socketPath)
listener, err := setupSocket(socketPath)
if err != nil {
log.Error(err)
}
defer func() {
listener.Close()
os.RemoveAll(filepath.Dir(socketPath))
}()
pvMetrics := metrics.NewMetrics()
pvMetrics.GetPVList()
if count := pvMetrics.GetContainerCountInDeployment(); count < 2 {
metrics.URL = "http://cortex-agent-service.maya-system.svc.cluster.local:80/api/v1/query?query="
}
log.Infof("Data Source URL %+v", metrics.URL)
go pvMetrics.UpdateMetrics()
http.HandleFunc("/report", pvMetrics.Report)
if err := http.Serve(listener, nil); err != nil {
log.Errorf("error: %v", err)
}
}