From addd5a7b00a291079b7aacf3736641a4c6e2e12e Mon Sep 17 00:00:00 2001 From: Dan Ballard Date: Thu, 28 Jun 2018 10:28:39 -0700 Subject: [PATCH] remove config sample; now generate config and populate defaults; add config to control logging --- server/app/main.go | 14 -------- server/app/serverConfig.json.sample | 7 ---- server/metrics/monitors.go | 21 ++++++----- server/server.go | 2 +- server/serverConfig.go | 56 ++++++++++++++++++----------- 5 files changed, 46 insertions(+), 54 deletions(-) delete mode 100644 server/app/serverConfig.json.sample diff --git a/server/app/main.go b/server/app/main.go index f632155..950e451 100644 --- a/server/app/main.go +++ b/server/app/main.go @@ -2,28 +2,14 @@ package main import ( cwtchserver "cwtch.im/cwtch/server" - "io/ioutil" "log" - "os" ) const ( serverConfigFile = "serverConfig.json" ) -func confirmOrCopySampleConfig() { - // if no config file, attempt to copy sample - if _, err := os.Stat(serverConfigFile); os.IsNotExist(err) { - raw, err := ioutil.ReadFile(serverConfigFile + ".sample") - if err != nil { - log.Fatal("Could not read sample config to copy: ", err) - } - ioutil.WriteFile(serverConfigFile, raw, 0600) - } -} - func main() { - confirmOrCopySampleConfig() serverConfig := cwtchserver.LoadConfig(serverConfigFile) server := new(cwtchserver.Server) diff --git a/server/app/serverConfig.json.sample b/server/app/serverConfig.json.sample deleted file mode 100644 index a6b2d13..0000000 --- a/server/app/serverConfig.json.sample +++ /dev/null @@ -1,7 +0,0 @@ -{ - "maxBufferLines": 100000, - "serverReporting": { - "reportingGroupId": "", - "reportingServerAddr": "" - } -} diff --git a/server/metrics/monitors.go b/server/metrics/monitors.go index 14fbc8d..a22c4e9 100644 --- a/server/metrics/monitors.go +++ b/server/metrics/monitors.go @@ -23,10 +23,12 @@ type Monitors struct { ClientConns MonitorHistory starttime time.Time breakChannel chan bool + log bool } // Start initializes a Monitors's monitors -func (mp *Monitors) Start(ra *application.RicochetApplication) { +func (mp *Monitors) Start(ra *application.RicochetApplication, log bool) { + mp.log = log mp.starttime = time.Now() mp.breakChannel = make(chan bool) mp.MessageCounter = NewCounter() @@ -35,19 +37,15 @@ func (mp *Monitors) Start(ra *application.RicochetApplication) { mp.Memory = NewMonitorHistory(MegaBytes, func() float64 { sysInfo, _ := pidusage.GetStat(os.Getpid()); return float64(sysInfo.Memory) }) mp.ClientConns = NewMonitorHistory(Count, func() float64 { return float64(ra.ConnectionCount()) }) - // Todo: replace with proper reporting - go mp.log() + if mp.log { + go mp.run() + } } -func (mp *Monitors) log() { +func (mp *Monitors) run() { for { select { case <-time.After(time.Minute): - messageMinutes := mp.Messages.Minutes() - cpuMinutes := mp.CPU.Minutes() - memoryMinutes := mp.Memory.Minutes() - listenConnsMinutes := mp.ClientConns.Minutes() - log.Printf("METRICS: Messages: %.0f ClientConns: %.0f CPU: %.2f Memory: %.0fMBs\n", messageMinutes[0], listenConnsMinutes[0], cpuMinutes[0], memoryMinutes[0]/1024/1024) mp.report() case <-mp.breakChannel: return @@ -80,12 +78,13 @@ func (mp *Monitors) report() { mp.Memory.Report(w) w.Flush() - } // Stop stops all the monitors in a Monitors func (mp *Monitors) Stop() { - mp.breakChannel <- true + if mp.log { + mp.breakChannel <- true + } mp.Messages.Stop() mp.CPU.Stop() mp.Memory.Stop() diff --git a/server/server.go b/server/server.go index e10825b..119b486 100644 --- a/server/server.go +++ b/server/server.go @@ -23,7 +23,7 @@ type Server struct { func (s *Server) Run(serverConfig *Config) { s.config = serverConfig cwtchserver := new(application.RicochetApplication) - s.metricsPack.Start(cwtchserver) + s.metricsPack.Start(cwtchserver, s.config.ServerReporting.LogMetricsToFile) l, err := application.SetupOnion("127.0.0.1:9051", "tcp4", "", s.config.PrivateKey(), 9878) diff --git a/server/serverConfig.go b/server/serverConfig.go index fdfb632..6012a3b 100644 --- a/server/serverConfig.go +++ b/server/serverConfig.go @@ -11,6 +11,7 @@ import ( // Reporting is a struct for storing a the config a server needs to be a peer, and connect to a group to report type Reporting struct { + LogMetricsToFile bool `json:"logMetricsToFile"` PeerPrivateKey string `json:"privateKey"` ReportingGroupID string `json:"reportingGroupId"` ReportingServerAddr string `json:"reportingServerAddr"` @@ -41,32 +42,45 @@ func (config *Config) Save(filename string) { ioutil.WriteFile(filename, bytes, 0600) } -// LoadConfig loads a Config from a json file specified by filename -func LoadConfig(filename string) *Config { - raw, err := ioutil.ReadFile(filename) - if err != nil { - log.Fatal("Could not read config: ", err) - } - +// newConfig generates a simple config with defaults. Unmarshal will return them if they aren't specified +func newConfig() *Config { config := Config{} - err = json.Unmarshal(raw, &config) - - if err != nil { - log.Fatal("Error reading config: ", err) - } - - if config.PrivateKeyBytes == "" { - config.generatePrivateKey() - config.Save(filename) - } - if config.ServerReporting.PeerPrivateKey == "" { - config.generatePeerPrivateKey() - config.Save(filename) - } + config.MaxBufferLines = 100000 + config.ServerReporting.LogMetricsToFile = false return &config } +// LoadConfig loads a Config from a json file specified by filename +func LoadConfig(filename string) *Config { + config := newConfig() + + raw, err := ioutil.ReadFile(filename) + if err == nil { + err = json.Unmarshal(raw, &config) + + if err != nil { + log.Println("Error reading config: ", err) + } + } + + configAutoPopulate(config) + // Always save (first time generation, new version with new variables populated) + config.Save(filename) + return config +} + +// Auto populate required values if missing and save +func configAutoPopulate(config *Config) { + if config.PrivateKeyBytes == "" { + config.generatePrivateKey() + } + + if config.ServerReporting.PeerPrivateKey == "" { + config.generatePeerPrivateKey() + } +} + func (config *Config) generatePrivateKey() { pk, err := utils.GeneratePrivateKey() if err != nil {