-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
61 lines (52 loc) · 1.54 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
package main
import (
"log"
"os"
"time"
"github.com/prometheus/client_golang/prometheus/push"
)
var (
// User-defined Environment-Variables
pushGateway = os.Getenv("PUSHGATEWAY")
user = os.Getenv("CHINACACHE_USER")
pass = os.Getenv("CHINACACHE_PASS")
channelIDs = os.Getenv("CHINACACHE_CHANNEL_IDS") // given a comma-separated list of available channel-ids for the specified chinacache user
queryInterval = os.Getenv("CHINACACHE_INTERVAL")
querytime = os.Getenv("QUERYTIME")
)
func main() {
// check if all the necessary environment-variables were provided and are not empty
if user == "" || pass == "" || pushGateway == "" || channelIDs == "" {
log.Fatal("error: Please provide the environment-variables PUSHGATEWAY, CHINACACHE_USER, CHINACACHE_PASS, CHINACACHE_CHANNEL_IDS (comma-separated list) and QUERYTIME (minutes)")
return
}
interval, err := time.ParseDuration(queryInterval)
if err != nil {
// Fetch values once and exit
interval = 0
}
// create new client and hand it over to create a new collector
client := NewChinaCacheClient(user, pass, channelIDs, querytime)
collector := NewChinaCacheCollector(client)
for {
// push the metrics exposed by the collector
err := push.AddCollectors(
"ChinaCachePush",
nil,
pushGateway,
collector,
)
if err != nil {
log.Printf("Could not push metrics: %s\n", err)
}
if interval == 0 {
break
} else {
time.Sleep(interval)
}
}
// Exit with non-zero statuscode if last fetch resulted in error
if err != nil {
os.Exit(1)
}
}