-
Notifications
You must be signed in to change notification settings - Fork 1
/
metrics_server.go
248 lines (230 loc) · 6.59 KB
/
metrics_server.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package main
import (
"fmt"
"net/http"
"strings"
"sync"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
type MetricsServer struct {
addr string
logins prometheus.Counter
loginsPerSecond prometheus.Gauge
loginsFailed prometheus.Counter
loginsSuccessful prometheus.Counter
commandsExecuted prometheus.Counter
commandsExecutedPerSecond prometheus.Gauge
knownHosts prometheus.Gauge
knownPasswords prometheus.Gauge
knownUsers prometheus.Gauge
knownPayloads prometheus.Gauge
activeSessions prometheus.Gauge
timeOnline prometheus.Gauge
timeWasted prometheus.Gauge
timeWastedPerSecond prometheus.Gauge
host *prometheus.GaugeVec
last struct {
logins int
loginsFailed int
loginsSuccessful int
commandsExecuted int
knownHosts int
knownUsers int
knownPasswords int
knownPayloads int
activeSessions int
timeOnline float64
timeWasted float64
}
lock *sync.Mutex
}
func (m *MetricsServer) IncrementLogins() {
m.lock.Lock()
defer m.lock.Unlock()
m.logins.Inc()
m.last.logins++
m.loginsPerSecond.Set(float64(m.last.logins) / m.last.timeOnline)
}
func (m *MetricsServer) IncrementFailedLogins() {
m.lock.Lock()
defer m.lock.Unlock()
m.loginsFailed.Inc()
m.last.loginsFailed++
}
func (m *MetricsServer) IncrementSuccessfulLogins() {
m.lock.Lock()
defer m.lock.Unlock()
m.loginsSuccessful.Inc()
m.last.loginsSuccessful++
}
func (m *MetricsServer) IncrementKnownHosts() {
m.lock.Lock()
defer m.lock.Unlock()
m.knownHosts.Inc()
m.last.knownHosts++
}
func (m *MetricsServer) IncrementKnownPasswords() {
m.lock.Lock()
defer m.lock.Unlock()
m.knownPasswords.Inc()
m.last.knownPasswords++
}
func (m *MetricsServer) IncrementKnownUsers() {
m.lock.Lock()
defer m.lock.Unlock()
m.knownUsers.Inc()
m.last.knownUsers++
}
func (m *MetricsServer) IncrementKnownPayloads() {
m.lock.Lock()
defer m.lock.Unlock()
m.knownPayloads.Inc()
m.last.knownPayloads++
}
func (m *MetricsServer) IncrementExecutedCommands() {
m.lock.Lock()
defer m.lock.Unlock()
m.commandsExecuted.Inc()
m.last.commandsExecuted++
m.commandsExecutedPerSecond.Set(float64(m.last.commandsExecuted) / m.last.timeOnline)
}
func (m *MetricsServer) IncrementSessions() {
m.lock.Lock()
defer m.lock.Unlock()
m.activeSessions.Inc()
m.last.activeSessions++
}
func (m *MetricsServer) DecrementSessions() {
m.lock.Lock()
defer m.lock.Unlock()
m.activeSessions.Dec()
m.last.activeSessions--
}
func (m *MetricsServer) SetTimeOnline(seconds float64) {
m.lock.Lock()
defer m.lock.Unlock()
m.timeOnline.Set(seconds)
m.last.timeOnline = seconds
m.timeWastedPerSecond.Set(m.last.timeWasted / m.last.timeOnline)
}
func (m *MetricsServer) AddTimeWasted(seconds float64) {
m.lock.Lock()
defer m.lock.Unlock()
m.timeWasted.Add(seconds)
m.last.timeWasted += seconds
}
func (m *MetricsServer) SetHostInfo() {
m.lock.Lock()
defer m.lock.Unlock()
ips := []string{}
for _, s := range Conf.Servers {
ips = append(ips, s.Host)
}
m.host.WithLabelValues(Conf.HostName, strings.Join(ips, ", ")).Add(1)
}
func (m *MetricsServer) Start() {
go func() {
http.Handle("/metrics", promhttp.Handler())
_ = http.ListenAndServe(m.addr, nil)
}()
}
func NewMetricsServer() *MetricsServer {
m := &MetricsServer{
addr: fmt.Sprintf("%s:%d", Conf.MetricsServer.Host, Conf.MetricsServer.Port),
logins: promauto.NewCounter(prometheus.CounterOpts{
Name: "ossh_logins",
Help: "The total number of logins",
}),
loginsPerSecond: promauto.NewGauge(prometheus.GaugeOpts{
Name: "ossh_logins_per_second",
Help: "The number of logins to this instance per second of online time",
}),
loginsFailed: promauto.NewCounter(prometheus.CounterOpts{
Name: "ossh_logins_failed",
Help: "The total number of failed logins",
}),
loginsSuccessful: promauto.NewCounter(prometheus.CounterOpts{
Name: "ossh_logins_successful",
Help: "The total number of successful logins",
}),
commandsExecuted: promauto.NewCounter(prometheus.CounterOpts{
Name: "ossh_commands_executed",
Help: "The total number of commands executed in SSH sessions",
}),
commandsExecutedPerSecond: promauto.NewGauge(prometheus.GaugeOpts{
Name: "ossh_commands_executed_per_second",
Help: "The number of commands this instance has executed per second of online time",
}),
knownHosts: promauto.NewGauge(prometheus.GaugeOpts{
Name: "ossh_known_hosts",
Help: "The number of hosts known to this instance",
}),
knownPasswords: promauto.NewGauge(prometheus.GaugeOpts{
Name: "ossh_known_passwords",
Help: "The number of passwords known to this instance",
}),
knownUsers: promauto.NewGauge(prometheus.GaugeOpts{
Name: "ossh_known_users",
Help: "The number of user names known to this instance",
}),
knownPayloads: promauto.NewGauge(prometheus.GaugeOpts{
Name: "ossh_known_payloads",
Help: "The number of payloads known to this instance",
}),
activeSessions: promauto.NewGauge(prometheus.GaugeOpts{
Name: "ossh_active_sessions",
Help: "The number of SSH session currently open on this instance",
}),
timeOnline: promauto.NewGauge(prometheus.GaugeOpts{
Name: "ossh_time_online",
Help: "The number of seconds this instance has been running",
}),
timeWasted: promauto.NewGauge(prometheus.GaugeOpts{
Name: "ossh_time_wasted",
Help: "The number of bot seconds this instance has wasted",
}),
timeWastedPerSecond: promauto.NewGauge(prometheus.GaugeOpts{
Name: "ossh_time_wasted_per_second",
Help: "The number of bot seconds this instance has wasted per second of online time",
}),
host: promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "ossh_host_info",
Help: "Info about the host oSSH runs on",
},
[]string{
"name",
"ip",
},
),
last: struct {
logins int
loginsFailed int
loginsSuccessful int
commandsExecuted int
knownHosts int
knownUsers int
knownPasswords int
knownPayloads int
activeSessions int
timeOnline float64
timeWasted float64
}{
logins: 0,
loginsFailed: 0,
loginsSuccessful: 0,
commandsExecuted: 0,
knownHosts: 0,
knownUsers: 0,
knownPasswords: 0,
knownPayloads: 0,
activeSessions: 0,
timeOnline: 0.0,
timeWasted: 0.0,
},
lock: &sync.Mutex{},
}
m.SetHostInfo()
return m
}