-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
150 lines (134 loc) · 4.39 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
// dcm4chee2-exporter
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
"log"
"net/http"
"os/exec"
"strconv"
"strings"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
const metricsPath = "/metrics"
var (
version = "unset" // version gets set via build flags
homepage = "https://github.com/bjoernalbers/dcm4chee2-exporter"
// All metrics collected from JMX server.
dcm4chee2Up = prometheus.NewDesc("dcm4chee2_up", "Availability of Dcm4chee 2.", []string{}, nil)
moveScuQueueMessageCountDesc = prometheus.NewDesc("dcm4chee2_movescu_queue_message_count", "The number of messages in the queue.", []string{}, nil)
moveScuQueueDeliveringCountDesc = prometheus.NewDesc("dcm4chee2_movescu_queue_delivering_count", "The number of messages currently being delivered.", []string{}, nil)
moveScuQueueScheduledMessageCountDesc = prometheus.NewDesc("dcm4chee2_movescu_queue_scheduled_message_count", "The number of scheduled messages in the queue.", []string{}, nil)
scrapeDurationSeconds = prometheus.NewDesc("dcm4chee2_scrape_duration_seconds", "Duration of backend response in seconds.", []string{}, nil)
)
func init() {
flag.Usage = usage
}
func usage() {
header := fmt.Sprintf(`Dcm4chee 2 Prometheus Exporter (version %s)
This exporter exposes metrics from (deprecated) dcm4chee 2 for Prometheus.
Usage:
`, version)
footer := fmt.Sprintf(`
Homepage: %s
`, homepage)
fmt.Fprintf(flag.CommandLine.Output(), header)
flag.PrintDefaults()
fmt.Fprintf(flag.CommandLine.Output(), footer)
}
// jmxServer provides access to a JMX server.
type jmxServer struct {
Script string
Username string
Password string
}
// Fetch returns metrics from the JMX server.
func (j jmxServer) Fetch() ([]byte, error) {
return exec.Command(
j.Script,
"-u",
j.Username,
"-p",
j.Password,
"get",
"dcm4chee.archive:name=MoveScu,service=Queue",
"MessageCount",
"DeliveringCount",
"ScheduledMessageCount",
).Output()
}
// collector collects metrics for Prometheus.
type collector struct {
jmx jmxServer
}
// Describe metrics.
func (c collector) Describe(ch chan<- *prometheus.Desc) {
prometheus.DescribeByCollect(c, ch)
}
// Collect metrics.
func (c collector) Collect(ch chan<- prometheus.Metric) {
start := time.Now()
output, err := c.jmx.Fetch()
if err != nil {
log.Printf("Fetching metrics failed: %v\n", err)
ch <- prometheus.MustNewConstMetric(dcm4chee2Up, prometheus.GaugeValue, float64(0))
return
}
ch <- prometheus.MustNewConstMetric(scrapeDurationSeconds, prometheus.GaugeValue, float64(time.Since(start).Seconds()))
ch <- prometheus.MustNewConstMetric(dcm4chee2Up, prometheus.GaugeValue, float64(1))
metrics := Translate(output)
if m, ok := metrics["MessageCount"]; ok {
ch <- prometheus.MustNewConstMetric(moveScuQueueMessageCountDesc, prometheus.GaugeValue, float64(m))
}
if m, ok := metrics["DeliveringCount"]; ok {
ch <- prometheus.MustNewConstMetric(moveScuQueueDeliveringCountDesc, prometheus.GaugeValue, float64(m))
}
if m, ok := metrics["ScheduledMessageCount"]; ok {
ch <- prometheus.MustNewConstMetric(moveScuQueueScheduledMessageCountDesc, prometheus.GaugeValue, float64(m))
}
}
// Translate metrics from JMX server into map
func Translate(in []byte) map[string]int {
metrics := map[string]int{}
scanner := bufio.NewScanner(bytes.NewReader(in))
for scanner.Scan() {
key, value, found := strings.Cut(scanner.Text(), "=")
if !found {
continue
}
metric, err := strconv.Atoi(value)
if err != nil {
continue
}
metrics[key] = metric
}
return metrics
}
func main() {
jmx := jmxServer{}
flag.StringVar(&jmx.Script, "s", "twiddle.sh", "Path to JBoss twiddle script")
flag.StringVar(&jmx.Username, "u", "admin", "Username of JBoss admin")
flag.StringVar(&jmx.Password, "p", "admin", "Password of JBoss admin")
address := flag.String("a", ":9404", "Address to listen on")
flag.Parse()
log.Printf("Starting dcm4chee2-exporter, version %s\n", version)
prometheus.MustRegister(&collector{jmx})
http.Handle(metricsPath, promhttp.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head>
<title>Dcm4chee 2 Prometheus Exporter</title>
</head>
<body>
<h1>Dcm4chee 2 Prometheus Exporter</h1>
<p><a href='` + metricsPath + `'>Metrics</a></p>
</body>
</html>
`))
})
log.Fatal(http.ListenAndServe(*address, nil))
}