gh-28 Logfile configuration option and cli flag. Empty string means log to STDOUT. Closes gh-28
This commit is contained in:
parent
323c76af19
commit
ea0a62598a
|
@ -45,6 +45,7 @@ type Conf struct {
|
||||||
Password string `yaml:"password"`
|
Password string `yaml:"password"`
|
||||||
Interval int `yaml:"interval"`
|
Interval int `yaml:"interval"`
|
||||||
EnableMetrics bool `yaml:"enable_metrics"`
|
EnableMetrics bool `yaml:"enable_metrics"`
|
||||||
|
LogFile string `yaml:"log_file"`
|
||||||
LogAccess bool `yaml:"log_access"`
|
LogAccess bool `yaml:"log_access"`
|
||||||
Debug bool `yaml:"debug"`
|
Debug bool `yaml:"debug"`
|
||||||
}
|
}
|
||||||
|
@ -282,6 +283,7 @@ func initDefaultConfig(confpath string) {
|
||||||
"enervent",
|
"enervent",
|
||||||
4,
|
4,
|
||||||
false,
|
false,
|
||||||
|
"",
|
||||||
false,
|
false,
|
||||||
false,
|
false,
|
||||||
}
|
}
|
||||||
|
@ -308,6 +310,7 @@ func configure() {
|
||||||
usernflag := flag.String("username", config.Username, "Username for HTTP Basic Authentication")
|
usernflag := flag.String("username", config.Username, "Username for HTTP Basic Authentication")
|
||||||
passwflag := flag.String("password", config.Password, "Password for HTTP Basic Authentication")
|
passwflag := flag.String("password", config.Password, "Password for HTTP Basic Authentication")
|
||||||
promflag := flag.Bool("enable-metrics", config.EnableMetrics, "Enable the built-in Prometheus exporter")
|
promflag := flag.Bool("enable-metrics", config.EnableMetrics, "Enable the built-in Prometheus exporter")
|
||||||
|
logflag := flag.String("logfile", config.LogFile, "Path to log file. Default is empty string, log to stdout")
|
||||||
// TODO: log file flag
|
// TODO: log file flag
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
config.Debug = *debugflag
|
config.Debug = *debugflag
|
||||||
|
@ -318,8 +321,17 @@ func configure() {
|
||||||
config.Username = *usernflag
|
config.Username = *usernflag
|
||||||
config.Password = *passwflag
|
config.Password = *passwflag
|
||||||
config.EnableMetrics = *promflag
|
config.EnableMetrics = *promflag
|
||||||
|
config.LogFile = *logflag
|
||||||
usernamehash = sha256.Sum256([]byte(config.Username))
|
usernamehash = sha256.Sum256([]byte(config.Username))
|
||||||
passwordhash = sha256.Sum256([]byte(config.Password))
|
passwordhash = sha256.Sum256([]byte(config.Password))
|
||||||
|
if len(config.LogFile) != 0 {
|
||||||
|
logfile, err := os.OpenFile(config.LogFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0640)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("Failed to open log file", config.LogFile)
|
||||||
|
}
|
||||||
|
log.SetOutput(logfile)
|
||||||
|
log.Println("Opened logfile")
|
||||||
|
}
|
||||||
// Check that certificate file exists, generate if needed
|
// Check that certificate file exists, generate if needed
|
||||||
if _, err := os.Stat(config.SslCertificate); err != nil || *generatecert {
|
if _, err := os.Stat(config.SslCertificate); err != nil || *generatecert {
|
||||||
generateCertificate(config.SslCertificate, config.SslPrivatekey)
|
generateCertificate(config.SslCertificate, config.SslPrivatekey)
|
||||||
|
@ -334,6 +346,7 @@ func configure() {
|
||||||
}
|
}
|
||||||
log.Println("Update interval set to", config.Interval, "seconds")
|
log.Println("Update interval set to", config.Interval, "seconds")
|
||||||
if config.EnableMetrics {
|
if config.EnableMetrics {
|
||||||
|
log.Println("Prometheus exporter enabled (/metrics)")
|
||||||
prometheus.MustRegister(&pingvin)
|
prometheus.MustRegister(&pingvin)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue