-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhealthmon.go
225 lines (202 loc) · 5.59 KB
/
healthmon.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
215
216
217
218
219
220
221
222
223
224
225
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"sync"
"syscall"
_ "github.com/owtf/health_monitor/api"
"github.com/owtf/health_monitor/cli"
"github.com/owtf/health_monitor/cpu"
"github.com/owtf/health_monitor/disk"
"github.com/owtf/health_monitor/live"
"github.com/owtf/health_monitor/notify"
"github.com/owtf/health_monitor/owtf"
"github.com/owtf/health_monitor/ram"
"github.com/owtf/health_monitor/setup"
"github.com/owtf/health_monitor/target"
"github.com/owtf/health_monitor/utils"
"github.com/owtf/health_monitor/webui"
)
const (
numberOfModules int = 6
)
// Flags holds the health_monitor command line arguments
type Flags struct {
NoWebUI *bool
NoCLI *bool
Quite *bool
}
func main() {
defer safeClose()
var (
wg sync.WaitGroup
flags Flags
chans [numberOfModules]chan bool // Channels to communicate with the modules.
)
for i := range chans {
chans[i] = make(chan bool, 1)
}
utils.LiveEmergency = make(chan bool)
defer close(utils.LiveEmergency)
utils.RestartModules = make(chan utils.Status, 1)
defer close(utils.RestartModules)
flags.NoWebUI = flag.Bool("nowebui", false, "Disables the web ui")
flags.NoCLI = flag.Bool("nocli", false, "Disables cli")
flags.Quite = flag.Bool("quite", false, "Disables all notifications except email")
flag.Parse()
if (*flags.NoCLI == true) || (*flags.NoWebUI == false) {
go webui.RunServer(setup.ConfigVars.Port)
fmt.Printf("[*] Server is starting at http://127.0.0.1:%s\n", setup.ConfigVars.Port)
}
utils.ExitChan = make(chan os.Signal, 1)
signal.Notify(utils.ExitChan, syscall.SIGINT, syscall.SIGTERM)
// The buffer size should atleast be double the number of modules implemented
utils.ControlChan = make(chan utils.Status, 2*numberOfModules)
wg.Add(1)
go notify.Notify()
go restartModules()
go tearDown(&wg)
Init()
utils.ModuleLogs(setup.MainLogFile, "Running modules from last saved profile")
runModules(chans, &wg)
go controlModule(chans, &wg)
if *flags.NoCLI == false {
go cli.Run()
}
wg.Wait()
print("\b\b")
}
func controlModule(chans [6]chan bool, wg *sync.WaitGroup) {
for {
data := <-utils.ControlChan
switch data.Module {
case "owtf":
controlModuleHelper(data.Run, &setup.OWTFModuleStatus, data.Module,
owtf.OWTF, chans[0], wg)
case "live":
controlModuleHelper(data.Run, &setup.InternalModuleState.Live, data.Module,
live.Live, chans[1], wg)
case "target":
controlModuleHelper(data.Run, &setup.InternalModuleState.Target, data.Module,
target.Target, chans[2], wg)
case "disk":
controlModuleHelper(data.Run, &setup.InternalModuleState.Disk, data.Module,
disk.Disk, chans[3], wg)
case "ram":
controlModuleHelper(data.Run, &setup.InternalModuleState.RAM, data.Module,
ram.RAM, chans[4], wg)
case "cpu":
controlModuleHelper(data.Run, &setup.InternalModuleState.CPU, data.Module,
cpu.CPU, chans[5], wg)
}
}
}
func controlModuleHelper(run bool, moduleStatus *bool, moduleName string,
module func(<-chan bool, *sync.WaitGroup), channel chan bool, wg *sync.WaitGroup) {
if run && !*moduleStatus {
*moduleStatus = true
wg.Add(1)
utils.ModuleLogs(setup.MainLogFile, "Started "+moduleName+"module")
go module(channel, wg)
} else if *moduleStatus {
*moduleStatus = false
utils.ModuleLogs(setup.MainLogFile, "Stopped "+moduleName+"module")
channel <- true
}
}
func runModules(chans [6]chan bool, wg *sync.WaitGroup) {
if setup.UserModuleState.Live {
wg.Add(1)
utils.ModuleLogs(setup.MainLogFile, "Started live module")
setup.InternalModuleState.Live = true
go live.Live(chans[1], wg)
}
if setup.UserModuleState.Target {
wg.Add(1)
utils.ModuleLogs(setup.MainLogFile, "Started target module")
setup.InternalModuleState.Target = true
utils.AddOWTFModuleDependence()
go target.Target(chans[2], wg)
}
if setup.UserModuleState.Disk {
wg.Add(1)
utils.ModuleLogs(setup.MainLogFile, "Started disk module")
setup.InternalModuleState.Disk = true
go disk.Disk(chans[3], wg)
}
if setup.UserModuleState.RAM {
wg.Add(1)
utils.ModuleLogs(setup.MainLogFile, "Started ram module")
setup.InternalModuleState.RAM = true
go ram.RAM(chans[4], wg)
}
if setup.UserModuleState.CPU {
wg.Add(1)
utils.ModuleLogs(setup.MainLogFile, "Started cpu module")
setup.InternalModuleState.CPU = true
go cpu.CPU(chans[5], wg)
}
}
//Init initialises all the modules of the monitor
func Init() {
notify.Init()
live.Init()
target.Init()
disk.Init()
ram.Init()
cpu.Init()
}
func initModule(module string) {
switch module {
case "live":
live.Init()
case "target":
target.Init()
case "disk":
disk.Init()
case "ram":
ram.Init()
case "cpu":
cpu.Init()
case "notify":
notify.Init()
}
}
func tearDown(wg *sync.WaitGroup) {
<-utils.ExitChan
utils.ModuleLogs(setup.MainLogFile, "Shutdown signal received.")
setup.SaveStatus()
utils.ModuleLogs(setup.MainLogFile, "Saved all config data. Stopping running modules")
var module string
for _, module = range utils.Modules {
utils.SendModuleStatus(module, false)
}
utils.SendModuleStatus("owtf", false)
wg.Done()
}
func safeClose() {
setup.Database.Close()
setup.MainLogFile.Close()
setup.DBLogFile.Close()
}
func restartModules() {
data := <-utils.RestartModules
if data.Module != "all" {
utils.SendModuleStatus(data.Module, false)
if data.Run {
initModule(data.Module)
}
utils.SendModuleStatus(data.Module, true)
} else {
// Send all the modules to stop
utils.SendStatusToAllModules(false)
// If true is sent then all the modules are started and their config variables are also initialised.
if data.Run {
Init()
}
// Send all the modules to start
utils.SendStatusToAllModules(true)
}
}