/
main.go
208 lines (189 loc) · 5.17 KB
/
main.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// GANGLIA-ACTIVEMQ
// https://github.com/jbuchbinder/ganglia-activemq
package main
import (
"encoding/json"
"encoding/xml"
"errors"
"flag"
"fmt"
"github.com/jbuchbinder/go-gmetric/gmetric"
"io/ioutil"
"log/syslog"
"net"
"net/http"
"net/http/httputil"
"os"
"strings"
"time"
)
var (
activeMqHost = flag.String("activeMqHost", "localhost", "ActiveMQ host")
activeMqPort = flag.Int("activeMqPort", 8161, "ActiveMQ port")
gangliaHost = flag.String("gangliaHost", "localhost", "Ganglia host name/IP, can be multiple comma separated")
gangliaPort = flag.Int("gangliaPort", 8649, "Ganglia port")
gangliaSpoof = flag.String("gangliaSpoof", "", "Ganglia spoof string (IP:host)")
gangliaGroup = flag.String("gangliaGroup", "activemq", "Ganglia group name")
gangliaInterval = flag.Int("gangliaInterval", 300, "Ganglia polling interval/metric TTL")
vdedServer = flag.String("vdedServer", "", "VDED server (default not used)")
verbose = flag.Bool("verbose", false, "Verbose")
ignoreQueues = flag.String("ignoreQueues", "", "Substring to ignore in queue names")
gm gmetric.Gmetric
log, _ = syslog.New(syslog.LOG_DEBUG, "ganglia-activemq")
)
func main() {
flag.Parse()
gm = gmetric.Gmetric{
Spoof: *gangliaSpoof,
Host: *gangliaSpoof,
}
gm.SetLogger(log)
if *verbose {
gm.SetVerbose(true)
} else {
gm.SetVerbose(false)
}
if strings.Contains(*gangliaHost, ",") {
segs := strings.Split(*gangliaHost, ",")
for i := 0; i < len(segs); i++ {
gIP, err := net.ResolveIPAddr("ip4", segs[i])
if err != nil {
panic(err.Error())
}
gm.AddServer(gmetric.Server{
Server: gIP.IP,
Port: *gangliaPort,
})
}
} else {
// Lookup host name
gIP, err := net.ResolveIPAddr("ip4", *gangliaHost)
if err != nil {
panic(err.Error())
}
gm.AddServer(gmetric.Server{
Server: gIP.IP,
Port: *gangliaPort,
})
}
hostname, err := os.Hostname()
if err != nil {
panic(err)
}
q, err := getQueues(*activeMqHost, *activeMqPort)
if err != nil {
panic(err)
}
for i := 0; i < len(q.Items); i++ {
if *ignoreQueues == "" || !strings.Contains(q.Items[i].Name, *ignoreQueues) {
sName := strings.Replace(q.Items[i].Name, ".", "_", -1)
log.Debug("Processing queue " + q.Items[i].Name)
if *verbose {
fmt.Printf("Sending queue_%s_size\n", q.Items[i].Name)
}
gm.SendMetric(
fmt.Sprintf("queue_%s_size", sName),
fmt.Sprint(q.Items[i].Stats.Size),
gmetric.VALUE_UNSIGNED_INT, "size", gmetric.SLOPE_BOTH,
uint32(*gangliaInterval), uint32(*gangliaInterval)*2,
*gangliaGroup)
if *verbose {
fmt.Printf("Sending queue_%s_consumers\n", q.Items[i].Name)
}
gm.SendMetric(
fmt.Sprintf("queue_%s_consumers", sName),
fmt.Sprint(q.Items[i].Stats.ConsumerCount),
gmetric.VALUE_UNSIGNED_INT, "consumers", gmetric.SLOPE_BOTH,
uint32(*gangliaInterval), uint32(*gangliaInterval)*2,
*gangliaGroup)
if *vdedServer != "" {
if *verbose {
fmt.Printf("VDED submitting %s queue_%s_enqueue : %d\n", hostname, sName, q.Items[i].Stats.EnqueueCount)
}
go submitVded(hostname, fmt.Sprintf("queue_%s_enqueue", sName), q.Items[i].Stats.EnqueueCount)
if *verbose {
fmt.Printf("VDED submitting %s queue_%s_dequeue : %d\n", hostname, sName, q.Items[i].Stats.DequeueCount)
}
go submitVded(hostname, fmt.Sprintf("queue_%s_dequeue", sName), q.Items[i].Stats.DequeueCount)
}
}
}
}
func getQueues(host string, port int) (q queues, e error) {
client := http.Client{}
url := fmt.Sprintf("http://%s:%d/admin/xml/queues.jsp", host, port)
if *verbose {
fmt.Println("url = '" + url + "'")
}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
e = err
return
}
if *verbose {
dump, _ := httputil.DumpRequestOut(req, true)
fmt.Println(string(dump))
}
req.Header.Set("User-Agent", "ganglia-activemq")
res, err := client.Do(req)
if err != nil {
e = err
return
}
defer res.Body.Close()
// Extract user resource from body
body, err := ioutil.ReadAll(res.Body)
if err != nil {
e = err
return
}
var obj queues
err = xml.Unmarshal(body, &obj)
if err != nil {
e = err
return
}
if *verbose {
m, err := json.MarshalIndent(obj, " ", " ")
if err != nil {
e = err
return
}
fmt.Println(string(m))
}
q = obj
return
}
func submitVded(host string, metricName string, value int64) (e error) {
if *vdedServer == "" {
return errors.New("No VDED server, cannot continue")
}
url := fmt.Sprintf("http://%s:48333/vector?host=%s&vector=delta_%s&value=%d&ts=%d&submit_metric=1", *vdedServer, host, metricName, value, time.Now().Unix())
log.Debug("VDED: " + url)
c := http.Client{
Transport: &http.Transport{
Dial: timeoutDialer(time.Duration(5) * time.Second),
},
}
resp, err := c.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
log.Debug("VDED returned: " + string(body))
return
}
func timeoutDialer(ns time.Duration) func(net, addr string) (c net.Conn, err error) {
return func(netw, addr string) (net.Conn, error) {
c, err := net.Dial(netw, addr)
if err != nil {
return nil, err
}
c.SetDeadline(time.Now().Add(ns))
return c, nil
}
}