-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
73 lines (60 loc) · 1.8 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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os/exec"
"strings"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
const (
namespace = "ctdb"
)
var (
addr = flag.String("web.listen-address", ":9727", "The address to listen on for HTTP requests.")
endpoint = flag.String("web.endpoint", "/metrics", "Path under which to expose metrics.")
ctdbBin = flag.String("ctdb.bin-path", "/usr/bin/ctdb", "Full path to CTDB binary.")
ctdbSudo = flag.Bool("ctdb.sudo", true, "Prefix ctdb commands with sudo.")
pnn = ""
leader = ""
)
type runner func(...string) (string, error)
func runCmd(arg ...string) (string, error) {
cmd := exec.Command(*ctdbBin, arg...)
if *ctdbSudo {
// This monstrosity of a command tries to run /bin/sh -c /usr/bin/sudo /usr/bin/ctdb with proper escaping
cmd = exec.Command("/bin/sh", append([]string{"-c"},
strings.Join(append([]string{"/usr/bin/sudo", *ctdbBin}, arg...), " "))...)
}
result, err := cmd.CombinedOutput()
if err != nil {
return "", fmt.Errorf("command '%v' failed with err : %v (%v)", cmd.String(), err, strings.TrimSpace(string(result)))
}
return strings.TrimSpace(string(result)), nil
}
func isMasterNode(run runner) (bool, error) {
var err error
pnn, err = run("pnn")
if err != nil {
return false, err
}
// Newer CTDB versions have renamed recmaster to leader
leader, err = run("leader")
if err != nil {
// Keep compatibility with older versions
leader, err = run("recmaster")
if err != nil {
return false, err
}
}
return pnn == leader, nil
}
func main() {
flag.Parse()
prometheus.MustRegister(NewStatusCollector())
prometheus.MustRegister(NewStatisticsCollector())
http.Handle(*endpoint, promhttp.Handler())
log.Fatal(http.ListenAndServe(*addr, nil))
}