This repository has been archived by the owner on Nov 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
rsyncd.go
148 lines (120 loc) · 3.03 KB
/
rsyncd.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
146
147
148
package main
import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"strings"
"time"
"github.com/COSI-Lab/datarithms"
"github.com/COSI-Lab/logging"
"github.com/nxadm/tail"
)
type RsyncdLogEntry struct {
time time.Time
sent int64
recv int64
}
func ReadRsyncdLogFile(logFile string, ch chan *RsyncdLogEntry) (err error) {
for {
f, err := os.Open(logFile)
if err != nil {
return err
}
scanner := bufio.NewScanner(f)
for scanner.Scan() {
entry, err := parseRsyncdLine(scanner.Text())
if err == nil {
// Send a pointer to the entry down the channel
ch <- entry
}
time.Sleep(10 * time.Millisecond)
}
f.Close()
}
}
func TailRSyncdLogFile(logFile string, lastUpdated time.Time, ch chan *RsyncdLogEntry) {
// Find the offset of the line where the date is past lastUpdated
start := time.Now()
offset, err := datarithms.BinarySearchFileByDate(logFile, lastUpdated, parseRsyncdDate)
if err != nil {
logging.Error(err)
return
}
logging.Info("Found rsyncd log offset in", time.Since(start))
// Tail the log file `tail -F` starting at the offset
seek := tail.SeekInfo{
Offset: offset,
Whence: io.SeekStart,
}
tail, err := tail.TailFile(logFile, tail.Config{Follow: true, ReOpen: true, MustExist: true, Location: &seek})
if err != nil {
logging.Error("Failed to start tailing `rsyncd.log`:", err)
return
}
logging.Success("Tailing rsyncd log file")
// Parse each line as we receive it
for line := range tail.Lines {
entry, err := parseRsyncdLine(line.Text)
if err == nil {
// Send a pointer to the entry down each channel
ch <- entry
}
}
}
type ParseLineError struct{}
func (e ParseLineError) Error() string {
return "Failed to parse line"
}
func parseRsyncdDate(line string) (time.Time, error) {
// Split the line over whitespace
parts := strings.Split(line, " ")
if len(parts) < 2 {
return time.Time{}, ParseLineError{}
}
// The 1st part is the date
dt := parts[0]
// 2nd part is the time
tm := parts[1]
// make the time.Time object
t, err := time.Parse("2006/01/02 15:04:05", dt+" "+tm)
if err != nil {
return time.Time{}, err
}
return t, nil
}
func parseRsyncdLine(line string) (*RsyncdLogEntry, error) {
// 2022/04/20 20:00:10 [pid] sent XXX bytes received XXX bytes total size XXX
// Split the line over whitespace
parts := strings.Split(line, " ")
// the line we want has 14 parts
if len(parts) != 14 {
return nil, ParseLineError{}
}
// the 4th part is "sent"
if parts[3] != "sent" {
return nil, ParseLineError{}
}
// The 1st part is the date
dt := parts[0]
// 2nd part is the time
tm := parts[1]
// make the time.Time object
t, err := time.Parse("2006/01/02 15:04:05", dt+" "+tm)
if err != nil {
return nil, err
}
// part 5 is the number of bytes sent
sent, err := strconv.ParseInt(parts[4], 10, 64)
if err != nil {
fmt.Println(err)
return nil, ParseLineError{}
}
recv, err := strconv.ParseInt(parts[8], 10, 64)
if err != nil {
fmt.Println(err)
return nil, ParseLineError{}
}
return &RsyncdLogEntry{sent: sent, recv: recv, time: t}, nil
}