Skip to content
This repository has been archived by the owner on Jul 25, 2022. It is now read-only.

Commit

Permalink
Fixed repeated down site and slow process check on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
mxmauro committed May 5, 2020
1 parent a8cfb4d commit 0645ec3
Show file tree
Hide file tree
Showing 29 changed files with 1,161 additions and 302 deletions.
2 changes: 1 addition & 1 deletion .idea/runConfigurations/ServerWatchdog.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ Designates the target Slack WebHook channel for messaage delivery. The channel f

##### `channels.{channel-name}.slack.severity`

Sets the severity type of the notification: `error`, `warn` or `info`.
Sets the severity type of the notification: `error`, `warn`, `info` or `debug`.

##### `channels.{channel-name}.email` (optional)

Expand Down Expand Up @@ -283,7 +283,7 @@ Establishes the channel to use when a notification must be sent because the chec

##### `webs[].severity`

Sets the severity type of the notification: `error`, `warn` or `info`.
Sets the severity type of the notification: `error`, `warn`, `info` or `debug`.

#### `tcpPorts` (optional)

Expand Down Expand Up @@ -311,7 +311,7 @@ Establishes the channel to use when a notification must be sent because the any

##### `tcpPorts[].severity`

Sets the severity type of the notification: `error`, `warn` or `info`.
Sets the severity type of the notification: `error`, `warn`, `info` or `debug`.

#### `processes` (optional)

Expand All @@ -335,7 +335,7 @@ Establishes the channel to use when a notification must be sent because the proc

##### `processes[].severity`

Sets the severity type of the notification: `error`, `warn` or `info`.
Sets the severity type of the notification: `error`, `warn`, `info` or `debug`.

#### `freeDiskSpace` (optional)

Expand All @@ -359,7 +359,7 @@ Establishes the channel to use when a notification must be sent because the chec

##### `freeDiskSpace[].severity`

Sets the severity type of the notification: `error`, `warn` or `info`.
Sets the severity type of the notification: `error`, `warn`, `info` or `debug`.

# License

