-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathssh_exporter.go
75 lines (57 loc) · 1.74 KB
/
ssh_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
package main
//
// Copyright 2017 Nordstrom. All rights reserved.
//
//
// Provides an HTTP endpoint to be consumed by Prometheus
// which hosts pre-configured statistics found in config.yaml.
//
// Default endpoint: http://localhost:9428/probe?pattern=.*
//
import (
"github.com/Nordstrom/ssh_exporter/util"
"fmt"
"log"
"net/http"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
//
// Global variables
//
var configPath string
var servePort string
var loggingEnabled bool
var patternHelpText = `<p>Please include a valid <code>?pattern=[regex]</code>
query parameter in your URL. This should match the <bold>name</bold> of the
scripts you want to run (e.g., <code>?pattern=.*logs</code> matches
<code>chef_logs</code> and not <code>proc_status</code>)</p>.`
//
// Main fetches configuration file and assigns handlers for the http server.
//
func main() {
util.ParseFlags(&configPath, &servePort)
http.HandleFunc("/", indexHandler)
http.HandleFunc("/probe", probeHandler)
http.Handle("/metrics", promhttp.Handler())
util.LogMsg(fmt.Sprintf("Listening on localhost:%s", servePort))
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", servePort), nil))
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
// Human readable navigation help.
response := `<h1>ssh exporter</h1>
<p><a href='/probe'>probe</a></p>
<p><a href='/metrics'>metrics</a></p>`
fmt.Fprintf(w, response)
}
func probeHandler(w http.ResponseWriter, r *http.Request) {
conf, err := util.ParseConfig(configPath)
util.SoftCheck(err)
pattern, err := util.ParseQuery(w, r)
if util.SoftCheck(err) {
fmt.Fprintf(w, patternHelpText)
} else {
util.BatchExecute(&conf, pattern)
response, _ := util.PrometheusFormatResponse(conf)
fmt.Fprintf(w, response)
}
}