-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjenkins-exporter.go
112 lines (99 loc) · 2.62 KB
/
jenkins-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
package main
import (
"log"
"flag"
"io/ioutil"
"encoding/json"
"regexp"
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"strconv"
"time"
)
type JenkinsJobs struct {
Class string `json:"_class"`
Jobs []struct {
Class string `json:"_class"`
Name string `json:"name"`
} `json:"jobs"`
}
var (
jenkins_builds = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "jenkins",
Help: "Number of Builds",
},
[]string{"Program"},
)
)
func init() {
prometheus.MustRegister(jenkins_builds)
}
func getFromJenkinsApi(url string,Url string, Username string, Password string) {
retry:
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
req.SetBasicAuth(Username, Password)
resp, err := client.Do(req)
if err != nil{
log.Printf("%s",err)
time.Sleep(20 * time.Second)
goto retry
}
bodyText, err := ioutil.ReadAll(resp.Body)
var jobs JenkinsJobs
json.Unmarshal([]byte(bodyText), &jobs)
r, _ := regexp.Compile("(.*(deploy|toggle|force))")
for _, job := range jobs.Jobs {
if (r.MatchString(job.Name)) {
url := Url+"/job/"+job.Name+"/api/xml?xpath=/*/lastStableBuild/number"
count := BuildCount(url, Username, Password)
jenkins_builds.With(prometheus.Labels{"Program":job.Name}).Set(count)
}
}
}
func BuildCount(url string, username string, password string, ) float64 {
var MetricValue = 0.1
repeat:
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
req.SetBasicAuth(username, password)
resp, err := client.Do(req)
if err != nil{
log.Printf("%s",err)
time.Sleep(20 * time.Second)
goto repeat
}
bodyText, err := ioutil.ReadAll(resp.Body)
if err != nil{
log.Printf("%s",err)
}
r, _ := regexp.Compile("[0-9]+")
re:= r.FindAllString(string(bodyText), -1)
if len(re) != 0 {
MetricValue, _ := strconv.ParseFloat(re[0],64)
return MetricValue
}
return MetricValue
}
func recordMetrics(JenkinsUrl string, url string,username string, password string) {
go func() {
for {
getFromJenkinsApi(JenkinsUrl, url, username, password)
time.Sleep(2 * time.Second)
}
}()
}
func main() {
port:=flag.Int( "port", 2115, "Port Number to listen")
url := flag.String("url", "" , "Jenkins Url")
username := flag.String("user", "" , "Jenkins username")
password := flag.String("pass", "" , "Jenkins password")
flag.Parse()
Port := ":"+strconv.Itoa(*port)
JenkinsUrl := *url + "/api/json?tree=jobs[name]"
recordMetrics(JenkinsUrl, *url, *username, *password)
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(Port, nil)
}