-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
80 lines (66 loc) · 1.86 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
package main
import (
"flag"
"gopkg.in/tylerb/graceful.v1"
"io/ioutil"
"log"
"net/http"
"regexp"
"time"
"strconv"
)
var (
statusLineRegexp = regexp.MustCompile(`(?m)^(.*):\s+(.*)$`)
fpmStatusURL = ""
)
func main() {
url := flag.String("status-url", "", "PHP-FPM status URL")
addr := flag.String("addr", "0.0.0.0:8080", "IP/port for the HTTP server")
flag.Parse()
if *url == "" {
log.Fatal("The status-url flag is required.")
} else {
fpmStatusURL = *url
}
scrapeFailures := 0
server := &graceful.Server{
Timeout: 10 * time.Second,
Server: &http.Server{
Addr: *addr,
ReadTimeout: time.Duration(5) * time.Second,
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
resp, err := http.Get(fpmStatusURL)
if err != nil {
log.Println(err)
scrapeFailures = scrapeFailures+1
x := strconv.Itoa(scrapeFailures)
NewMetricsFromMatches([][]string{{"scrape failure:","scrape failure",x}}).WriteTo(w)
return
}
if (resp.StatusCode != http.StatusOK){
log.Println("php-fpm status code is not OK.")
scrapeFailures = scrapeFailures+1
x := strconv.Itoa(scrapeFailures)
NewMetricsFromMatches([][]string{{"scrape failure:","scrape failure",x}}).WriteTo(w)
return
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println(err)
scrapeFailures = scrapeFailures+1
x := strconv.Itoa(scrapeFailures)
NewMetricsFromMatches([][]string{{"scrape failure:","scrape failure",x}}).WriteTo(w)
return
}
resp.Body.Close()
x := strconv.Itoa(scrapeFailures)
matches := statusLineRegexp.FindAllStringSubmatch(string(body), -1)
matches = append(matches,[]string{"scrape failure:","scrape failure",x})
NewMetricsFromMatches(matches).WriteTo(w)
}),
},
}
if err := server.ListenAndServe(); err != nil {
log.Fatal(err)
}
}