1
0
mirror of https://github.com/mgerb/ServerStatus synced 2026-01-11 19:52:49 +00:00

6 Commits

Author SHA1 Message Date
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
10 changed files with 190 additions and 77 deletions

1
.gitignore vendored
View File

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

41
Gopkg.lock generated Normal file
View File

@@ -0,0 +1,41 @@
# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.
[[projects]]
branch = "master"
name = "github.com/anvie/port-scanner"
packages = [
".",
"predictors",
"predictors/webserver"
]
revision = "8159197d3770eb6dbf3a9706a6d40462ebb69cec"
[[projects]]
name = "github.com/bwmarrin/discordgo"
packages = ["."]
revision = "4a33b9bc7c56cfdb9bb244e33e83cb3941fe2bdc"
version = "v0.18.0"
[[projects]]
name = "github.com/gorilla/websocket"
packages = ["."]
revision = "ea4d1f681babbce9545c9c5f3d5194a789c89f5b"
version = "v1.2.0"
[[projects]]
branch = "master"
name = "golang.org/x/crypto"
packages = [
"nacl/secretbox",
"poly1305",
"salsa20/salsa"
]
revision = "c4a91bd4f524f10d064139674cf55852e055ad01"
[solve-meta]
analyzer-name = "dep"
analyzer-version = 1
inputs-digest = "0832269100c492c595c4fe5f02e10d70889b9fa4d6a869c3ad59584cda0c5d31"
solver-name = "gps-cdcl"
solver-version = 1

38
Gopkg.toml Normal file
View File

@@ -0,0 +1,38 @@
# 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

View File

@@ -1,33 +1,18 @@
{ {
"Token": "your_bot_token_id", "Token": "your bot token",
"RoomIDList": ["id_1", "id_2", "id_3"], "RoomIDList":["room id list goes here"],
"RoleToNotify": "@Elysium", "RolesToNotify": ["<@&roleid>", "<@userid>"],
"GameStatus": "current playing game", "GameStatus": "current playing game",
"PollingInterval": 10,
"Servers": [ "Servers": [
{ {
"Name": "Elysium PvP", "Name": "Your awesome server",
"Address": "149.202.207.235", "Address": "game.server.com",
"Port": 8099 "Port": 80
}, }, {
{ "Name": "Another awesome server",
"Name": "Zethkur PvP", "Address": "awesome.server.com",
"Address": "151.80.103.221", "Port": 8080
"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
} }
] ]
} }

View File

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

View File

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

View File

