-
Notifications
You must be signed in to change notification settings - Fork 0
/
activity.go
145 lines (128 loc) · 3.54 KB
/
activity.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// main.go
// vim:noet:ts=4
package main
import (
"fmt"
"log"
"os"
"regexp"
"strings"
"time"
"github.com/nxadm/tail"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/dbx"
"github.com/pocketbase/pocketbase/daos"
"github.com/pocketbase/pocketbase/models"
)
func getLastTime(dao *daos.Dao) (int64, error) {
collection, err := dao.FindCollectionByNameOrId("activity")
if err != nil {
return 0, err
}
query := dao.RecordQuery(collection).
AndWhere(dbx.HashExp{"system": "299"}).
OrderBy("ts DESC").
Limit(1)
rows := []dbx.NullStringMap{}
if err := query.All(&rows); err != nil {
return 0, err
}
return int64(models.NewRecordsFromNullStringMaps(collection, rows)[0].GetFloat("ts")), nil
}
func doTail(a *pocketbase.PocketBase) {
onair := make(map[string]string) // map of module to last record id
reOpening := regexp.MustCompile(`Opening stream on module (?P<module>[A-Z]) for client (?P<client>[^\s]+)\s+(?P<clientmod>.) with sid \d{1,} by user (?P<user>.*)`)
reClosing := regexp.MustCompile(`Closing stream of module ([A-Z])`)
time.Sleep(1 * time.Second)
t, err := tail.TailFile(
"/var/log/syslog", tail.Config{Follow: true, ReOpen: true})
if err != nil {
panic(err)
}
collection, err := a.Dao().FindCollectionByNameOrId("activity")
if err != nil {
log.Fatal(err)
}
lastTime, err := getLastTime(a.Dao())
log.Printf("lastTime %d\n", lastTime)
time.Sleep(4 * time.Second)
// Print the text of each received line
tzLocation, err := time.LoadLocation("Pacific/Auckland")
if err != nil {
log.Fatal(err)
}
for line := range t.Lines {
parts := strings.Split(line.Text, " ")
if len(parts) < 3 || parts[2] != "xlxd:" {
continue
}
if strings.Contains(line.Text, "Sending connect packet to XLX peer") {
continue
}
ts, err := time.ParseInLocation(time.RFC3339Nano, parts[0], tzLocation)
if err != nil {
// tail sometimes leaves a truncated date
// log.Fatal(err)
ts = time.Now() // or maybe last parsed time plus inc
log.Printf("Error: Unable to parse time %s\n", parts[0])
}
uTs := ts.UnixMilli()
if uTs <= lastTime {
continue
}
log.Println(line.Text)
groups := reOpening.FindStringSubmatch(line.Text)
if len(groups) == 5 {
record := models.NewRecord(collection)
via := groups[2]
if groups[3] != " " {
via = via + "-" + groups[3]
}
record.Set("ts", uTs)
record.Set("tsoff", 0)
record.Set("system", "299")
record.Set("module", groups[1])
record.Set("call", strings.Split(groups[4], " ")[0])
record.Set("via", via)
if err := a.Dao().SaveRecord(record); err != nil {
log.Fatal(err)
}
// save the Id of the onair record
onair[groups[1]] = record.Id;
log.Printf("+++ on +++ %s on %s %d %s", strings.Split(groups[4], " ")[0], groups[1], uTs, record.Id);
}
groups = reClosing.FindStringSubmatch(line.Text)
if len(groups) == 2 {
module := parts[7]
id, ok := onair[module]
log.Printf("--- off -- mod %s %s %d", module, id, uTs);
if ok {
record, err := a.Dao().FindRecordById("activity", id)
if err != nil {
log.Fatal(err)
}
record.Set("tsoff", uTs)
if err := a.Dao().SaveRecord(record); err != nil {
log.Fatal(err)
}
} else {
log.Printf("!!! off with no on !!! mod %s", module);
}
}
}
fmt.Println("about to cleanup tailing")
t.Cleanup()
fmt.Println("clean")
}
func main() {
fmt.Println("Activity monitor")
fmt.Println(os.Args)
app := pocketbase.New()
if err := app.Bootstrap(); err != nil {
log.Fatal(err)
}
go doTail(app)
if err := app.Start(); err != nil {
log.Fatal(err)
}
}