This repository has been archived by the owner on Sep 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql.go
344 lines (266 loc) · 9.1 KB
/
sql.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package main
import (
// "fmt"
"strings"
"strconv"
"database/sql"
"log"
_ "github.com/go-sql-driver/mysql"
)
func filters_to_sql(in[] string) (string) {
s := strings.Join(filter_handle(in), " AND ")
return s
}
func stats_to_sql(in[] string) ([]string) {
out := filter_handle(in)
return out
}
func generate_stats_sql_cases(stats_filters[] string)(string) {
cases := ""
i := 0
for _, s := range stats_filters {
if i > 0 {
cases = cases + ","
}
cases = cases + "COUNT(CASE WHEN (" + s + ") THEN 1 ELSE NULL END ) as st" + strconv.Itoa(i)
i++
}
return cases
}
func add_parent_table_if_need(columns[] string)(string) {
for _, s := range columns {
if s == "parents" {
sql_str :=
`
LEFT JOIN
(
SELECT
hosts_hosts_parents.child_id,
GROUP_CONCAT(hosts.name SEPARATOR ',') AS parents
FROM
hosts_hosts_parents
JOIN
hosts
ON (
hosts_hosts_parents.parent_id = hosts.host_id
)
GROUP BY
hosts_hosts_parents.child_id
ORDER BY
NULL
) AS p
ON (
H.host_id = p.child_id
)
`
return sql_str
}
}
return ""
}
func add_parent_services_table_if_need(columns[] string)(string) {
for _, s := range columns {
if s == "host_parents" {
sql_str :=
`
LEFT JOIN
(
SELECT
hosts_hosts_parents.child_id,
GROUP_CONCAT(hosts.name SEPARATOR ',') AS host_parents
FROM
hosts_hosts_parents
JOIN
hosts
ON (
hosts_hosts_parents.parent_id = hosts.host_id
)
GROUP BY
hosts_hosts_parents.child_id
ORDER BY
NULL
) AS p
ON (
S.host_id = p.child_id
)
`
return sql_str
}
}
return ""
}
func add_services_num_table_if_need(columns[] string)(string) {
for _, s := range columns {
if (s == "num_services_crit" || s == "num_services_ok" || s == "num_services_pending" ||
s == "num_services_unknown" || s == "num_services_warn" || s == "num_services") {
sql_str :=
`
LEFT JOIN
(
SELECT
host_id as host_id_t,
COUNT(CASE WHEN state=0 THEN 1 ELSE NULL END) as num_services_crit,
COUNT(CASE WHEN state=1 THEN 1 ELSE NULL END) as num_services_ok,
COUNT(CASE WHEN state=2 THEN 1 ELSE NULL END) as num_services_pending,
COUNT(CASE WHEN state=3 THEN 1 ELSE NULL END) as num_services_unknown,
COUNT(CASE WHEN state=4 THEN 1 ELSE NULL END) as num_services_warn,
COUNT(state) as num_services
FROM services where enabled = 1 group by host_id
) as S on H.host_id = S.host_id_t
`
return sql_str
}
}
return "";
}
func add_host_table_if_need()(string) {
sql_str :=
`
JOIN (
SELECT host_id,
state as host_state, acknowledged as host_acknowledged, scheduled_downtime_depth as host_scheduled_downtime_depth,
display_name as host_display_name, notify as host_notifications_enabled, notes as host_notes, notes_url as host_notes_url_expanded,
name as host_name, flapping as host_is_flapping, icon_image_alt as host_icon_image_alt, address as host_address, alias as host_alias,
check_type as host_check_type, latency as host_latency, output as host_plugin_output, perfdata as host_perf_data, check_attempt as host_current_attempt,
check_command as host_check_command, checked as host_has_been_checked, icon_image as host_icon_image_expanded, last_state_change as host_last_state_change,
action_url as host_action_url_expanded, active_checks as host_active_checks_enabled, passive_checks as host_accept_passive_checks
FROM hosts) as H on (S.host_id = H.host_id)
`
return sql_str
}
func generate_hosts_sql(columns[] string, sql_filters string, stats_filters[] string, limit string) (string) {
cases := generate_stats_sql_cases(stats_filters)
sql_str := "SELECT " + strings.Join(columns, ", ") + " " + cases +
" FROM hosts as H"
sql_str = sql_str + add_services_num_table_if_need(columns) + add_parent_table_if_need(columns)
sql_str = sql_str + " where H.enabled = 1"
if sql_filters != "" {
sql_str = sql_str + " and"
sql_str = sql_str + " " + sql_filters
}
if limit != "" {
sql_str = sql_str + " limit " + limit
}
// fmt.Println(sql_str)
return sql_str
}
func generate_services_sql(columns[] string, sql_filters string, stats_filters[] string, limit string) (string) {
cases := generate_stats_sql_cases(stats_filters)
sql_str := "SELECT " + strings.Join(columns, ", ") + " " + cases +
" FROM services as S"
sql_str = sql_str + add_parent_services_table_if_need(columns) + add_host_table_if_need()
sql_str = sql_str + " where S.enabled = 1"
if sql_filters != "" {
sql_str = sql_str + " and"
sql_str = sql_str + " " + sql_filters
}
if limit != "" {
sql_str = sql_str + " limit " + limit
}
// fmt.Println(sql_str)
return sql_str
}
func exec_sql(sql_str string, table string)([]interface{}) {
config := get_config()
db, err := sql.Open("mysql", config.Database.Username + ":" + config.Database.Password +
"@tcp(" + config.Database.Host + ":" + config.Database.Port + ")/" +
config.Database.Name)
defer db.Close()
if err != nil {
log.Fatal(err)
}
res, err := db.Query(sql_str)
defer res.Close()
if err != nil {
log.Fatal(err)
}
return getSQLResult(res, table)
}
func handle_field(name string, value string, table string)(interface{}) {
var empty[] string = make([]string, 0)
t := STR_TYPE
c := get_column(name, table)
if c.alias != "" {
t = c.t
}
switch t {
case GRP_TYPE:
if value == "" {
return empty
}
r := strings.Split(value, ",")
return r
default:
return value
}
}
func getSQLResult(rows *sql.Rows, table string) ([]interface{}) {
columnTypes, err := rows.ColumnTypes()
if err != nil {
log.Fatal(err)
}
count := len(columnTypes)
finalRows := []interface{}{}
for rows.Next() {
scanArgs := make([]interface{}, count)
for i, v := range columnTypes {
//fmt.Println(v.DatabaseTypeName())
switch v.DatabaseTypeName() {
case "VARCHAR", "TEXT", "UUID", "TIMESTAMP":
scanArgs[i] = new(sql.NullString)
break;
case "BOOL":
scanArgs[i] = new(sql.NullBool)
break;
case "TINYINT", "BIGINT", "SMALLINT", "INT4", "INT":
scanArgs[i] = new(sql.NullInt64)
break;
case "DOUBLE":
scanArgs[i] = new(sql.NullFloat64)
break;
default:
scanArgs[i] = new(sql.NullString)
}
}
err := rows.Scan(scanArgs...)
if err != nil {
log.Fatal(err)
}
//masterData := map[string]interface{}{}
var masterData[] interface{}
for i, v := range columnTypes {
//fmt.Println(v.DatabaseTypeName())
// manipulate special field
if z, ok := (scanArgs[i]).(*sql.NullBool); ok {
//masterData[v.Name()] = z.Bool
masterData = append(masterData, z.Bool)
continue;
}
if z, ok := (scanArgs[i]).(*sql.NullString); ok {
//masterData[v.Name()] = z.String
masterData = append(masterData, handle_field(v.Name(), z.String, table))
continue;
}
if z, ok := (scanArgs[i]).(*sql.NullInt64); ok {
//masterData[v.Name()] = z.Int64
masterData = append(masterData, z.Int64)
continue;
}
if z, ok := (scanArgs[i]).(*sql.NullFloat64); ok {
//masterData[v.Name()] = z.Float64
masterData = append(masterData, z.Float64)
continue;
}
if z, ok := (scanArgs[i]).(*sql.NullInt32); ok {
//masterData[v.Name()] = z.Int32
masterData = append(masterData, z.Int32)
continue;
}
//masterData[v.Name()] = scanArgs[i]
masterData = append(masterData, scanArgs[i])
}
finalRows = append(finalRows, masterData)
}
//z, err := json.Marshal(finalRows)
return finalRows
}