-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession.go
214 lines (171 loc) · 4.19 KB
/
session.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package main
import (
"errors"
"fmt"
"os"
"strconv"
"time"
"github.com/satyrius/gonx"
)
// Session represents a monitoring session and handles all accumulated data.
type Session struct {
AlertThreshold int
Entries []*Entry
File *os.File
Parser *gonx.Parser
PollInt int
Report *Report
State uint8
}
// Report accumulates data for reports.
type Report struct {
StatusCodes map[uint8]int // 4 status code groups: 2xx, 3xx, 4xx, 5xx
Time *time.Time
TopSectionHits map[int]Pair
TotalHits int
}
// NewSession returns a new Session object.
func NewSession(threshold, pollInt int, p *gonx.Parser) *Session {
return &Session{
AlertThreshold: threshold,
Parser: p,
PollInt: pollInt,
Report: NewReport(nil),
State: stateOK,
}
}
// SetLog adds file to the session.
func (s *Session) SetLog(f string) error {
var err error
if f == "" {
return errors.New("No file provided")
}
s.File, err = os.Open(f)
if err != nil {
return err
}
return nil
}
// Close closes log file opened for this session.
func (s *Session) Close() {
if s.File != nil {
s.File.Close()
}
}
// NewReport represents traffic summary for a user-defined report period.
func NewReport(t *time.Time) *Report {
return &Report{
StatusCodes: map[uint8]int{
5: 0,
4: 0,
3: 0,
2: 0,
},
Time: t,
TopSectionHits: make(map[int]Pair),
}
}
// ConsumeLines receives log entries accumulated since last poll,
// converts them into Entry objects and adds to the session buffer.
func (s *Session) ConsumeLines(l []string) error {
for _, line := range l {
err := s.AddLine(line)
if err != nil {
return fmt.Errorf(" Error adding log entry %s \n Err: %s ", line, err.Error())
}
}
return nil
}
// AddLine adds a log entry as an Entry object to the session buffer.
func (s *Session) AddLine(line string) error {
r := NewEntry(s.Parser)
err := r.ParseLine(line)
if err != nil {
return err
}
s.Entries = append(s.Entries, r)
s.Report.TotalHits++
return nil
}
// FlushReport returns interval report and resets accumulated stats.
func (s *Session) FlushReport(cfg *Config, t *time.Time) *Report {
s.GetSectionHits(cfg.TopN) // sets i.Summary.TopSectionHits
s.GetStatusCodes() // sets i.Summary.StatusCodes
out := NewReport(t)
out.TotalHits = s.Report.TotalHits
for k, v := range s.Report.StatusCodes {
out.StatusCodes[k] = v
}
for k, v := range s.Report.TopSectionHits {
out.TopSectionHits[k] = v
}
s.reset()
return out
}
// reset nullifies traffic data accumulated since last report.
func (s *Session) reset() {
s.Report = NewReport(nil)
s.Entries = []*Entry{}
}
// GetStatusCodes calculates status code summary.
func (s *Session) GetStatusCodes() {
for _, e := range s.Entries {
i, err := strconv.Atoi(e.StatusCode[0:1])
if err != nil {
// todo: add error-logging
continue
}
codeGroup := uint8(i)
_, ok := s.Report.StatusCodes[codeGroup]
if !ok {
s.Report.StatusCodes[codeGroup] = 1
} else {
s.Report.StatusCodes[codeGroup]++
}
}
}
// GetSectionHits calculates top n section hits during the interval poll time.
func (s *Session) GetSectionHits(n uint) {
sectionHits := make(map[string]int, len(s.Entries))
// Get hits by section.
for _, e := range s.Entries {
_, ok := sectionHits[e.Section]
if !ok {
sectionHits[e.Section] = 1
} else {
sectionHits[e.Section]++
}
}
// Sort.
sorted := RankByHits(sectionHits)
// Limit to top n.
s.Report.TopSectionHits = CutTopN(sorted, n)
}
// ShouldEscalate returns escalation action code.
func (s *Session) ShouldEscalate(traffic int) bool {
if !s.IsAlert() && traffic >= s.AlertThreshold {
s.SetAlert()
return true
}
return false
}
// ShouldDeescalate returns escalation action code.
func (s *Session) ShouldDeescalate(traffic int) bool {
if s.IsAlert() && traffic < s.AlertThreshold {
s.SetOK()
return true
}
return false
}
// IsAlert checks for alert state.
func (s *Session) IsAlert() bool {
return s.State == stateAlert
}
// SetAlert sets current sta te to Alert.
func (s *Session) SetAlert() {
s.State = stateAlert
}
// SetOK sets current sta te to OK.
func (s *Session) SetOK() {
s.State = stateOK
}