Skip to content

Commit

Permalink
Add downtime detection&reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
rkfg committed Mar 29, 2021
1 parent 17995ad commit 169cf49
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
3 changes: 3 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ type ns2server struct {
currentMap string
avgSkill int
restartChan chan bool
failures int
downSince *time.Time
}

func (s *ns2server) playersString() string {
Expand Down Expand Up @@ -72,6 +74,7 @@ var config struct {
LevelDBPath string `json:"ldb_database_path"`
BoltDBPath string `json:"bdb_database_path"`
QueryInterval time.Duration `json:"query_interval"`
FailureLimit int `json:"failure_limit"`
Servers []*ns2server
Seeding seeding
}
Expand Down
1 change: 1 addition & 0 deletions config_sample.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"steam_key": "get your key at https://steamcommunity.com/dev/apikey",
"bdb_database_path": "/path/to/botdb/",
"query_interval": 60,
"failure_limit": 10,
"servers": [
{
"name": "Server 1",
Expand Down
20 changes: 20 additions & 0 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import (
"github.com/rumblefrog/go-a2s"
)

const (
timeFormat = "2 Jan 2006 15:04:05 -0700"
)

type queryError struct {
description string
err error
Expand Down Expand Up @@ -52,6 +56,9 @@ func serverStatus(srv *ns2server) *discordgo.MessageSend {
Footer: &discordgo.MessageEmbedFooter{Text: fmt.Sprintf("Skill: %d", srv.avgSkill)},
},
}
if srv.failures > config.FailureLimit && srv.downSince != nil {
msg.Embed.Title = fmt.Sprintf("%s [%s] currently DOWN since %s", srv.Name, srv.currentMap, srv.downSince.Format(timeFormat))
}
playersCount := len(srv.players)
if playersCount < config.Seeding.AlmostFull {
msg.Embed.Color = 0x009900
Expand Down Expand Up @@ -155,6 +162,19 @@ func query(srv *ns2server) {
err = queryServer(client, srv)
if err != nil {
log.Printf("Error: %s", err)
srv.failures++
if srv.failures > config.FailureLimit && srv.downSince == nil {
now := time.Now()
srv.downSince = &now
sendChan <- message{MessageSend: &discordgo.MessageSend{Content: fmt.Sprintf("Server %s is down!", srv.Name)}}
}
} else {
if srv.failures > config.FailureLimit && srv.downSince != nil {
sendChan <- message{MessageSend: &discordgo.MessageSend{Content: fmt.Sprintf("Server %s is back up! Was down since: %s",
srv.Name, srv.downSince.Format(timeFormat))}}
srv.downSince = nil
}
srv.failures = 0
}
if err, ok := err.(queryError); ok {
if err, ok := err.err.(*net.OpError); ok && err.Op == "write" {
Expand Down

0 comments on commit 169cf49

Please sign in to comment.