forked from adejoux/nmon2influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
250 lines (221 loc) · 7.1 KB
/
config.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// nmon2influxdb
// import nmon data in InfluxDB
// author: adejoux@djouxtech.net
package main
import (
"bufio"
"bytes"
"fmt"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"github.com/adejoux/influxdbclient"
"github.com/codegangsta/cli"
"github.com/naoina/toml"
)
// Config is the configuration structure used by nmon2influxdb
type Config struct {
Debug bool
Timezone string
InfluxdbUser string
InfluxdbPassword string
InfluxdbServer string
InfluxdbPort string
InfluxdbDatabase string
GrafanaUser string
GrafanaPassword string
GrafanaURL string `toml:"grafana_URL"`
GrafanaAccess string
GrafanaDatasource string
ImportSkipDisks bool
ImportAllCpus bool
ImportBuildDashboard bool
ImportForce bool
ImportSkipMetrics string
ImportLogDatabase string
ImportLogRetention string
ImportDataRetention string
ImportSSHUser string `toml:"import_ssh_user"`
ImportSSHKey string `toml:"import_ssh_key"`
DashboardWriteFile bool
StatsLimit int
StatsSort string
StatsFilter string
StatsFrom string
StatsTo string
StatsHost string
Metric string `toml:"metric,omitempty"`
ListFilter string `toml:",omitempty"`
ListHost string `toml:",omitempty"`
}
// InitConfig setup initial configuration with sane values
func InitConfig() Config {
currUser, _ := user.Current()
home := currUser.HomeDir
sshKey := filepath.Join(home, "/.ssh/id_rsa")
return Config{Debug: false,
Timezone: "Europe/Paris",
InfluxdbUser: "root",
InfluxdbPassword: "root",
InfluxdbServer: "localhost",
InfluxdbPort: "8086",
InfluxdbDatabase: "nmon_reports",
GrafanaUser: "admin",
GrafanaPassword: "admin",
GrafanaURL: "http://localhost:3000",
GrafanaAccess: "direct",
GrafanaDatasource: "nmon2influxdb",
ImportSkipDisks: false,
ImportAllCpus: false,
ImportBuildDashboard: false,
ImportForce: false,
ImportLogDatabase: "nmon2influxdb_log",
ImportLogRetention: "2d",
ImportSSHUser: currUser.Username,
ImportSSHKey: sshKey,
DashboardWriteFile: false,
ImportSkipMetrics: "JFSINODE|TOP|PCPU",
StatsLimit: 20,
StatsSort: "mean",
StatsFilter: "",
StatsFrom: "",
StatsTo: "",
StatsHost: "",
}
}
//GetCfgFile returns the current configuration file path
func GetCfgFile() (cfgfile string) {
currUser, _ := user.Current()
home := currUser.HomeDir
cfgfile = filepath.Join(home, ".nmon2influxdb.cfg")
return
}
//IsNotFile returns true if the file doesn't exist
func IsNotFile(file string) bool {
stat, err := os.Stat(file)
if err != nil {
return true
}
if stat.Mode().IsRegular() {
return false
}
return true
}
//BuildCfgFile creates a default configuration file
func (config *Config) BuildCfgFile(cfgfile string) {
file, err := os.Create(cfgfile)
check(err)
defer file.Close()
writer := bufio.NewWriter(file)
b, err := toml.Marshal(*config)
check(err)
r := bytes.NewReader(b)
r.WriteTo(writer)
writer.Flush()
fmt.Printf("Generating default configuration file : %s\n", cfgfile)
}
// LoadCfgFile loads current configuration file settings
func (config *Config) LoadCfgFile() {
cfgfile := GetCfgFile()
if IsNotFile(cfgfile) {
config.BuildCfgFile(cfgfile)
}
file, err := os.Open(cfgfile)
if err != nil {
fmt.Printf("Error opening configuration file %s\n", cfgfile)
return
}
defer file.Close()
buf, err := ioutil.ReadAll(file)
if err != nil {
check(err)
}
if err := toml.Unmarshal(buf, &config); err != nil {
check(err)
}
}
// AddDashboardParams initialize default parameters for dashboard
func (config *Config) AddDashboardParams() {
dfltConfig := InitConfig()
dfltConfig.LoadCfgFile()
config.GrafanaAccess = dfltConfig.GrafanaAccess
config.GrafanaURL = dfltConfig.GrafanaURL
config.GrafanaDatasource = dfltConfig.GrafanaDatasource
config.GrafanaUser = dfltConfig.GrafanaUser
config.GrafanaPassword = dfltConfig.GrafanaPassword
}
// ParseParameters parse parameter from command line in Config struct
func ParseParameters(c *cli.Context) (config *Config) {
config = new(Config)
*config = InitConfig()
config.LoadCfgFile()
config.Metric = c.String("metric")
config.StatsHost = c.String("statshost")
config.StatsFrom = c.String("from")
config.StatsTo = c.String("to")
config.StatsLimit = c.Int("limit")
config.StatsFilter = c.String("filter")
config.ImportSkipDisks = c.Bool("nodisks")
if c.IsSet("cpus") {
config.ImportAllCpus = c.Bool("cpus")
}
config.ImportBuildDashboard = c.Bool("build")
config.ImportSkipMetrics = c.String("skip_metrics")
config.ImportLogDatabase = c.String("log_database")
config.ImportLogRetention = c.String("log_retention")
config.DashboardWriteFile = c.Bool("file")
config.ListFilter = c.String("filter")
config.ImportForce = c.Bool("force")
config.ListHost = c.String("host")
config.GrafanaUser = c.String("guser")
config.GrafanaPassword = c.String("gpassword")
config.GrafanaAccess = c.String("gaccess")
config.GrafanaURL = c.String("gurl")
config.GrafanaDatasource = c.String("datasource")
config.Debug = c.GlobalBool("debug")
config.InfluxdbServer = c.GlobalString("server")
config.InfluxdbUser = c.GlobalString("user")
config.InfluxdbPort = c.GlobalString("port")
config.InfluxdbDatabase = c.GlobalString("db")
config.InfluxdbPassword = c.GlobalString("pass")
config.Timezone = c.GlobalString("tz")
if config.ImportBuildDashboard {
config.AddDashboardParams()
}
return
}
// GetDataDB create or get the influxdb database like defined in config
func (config *Config) GetDataDB() (influxdb *influxdbclient.InfluxDB) {
influxdb = influxdbclient.NewInfluxDB(config.InfluxdbServer, config.InfluxdbPort, config.InfluxdbDatabase, config.InfluxdbUser, config.InfluxdbPassword)
influxdb.SetDebug(config.Debug)
err := influxdb.Connect()
check(err)
if exist, _ := influxdb.ExistDB(config.InfluxdbDatabase); exist != true {
_, createErr := influxdb.CreateDB(config.InfluxdbDatabase)
check(createErr)
}
// update default retention policy if ImportDataRetention is set
if len(config.ImportDataRetention) > 0 {
_, err := influxdb.UpdateRetentionPolicy("default", config.ImportDataRetention, true)
check(err)
}
return
}
// GetLogDB create or get the influxdb database like defined in config
func (config *Config) GetLogDB() (influxdb *influxdbclient.InfluxDB) {
influxdb = influxdbclient.NewInfluxDB(config.InfluxdbServer, config.InfluxdbPort, config.ImportLogDatabase, config.InfluxdbUser, config.InfluxdbPassword)
influxdb.SetDebug(config.Debug)
err := influxdb.Connect()
check(err)
if exist, _ := influxdb.ExistDB(config.ImportLogDatabase); exist != true {
_, err := influxdb.CreateDB(config.ImportLogDatabase)
check(err)
_, err = influxdb.SetRetentionPolicy("log_retention", config.ImportLogRetention, true)
check(err)
} else {
_, err := influxdb.UpdateRetentionPolicy("log_retention", config.ImportLogRetention, true)
check(err)
}
return
}