This repository has been archived by the owner on Mar 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
dmr.go
146 lines (124 loc) · 4.14 KB
/
dmr.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
package sources
import (
"bytes"
"encoding/json"
"fmt"
"github.com/golang/glog"
"net/http"
"time"
)
type DmrContainer struct {
Name string `json:"name,omitempty"`
Host string `json:"host"`
DmrPort int `json:"dmrPort"`
Stats *StatsEntry `json:"stats,omitempty"`
}
type DmrAttributeRequest struct {
Operation string `json:"operation"`
Name string `json:"name"`
Pretty int `json:"json.pretty"`
}
type DmrResourceRequest struct {
Operation string `json:"operation"`
IncludeRuntime bool `json:"include-runtime"`
Address []string `json:"address"`
Pretty int `json:"json.pretty"`
}
type DmrResponse struct {
Outcome string `json:"outcome"`
Result interface{} `json:"result"`
FailureDescription string `json:"failure-description"`
RolledBacked bool `json:"rolled-back"`
}
type WebResult struct {
BytesReceived StringInt `json:"bytesReceived"`
BytesSent StringInt `json:"bytesSent"`
EnableLookups bool `json:"enable-lookups"`
Enabled bool `json:"enabled"`
ErrorCount StringInt `json:"errorCount"`
Executor string `json:"executor"`
MaxConnections int `json:"max-connections"`
MaxPostSize int64 `json:"max-post-size"`
MaxSavePostSize int64 `json:"max-save-post-size"`
MaxTime StringInt `json:"maxTime"`
Name string `json:"name"`
ProcessingTime StringInt `json:"processingTime"`
Protocol string `json:"protocol"`
ProxyName string `json:"proxy-name"`
ProxyPort string `json:"proxy-port"`
RedirectPort int `json:"redirect-port"`
RequestCount StringInt `json:"requestCount"`
Scheme string `json:"scheme"`
Secure bool `json:"secure"`
SocketBinding string `json:"socket-binding"`
SSL string `json:"ssl"`
VirtualServer string `json:"virtual-server"`
}
func (self *DmrContainer) GetName() string {
return self.Name
}
func (self *DmrContainer) GetStats() (*StatsEntry, error) {
dmrRequest := DmrResourceRequest{
Operation: "read-resource",
IncludeRuntime: true,
Address: []string{"subsystem", "web", "connector", "http"},
Pretty: 1,
}
wr := WebResult{}
dmrResponse := DmrResponse{
Result: &wr,
}
err := self.getStats(&dmrRequest, &dmrResponse)
if err != nil {
return nil, err
}
glog.Infof("outcome: %s, result: %s, failure: %s", dmrResponse.Outcome, dmrResponse.Result, dmrResponse.FailureDescription)
dmrStats := make(map[string]interface{})
dmrStats["bytes_received"] = wr.BytesReceived
dmrStats["bytes_sent"] = wr.BytesSent
dmrStats["enable_lookups"] = wr.EnableLookups
dmrStats["enabled"] = wr.Enabled
dmrStats["error_count"] = wr.ErrorCount
dmrStats["executor"] = wr.Executor
dmrStats["max_connections"] = wr.MaxConnections
dmrStats["max_post_size"] = wr.MaxPostSize
dmrStats["max_save_post_size"] = wr.MaxSavePostSize
dmrStats["max_time"] = wr.MaxTime
dmrStats["name"] = wr.Name
dmrStats["processing_time"] = wr.ProcessingTime
dmrStats["protocol"] = wr.Protocol
dmrStats["proxy_name"] = wr.ProxyName
dmrStats["proxy_port"] = wr.ProxyPort
dmrStats["redirect_port"] = wr.RedirectPort
dmrStats["request_count"] = wr.RequestCount
dmrStats["scheme"] = wr.Scheme
dmrStats["secure"] = wr.Secure
dmrStats["socket_binding"] = wr.SocketBinding
dmrStats["ssl"] = wr.SSL
dmrStats["virtual_server"] = wr.VirtualServer
responseStats := make(map[string]StatsValue)
responseStats["dmr"] = dmrStats
result := &StatsEntry{
Timestamp: time.Now().Local(),
Stats: responseStats,
}
glog.V(2).Infof("Retrieved DMR stats: %v", dmrStats)
return result, nil
}
func (self *DmrContainer) getStats(request interface{}, result interface{}) error {
reqBody, err := json.Marshal(request)
if err != nil {
return err
}
url := fmt.Sprintf("http://%s:%d/management", self.Host, self.DmrPort)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(reqBody))
if err != nil {
return err
}
req.Header.Add("Content-Type", "application/json")
err = PostRequestAndGetValue(&http.Client{}, req, result)
if err != nil {
return err
}
return nil
}