This repository has been archived by the owner on Apr 14, 2022. It is now read-only.
/
monitor.go
110 lines (94 loc) · 2.8 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
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
package controllers
import (
"encoding/json"
"github.com/alastria/monitor/lib"
"github.com/alastria/monitor/models"
"github.com/astaxie/beego"
)
// Monitor operations
type MonitorController struct {
beego.Controller
}
// @Title RestPostStatus
// @Description Test simple POST method over the monitor
// @Param body body models.StatusReq true "String to test simple POST method"
// @Success 200 {map[string]string} Status information
// @Failure 403 overload
// @router /pingpong [post]
func (m *MonitorController) RestPostStatus() {
var r models.StatusReq
json.Unmarshal(m.Ctx.Input.RequestBody, &r)
// m.Data["json"] = &StatusReq
m.Data["json"] = map[string]string{"status": "ok", "testString": (&r).TestString}
m.ServeJSON()
}
// @Title RestGetStatus
// @Description Check status of the monitor REST
// @Success 200 {status} string Status
// @Failure 403 : overload
// @router /status [get]
func (m *MonitorController) RestGetStatus() {
output := make(map[string]string)
_, port1 := lib.RunCommand("lsof -iTCP -sTCP:LISTEN -P -n | grep *:21000 | awk '{print $8 $9 $10}'")
_, port2UDP := lib.RunCommand("lsof -iUDP -P -n | grep *:21000 | awk '{print $8 $9 $10}'")
_, port2TCP := lib.RunCommand("lsof -iTCP -sTCP:LISTEN -P -n | grep *:22000 | awk '{print $8 $9 $10}'")
_, port3 := lib.RunCommand("lsof -iTCP -sTCP:LISTEN -P -n | grep *:9000 | awk '{print $8 $9 $10}'")
if port1 != "" {
output["port21000"] = "up"
} else {
output["port21000"] = "down"
}
if port2UDP != "" && port2TCP != "" {
output["port22000"] = "up"
} else {
output["port22000"] = "down"
}
if port3 != "" {
output["port9000"] = "up"
} else {
output["port9000"] = "down"
}
output["status"] = "ok"
m.Data["json"] = &output
m.ServeJSON()
}
// @Title GetVersion
// @Description Check monitor version
// @Success 200 {status, version} string Status, string Version
// @Failure 403 : overload
// @router /version [get]
func (m *MonitorController) GetVersion() {
output := make(map[string]string)
_, current := lib.CurrentMonitorVersion()
_, latest := lib.LatestMonitorVersion()
if current != latest {
output["status"] = "outdated"
} else {
output["status"] = "ok"
}
output["version"] = current
m.Data["json"] = &output
m.ServeJSON()
}
// @Title GetVersionUpdate
// @Description Check monitor version.
// involved we decide to user GET.
// @Success 202 {status, version} string Status (latest | updated)
// @Failure 403 : overload
// @router /update [get]
func (m *MonitorController) GetVersionUpdate() {
output := make(map[string]string)
_, current := lib.CurrentMonitorVersion()
_, latest := lib.LatestMonitorVersion()
if current != latest {
if lib.UpdateMonitor() {
output["status"] = "updated"
} else {
output["status"] = "error"
}
} else {
output["status"] = "latest"
}
m.Data["json"] = &output
m.ServeJSON()
}