-
Notifications
You must be signed in to change notification settings - Fork 308
/
monitor.go
73 lines (64 loc) · 1.69 KB
/
monitor.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
package zgrab2
import "sync"
// Monitor is a collection of states per scans and a channel to communicate
// those scans to the monitor
type Monitor struct {
states map[string]*State
statusesChan chan moduleStatus
// Callback is invoked after each scan.
Callback func(string)
}
// State contains the respective number of successes and failures
// for a given scan
type State struct {
Successes uint `json:"successes"`
Failures uint `json:"failures"`
}
type moduleStatus struct {
name string
st status
}
type status uint
const (
statusSuccess status = iota
statusFailure status = iota
)
// GetStatuses returns a mapping from scanner names to the current number
// of successes and failures for that scanner
func (m *Monitor) GetStatuses() map[string]*State {
return m.states
}
// Stop indicates the monitor is done and the internal channel should be closed.
// This function does not block, but will allow a call to Wait() on the
// WaitGroup passed to MakeMonitor to return.
func (m *Monitor) Stop() {
close(m.statusesChan)
}
// MakeMonitor returns a Monitor object that can be used to collect and send
// the status of a running scan
func MakeMonitor(statusChanSize int, wg *sync.WaitGroup) *Monitor {
m := new(Monitor)
m.statusesChan = make(chan moduleStatus, statusChanSize)
m.states = make(map[string]*State, 10)
wg.Add(1)
go func() {
defer wg.Done()
for s := range m.statusesChan {
if m.states[s.name] == nil {
m.states[s.name] = new(State)
}
if m.Callback != nil {
m.Callback(s.name)
}
switch s.st {
case statusSuccess:
m.states[s.name].Successes++
case statusFailure:
m.states[s.name].Failures++
default:
continue
}
}
}()
return m
}