-
Notifications
You must be signed in to change notification settings - Fork 1
/
alicloud_redis_exporter.go
165 lines (144 loc) · 4.16 KB
/
alicloud_redis_exporter.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
package main
import (
"fmt"
"os"
"os/exec"
"strings"
"encoding/json"
"time"
"github.com/aliyun/alibaba-cloud-sdk-go/services/cms"
"github.com/patrickmn/go-cache"
"github.com/prometheus/common/log"
)
var (
intervals = 100 * time.Millisecond
metricList = []string{
"MemoryUsage",
"ConnectionUsage",
"IntranetInRatio",
"IntranetOutRatio",
"IntranetIn",
"IntranetOut",
"FailedCount",
"CpuUsage",
"UsedMemory",
}
)
type Auth struct {
RoleName string
Region string
AccessKeyID string
AccessKeySecret string
}
type RedisInstance struct {
Id string `json:"id"`
MemoryUsage float64 `json:"memoryUsage"`
ConnectionUsage float64 `json:"connectionUsage"`
IntranetInRatio float64 `json:"intranetInRatio"`
IntranetOutRatio float64 `json:"intranetOutRatio"`
IntranetIn float64 `json:"intranetIn"`
IntranetOut float64 `json:"intranetOut"`
FailedCount float64 `json:"failedCount"`
CpuUsage float64 `json:"cpuUsage"`
UsedMemory float64 `json:"usedMemory"`
}
type aliResponse struct {
Average float64
}
func GetValue(InstanceId string, metric string) float64 {
defaultRegion, accessKeyID, accessKeySecret := getAuth()
client, err := cms.NewClientWithAccessKey(defaultRegion, accessKeyID, accessKeySecret)
HandleErr(err)
request := cms.CreateQueryMetricLastRequest()
request.Project = "acs_kvstore"
request.Dimensions = fmt.Sprintf("{\"instanceId\":\"%s\"}", InstanceId)
request.Metric = metric
response, err := client.QueryMetricLast(request)
HandleErr(err)
var re aliResponse
HandleErr(json.Unmarshal([]byte(strings.Trim(response.Datapoints, "[]")), &re))
time.Sleep(intervals)
return re.Average
}
func checkInstanceList(id string) {
if _, found := c.Get(id); found {
c.Add(id, nil, time.Hour)
} else {
c.Set(id, nil, time.Hour)
}
}
func StoreData() {
var redisList []RedisInstance
instanceList := c.Items()
for id := range instanceList {
if id == "redislist" {
continue
}
memoryUsageValue := GetValue(id, "Memoryusage")
connectionValue := GetValue(id, "ConnectionUsage")
inratioValue := GetValue(id, "IntranetInRatio")
outratioValue := GetValue(id, "IntranetOutRatio")
intranetInValue := GetValue(id, "IntranetIn")
intranetOutValue := GetValue(id, "IntranetOut")
failedCountValue := GetValue(id, "FailedCount")
cpuUsageValue := GetValue(id, "CpuUsage")
usedMemoryValue := GetValue(id, "UsedMemory")
redis := RedisInstance{
Id: id,
MemoryUsage: memoryUsageValue,
ConnectionUsage: connectionValue,
IntranetInRatio: inratioValue,
IntranetOutRatio: outratioValue,
IntranetIn: intranetInValue,
IntranetOut: intranetOutValue,
FailedCount: failedCountValue,
CpuUsage: cpuUsageValue,
UsedMemory: usedMemoryValue,
}
redisList = append(redisList, redis)
}
c.Set("redislist", &redisList, cache.DefaultExpiration)
}
func readCache(id string) (result RedisInstance) {
var redisList []RedisInstance
if x, found := c.Get("redislist"); found {
redisList = *x.(*[]RedisInstance)
}
for _, redis := range redisList {
if redis.Id == id {
result = redis
}
}
return result
}
func getAuth() (defaultRegion string, accessKeyID string, accessKeySecret string) {
var a Auth
//curl path
path, _ := exec.LookPath("curl")
//get rolename
cmdGetRoleName := exec.Command(path,
"http://100.100.100.200/latest/meta-data/ram/security-credentials/")
roleNameRaw, err := cmdGetRoleName.Output()
HandleErr(err)
a.RoleName = string(roleNameRaw)
cmdGetRoleName.Run()
//according to the rolename, get a json file.
cmdGetJSON := exec.Command(path,
"http://100.100.100.200/latest/meta-data/ram/security-credentials/"+a.RoleName)
jsonRaw, _ := cmdGetJSON.Output()
cmdGetJSON.Run()
//convert json file to map
var roleMap map[string]*json.RawMessage
json.Unmarshal(jsonRaw, &roleMap)
//extract related content from map
json.Unmarshal(*roleMap["AccessKeyId"], &a.AccessKeyID)
json.Unmarshal(*roleMap["AccessKeySecret"], &a.AccessKeySecret)
a.Region = "cn-shanghai"
return a.Region, a.AccessKeyID, a.AccessKeySecret
}
func HandleErr(err error) {
if err != nil {
log.Errorf("ERROR:%v\n", err)
os.Exit(-1)
}
}