forked from gaochao1/swcollector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
var.go
144 lines (122 loc) · 2.64 KB
/
var.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
package g
import (
"log"
"os"
"strings"
"sync"
"github.com/toolkits/slice"
"time"
"github.com/open-falcon/common/model"
"github.com/toolkits/net"
)
var Root string
func InitRootDir() {
var err error
Root, err = os.Getwd()
if err != nil {
log.Fatalln("getwd fail:", err)
}
}
var LocalIps []string
func InitLocalIps() {
var err error
LocalIps, err = net.IntranetIP()
if err != nil {
log.Fatalln("get intranet ip fail:", err)
}
}
var (
TransferClient *SingleConnRpcClient
)
func InitRpcClients() {
if Config().Transfer.Enabled {
TransferClient = &SingleConnRpcClient{
RpcServer: Config().Transfer.Addr,
Timeout: time.Duration(Config().Transfer.Timeout) * time.Millisecond,
}
}
}
func SendToTransfer(metrics []*model.MetricValue) {
if len(metrics) == 0 {
return
}
debug := Config().Debug
debug_endpoints := Config().Debugmetric.Endpoints
debug_metrics := Config().Debugmetric.Metrics
debug_tags := Config().Debugmetric.Tags
debug_Tags := strings.Split(debug_tags, ",")
if Config().SwitchHosts.Enabled {
hosts := HostConfig().Hosts
for i, metric := range metrics {
if hostname, ok := hosts[metric.Endpoint]; ok {
metrics[i].Endpoint = hostname
}
}
}
if debug {
for _, metric := range metrics {
metric_tags := strings.Split(metric.Tags, ",")
if in_array(metric.Endpoint, debug_endpoints) && in_array(metric.Metric, debug_metrics) {
if debug_tags == "" {
log.Printf("=> <Total=%d> %v\n", len(metrics), metric)
continue
}
if array_include(debug_Tags, metric_tags) {
log.Printf("=> <Total=%d> %v\n", len(metrics), metric)
}
}
}
}
var resp model.TransferResponse
err := TransferClient.Call("Transfer.Update", metrics, &resp)
if err != nil {
log.Println("call Transfer.Update fail", err)
if debug {
for _, metric := range metrics {
log.Printf("=> <Total=%d> %v\n", len(metrics), metric)
}
}
}
if debug {
log.Println("<=", &resp)
}
}
func array_include(array_a []string, array_b []string) bool { //b include a
for _, v := range array_a {
if in_array(v, array_b) {
continue
} else {
return false
}
}
return true
}
func in_array(a string, array []string) bool {
for _, v := range array {
if a == v {
return true
}
}
return false
}
var (
ips []string
ipsLock = new(sync.Mutex)
)
func TrustableIps() []string {
ipsLock.Lock()
defer ipsLock.Unlock()
ips := Config().Http.TrustIps
return ips
}
func IsTrustable(remoteAddr string) bool {
ip := remoteAddr
idx := strings.LastIndex(remoteAddr, ":")
if idx > 0 {
ip = remoteAddr[0:idx]
}
if ip == "127.0.0.1" {
return true
}
return slice.ContainsString(TrustableIps(), ip)
}