-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
113 lines (97 loc) · 2.28 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
package main
import (
"fmt"
"net/http"
"os"
"os/exec"
"strconv"
"strings"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
eventCountGauge = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "strfry_event_total",
Help: "The total number of handled events",
},
[]string{"kind"},
)
dbSizeGauge = promauto.NewGauge(
prometheus.GaugeOpts{
Name: "strfry_dbsize_total",
Help: "strfry DB size",
},
)
connectionEstablishedGauge = promauto.NewGauge(
prometheus.GaugeOpts{
Name: "strfry_open_connections",
Help: "The total number of open connections",
},
)
)
func scrape() {
go func() {
for {
fetchDbSize()
fetchEventCount()
fetchConnectionEstablishedCount()
time.Sleep(30 * time.Second)
}
}()
}
func fetchDbSize() {
go func() {
dbFilePath := "/app/strfry-db/data.mdb"
fileInfo, statErr := os.Stat(dbFilePath)
if statErr != nil {
fmt.Println(statErr)
return
}
dbSizeGauge.Set(float64(fileInfo.Size()))
}()
}
func fetchEventCount() {
go func() {
kinds := []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "40", "41", "42", "43", "44", "1984", "9735", "10000", "10001", "10002", "30000", "30001", "30008", "30009", "30023"}
for _, kind := range kinds {
searchOpts := fmt.Sprintf(`{"kinds": [%s]}`, kind)
out, err := exec.Command("/app/strfry", "scan", "--count", searchOpts).Output()
if err != nil {
fmt.Println(string(out))
fmt.Println(err)
continue
}
kindCount, _ := strconv.ParseFloat(strings.TrimSpace(string(out)), 64)
eventCountGauge.With(prometheus.Labels{"kind": kind}).Set(float64(kindCount))
}
}()
}
func countRune(s string, r rune) int {
count := 0
for _, c := range s {
if c == r {
count++
}
}
return count
}
func fetchConnectionEstablishedCount() {
go func() {
out, err := exec.Command("/usr/bin/lsof", "-ni:7777", "-sTCP:ESTABLISHED").Output()
if err != nil {
fmt.Println(string(out))
fmt.Println(err)
return
}
connectionCount := countRune(string(out), '\n')
connectionEstablishedGauge.Set(float64(connectionCount))
}()
}
func main() {
scrape()
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(":8080", nil)
}