This repository has been archived by the owner on Jun 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
126 lines (106 loc) · 2.86 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
package main
import (
"bufio"
"flag"
"fmt"
"github.com/cernbox/cboxawareness/lbproxy"
"github.com/cernbox/cboxawareness/revad"
"github.com/cernbox/cboxawareness/samba"
"net"
"os"
"path"
"path/filepath"
"strings"
"time"
)
var pushFlag bool
var prefixFlag string
var carbonServerFlag string
var pickFlag string
func init() {
flag.BoolVar(&pushFlag, "p", false, "push metrics to carbon metrics store")
flag.StringVar(&carbonServerFlag, "carbon-server", "filer-carbon.cern.ch:2003", "carbon server")
flag.StringVar(&prefixFlag, "prefix", "cernbox.awareness", "carbon metrics prefix")
flag.StringVar(&pickFlag, "pick", "", "pick only one file to parse")
flag.StringVar(&pathFlag, "path", "/data/cephfs/logs/", "path where the logs are stored")
flag.Parse()
}
func main() {
today := time.Now().Format("2006/01/02")
// analyze lbproxy logs
uniqUsersMetric := lbproxy.NewUniqUsersMetric()
syncDistrMetric := lbproxy.NewSyncDistrMetric()
countryMetric := lbproxy.NewCountryMetric()
appsMetrics := lbproxy.NewAppsMetric()
pattern := path.Join(pathFlag, today, "box.lbproxy/*/td.var.log.cboxredirectd.cboxredirectd_http.log")
metrics := parse(pattern, uniqUsersMetric, syncDistrMetric, countryMetric, appsMetrics)
// analyze revad logs
userCreated := revad.NewUserCreated()
uniqWeb := revad.NewUniqUsers()
pattern = path.Join(pathFlag, today, "box.*/*/td.var.log.revad.revad_app.log")
metrics = append(metrics, parse(pattern, userCreated, uniqWeb)...)
// analyze samba logs
uniqSamba := samba.NewUniqUsers()
pattern = path.Join(pathFlag, today, "box.samba*/*/td.var.log.samba.smbclients.log")
metrics = append(metrics, parse(pattern, uniqSamba)...)
// send to carbon
push(metrics)
}
func push(metrics []map[string]int) {
// create tcp connection
conn, err := net.Dial("tcp", carbonServerFlag)
if err != nil {
er(err)
}
now := time.Now().Unix()
format := "%s.%s %d %d\n"
for _, m := range metrics {
for k, v := range m {
payload := fmt.Sprintf(format, prefixFlag, k, v, now)
fmt.Print(payload)
if _, err := conn.Write([]byte(payload)); err != nil {
er(err)
}
}
}
}
func parse(pattern string, metrics ...Metric) (counters []map[string]int) {
files, err := filepath.Glob(pattern)
if err != nil {
er(err)
}
for _, f := range files {
if pickFlag != "" && !strings.Contains(f, pickFlag) {
continue
}
fd, err := os.Open(f)
if err != nil {
er(err)
}
defer fd.Close()
scanner := bufio.NewScanner(fd)
for scanner.Scan() {
data := scanner.Text()
for _, m := range metrics {
m.Do([]byte(data))
}
}
if err := scanner.Err(); err != nil {
er(err)
}
}
for _, m := range metrics {
counters = append(counters, m.Metrics())
}
return
}
type Metric interface {
Do(data []byte)
Metrics() map[string]int
}
func er(err error) {
fmt.Fprintf(os.Stderr, "error: %+v", err)
}
func d(v interface{}) {
fmt.Println(v)
}