-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
123 lines (98 loc) · 2.61 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
package main
import (
"database/sql"
"flag"
"fmt"
"log"
"os"
"os/exec"
"strings"
"time"
mysql "github.com/go-sql-driver/mysql"
"github.com/go-routeros/routeros"
)
var (
// Routeros connection parameters
useTLS = flag.Bool("tls", false, "use tls")
routerIpPort = flag.String("router-ip-port", os.Getenv("ROUTER_IP_PORT"), "mikrotik ip")
routerUser = flag.String("router-user", os.Getenv("ROUTER_USER"), "mikrotik username")
routerPwd = flag.String("router-pwd", os.Getenv("ROUTER_PWD"), "mikrotik password")
printProperties = flag.String("print-parameters", os.Getenv("PRINT_PARAMETERS"), "Properties")
// In app switches
interval = flag.Duration("interval", 1*time.Second, "Interval")
testMysqlConnection = flag.Bool("test-mysql", false, "Test mysql")
watchOnlineUsers = flag.Bool("watch-onlines", false, "Watch onlines")
// Mysql connection parameters
mysqlUser = flag.String("mysql-user", os.Getenv("MYSQL_USER"), "mysql username")
mysqlPwd = flag.String("mysql-pwd", os.Getenv("MYSQL_PWD"), "mysql password")
mysqlDb = flag.String("mysql-db", os.Getenv("MYSQL_DB"), "mysql database")
mysqlHostPort = flag.String("mysql-host-port", os.Getenv("MYSQL_HOST_PORT"), "mysql host port")
)
var db *sql.DB
func dial() (*routeros.Client, error) {
if *useTLS {
return routeros.DialTLS(*routerIpPort, *routerUser, *routerPwd, nil)
}
return routeros.Dial(*routerIpPort, *routerUser, *routerPwd)
}
func main() {
flag.Parse()
// Testing for mysql connection
if *testMysqlConnection {
testMysql()
}
if *watchOnlineUsers {
watchOnlines()
} else {
checkOnlines()
}
}
func checkOnlines() {
fmt.Println("Checking onlines")
}
func watchOnlines() {
client, err := dial()
if err != nil {
log.Fatal(err)
os.Exit(1)
}
// Print hotspot active users forevah
for {
reply, runErr := client.Run("/ip/hotspot/active/print")
if runErr != nil {
log.Fatal(runErr)
os.Exit(1)
}
clear()
for _, re := range reply.Re {
for _, p := range strings.Split(*printProperties, ",") {
fmt.Print(re.Map[p], "\t")
}
fmt.Print("\n")
}
time.Sleep(*interval)
}
}
func testMysql() {
config := mysql.Config{
User: *mysqlUser,
Passwd: *mysqlPwd,
Net: "tcp",
Addr: *mysqlHostPort,
DBName: *mysqlDb,
AllowNativePasswords: true,
}
db, dbErr := sql.Open("mysql", config.FormatDSN())
if dbErr != nil {
log.Fatal(dbErr)
os.Exit(1)
}
fmt.Println(db.Ping())
os.Exit(0)
}
func clear() {
cmdName := "clear"
cmd := exec.Command(cmdName)
cmd.Stdout = os.Stdout
cmd.Run()
}