-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
93 lines (82 loc) · 2.17 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
88
89
90
91
92
93
package main
import (
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
"hwinfo-to-influxdb/client"
"os"
"strconv"
"time"
)
var log = logrus.New()
type Config struct {
HWinfoConfig struct {
ConnectionString string `yaml:"connection_string"`
RefreshInterval int `yaml:"refresh_interval"`
} `yaml:"hwinfo_config"`
InfluxDBConfig struct {
ConnectionString string `yaml:"connection_string"`
Org string `yaml:"org"`
Bucket string `yaml:"bucket"`
Username string `yaml:"username"`
SharedSecretEnv string `yaml:"shared_secret_env"`
} `yaml:"influxdb_config"`
}
func main() {
log.Out = os.Stdout
if val, exists := os.LookupEnv("LOG_LEVEL"); exists {
logLevel, err := logrus.ParseLevel(val)
if err != nil {
log.Fatal(err)
}
log.Level = logLevel
} else {
log.Level = logrus.DebugLevel
}
config, err := parseConfig()
if err != nil {
log.Fatal(err)
}
hwinfoClient, err := client.NewHWinfoClient(config.HWinfoConfig.ConnectionString)
if err != nil {
log.Fatal(err)
}
influxdbClient, err := client.NewInfluxDBClient(
config.InfluxDBConfig.ConnectionString,
config.InfluxDBConfig.Org,
config.InfluxDBConfig.Bucket,
config.InfluxDBConfig.Username,
config.InfluxDBConfig.SharedSecretEnv)
if err != nil {
log.Fatal(err)
}
log.Printf("Starting hwinfo-to-influxdb - %ds refresh interval", config.HWinfoConfig.RefreshInterval)
for range time.Tick(time.Duration(config.HWinfoConfig.RefreshInterval) * time.Second) {
start := time.Now().UnixNano()
sensorReadings, err := hwinfoClient.GetCurrentSensorReadings()
if err != nil {
log.Fatal(err)
}
err = influxdbClient.PostToServer(sensorReadings)
if err != nil {
log.Fatal(err)
}
end := time.Now().UnixNano()
log.WithFields(logrus.Fields{
"elapsed": strconv.FormatInt((end-start)/int64(time.Millisecond), 10) + " ms",
}).Debug("finished scanning sensor data and wrote to influxdb")
}
}
func parseConfig() (*Config, error) {
f, err := os.Open("config.yml")
if err != nil {
return nil, err
}
defer f.Close()
var config Config
decoder := yaml.NewDecoder(f)
err = decoder.Decode(&config)
if err != nil {
return nil, err
}
return &config, nil
}