-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpwstats.go
80 lines (73 loc) · 1.58 KB
/
pwstats.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
package main
import (
"encoding/json"
"strings"
"time"
)
type PWStats struct {
lastsent int64
lastrecv int64
lastrtt time.Duration
lastrtt_as_string string
last_loss_nano int64
last_loss_duration int64
last_seen_nano int64
state bool
first_called bool
has_ever_received bool
startup_time int64
transition_writer *TransitionWriter
error_message string
hrepr string
iprepr string
}
func (p *PWStats) ComputeState(timeout_threshold int64) {
if p.startup_time == 0 {
p.startup_time = time.Now().UnixNano()
}
old_last_seen := p.last_seen_nano
p.last_seen_nano = time.Now().UnixNano() - p.lastrecv
new_state := p.last_seen_nano < timeout_threshold
// TODO: Algo to review completely
if !p.state && new_state {
if p.first_called {
p.last_loss_nano = time.Now().UnixNano()
p.last_loss_duration = old_last_seen
} else {
p.first_called = true
}
}
if p.state != new_state {
var sb strings.Builder
now := time.Now()
var transition string
if new_state {
transition = "down to up"
} else {
transition = "up to down"
}
jsonString, _ := json.Marshal(
struct {
Timestamp string
UnixNano int64
Host string
Ip string
Transition string
State bool
}{
now.String(),
now.UnixNano(),
p.hrepr,
p.iprepr,
transition,
new_state,
},
)
sb.Write(jsonString)
sb.WriteString("\n")
if p.transition_writer != nil {
p.transition_writer.WriteString(sb.String())
}
}
p.state = new_state
}