Browse Source

Merge pull request 'Remove windows pidusage monitoring' (#24) from rmWinMonitor into trunk

Reviewed-on: https://git.openprivacy.ca/cwtch.im/server/pulls/24
monitorsSql v1.3.4
Sarah Jamie Lewis 3 weeks ago
parent
commit
e6c184df4e
  1. 10
      README.md
  2. 47
      metrics/monitors.go
  3. 30
      metrics/monitors_test.go

10
README.md

@ -1,5 +1,15 @@
# Cwtch Server
## Building
Pretty straight forward:
- build the app in `app/` with `go build`
- build the docker container in `docker/` with `docker build . -t openpriv/server`
### Windows
The server package relies on sqlite which in turn requires the use of CGO. As per [this issue](https://github.com/golang/go/issues/12029) that means [TDM-GCC](https://jmeubank.github.io/tdm-gcc/download/) is required to be installed and used to compile on Windows
## Running
- cd app

47
metrics/monitors.go

@ -8,6 +8,7 @@ import (
"github.com/struCoder/pidusage"
"os"
"path"
"runtime"
"sync"
"time"
)
@ -31,8 +32,8 @@ type Monitors struct {
}
// Start initializes a Monitors's monitors
func (mp *Monitors) Start(ts tapir.Service, configDir string, log bool) {
mp.log = log
func (mp *Monitors) Start(ts tapir.Service, configDir string, doLogging bool) {
mp.log = doLogging
mp.configDir = configDir
mp.starttime = time.Now()
mp.breakChannel = make(chan bool)
@ -48,18 +49,36 @@ func (mp *Monitors) Start(ts tapir.Service, configDir string, log bool) {
})
var pidUsageLock sync.Mutex
mp.CPU = NewMonitorHistory(Percent, Average, func() float64 {
pidUsageLock.Lock()
defer pidUsageLock.Unlock()
sysInfo, _ := pidusage.GetStat(os.Getpid())
return float64(sysInfo.CPU)
})
mp.Memory = NewMonitorHistory(MegaBytes, Average, func() float64 {
pidUsageLock.Lock()
defer pidUsageLock.Unlock()
sysInfo, _ := pidusage.GetStat(os.Getpid())
return float64(sysInfo.Memory)
})
// pidusage doesn't support windows
if runtime.GOOS != "windows" {
mp.CPU = NewMonitorHistory(Percent, Average, func() float64 {
pidUsageLock.Lock()
defer pidUsageLock.Unlock()
sysInfo, err := pidusage.GetStat(os.Getpid())
if err != nil {
log.Errorf("pidusage.GetStat failed with: %s", err)
return 0.0
}
return float64(sysInfo.CPU)
})
mp.Memory = NewMonitorHistory(MegaBytes, Average, func() float64 {
pidUsageLock.Lock()
defer pidUsageLock.Unlock()
sysInfo, err := pidusage.GetStat(os.Getpid())
if err != nil {
log.Errorf("pidusage.GetStat failed with: %s", err)
return 0.0
}
return float64(sysInfo.Memory)
})
} else {
mp.CPU = NewMonitorHistory(Percent, Average, func() float64 {
return 0.0
})
mp.Memory = NewMonitorHistory(MegaBytes, Average, func() float64 {
return 0.0
})
}
// TODO: replace with ts.
mp.ClientConns = NewMonitorHistory(Count, Average, func() float64 { return float64(ts.Metrics().ConnectionCount) })

30
metrics/monitors_test.go

@ -0,0 +1,30 @@
package metrics
import (
tor2 "git.openprivacy.ca/cwtch.im/tapir/networks/tor"
"git.openprivacy.ca/openprivacy/log"
"os"
"path/filepath"
"testing"
"time"
)
func TestMonitors(t *testing.T) {
log.SetLevel(log.LevelInfo)
os.RemoveAll("testLog")
os.Mkdir("testLog", 0700)
service := new(tor2.BaseOnionService)
mp := Monitors{}
mp.Start(service, "testLog", true)
log.Infof("sleeping for minute to give to for monitors to trigger...")
// wait a minute for it to trigger
time.Sleep(62 * time.Second)
// it didn't segfault? that's good, did it create a log file?
if _, err := os.Stat(filepath.Join("testLog", "serverMonitorReport.txt")); err != nil {
t.Errorf("serverMonitorReport.txt not generated")
}
mp.Stop()
os.RemoveAll("testLog")
}
Loading…
Cancel
Save