forked from Cepave/open-falcon-backend
/
paltfromApi.go
132 lines (123 loc) · 3.18 KB
/
paltfromApi.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
package boss
import (
"crypto/md5"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"strconv"
"strings"
"time"
"github.com/Cepave/open-falcon-backend/modules/fe/g"
"github.com/astaxie/beego/orm"
"github.com/emirpasic/gods/maps/hashmap"
"github.com/emirpasic/gods/sets/hashset"
log "github.com/sirupsen/logrus"
)
func getFctoken() string {
hasher := md5.New()
io.WriteString(hasher, g.Config().Api.Token)
s := hex.EncodeToString(hasher.Sum(nil))
t := time.Now()
now := t.Format("20060102")
s = now + s
hasher = md5.New()
io.WriteString(hasher, s)
fctoken := hex.EncodeToString(hasher.Sum(nil))
return fctoken
}
func GetPlatformASJSON() (repons PlatformList, err error) {
config := g.Config()
fctoken := getFctoken()
url := config.Api.Map + "/fcname/" + config.Api.Name + "/fctoken/" + fctoken
url += "/show_active/yes/hostname/yes/pop_id/yes/ip/yes.json"
log.Debugf("platform get url: %s", url)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return
}
body, _ := ioutil.ReadAll(resp.Body)
err = json.Unmarshal(body, &repons)
if repons.Status != 1 {
err = errors.New("Platform list quer failed")
}
return
}
//generate platform mapping of triggered alarm cases
func GenPlatMap(repons PlatformList, filterList *hashset.Set) (ipMapper *hashmap.Map, platList *hashset.Set, popIds []int) {
log.Debugf("filterLIST: %v", filterList.Values())
res := repons.Result
ipMapper = hashmap.New()
platList = hashset.New()
popIDSet := hashset.New()
for _, platform := range res {
for _, ipInfo := range platform.IPList {
//only get platform info that matched triggered alarm case
if filterList.Contains(ipInfo.HostName) {
platList.Add(platform.Platform)
ipInfo.Platform = platform.Platform
popID, _ := strconv.Atoi(ipInfo.POPID)
popIDSet.Add(popID)
ip := ipInfo.IP
if len(ip) > 0 && ip == getIPFromHostname(ipInfo.HostName) {
ipMapper.Put(ipInfo.HostName, ipInfo)
}
}
}
}
log.Debugf("popID set: %v", popIDSet.Values())
popIds = []int{}
for _, popID := range popIDSet.Values() {
popIds = append(popIds, popID.(int))
}
return
}
func getIPFromHostname(hostname string) string {
ip := ""
fragments := strings.Split(hostname, "-")
slice := []string{}
if len(fragments) == 6 {
fragments := fragments[2:]
for _, fragment := range fragments {
num, err := strconv.Atoi(fragment)
if err == nil {
slice = append(slice, strconv.Itoa(num))
}
}
if len(slice) == 4 {
ip = strings.Join(slice, ".")
}
}
return ip
}
func IdcMapping(popIDs []int) (idcmapping map[int]string, err error) {
if len(popIDs) == 0 {
err = errors.New("popIdDs is empty")
return
}
q := orm.NewOrm()
q.Using("grafana")
popFilter := ""
for idx, pop := range popIDs {
if idx == 0 {
popFilter = fmt.Sprintf("%d", pop)
}
popFilter = fmt.Sprintf("%s, %d", popFilter, pop)
}
sqlcommd := fmt.Sprintf("select name,pop_id from grafana.idc where pop_id IN (%s)", popFilter)
idcs := []IDC{}
_, err = q.Raw(sqlcommd).QueryRows(&idcs)
idcmapping = map[int]string{}
for _, idc := range idcs {
idcmapping[idc.PopId] = idc.Name
}
return
}