forked from open-falcon/falcon-plus
/
host.go
47 lines (39 loc) · 927 Bytes
/
host.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
package service
import (
"fmt"
"log"
"time"
)
// FIX ME: too many JOIN
func GetHostsFromGroup(grpName string) map[string]int {
hosts := make(map[string]int)
now := time.Now().Unix()
q := fmt.Sprintf("SELECT host.id, host.hostname FROM grp_host AS gh "+
" INNER JOIN host ON host.id=gh.host_id AND (host.maintain_begin > %d OR host.maintain_end < %d)"+
" INNER JOIN grp ON grp.id=gh.grp_id AND grp.grp_name='%s'", now, now, grpName)
dbConn, err := GetDbConn("nodata.host")
if err != nil {
log.Println("db.get_conn error, host", err)
return hosts
}
rows, err := dbConn.Query(q)
if err != nil {
log.Println("[ERROR]", err)
return hosts
}
defer rows.Close()
for rows.Next() {
hid := -1
hostname := ""
err = rows.Scan(&hid, &hostname)
if err != nil {
log.Println("[ERROR]", err)
continue
}
if hid < 0 || hostname == "" {
continue
}
hosts[hostname] = hid
}
return hosts
}