forked from giantswarm/kemp-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats.go
127 lines (105 loc) · 3.18 KB
/
stats.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
package kempclient
import (
"encoding/xml"
"fmt"
"sort"
"github.com/juju/errgo"
)
// StatisticsResponse represents the API response from the `stats` endpoint.
type StatisticsResponse struct {
Debug string `xml:",innerxml"`
XMLName xml.Name `xml:"Response"`
Data Statistics `xml:"Success>Data"`
}
// Statistics represents the statistics data returned by the API.
type Statistics struct {
Totals Totals `xml:"VStotals"`
VirtualServices VirtualServiceStatsList `xml:"Vs"`
RealServers RealServerStatsList `xml:"Rs"`
}
// VirtualServiceStatsList is a list of VirtualServiceStats.
type VirtualServiceStatsList []VirtualServiceStats
func (l VirtualServiceStatsList) Len() int {
return len(l)
}
func (l VirtualServiceStatsList) Less(i int, j int) bool {
if l[i].Address == l[j].Address {
return l[i].Port < l[j].Port
}
return l[i].Address < l[j].Address
}
func (l VirtualServiceStatsList) Swap(i int, j int) {
l[i], l[j] = l[j], l[i]
}
// RealServerStatsList is a list of RealServerStats.
type RealServerStatsList []RealServerStats
func (l RealServerStatsList) Len() int {
return len(l)
}
func (l RealServerStatsList) Less(i int, j int) bool {
if l[i].Address == l[j].Address {
return l[i].Port < l[j].Port
}
return l[i].Address < l[j].Address
}
func (l RealServerStatsList) Swap(i int, j int) {
l[i], l[j] = l[j], l[i]
}
// Totals represents global statistics data.
type Totals struct {
ConnectionsPerSec int `xml:"ConnsPerSec"`
BitsPerSec int
BytesPerSec int
PacketsPerSec int `xml:"PktsPerSec"`
}
// VirtualServiceStats represents statistics for a Virtual Service.
type VirtualServiceStats struct {
Index int
Address string `xml:"VSAddress"`
Port int `xml:"VSPort"`
Protocol string `xml:"VSProt"`
TotalConnections int `xml:"TotalConns"`
TotalPackets int `xml:"TotalPkts"`
TotalBytes int
TotalBits int
ActiveConnections int `xml:"ActiveConns"`
ConnectionsPerSec int `xml:"ConnsPerSec"`
BytesRead int
BytesWritten int
Enabled int `xml:"Enable"`
WafEnable int
ErrorCode int
}
// RealServerStats represents statistics for a Real Server.
type RealServerStats struct {
VSIndex int
RSIndex int
Address string `xml:"Addr"`
Port int
TotalConnections int `xml:"Conns"`
TotalPackets int `xml:"Pkts"`
TotalBytes int `xml:"Bytes"`
TotalBits int `xml:"Bits"`
ActiveConnections int `xml:"ActivConns"`
ConnectionsPerSec int `xml:"ConnsPerSec"`
BytesRead int
BytesWritten int
Enabled int `xml:"Enable"`
Weight int
Persist int
}
// GetStatistics calls the API, and returns a Statistics object.
func (c *Client) GetStatistics() (Statistics, error) {
parameters := make(map[string]string)
data := StatisticsResponse{}
err := c.Request("stats", parameters, &data)
if err != nil {
return Statistics{}, errgo.NoteMask(err, "kemp could not return stats", errgo.Any)
}
if c.debug {
fmt.Println("DEBUG:", data.Debug)
}
sort.Sort(data.Data.VirtualServices)
sort.Sort(data.Data.RealServers)
return data.Data, nil
}