Browse Source

initial commit

master
f0x 1 year ago
commit
d1b50a4b68
1 changed files with 53 additions and 0 deletions
  1. 53
    0
      mumble.go

+ 53
- 0
mumble.go View File

@@ -0,0 +1,53 @@
1
+package main
2
+
3
+import (
4
+	"fmt"
5
+	"github.com/hpcloud/tail"
6
+	"github.com/prometheus/client_golang/prometheus"
7
+	"github.com/prometheus/client_golang/prometheus/promauto"
8
+	"github.com/prometheus/client_golang/prometheus/promhttp"
9
+	"net/http"
10
+	"strings"
11
+)
12
+
13
+var (
14
+	Connections = promauto.NewGauge(prometheus.GaugeOpts{
15
+		Name: "mumble_connections",
16
+		Help: "Number of connected Mumble clients",
17
+	})
18
+	counter = 0
19
+)
20
+
21
+func metrics() {
22
+	fmt.Println("Watching mumble log")
23
+	go func() {
24
+		t, err := tail.TailFile("/var/log/mumble-server/mumble-server.log", tail.Config{Follow: true})
25
+		if err != nil {
26
+			fmt.Println(err)
27
+		}
28
+
29
+		for line := range t.Lines {
30
+			if strings.Contains(line.Text, "onnection") {
31
+				fmt.Println(line)
32
+				if strings.Contains(line.Text, "New connection") {
33
+					fmt.Println("new connection")
34
+					Connections.Inc()
35
+					counter++
36
+				}
37
+
38
+				if counter > 0 && strings.Contains(line.Text, "Connection closed") {
39
+					fmt.Println("lost a connection")
40
+					Connections.Dec()
41
+					counter--
42
+				}
43
+				fmt.Println(counter)
44
+			}
45
+		}
46
+	}()
47
+}
48
+
49
+func main() {
50
+	metrics()
51
+	http.Handle("/metrics", promhttp.Handler())
52
+	http.ListenAndServe(":2112", nil)
53
+}

Loading…
Cancel
Save