/
plus.go
139 lines (123 loc) · 3.15 KB
/
plus.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
package collector
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
)
// PlusCollector is a Collector that collects Nginx Plus status page.
type PlusCollector struct {
http *http.Client
}
// NewPlusCollector constructs a new PlusCollector.
func NewPlusCollector() Collector {
return &PlusCollector{http: HTTPClient()}
}
// Collect Nginx Plus status from given url.
func (c *PlusCollector) Collect(u url.URL) (map[string]interface{}, error) {
res, err := c.http.Get(u.String())
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != 200 {
return nil, fmt.Errorf("HTTP%s", res.Status)
}
// Nginx Plus status is expected to be a JSON document.
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
var payload map[string]interface{}
if err := json.Unmarshal([]byte(body), &payload); err != nil {
return nil, err
}
// Convert float to int values.
status := Ftoi(payload)
version := status["version"].(int)
// Convert 'server_zones' into nested data type instead of
// object data type with arbitrary keys.
if version >= 2 {
mapping := status["server_zones"].(map[string]interface{})
zones := make([]interface{}, len(mapping))
i := 0
for k, v := range mapping {
vt := v.(map[string]interface{})
vt["name"] = k
zones[i] = vt
i++
}
status["server_zones"] = zones
} else {
status["server_zones"] = []interface{}{}
}
// Convert 'upstreams' into nested data type instead of
// object data type with arbitrary keys.
if version >= 4 {
mapping := status["upstreams"].(map[string]interface{})
upstreams := make([]interface{}, len(mapping))
i := 0
for k, v := range mapping {
vt := v.(map[string]interface{})
vt["name"] = k
upstreams[i] = vt
i++
}
status["upstreams"] = upstreams
} else {
status["upstreams"] = []interface{}{}
}
// Convert 'caches' into nested data type instead of
// object data type with arbitrary keys.
if version >= 2 {
mapping := status["caches"].(map[string]interface{})
caches := make([]interface{}, len(mapping))
i := 0
for k, v := range mapping {
vt := v.(map[string]interface{})
vt["name"] = k
caches[i] = vt
i++
}
status["caches"] = caches
} else {
status["caches"] = []interface{}{}
}
// Convert 'stream' into nested data type instead of
// object data type with arbitrary keys.
if version >= 5 {
var (
mapping map[string]interface{}
i int
)
stream := status["stream"].(map[string]interface{})
mapping = stream["server_zones"].(map[string]interface{})
zones := make([]interface{}, len(mapping))
i = 0
for k, v := range mapping {
vt := v.(map[string]interface{})
vt["name"] = k
zones[i] = vt
i++
}
stream["server_zones"] = zones
mapping = stream["upstreams"].(map[string]interface{})
upstreams := make([]interface{}, len(mapping))
i = 0
for k, v := range mapping {
vt := v.(map[string]interface{})
vt["name"] = k
upstreams[i] = vt
i++
}
stream["upstreams"] = upstreams
status["stream"] = stream
} else {
status["stream"] = map[string]interface{}{
"server_zones": []interface{}{},
"upstreams": []interface{}{},
}
}
return status, nil
}