Expand Down
63 changes: 40 additions & 23 deletions src/console/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ import (
//------------------------------------------------------------------------------

const(
classInfo int = 0
classError int = 0
classWarn = 1
classError = 2
classInfo = 2
classDebug = 3
)

//------------------------------------------------------------------------------
Expand All @@ -34,8 +35,8 @@ func SetupService(s service.Service) error {
return err
}

func Info(format string, a ...interface{}) {
printCommon("", classInfo, getTimestamp(), fmt.Sprintf(format, a...))
func Error(format string, a ...interface{}) {
printCommon("", classError, getTimestamp(), fmt.Sprintf(format, a...))
return
}

Expand All @@ -44,13 +45,18 @@ func Warn(format string, a ...interface{}) {
return
}

func Error(format string, a ...interface{}) {
printCommon("", classError, getTimestamp(), fmt.Sprintf(format, a...))
func Info(format string, a ...interface{}) {
printCommon("", classInfo, getTimestamp(), fmt.Sprintf(format, a...))
return
}

func LogInfo(title string, timestamp string, msg string) {
printCommon(title, classInfo, timestamp, msg)
func Debug(format string, a ...interface{}) {
printCommon("", classDebug, getTimestamp(), fmt.Sprintf(format, a...))
return
}

func LogError(title string, timestamp string, msg string) {
printCommon(title, classError, timestamp, msg)
return
}

Expand All @@ -59,8 +65,13 @@ func LogWarn(title string, timestamp string, msg string) {
return
}

func LogError(title string, timestamp string, msg string) {
printCommon(title, classError, timestamp, msg)
func LogInfo(title string, timestamp string, msg string) {
printCommon(title, classInfo, timestamp, msg)
return
}

func LogDebug(title string, timestamp string, msg string) {
printCommon(title, classDebug, timestamp, msg)
return
}

Expand All @@ -71,7 +82,7 @@ func printCommon(title string, cls int, timestamp string, msg string) {
m.Lock()
defer m.Unlock()

if cls == classInfo {
if cls == classInfo || cls == classDebug {
color.SetOutput(os.Stdout)
} else {
color.SetOutput(os.Stderr)
Expand All @@ -80,12 +91,14 @@ func printCommon(title string, cls int, timestamp string, msg string) {
color.Printf("%v ", timestamp)

switch cls {
case classInfo:
color.Info.Print("[INFO]")
case classWarn:
color.Warn.Print("[WARN]")
case classError:
color.Error.Print("[ERROR]")
case classWarn:
color.Warn.Print("[WARN]")
case classInfo:
color.Info.Print("[INFO]")
case classDebug:
color.Debug.Print("[DEBUG]")
}

if len(title) > 0 {
Expand All @@ -98,21 +111,25 @@ func printCommon(title string, cls int, timestamp string, msg string) {
} else {
if len(title) > 0 {
switch cls {
case classInfo:
(*serviceLogger).Infof("[INFO] %v - %v", title, msg)
case classWarn:
(*serviceLogger).Warningf("[WARN] %v - %v", title, msg)
case classError:
(*serviceLogger).Errorf("[ERROR] %v - %v", title, msg)
case classWarn:
(*serviceLogger).Warningf("[WARN] %v - %v", title, msg)
case classInfo:
(*serviceLogger).Infof("[INFO] %v - %v", title, msg)
case classDebug:
(*serviceLogger).Infof("[DEBUG] %v - %v", title, msg)
}
} else {
switch cls {
case classInfo:
(*serviceLogger).Infof("[INFO] - %v", msg)
case classWarn:
(*serviceLogger).Warningf("[WARN] - %v", msg)
case classError:
(*serviceLogger).Errorf("[ERROR] - %v", msg)
case classWarn:
(*serviceLogger).Warningf("[WARN] - %v", msg)
case classInfo:
(*serviceLogger).Infof("[INFO] - %v", msg)
case classDebug:
(*serviceLogger).Infof("[DEBUG] - %v", msg)
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ require (
github.com/vmihailenco/msgpack/v4 v4.2.1
google.golang.org/appengine v1.6.5 // indirect
)

replace github.com/shirou/gopsutil v2.19.12+incompatible => github.com/mxmauro/gopsutil v2.20.4+incompatible
5 changes: 5 additions & 0 deletions src/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/djherbis/atime v1.0.0/go.mod h1:5W+KBIuTwVGcqjIfaTwt+KSYX1o6uep8dtevevQP/f8=
Expand Down Expand Up @@ -209,6 +210,8 @@ github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3Rllmb
github.com/mschoch/smat v0.0.0-20160514031455-90eadee771ae h1:VeRdUYdCw49yizlSbMEn2SZ+gT+3IUKx8BqxyQdz+BY=
github.com/mschoch/smat v0.0.0-20160514031455-90eadee771ae/go.mod h1:qAyveg+e4CE+eKJXWVjKXM4ck2QobLqTDytGJbLLhJg=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/mxmauro/gopsutil v2.20.4+incompatible h1:PQMCZFzCD9A6hlXu3fLB5eCb/N8NqmH97S6iTPUB8SM=
github.com/mxmauro/gopsutil v2.20.4+incompatible/go.mod h1:aGKuDiZnqaSzhEZAyzPR5gbtmTYuI7Iwcz1mb+qhkDY=
github.com/nats-io/gnatsd v1.4.1/go.mod h1:nqco77VO78hLCJpIcVfygDP2rPGfsEHkGTUk94uh5DQ=
github.com/nats-io/go-nats v1.7.2/go.mod h1:+t7RHT5ApZebkrQdnn6AhQJmhJJiKAvJUio1PiiCtj0=
github.com/nats-io/go-nats-streaming v0.4.4/go.mod h1:gfq4R3c9sKAINOpelo0gn/b9QDMBZnmrttcsNF+lqyo=
Expand Down Expand Up @@ -286,6 +289,7 @@ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/tidwall/gjson v1.3.5/go.mod h1:P256ACg0Mn+j1RXIDXoss50DeIABTYK1PULOJHhxOls=
github.com/tidwall/match v1.0.1/go.mod h1:LujAq0jyVjBy028G1WhWfIzbpQfMO8bBZ6Tyb0+pL9E=
Expand Down Expand Up @@ -431,6 +435,7 @@ gopkg.in/urfave/cli.v1 v1.20.0/go.mod h1:vuBzUtMdQeixQj8LVd+/98pzhxNGQoyuPBlsXHO
gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
2 changes: 1 addition & 1 deletion src/modules/backend/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func onPostWatchProcess(ctx *server.RequestCtx) {
}

//add to watch list
err = processwatcher.AddProcess(r.Pid, r.Name, r.Severity, r.Channel)
err = processwatcher.AddProcess(r.Pid, r.Name, r.MaxMemUsage, r.Severity, r.Channel)
if err != nil {
server.SendBadRequest(ctx, err.Error())
return
Expand Down
9 changes: 5 additions & 4 deletions src/modules/backend/handlers/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ type NotifyRequest struct {
}

type WatchProcessRequest struct {
Channel string `json:"channel"`
Pid int `json:"pid"`
Name string `json:"name,omitempty"`
Severity string `json:"severity,omitempty"`
Channel string `json:"channel"`
Pid int `json:"pid"`
MaxMemUsage string `json:"maxMem,omitempty"`
Name string `json:"name,omitempty"`
Severity string `json:"severity,omitempty"`
}

type UnwatchProcessRequest struct {
Expand Down
13 changes: 9 additions & 4 deletions src/modules/logger/email/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ func Run(wg sync.WaitGroup) {
return
}

func Info(channel string, timestamp string, msg string) {
emailModule.sendEmailNotification(channel, "[INFO]", timestamp, msg)
func Error(channel string, timestamp string, msg string) {
emailModule.sendEmailNotification(channel, "[ERROR]", timestamp, msg)
return
}

Expand All @@ -76,8 +76,13 @@ func Warn(channel string, timestamp string, msg string) {
return
}

func Error(channel string, timestamp string, msg string) {
emailModule.sendEmailNotification(channel, "[ERROR]", timestamp, msg)
func Info(channel string, timestamp string, msg string) {
emailModule.sendEmailNotification(channel, "[INFO]", timestamp, msg)
return
}

func Debug(channel string, timestamp string, msg string) {
emailModule.sendEmailNotification(channel, "[DEBUG]", timestamp, msg)
return
}

Expand Down
13 changes: 9 additions & 4 deletions src/modules/logger/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ func Run(wg sync.WaitGroup) {
return
}

func Info(channel string, timestamp string, msg string) {
fileModule.writeFileLog(channel, "[INFO]", timestamp, msg)
func Error(channel string, timestamp string, msg string) {
fileModule.writeFileLog(channel, "[ERROR]", timestamp, msg)
return
}

Expand All @@ -146,8 +146,13 @@ func Warn(channel string, timestamp string, msg string) {
return
}

func Error(channel string, timestamp string, msg string) {
fileModule.writeFileLog(channel, "[ERROR]", timestamp, msg)
func Info(channel string, timestamp string, msg string) {
fileModule.writeFileLog(channel, "[INFO]", timestamp, msg)
return
}

func Debug(channel string, timestamp string, msg string) {
fileModule.writeFileLog(channel, "[DEBUG]", timestamp, msg)
return
}

Expand Down
14 changes: 14 additions & 0 deletions src/modules/logger/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ func Log(severity string, channel string, format string, a ...interface{}) error
case "info":
LogInfo(channel, format, a...)

case "debug":
LogDebug(channel, format, a...)

default:
return errors.New("Invalid severity")
}
Expand Down Expand Up @@ -94,6 +97,17 @@ func LogInfo(channel string, format string, a ...interface{}) {
return
}

func LogDebug(channel string, format string, a ...interface{}) {
timestamp := getTimestamp()
msg := fmt.Sprintf(format, a...)

console.LogDebug(channel, timestamp, msg)
file.Debug(channel, timestamp, msg)
slack.Debug(channel, timestamp, msg)
email.Debug(channel, timestamp, msg)
return
}

func getTimestamp() string {
now := time.Now()
if !settings.Config.Log.UseLocalTime {
Expand Down
13 changes: 9 additions & 4 deletions src/modules/logger/slack/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ func Run(wg sync.WaitGroup) {
return
}

func Info(channel string, timestamp string, msg string) {
slackModule.sendSlackNotification(channel, "[INFO]", timestamp, msg)
func Error(channel string, timestamp string, msg string) {
slackModule.sendSlackNotification(channel, "[ERROR]", timestamp, msg)
return
}

Expand All @@ -80,8 +80,13 @@ func Warn(channel string, timestamp string, msg string) {
return
}

func Error(channel string, timestamp string, msg string) {
slackModule.sendSlackNotification(channel, "[ERROR]", timestamp, msg)
func Info(channel string, timestamp string, msg string) {
slackModule.sendSlackNotification(channel, "[INFO]", timestamp, msg)
return
}

func Debug(channel string, timestamp string, msg string) {
slackModule.sendSlackNotification(channel, "[DEBUG", timestamp, msg)
return
}

Expand Down
9 changes: 5 additions & 4 deletions src/modules/processwatcher/processwatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type ProcessItem struct {
Name string
Channel string
Severity string
MaxMemUsage string
}

//------------------------------------------------------------------------------
Expand Down Expand Up @@ -115,7 +116,7 @@ func Run(wg sync.WaitGroup) {
return
}

func AddProcess(pid int, name string, severity string, channel string) error {
func AddProcess(pid int, name string, maxMemUsage string, severity string, channel string) error {
lock.RLock()
localModule := module
lock.RUnlock()
Expand All @@ -126,7 +127,7 @@ func AddProcess(pid int, name string, severity string, channel string) error {

var err error = nil
if localModule.r.Acquire() {
err = localModule.addProcessInternal(pid, name, severity, channel)
err = localModule.addProcessInternal(pid, name, maxMemUsage, severity, channel)
if err == nil {
localModule.runSaveState()
} else {
Expand Down Expand Up @@ -182,7 +183,7 @@ func RemoveProcess(pid int, channel string) error {

//------------------------------------------------------------------------------

func (m *Module) addProcessInternal(pid int, name string, severity string, channel string) error {
func (m *Module) addProcessInternal(pid int, name string, maxMemUsage string, severity string, channel string) error {
var i int
var err error = nil

Expand Down Expand Up @@ -364,7 +365,7 @@ func (m *Module) checkForNewProcesses() {
name = filepath.Base(exeName)
}

err = m.addProcessInternal(int(proc.Pid), name, cfgProc.Severity, cfgProc.Channel)
err = m.addProcessInternal(int(proc.Pid), name, "", cfgProc.Severity, cfgProc.Channel)
if err != nil {
console.Error("Unable to watch process #%v (%v) [%v]", int(proc.Pid), name, err.Error())
}
Expand Down
Loading

0 comments on commit 0645ec3

Please sign in to comment.