This repository has been archived by the owner on Jan 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.go
86 lines (71 loc) · 2.14 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
81
82
83
84
85
86
package main
import (
"flag"
"log"
"os"
"strconv"
"time"
"github.com/kpacha/mesos-influxdb-collector/collector"
"github.com/kpacha/mesos-influxdb-collector/config"
"github.com/kpacha/mesos-influxdb-collector/store"
)
const (
ConfigPath = "conf.hcl"
InfluxdbUser = "root"
InfluxdbPass = "root"
InfluxdbEnvName = "INFLUXDB_HOST"
InfluxdbDBEnvName = "INFLUXDB_DB"
InfluxdbPortEnvName = "INFLUXDB_PORT"
ConfigPathEnvName = "CONFIG_FILE"
InfluxdbUserEnvName = "INFLUXDB_USER"
InfluxdbPassEnvName = "INFLUXDB_PWD"
)
func main() {
ihost := flag.String("Ih", getStringParam(InfluxdbEnvName, config.EmptyString), "influxdb host")
iport := flag.Int("Ip", getIntParam(InfluxdbPortEnvName, config.EmptyInt), "influxdb port")
idb := flag.String("Id", getStringParam(InfluxdbDBEnvName, config.EmptyString), "influxdb database")
configPath := flag.String("c", ConfigPath, "path to the config file")
flag.Parse()
cp := config.ConfigParser{
Path: *configPath,
AllowDNS: true,
Default: config.DefaultConfig,
}
conf, err := cp.ParseAndMerge(ihost, iport, idb)
if err != nil {
log.Println("Error parsing config file:", err.Error())
return
}
col := collector.NewCollectorFromConfig(conf)
influxdb := store.NewInfluxdbFromConfig(
conf,
getStringParam(InfluxdbUserEnvName, InfluxdbUser),
getStringParam(InfluxdbPassEnvName, InfluxdbPass))
subscription := NewCollectorSubscription(&conf.Lapse, &col, &influxdb)
go report(&subscription, conf.InfluxDB.CheckLapse)
time.Sleep(time.Second * time.Duration(conf.DieAfter))
subscription.Cancel()
log.Println("Mesos collector stopped")
}
func getStringParam(envName string, defaultValue string) string {
env := os.Getenv(envName)
if env == "" {
return defaultValue
}
return env
}
func getIntParam(envName string, defaultValue int) int {
env, err := strconv.Atoi(os.Getenv(envName))
if err != nil {
return defaultValue
}
return env
}
func report(subscription *Subscription, lapse int) {
ticker := time.NewTicker(time.Second * time.Duration(lapse))
var collects int
for _ = range ticker.C {
collects = <-subscription.Stats
log.Println("Total collected stats:", collects)
}
}