1
0
mirror of https://github.com/mgerb/ServerStatus synced 2026-01-11 11:42:50 +00:00

9 Commits

Author SHA1 Message Date
92d94ed4d4 feat(RCON): added RCON support for UDP servers 2019-01-19 20:13:55 -06:00
d0cf0eae78 Merge pull request #23 from mgerb/development
added docker support
2018-10-04 19:10:23 -05:00
642ccc7bf5 added docker support 2018-10-04 19:07:30 -05:00
bcb57c72a3 add polling interval to configurations - resolves #15 2018-07-18 20:22:37 -05:00
1cd9f139c2 Merge pull request #13 from mgerb/mitchell
added embedded messages resolves #9
2018-03-15 21:07:58 -05:00
8d26fba95d added embedded messages resolves #9 2018-03-15 21:06:08 -05:00
217c6c8baa update dependencies - change imports 2018-01-13 12:29:20 -06:00
5a2f81a23a Update readme.md 2017-08-07 23:30:33 -05:00
f8e552caa6 update for felmyst 2017-07-17 19:52:57 -05:00
16 changed files with 375 additions and 148 deletions

2
.dockerignore Normal file
View File

@@ -0,0 +1,2 @@
/vendor
/dist

1
.gitignore vendored
View File

@@ -1,2 +1,3 @@
config.json
dist
vendor

17
Dockerfile Normal file
View File

