forked from Lusitaniae/apache_exporter
/
apache_exporter.go
172 lines (147 loc) · 4.07 KB
/
apache_exporter.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
package main
import (
"crypto/tls"
"flag"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"strings"
"sync"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/log"
)
const (
namespace = "apache" // For Prometheus metrics.
)
var (
listeningAddress = flag.String("telemetry.address", ":9117", "Address on which to expose metrics.")
metricsEndpoint = flag.String("telemetry.endpoint", "/metrics", "Path under which to expose metrics.")
scrapeURI = flag.String("scrape_uri", "http://localhost/server-status/?auto", "URI to apache stub status page.")
insecure = flag.Bool("insecure", false, "Ignore server certificate if using https.")
)
type Exporter struct {
URI string
mutex sync.RWMutex
client *http.Client
scrapeFailures prometheus.Counter
accessesTotal prometheus.Counter
kBytesTotal prometheus.Counter
uptime prometheus.Counter
workers *prometheus.GaugeVec
}
func NewExporter(uri string) *Exporter {
return &Exporter{
URI: uri,
scrapeFailures: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Name: "exporter_scrape_failures_total",
Help: "Number of errors while scraping apache.",
}),
accessesTotal: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Name: "accesses_total",
Help: "Current total apache accesses",
}),
kBytesTotal: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Name: "sent_kilobytes_total",
Help: "Current total kbytes sent",
}),
uptime: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Name: "uptime_seconds_total",
Help: "Current uptime in seconds",
}),
workers: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "workers",
Help: "Apache worker statuses",
},
[]string{"state"},
),
client: &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: *insecure},
},
},
}
}
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
e.scrapeFailures.Describe(ch)
e.accessesTotal.Describe(ch)
e.kBytesTotal.Describe(ch)
e.uptime.Describe(ch)
e.workers.Describe(ch)
}
// Split colon separated string into two fields
func splitkv(s string) (string, string) {
if len(s) == 0 {
return s, s
}
slice := strings.SplitN(s, ":", 2)
if len(slice) == 1 {
return slice[0], ""
}
return strings.TrimSpace(slice[0]), strings.TrimSpace(slice[1])
}
func (e *Exporter) collect(ch chan<- prometheus.Metric) error {
resp, err := e.client.Get(e.URI)
if err != nil {
return fmt.Errorf("Error scraping apache: %v", err)
}
data, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if resp.StatusCode != 200 {
if err != nil {
data = []byte(err.Error())
}
return fmt.Errorf("Status %s (%d): %s", resp.Status, resp.StatusCode, data)
}
lines := strings.Split(string(data), "\n")
for _, l := range lines {
key, v := splitkv(l)
if key == "Scoreboard" || key == "" {
continue
}
val, err := strconv.ParseFloat(v, 64)
if err != nil {
return err
}
switch {
case key == "Total Accesses":
e.accessesTotal.Set(val)
e.accessesTotal.Collect(ch)
case key == "Total kBytes":
e.kBytesTotal.Set(val)
e.kBytesTotal.Collect(ch)
case key == "Uptime":
e.uptime.Set(val)
e.uptime.Collect(ch)
case key == "BusyWorkers":
e.workers.WithLabelValues("busy").Set(val)
case key == "IdleWorkers":
e.workers.WithLabelValues("idle").Set(val)
}
}
e.workers.Collect(ch)
return nil
}
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
e.mutex.Lock() // To protect metrics from concurrent collects.
defer e.mutex.Unlock()
if err := e.collect(ch); err != nil {
log.Printf("Error scraping apache: %s", err)
e.scrapeFailures.Inc()
e.scrapeFailures.Collect(ch)
}
return
}
func main() {
flag.Parse()
exporter := NewExporter(*scrapeURI)
prometheus.MustRegister(exporter)
log.Printf("Starting Server: %s", *listeningAddress)
http.Handle(*metricsEndpoint, prometheus.Handler())
log.Fatal(http.ListenAndServe(*listeningAddress, nil))
}