@@ -1,14 +1,16 @@
VERSION := $(shell git describe --tags)
run: run:
go run ./src/main.go go run ./src/main.go
linux: linux:
go build -o ./dist/ServerStatus-linux ./src/main.go go build -o ./dist/ServerStatus-linux -ldflags="-X main.version=${VERSION}" ./main.go
mac: 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: 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: clean:
rm -rf ./dist rm -rf ./dist
@@ -16,4 +18,7 @@ clean:
copyfiles: copyfiles:
cp config.template.json ./dist/config.json 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,36 @@
## Server Status # Server Status
Scans a list of TCP 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. I originally made this bot to check if private World of Warcraft servers were up or not.
This bot will send a chat notification when the status of a server changes. It's actually much more useful than that and can be used for most servers.
I originally made this bot the check if private World of Warcraft servers were up or not. NOTE: This bot currently does not work for UDP servers.
This bot is actually much more useful than that and can be used for any type of server.
## Configuration ## Configuration
- Download the latest release [here](https://github.com/mgerb/ServerStatus/releases) - Download the latest release [here](https://github.com/mgerb/ServerStatus/releases)
- Add your bot token as well as other configurations to config.json - Add your bot token as well as other configurations to config.json
- Execute the OS specific binary! - Execute the OS specific binary!
## Compiling from source ### Mentioning Roles/Users
- you must first get your role/user id
- 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`
## 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 sure Go and Make are installed
- make all - make all
@@ -21,19 +38,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 https://github.com/reactiflux/discord-irc/wiki/Creating-a-discord-bot-&-getting-a-token
### How to get your room ID ### 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. 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"/> <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

@@ -1,23 +1,33 @@
package serverstatus package serverstatus
import ( import (
"../bot" "log"
"../config" "strings"
"fmt" "time"
"github.com/anvie/port-scanner" "github.com/anvie/port-scanner"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
"log" "github.com/mgerb/ServerStatus/bot"
"time" "github.com/mgerb/ServerStatus/config"
) )
const (
red = 0xf4425c
green = 0x42f477
blue = 0x42adf4
)
// Start - start port scanner and bot listeners
func Start() { func Start() {
//set each server status as online to start //set each server status as online to start
for i, _ := range config.Config.Servers { for i := range config.Config.Servers {
config.Config.Servers[i].Online = true config.Config.Servers[i].Online = true
} }
err := bot.Session.UpdateStatus(0, config.Config.GameStatus) 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 { if err != nil {
log.Println(err) log.Println(err)
} }
@@ -30,7 +40,7 @@ func scanServers() {
//check if server are in config file //check if server are in config file
if len(config.Config.Servers) < 1 { if len(config.Config.Servers) < 1 {
fmt.Println("No servers in config file.") log.Println("No servers in config file.")
return return
} }
@@ -39,29 +49,44 @@ func scanServers() {
for index, server := range config.Config.Servers { for index, server := range config.Config.Servers {
prevServerUp := server.Online //set value to previous server status prevServerUp := server.Online //set value to previous server status
serverScanner := portscanner.NewPortScanner(server.Address, time.Second*2) serverScanner := portscanner.NewPortScanner(server.Address, time.Second*2, 1)
serverUp := serverScanner.IsOpen(server.Port) //check if the port is open serverUp := serverScanner.IsOpen(server.Port) //check if the port is open
if serverUp && serverUp != prevServerUp { if serverUp && serverUp != prevServerUp {
sendMessage(config.Config.RoleToNotify + " " + server.Name + " is now online!") sendMessageToRooms(green, server.Name, "Is now online :smiley:", true)
} else if !serverUp && serverUp != prevServerUp { } else if !serverUp && serverUp != prevServerUp {
sendMessage(config.Config.RoleToNotify + " " + server.Name + " went offline!") sendMessageToRooms(red, server.Name, "Has gone offline :frowning2:", true)
} }
config.Config.Servers[index].Online = serverUp config.Config.Servers[index].Online = serverUp
} }
time.Sleep(time.Second * 5) time.Sleep(time.Second * config.Config.PollingInterval)
} }
} }
func sendMessage(message string) { func sendMessageToRooms(color int, title, description string, mentionRoles bool) {
for _, roomID := range config.Config.RoomIDList { for _, roomID := range config.Config.RoomIDList {
bot.Session.ChannelMessageSend(roomID, message) if mentionRoles {
content := strings.Join(config.Config.RolesToNotify, " ")
bot.Session.ChannelMessageSend(roomID, content)
}
sendEmbededMessage(roomID, color, title, description)
} }
} }
// This function will be called every time a new 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. // message is created on any channel that the autenticated bot has access to.
func MessageHandler(s *discordgo.Session, m *discordgo.MessageCreate) { func MessageHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
@@ -73,9 +98,9 @@ func MessageHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Content == "!ServerStatus" { if m.Content == "!ServerStatus" {
for _, server := range config.Config.Servers { for _, server := range config.Config.Servers {
if server.Online { if server.Online {
s.ChannelMessageSend(m.ChannelID, server.Name+" is online!") sendEmbededMessage(m.ChannelID, green, server.Name, "Online!")
} else { } else {
s.ChannelMessageSend(m.ChannelID, server.Name+" is down!") sendEmbededMessage(m.ChannelID, red, server.Name, "Offline!")
} }
} }
} }