Create a gist now

Instantly share code, notes, and snippets.

@moznion /h2o_exporter.go Secret
Created Jun 14, 2016

package main
import (
"encoding/json"
"log"
"net/http"
"github.com/prometheus/client_golang/prometheus"
)
const namespace = "h2o"
type exporter struct {
scrapeFailures prometheus.Counter
uptime prometheus.Gauge
connections prometheus.Gauge
maxConnections prometheus.Gauge
listeners prometheus.Gauge
workerThreads prometheus.Gauge
}
type h2oServerStatus struct {
ServerVersion string `json:"server-version"`
OpensslVersion string `json:"openssl-version"`
CurrentTime string `json:"current-time"`
RestartTime string `json:"restart-time"`
Uptime int `json:"uptime"`
Generation interface{} `json:"generation"`
Connections int `json:"connections"`
MaxConnections int `json:"max-connections"`
Listeners int `json:"listeners"`
WorkerThreads int `json:"worker-threads"`
}
func newExporter() *exporter {
return &exporter{
uptime: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "uptime",
Help: "uptime",
}),
connections: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "connections",
Help: "connections",
}),
maxConnections: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "max_connections",
Help: "max_connections",
}),
listeners: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "listeners",
Help: "listeners",
}),
workerThreads: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "worker_threads",
Help: "worker_threads",
}),
scrapeFailures: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Name: "exporter_scrape_failures_total",
Help: "Number of errors while scraping.",
}),
}
}
func (e *exporter) Describe(ch chan<- *prometheus.Desc) {
e.scrapeFailures.Describe(ch)
e.uptime.Describe(ch)
e.connections.Describe(ch)
e.maxConnections.Describe(ch)
e.listeners.Describe(ch)
e.workerThreads.Describe(ch)
}
func (e *exporter) Collect(ch chan<- prometheus.Metric) {
resp, _ := http.Get("http://127.0.0.1:7777/server-status/json")
defer resp.Body.Close()
var status h2oServerStatus
dec := json.NewDecoder(resp.Body)
dec.Decode(&status)
e.uptime.Set(float64(status.Uptime))
e.connections.Set(float64(status.Connections))
e.maxConnections.Set(float64(status.MaxConnections))
e.listeners.Set(float64(status.Listeners))
e.workerThreads.Set(float64(status.WorkerThreads))
e.uptime.Collect(ch)
e.connections.Collect(ch)
e.maxConnections.Collect(ch)
e.listeners.Collect(ch)
e.workerThreads.Collect(ch)
}
func main() {
exporter := newExporter()
prometheus.MustRegister(exporter)
http.Handle("/metrics", prometheus.Handler())
port := ":9999"
log.Print("Listening 127.0.0.1", port)
log.Fatal(http.ListenAndServe(port, nil))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment