-
Notifications
You must be signed in to change notification settings - Fork 29
/
config.go
356 lines (314 loc) · 10.6 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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// nmon2influxdb
// author: adejoux@djouxtech.net
package nmon2influxdblib
import (
"bufio"
"bytes"
"io/ioutil"
"log"
"os"
"os/user"
"path/filepath"
"github.com/adejoux/influxdbclient"
"github.com/codegangsta/cli"
"github.com/naoina/toml"
)
//used for debug
var secretUser = "secretuser"
var secretPassword = "secret"
// Config is the configuration structure used by nmon2influxdb
type Config struct {
Debug bool
DebugFile string
Timezone string
InfluxdbUser string
InfluxdbPassword string
InfluxdbServer string
InfluxdbPort string
InfluxdbSecure bool
InfluxdbSkipCertCheck bool
InfluxdbDatabase string
GrafanaUser string
GrafanaPassword string
GrafanaURL string `toml:"grafana_URL"`
GrafanaAccess string
GrafanaDatasource string
HMCServer string `toml:"hmc_server"`
HMCUser string `toml:"hmc_user"`
HMCPassword string `toml:"hmc_password"`
HMCDatabase string `toml:"hmc_database"`
HMCDataRetention string `toml:"hmc_data_retention"`
HMCManagedSystem string `toml:"hmc_managed_system"`
HMCManagedSystemOnly bool `toml:"hmc_managed_system_only"`
HMCSamples int `toml:"hmc_samples"`
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"`
Inputs Inputs `toml:"input"`
}
// Inputs allows to put multiple input in the configuration file
type Inputs []Input
// Input specify how to apply new filters
type Input struct {
Measurement string
Name string
Match string
Tags Tags `toml:"tag"`
}
// 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",
InfluxdbSecure: false,
InfluxdbSkipCertCheck: false,
HMCUser: "hscroot",
HMCPassword: "abc123",
HMCDatabase: "nmon2influxdbHMC",
HMCSamples: 10,
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() string {
// if configuration file exist in /etc/nmon2influxdb. Stop here.
if IsFile("/etc/nmon2influxdb/nmon2influxdb.cfg") {
return "/etc/nmon2influxdb/nmon2influxdb.cfg"
}
currUser, _ := user.Current()
home := currUser.HomeDir
homeCFGfile := filepath.Join(home, ".nmon2influxdb.cfg")
return homeCFGfile
}
//IsFile returns true if the file doesn't exist
func IsFile(file string) bool {
stat, err := os.Stat(file)
if err != nil {
return false
}
if stat.Mode().IsRegular() {
return true
}
return false
}
//BuildCfgFile creates a default configuration file
func (config *Config) BuildCfgFile(cfgfile string) {
file, err := os.Create(cfgfile)
CheckError(err)
defer file.Close()
writer := bufio.NewWriter(file)
b, err := toml.Marshal(*config)
CheckError(err)
r := bytes.NewReader(b)
r.WriteTo(writer)
writer.Flush()
log.Printf("Generating default configuration file : %s\n", cfgfile)
}
// LoadCfgFile loads current configuration file settings
func (config *Config) LoadCfgFile() (cfgfile string) {
cfgfile = GetCfgFile()
//it would be only if no conf file exists. And it will build a configuration file in the home directory
if !IsFile(cfgfile) {
config.BuildCfgFile(cfgfile)
}
file, err := os.Open(cfgfile)
if err != nil {
log.Printf("Error opening configuration file %s\n", cfgfile)
return
}
defer file.Close()
buf, err := ioutil.ReadAll(file)
if err != nil {
CheckError(err)
}
if err := toml.Unmarshal(buf, &config); err != nil {
log.Printf("syntax error in configuration file: %s \n", err.Error())
os.Exit(1)
}
return
}
// 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
config.DashboardWriteFile = dfltConfig.DashboardWriteFile
}
// 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.DebugFile = c.GlobalString("debug-file")
config.HMCServer = c.String("hmc")
config.HMCUser = c.String("hmcuser")
config.HMCPassword = c.String("hmcpass")
config.HMCManagedSystem = c.String("managed_system")
config.HMCManagedSystemOnly = c.Bool("managed_system-only")
config.HMCSamples = c.Int("samples")
config.InfluxdbServer = c.GlobalString("server")
config.InfluxdbUser = c.GlobalString("user")
config.InfluxdbPort = c.GlobalString("port")
config.InfluxdbDatabase = c.GlobalString("db")
config.InfluxdbSecure = c.GlobalBool("secure")
config.InfluxdbSkipCertCheck = c.GlobalBool("skip_cert_check")
config.InfluxdbPassword = c.GlobalString("pass")
config.Timezone = c.GlobalString("tz")
if len(config.DebugFile) > 0 {
//if a debug file is set. Debug is true
config.Debug = true
debugFile, err := os.OpenFile(config.DebugFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600)
if err != nil {
log.Fatalf("error opening file: %v", err)
}
// never closing the file here to be able to change the log output in all packages
// No better solution for now.
// defer debugFile.Close()
log.SetOutput(debugFile)
log.Printf("NEW NMON2INFLUXDB EXECUTION\n")
}
if config.ImportBuildDashboard {
config.AddDashboardParams()
}
return
}
// ConnectDB connect to the specified influxdb database
func (config *Config) ConnectDB(db string) *influxdbclient.InfluxDB {
influxdbConfig := influxdbclient.InfluxDBConfig{
Host: config.InfluxdbServer,
Port: config.InfluxdbPort,
Database: db,
User: config.InfluxdbUser,
Pass: config.InfluxdbPassword,
Debug: config.Debug,
Secure: config.InfluxdbSecure,
SkipCertCheck: config.InfluxdbSkipCertCheck,
}
influxdb, err := influxdbclient.NewInfluxDB(influxdbConfig)
CheckError(err)
return &influxdb
}
// GetDB create or get the influxdb database used for nmon data
func (config *Config) GetDB(dbType string) *influxdbclient.InfluxDB {
db := config.InfluxdbDatabase
retention := config.ImportDataRetention
if dbType == "hmc" {
db = config.HMCDatabase
retention = config.HMCDataRetention
}
influxdb := config.ConnectDB(db)
if exist, _ := influxdb.ExistDB(db); exist != true {
log.Printf("Creating InfluxDB database %s\n", db)
_, createErr := influxdb.CreateDB(db)
CheckError(createErr)
}
// update default retention policy if ImportDataRetention is set
if len(retention) > 0 {
// Get default retention policy name
policyName, policyErr := influxdb.GetDefaultRetentionPolicy()
CheckError(policyErr)
log.Printf("Updating %s retention policy to keep only the last %s days. Timestamp based.\n", policyName, retention)
_, err := influxdb.UpdateRetentionPolicy(policyName, retention, true)
CheckError(err)
}
return influxdb
}
// GetLogDB create or get the influxdb database like defined in config
func (config *Config) GetLogDB() *influxdbclient.InfluxDB {
influxdb := config.ConnectDB(config.ImportLogDatabase)
if exist, _ := influxdb.ExistDB(config.ImportLogDatabase); exist != true {
_, err := influxdb.CreateDB(config.ImportLogDatabase)
CheckError(err)
_, err = influxdb.SetRetentionPolicy("log_retention", config.ImportLogRetention, true)
CheckError(err)
} else {
logPolicyName, logPolicyErr := influxdb.GetDefaultRetentionPolicy()
CheckError(logPolicyErr)
_, err := influxdb.UpdateRetentionPolicy(logPolicyName, config.ImportLogRetention, true)
CheckError(err)
}
return influxdb
}
// Sanitized returns a copy of the config struct without the password. Used for debug
func (config *Config) Sanitized() (debugConfig Config) {
debugConfig = *config
debugConfig.HMCUser = secretUser
debugConfig.HMCPassword = secretPassword
debugConfig.GrafanaUser = secretUser
debugConfig.GrafanaPassword = secretPassword
debugConfig.InfluxdbUser = secretUser
debugConfig.InfluxdbPassword = secretPassword
return
}