/
soma_handler_capability.go
289 lines (259 loc) · 6.34 KB
/
soma_handler_capability.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package main
import (
"database/sql"
"errors"
"fmt"
"log"
"github.com/satori/go.uuid"
"github.com/1and1/soma/internal/stmt"
"github.com/1and1/soma/lib/proto"
)
type somaCapabilityRequest struct {
action string
Capability proto.Capability
reply chan somaResult
}
type somaCapabilityResult struct {
ResultError error
Capability proto.Capability
}
func (a *somaCapabilityResult) SomaAppendError(r *somaResult, err error) {
if err != nil {
r.Capabilities = append(r.Capabilities, somaCapabilityResult{ResultError: err})
}
}
func (a *somaCapabilityResult) SomaAppendResult(r *somaResult) {
r.Capabilities = append(r.Capabilities, *a)
}
/* Read Access
*/
type somaCapabilityReadHandler struct {
input chan somaCapabilityRequest
shutdown chan bool
conn *sql.DB
list_stmt *sql.Stmt
show_stmt *sql.Stmt
}
func (r *somaCapabilityReadHandler) run() {
var err error
if r.list_stmt, err = r.conn.Prepare(stmt.ListAllCapabilities); err != nil {
log.Fatal("capability/list: ", err)
}
defer r.list_stmt.Close()
if r.show_stmt, err = r.conn.Prepare(stmt.ShowCapability); err != nil {
log.Fatal("capability/show: ", err)
}
defer r.show_stmt.Close()
runloop:
for {
select {
case <-r.shutdown:
break runloop
case req := <-r.input:
go func() {
r.process(&req)
}()
}
}
}
func (r *somaCapabilityReadHandler) process(q *somaCapabilityRequest) {
var (
id, monitoring, metric, view, monName string
rows *sql.Rows
thresholds int
err error
)
result := somaResult{}
switch q.action {
case "list":
log.Printf("R: capability/list")
rows, err = r.list_stmt.Query()
defer rows.Close()
if result.SetRequestError(err) {
q.reply <- result
return
}
for rows.Next() {
err := rows.Scan(
&id,
&monitoring,
&metric,
&view,
&monName,
)
result.Append(err, &somaCapabilityResult{
Capability: proto.Capability{
Id: id,
MonitoringId: monitoring,
Metric: metric,
View: view,
Name: fmt.Sprintf("%s.%s.%s", monName, view, metric),
},
})
}
case "show":
log.Printf("R: capability/show for %s", q.Capability.Id)
if err = r.show_stmt.QueryRow(q.Capability.Id).Scan(
&id,
&monitoring,
&metric,
&view,
&thresholds,
&monName,
); err != nil {
if err == sql.ErrNoRows {
result.SetNotFound()
} else {
_ = result.SetRequestError(err)
}
q.reply <- result
return
}
result.Append(err, &somaCapabilityResult{
Capability: proto.Capability{
Id: id,
MonitoringId: monitoring,
Metric: metric,
View: view,
Thresholds: uint64(thresholds),
Name: fmt.Sprintf("%s.%s.%s", monName, view, metric),
},
})
default:
result.SetNotImplemented()
}
q.reply <- result
}
/* Write Access
*/
type somaCapabilityWriteHandler struct {
input chan somaCapabilityRequest
shutdown chan bool
conn *sql.DB
add_stmt *sql.Stmt
del_stmt *sql.Stmt
}
func (w *somaCapabilityWriteHandler) run() {
var err error
w.add_stmt, err = w.conn.Prepare(`
INSERT INTO soma.monitoring_capabilities (
capability_id,
capability_monitoring,
capability_metric,
capability_view,
threshold_amount)
SELECT $1::uuid, $2::uuid, $3::varchar, $4::varchar, $5::integer
WHERE NOT EXISTS (
SELECT capability_id
FROM soma.monitoring_capabilities
WHERE capability_id = $1::uuid
OR ( capability_monitoring = $2::uuid
AND capability_metric = $3::varchar
AND capability_view = $4::varchar));`)
if err != nil {
log.Fatal("capability/add: ", err)
}
defer w.add_stmt.Close()
w.del_stmt, err = w.conn.Prepare(`
DELETE FROM soma.monitoring_capabilities
WHERE capability_id = $1::uuid;`)
if err != nil {
log.Fatal("capability/delete: ", err)
}
defer w.del_stmt.Close()
runloop:
for {
select {
case <-w.shutdown:
break runloop
case req := <-w.input:
w.process(&req)
}
}
}
func (w *somaCapabilityWriteHandler) process(q *somaCapabilityRequest) {
var (
inputVal string
res sql.Result
err error
)
result := somaResult{}
switch q.action {
case "add":
log.Printf("R: capability/add for %s.%s.%s",
q.Capability.MonitoringId,
q.Capability.View,
q.Capability.Metric,
)
// input validation: MonitoringId
if w.conn.QueryRow("SELECT monitoring_id FROM soma.monitoring_systems WHERE monitoring_id = $1::uuid;",
q.Capability.MonitoringId).Scan(&inputVal); err == sql.ErrNoRows {
err = fmt.Errorf("Monitoring system with ID %s is not registered", q.Capability.MonitoringId)
goto bailout
} else if err != nil {
goto bailout
}
// input validation: metric
if w.conn.QueryRow("SELECT metric FROM soma.metrics WHERE metric = $1::varchar;",
q.Capability.Metric).Scan(&inputVal); err == sql.ErrNoRows {
err = fmt.Errorf("Metric %s is not registered", q.Capability.Metric)
goto bailout
} else if err != nil {
goto bailout
}
// input validation: view
if w.conn.QueryRow("SELECT view FROM soma.views WHERE view = $1::varchar;",
q.Capability.View).Scan(&inputVal); err == sql.ErrNoRows {
err = fmt.Errorf("View %s is not registered", q.Capability.View)
goto bailout
} else if err != nil {
goto bailout
}
id := uuid.NewV4()
res, err = w.add_stmt.Exec(
id.String(),
q.Capability.MonitoringId,
q.Capability.Metric,
q.Capability.View,
q.Capability.Thresholds,
)
q.Capability.Id = id.String()
case "delete":
log.Printf("R: capability/delete for %s", q.Capability.Id)
res, err = w.del_stmt.Exec(
q.Capability.Id,
)
default:
log.Printf("R: unimplemented capability/%s", q.action)
result.SetNotImplemented()
q.reply <- result
return
}
bailout:
if result.SetRequestError(err) {
q.reply <- result
return
}
rowCnt, _ := res.RowsAffected()
switch {
case rowCnt == 0:
result.Append(errors.New("No rows affected"), &somaCapabilityResult{})
case rowCnt > 1:
result.Append(fmt.Errorf("Too many rows affected: %d", rowCnt),
&somaCapabilityResult{})
default:
result.Append(nil, &somaCapabilityResult{
Capability: q.Capability,
})
}
q.reply <- result
}
/* Ops Access
*/
func (r *somaCapabilityReadHandler) shutdownNow() {
r.shutdown <- true
}
func (w *somaCapabilityWriteHandler) shutdownNow() {
w.shutdown <- true
}
// vim: ts=4 sw=4 sts=4 noet fenc=utf-8 ffs=unix