-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
87 lines (73 loc) · 1.84 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
87
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"runtime"
"runtime/debug"
"time"
"github.com/patrickmn/go-cache"
collectd "github.com/paulhammond/gocollectd"
"gopkg.in/yaml.v2"
)
func parseCommandLineParams() {
flag.StringVar(&configPath, "c", "./config.yml", "Path to config.yml")
flag.Parse()
}
func initConfigs() {
data, err := ioutil.ReadFile(configPath)
if err != nil {
log.Fatalln(err)
}
err = yaml.Unmarshal(data, &config)
if err != nil {
log.Fatalln("error reading config", err)
}
}
func initRuntime() {
numCPU := runtime.NumCPU()
runtime.GOMAXPROCS(numCPU)
log.Printf("Init runtime to use %d CPUs and %d threads", numCPU, config.System.MaxThreads)
debug.SetMaxThreads(config.System.MaxThreads)
}
func initInternalCache() {
cached = cache.New(60*time.Minute, 1*time.Minute)
}
func main() {
fmt.Printf("Version: [%s]\nBuild: [%s]\nBuild Date: [%s]\n", version, build, buildDate)
parseCommandLineParams()
initConfigs()
initRuntime()
initInternalCache()
connectClickDB()
c := make(chan collectd.Packet)
log.Println("Starting listner on " + config.System.ListenOn)
go collectd.Listen(config.System.ListenOn, c)
for {
packet := <-c
// do something with the packet
processCollectDPacket(packet)
}
}
func processCollectDPacket(packet collectd.Packet) {
hostname := packet.Hostname
pkgtime := packet.Time() // A go time value
pkgplugin := packet.Plugin // "Load"
values, err := packet.ValueNumbers()
if err != nil {
log.Fatalln("Wrong packet from collectd", err)
}
names := packet.ValueNames()
var insData []cdElementData
for i, dat := range names {
var tmpIns cdElementData
tmpIns.EventDateTime = pkgtime
tmpIns.Hostname = hostname
tmpIns.Plugin = pkgplugin
tmpIns.ParamName = dat
tmpIns.ParamValue = values[i].Float64()
insData = append(insData, tmpIns)
}
insertCollectDToCH(insData)
}