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
/
jolokia.go
96 lines (77 loc) · 2.44 KB
/
jolokia.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
package sources
import (
"bytes"
"encoding/json"
"fmt"
"github.com/golang/glog"
"net/http"
"time"
)
type JolokiaRequestType string
const (
Search JolokiaRequestType = "search"
Read JolokiaRequestType = "read"
List JolokiaRequestType = "list"
Exec JolokiaRequestType = "exec"
Write JolokiaRequestType = "write"
)
type JolokiaRequest struct {
Type JolokiaRequestType `json:"type"`
MBean string `json:"mbean,omitempty"`
Attribute interface{} `json:"attribute,omitempty"`
Path string `json:"path,omitempty"`
MaxDepth uint `json:"maxDepth,omitempty"`
}
type JolokiaResponse struct {
Status uint32
Timestamp uint32
Request map[string]interface{}
Value StatsValue
Error string
}
type JolokiaContainer struct {
Name string `json:"name,omitempty"`
Host string `json:"host"`
JolokiaPort int `json:"jolokiaPort"`
Stats *StatsEntry `json:"stats,omitempty"`
}
func (self *JolokiaContainer) GetName() string {
return self.Name
}
func (self *JolokiaContainer) GetStats() (*StatsEntry, error) {
url := fmt.Sprintf("http://%s:%d", self.Host, self.JolokiaPort) + "/jolokia/"
glog.V(2).Infof("Requesting jolokia stats from %s", url)
jolokiaRequests := []JolokiaRequest{
JVMRequest,
}
if amqRequests, err := GetAMQRequests(url); err == nil {
jolokiaRequests = append(jolokiaRequests, amqRequests...)
}
reqBody, err := json.Marshal(jolokiaRequests)
if err != nil {
return &StatsEntry{}, err
}
glog.V(2).Infof("Sending Jolokia request: %v", string(reqBody))
req, err := http.NewRequest("POST", url, bytes.NewBuffer(reqBody))
if err != nil {
return &StatsEntry{}, err
}
var jolokiaResponses []JolokiaResponse
err = PostRequestAndGetValue(&http.Client{}, req, &jolokiaResponses)
if err != nil {
glog.Errorf("failed to get stats from jolokia url: %s - %s\n", url, err)
return &StatsEntry{}, nil
}
glog.V(2).Infof("Received jolokia response: %v", jolokiaResponses)
jolokiaResponseStats := make(map[string]StatsValue)
// Retrieve mbean stats
for _, jolokiaResponse := range jolokiaResponses {
jolokiaResponseStats[jolokiaResponse.Request["mbean"].(string)] = jolokiaResponse.Value
}
jolokiaStats := &StatsEntry{
Timestamp: time.Unix(0, int64(jolokiaResponses[0].Timestamp)*int64(time.Second)),
Stats: jolokiaResponseStats,
}
glog.V(2).Infof("Retrieved jolokia stats: %v", jolokiaStats)
return jolokiaStats, nil
}