-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
130 lines (117 loc) · 2.69 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"flag"
"log"
"strings"
"time"
"acamar/ping"
"github.com/BurntSushi/toml"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
)
type config struct {
Timeout duration
Period duration
Log bool
Mysql struct {
Connect string
Online string
Offline string
Insert string
}
Target []Target
}
var (
conf config
db *sqlx.DB
)
func main() {
configFile := flag.String("config", "config.toml", "path to the config file")
flag.Parse()
var err error
if _, err = toml.DecodeFile(*configFile, &conf); err != nil {
log.Fatalln(err)
}
if db, err = sqlx.Open("mysql", conf.Mysql.Connect); err == nil {
db.SetConnMaxLifetime(2 * time.Minute)
db.SetMaxOpenConns(2)
db.SetMaxIdleConns(2)
} else {
log.Fatalln(err)
}
if err = db.Ping(); err != nil {
log.Fatalln("mysql:", err)
}
for _, target := range conf.Target {
go targetRoutine(target)
}
time.Sleep(time.Duration(1<<63 - 1))
}
func targetRoutine(target Target) {
if target.Period.Duration == 0 {
target.Period = conf.Period
}
tick := time.Tick(target.Period.Duration)
adapter := &DbStatusAdapter{
Name: target.Name,
Address: target.Address,
}
if _, err := db.NamedExec(conf.Mysql.Insert, adapter); err != nil {
log.Println("mysql:", err)
}
var pinger ping.Pinger
if target.Legacy {
pinger = &ping.Legacy{
Addr: target.Address,
LocalAddress: target.LocalAddress,
Timeout: conf.Timeout.Duration,
}
} else {
pinger = &ping.Standard{
Addr: target.Address,
LocalAddress: target.LocalAddress,
Timeout: conf.Timeout.Duration,
}
}
for ; true; <-tick {
status, pingError := pinger.Ping()
query := ""
if pingError != nil {
query = conf.Mysql.Offline
adapter.Online = 0
adapter.Max = 0
adapter.Latency = 0
adapter.Favicon = ""
adapter.Protocol = 0
adapter.Version = ""
adapter.Motd = ""
} else {
query = conf.Mysql.Online
adapter.Online = status.Players.Online
adapter.Max = status.Players.Max
adapter.Latency = int(status.Latency.Milliseconds())
adapter.Favicon = string(status.Favicon)
adapter.Protocol = status.Version.Protocol
adapter.Version = status.Version.Name
if strings.Contains(query, ":motd") {
adapter.Motd = status.Description.ClearString()
}
}
adapter.Time = time.Now().Unix()
if _, err := db.NamedExec(query, adapter); err != nil {
log.Println(target.Name, "mysql:", err)
} else if conf.Log {
if pingError != nil {
log.Printf(
"%s: Error: %v",
target.Name, pingError,
)
} else {
log.Printf(
"%s: Ping: %dms, Version: %s, Online: %d/%d",
target.Name, adapter.Latency, adapter.Version, adapter.Online, adapter.Max,
)
}
}
}
}