@@ -0,0 +1,17 @@
FROM golang:1.11.1-alpine
WORKDIR /go/src/github.com/mgerb/ServerStatus
ADD . .
RUN apk add --no-cache git alpine-sdk
RUN go get -u github.com/golang/dep/cmd/dep
RUN dep ensure
RUN make linux
FROM alpine:3.8
RUN apk update && apk add ca-certificates && rm -rf /var/cache/apk/*
WORKDIR /server-status
COPY --from=0 /go/src/github.com/mgerb/ServerStatus/dist/ServerStatus-linux .
ENTRYPOINT ./ServerStatus-linux

89
Gopkg.lock generated Normal file
View File

@@ -0,0 +1,89 @@
# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.
[[projects]]
digest = "1:87c2e02fb01c27060ccc5ba7c5a407cc91147726f8f40b70cceeedbc52b1f3a8"
name = "github.com/Sirupsen/logrus"
packages = ["."]
pruneopts = "UT"
revision = "e1e72e9de974bd926e5c56f83753fba2df402ce5"
version = "v1.3.0"
[[projects]]
branch = "master"
digest = "1:b7e05c8da029b985907b15e126cde5f42f2814b08211a184dac436eeec6fe780"
name = "github.com/anvie/port-scanner"
packages = [
".",
"predictors",
"predictors/webserver",
]
pruneopts = "UT"
revision = "8159197d3770eb6dbf3a9706a6d40462ebb69cec"
[[projects]]
digest = "1:4fd5ce7844c22e194005b9e12fee8adc70fb5ba0bbba9e1964d2e3d1f301d789"
name = "github.com/bwmarrin/discordgo"
packages = ["."]
pruneopts = "UT"
revision = "4a33b9bc7c56cfdb9bb244e33e83cb3941fe2bdc"
version = "v0.18.0"
[[projects]]
digest = "1:43dd08a10854b2056e615d1b1d22ac94559d822e1f8b6fcc92c1a1057e85188e"
name = "github.com/gorilla/websocket"
packages = ["."]
pruneopts = "UT"
revision = "ea4d1f681babbce9545c9c5f3d5194a789c89f5b"
version = "v1.2.0"
[[projects]]
branch = "master"
digest = "1:50993a8fbb3042b88dfecb8f4473a42f13ac70d6fd86f64df525a28b357c2d5b"
name = "github.com/kidoman/go-steam"
packages = ["."]
pruneopts = "UT"
revision = "2e40e0d508cbac591bab4ae18b231153295f3a0a"
[[projects]]
digest = "1:0a69a1c0db3591fcefb47f115b224592c8dfa4368b7ba9fae509d5e16cdc95c8"
name = "github.com/konsorten/go-windows-terminal-sequences"
packages = ["."]
pruneopts = "UT"
revision = "5c8c8bd35d3832f5d134ae1e1e375b69a4d25242"
version = "v1.0.1"
[[projects]]
branch = "master"
digest = "1:e035fb07be76ce6e4ce005add75491d9f0749403f595f153d6e2823a71c24149"
name = "golang.org/x/crypto"
packages = [
"nacl/secretbox",
"poly1305",
"salsa20/salsa",
"ssh/terminal",
]
pruneopts = "UT"
revision = "c4a91bd4f524f10d064139674cf55852e055ad01"
[[projects]]
branch = "master"
digest = "1:43cde116ff48f299eddb7e6515677e6d0a2c915854bb05a333877f07c3bb3033"
name = "golang.org/x/sys"
packages = [
"unix",
"windows",
]
pruneopts = "UT"
revision = "11f53e03133963fb11ae0588e08b5e0b85be8be5"
[solve-meta]
analyzer-name = "dep"
analyzer-version = 1
input-imports = [
"github.com/anvie/port-scanner",
"github.com/bwmarrin/discordgo",
"github.com/kidoman/go-steam",
]
solver-name = "gps-cdcl"
solver-version = 1

42
Gopkg.toml Normal file
View File

@@ -0,0 +1,42 @@
# Gopkg.toml example
#
# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"
#
# [prune]
# non-go = false
# go-tests = true
# unused-packages = true
[[constraint]]
branch = "master"
name = "github.com/anvie/port-scanner"
[[constraint]]
name = "github.com/bwmarrin/discordgo"
version = "0.18.0"
[prune]
go-tests = true
unused-packages = true
[[constraint]]
branch = "master"
name = "github.com/kidoman/go-steam"

View File

@@ -1,33 +1,18 @@
{
"Token": "your_bot_token_id",
"RoomIDList": ["id_1", "id_2", "id_3"],
"RoleToNotify": "@Elysium",
"Token": "your bot token",
"RoomIDList":["room id list goes here"],
"RolesToNotify": ["<@&roleid>", "<@userid>"],
"GameStatus": "current playing game",
"PollingInterval": 10,
"Servers": [
{
"Name": "Elysium PvP",
"Address": "149.202.207.235",
"Port": 8099
},
{
"Name": "Zethkur PvP",
"Address": "151.80.103.221",
"Port": 8093
},
{
"Name": "Anathema PvP",
"Address": "149.202.211.5",
"Port": 8095
},
{
"Name": "Darrowshire PvE",
"Address": "164.132.233.125",
"Port": 8097
},
{
"Name": "Elysium Authentication Server",
"Address": "logon.elysium-project.org",
"Port": 3724
"Name": "Your awesome server",
"Address": "game.server.com",
"Port": 80
}, {
"Name": "Another awesome server",
"Address": "awesome.server.com",
"Port": 8080
}
]
}
}

View File

@@ -2,20 +2,23 @@ package config
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"time"
)
// Variables used for command line parameters
var Config configStruct
type configStruct struct {
Token string `json:"Token"`
RoomIDList []string `json:"RoomIDList"`
RoleToNotify string `json:"RoleToNotify"`
Servers []server `json:"Servers"`
GameStatus string `json:"GameStatus"`
Token string `json:"Token"`
RoomIDList []string `json:"RoomIDList"`
RolesToNotify []string `json:"RolesToNotify"`
Servers []server `json:"Servers"`
GameStatus string `json:"GameStatus"`
PollingInterval time.Duration `json:"PollingInterval"`
}
type server struct {
@@ -27,7 +30,7 @@ type server struct {
func Configure() {
log.Println("Reading config file...")
fmt.Println("Reading config file...")
file, e := ioutil.ReadFile("./config.json")
@@ -36,12 +39,14 @@ func Configure() {
os.Exit(1)
}
log.Printf("%s\n", string(file))
err := json.Unmarshal(file, &Config)
if err != nil {
log.Println(err)
}
if Config.PollingInterval == 0 {
log.Fatal("Please set your PollingInterval > 0 in your config file.")
}
}

5
docker-build.sh Executable file
View File

@@ -0,0 +1,5 @@
version=$(git describe --tags)
docker build -t mgerb/server-status:latest .
docker tag mgerb/server-status:latest mgerb/server-status:$version

7
docker-compose.yml Normal file
View File

@@ -0,0 +1,7 @@
version: "2"
services:
server-status:
image: mgerb/server-status:latest
volumes:
- ./config.json:/server-status/config.json

5
docker-push.sh Executable file
View File

@@ -0,0 +1,5 @@
version=$(git describe --tags)
docker push mgerb/server-status:latest;
docker push mgerb/server-status:$version;

View File

@@ -1,15 +1,18 @@
package main
import (
"./bot"
"./config"
"./serverstatus"
"fmt"
"github.com/mgerb/ServerStatus/bot"
"github.com/mgerb/ServerStatus/config"
"github.com/mgerb/ServerStatus/serverstatus"
)
// Variables used for command line parameters
var (
BotID string
)
var version = "undefined"
func init() {
fmt.Println("Starting Server Status " + version)
}
func main() {
//read config file

View File

@@ -1,14 +1,16 @@
VERSION := $(shell git describe --tags)
run:
go run ./src/main.go
linux:
go build -o ./dist/ServerStatus-linux ./src/main.go
go build -o ./dist/ServerStatus-linux -ldflags="-X main.version=${VERSION}" ./main.go
mac:
GOOS=darwin GOARCH=amd64 go build -o ./dist/ServerStatus-mac ./src/main.go
GOOS=darwin GOARCH=amd64 go build -o ./dist/ServerStatus-mac -ldflags="-X main.version=${VERSION}" ./main.go
windows:
GOOS=windows GOARCH=386 go build -o ./dist/ServerStatus-windows.exe ./src/main.go
GOOS=windows GOARCH=386 go build -o ./dist/ServerStatus-windows.exe -ldflags="-X main.version=${VERSION}" ./main.go
clean:
rm -rf ./dist
@@ -16,4 +18,7 @@ clean:
copyfiles:
cp config.template.json ./dist/config.json
all: linux mac windows copyfiles
zip:
zip -r dist.zip dist
all: linux mac windows copyfiles

View File

@@ -1,19 +1,52 @@
## Server Status
# Server Status
Scans a list of servers checking checking which are currently online.
This bot will send a chat notification when the status of a server changes (goes on or offline).
Scans a list of servers checking whether the ports are open or not.
This bot will send a chat notification when the status of a server changes.
I originally made this bot the check if private World of Warcraft servers were up or not.
This bot is actually much more useful than that and can be used for any type of server.
- **TCP** - should work with all servers
- **UDP** - [Source RCON Protocol](https://developer.valvesoftware.com/wiki/Source_RCON_Protocol) is supported
## Configuration
- Download the latest release [here](https://github.com/mgerb/ServerStatus/releases)
- Add your bot token as well as other configurations to config.json
- Execute the OS specific binary!
## Compiling from source
### Mentioning Roles/Users
- you must first get your role/user id (see below of obtaining ID's)
- for user `<@userid>`
- for role `<@&roleid>`
### Polling Interval
The polling interval is how often the bot will try to ping the servers.
A good interval is 10 seconds, but this may need some adjustment if
it happens to be spamming notifications.
- time in seconds
- configurable in `config.json`
## With Docker
```
docker run -it -v /path/to/your/config.json:/server-status/config.json:ro mgerb/server-status
```
### Docker Compose
```
version: "2"
services:
server-status:
image: mgerb/server-status:latest
volumes:
- /path/to/your/config.json:/server-status/config.json
```
## Usage
To get the current status of your servers simply type `!ServerStatus` in chat.
![Server Status](https://i.imgur.com/ZzQSBJp.png)
## Compiling from source
- Make sure Go and Make are installed
- make all
@@ -21,19 +54,7 @@ This bot is actually much more useful than that and can be used for any type of
https://github.com/reactiflux/discord-irc/wiki/Creating-a-discord-bot-&-getting-a-token
### How to get your room ID
To get IDs, turn on Developer Mode in the Discord client (User Settings -> Appearance) and then right-click your name/icon anywhere in the client and select Copy ID.
<img src="https://camo.githubusercontent.com/9f759ec8b45a6e9dd2242bc64c82897c74f84a25/687474703a2f2f692e696d6775722e636f6d2f47684b70424d512e676966"/>
## List server status in discord channel
`!ServerStatus`
```
Elysium PvP is online!
Zethkur PvP is online!
Anathema PvP is online!
Darrowshire PvE is online!
Elysium Authentication Server is online!
```

View File

@@ -0,0 +1,122 @@
package serverstatus
import (
"log"
"strconv"
"strings"
"time"
"github.com/anvie/port-scanner"
"github.com/bwmarrin/discordgo"
steam "github.com/kidoman/go-steam"
"github.com/mgerb/ServerStatus/bot"
"github.com/mgerb/ServerStatus/config"
)
const (
red = 0xf4425c
green = 0x42f477
blue = 0x42adf4
)
// Start - start port scanner and bot listeners
func Start() {
//set each server status as online to start
for i := range config.Config.Servers {
config.Config.Servers[i].Online = true
}
err := bot.Session.UpdateStatus(0, config.Config.GameStatus)
sendMessageToRooms(blue, "Server Status", "Bot started! Type !ServerStatus to see the status of your servers :smiley:", false)
if err != nil {
log.Println(err)
}
//start a new go routine
go scanServers()
}
func scanServers() {
//check if server are in config file
if len(config.Config.Servers) < 1 {
log.Println("No servers in config file.")
return
}
for {
for index, server := range config.Config.Servers {
prevServerUp := server.Online //set value to previous server status
serverScanner := portscanner.NewPortScanner(server.Address, time.Second*2, 1)
serverUp := serverScanner.IsOpen(server.Port) //check if the port is open
// if server isn't up check RCON protocol (UDP)
if !serverUp {
host := server.Address + ":" + strconv.Itoa(server.Port)
steamConnection, err := steam.Connect(host)
if err == nil {
defer steamConnection.Close()
_, err := steamConnection.Ping()
if err == nil {
serverUp = true
}
}
}
if serverUp && serverUp != prevServerUp {
sendMessageToRooms(green, server.Name, "Is now online :smiley:", true)
} else if !serverUp && serverUp != prevServerUp {
sendMessageToRooms(red, server.Name, "Has gone offline :frowning2:", true)
}
config.Config.Servers[index].Online = serverUp
}
time.Sleep(time.Second * config.Config.PollingInterval)
}
}
func sendMessageToRooms(color int, title, description string, mentionRoles bool) {
for _, roomID := range config.Config.RoomIDList {
if mentionRoles {
content := strings.Join(config.Config.RolesToNotify, " ")
bot.Session.ChannelMessageSend(roomID, content)
}
sendEmbededMessage(roomID, color, title, description)
}
}
func sendEmbededMessage(roomID string, color int, title, description string) {
embed := &discordgo.MessageEmbed{
Color: color,
Title: title,
Description: description,
}
bot.Session.ChannelMessageSendEmbed(roomID, embed)
}
// MessageHandler will be called every time a new
// message is created on any channel that the autenticated bot has access to.
func MessageHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
// Ignore all messages created by the bot itself
if m.Author.ID == bot.BotID {
return
}
if m.Content == "!ServerStatus" {
for _, server := range config.Config.Servers {
if server.Online {
sendEmbededMessage(m.ChannelID, green, server.Name, "Online!")
} else {
sendEmbededMessage(m.ChannelID, red, server.Name, "Offline!")
}
}
}
}

View File

@@ -1,82 +0,0 @@
package serverstatus
import (
"../bot"
"../config"
"fmt"
"github.com/anvie/port-scanner"
"github.com/bwmarrin/discordgo"
"log"
"time"
)
func Start() {
//set each server status as online to start
for i, _ := range config.Config.Servers {
config.Config.Servers[i].Online = true
}
err := bot.Session.UpdateStatus(0, config.Config.GameStatus)
if err != nil {
log.Println(err)
}
//start a new go routine
go scanServers()
}
func scanServers() {
//check if server are in config file
if len(config.Config.Servers) < 1 {
fmt.Println("No servers in config file.")
return
}
for {
for index, server := range config.Config.Servers {
prevServerUp := server.Online //set value to previous server status
serverScanner := portscanner.NewPortScanner(server.Address, time.Second*2)
serverUp := serverScanner.IsOpen(server.Port) //check if the port is open
if serverUp && serverUp != prevServerUp {
sendMessage(config.Config.RoleToNotify + " " + server.Name + " is now online!")
} else if !serverUp && serverUp != prevServerUp {
sendMessage(config.Config.RoleToNotify + " " + server.Name + " went offline!")
}
config.Config.Servers[index].Online = serverUp
}
time.Sleep(time.Second * 5)
}
}
func sendMessage(message string) {
for _, roomID := range config.Config.RoomIDList {
bot.Session.ChannelMessageSend(roomID, message)
}
}
// This function will be called every time a new
// message is created on any channel that the autenticated bot has access to.
func MessageHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
// Ignore all messages created by the bot itself
if m.Author.ID == bot.BotID {
return
}
if m.Content == "!ServerStatus" {
for _, server := range config.Config.Servers {
if server.Online {
s.ChannelMessageSend(m.ChannelID, server.Name+" is online!")
} else {
s.ChannelMessageSend(m.ChannelID, server.Name+" is down!")
}
}
}
}