-
Notifications
You must be signed in to change notification settings - Fork 19
/
machines.go
81 lines (66 loc) · 1.51 KB
/
machines.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
package main
import (
"os/exec"
"strings"
"time"
"github.com/go-telegram-bot-api/telegram-bot-api"
"gobot.io/x/gobot/drivers/gpio"
)
//Rig structure
type Rig struct {
name string
pin *gpio.RelayDriver
ip string
info string
}
//CheckMachines - ping machines, if there is no responce >> hard-reset
func CheckMachines(r []Rig) {
log.Notice("Checking machines: ")
for i := 0; i < len(r); i++ {
log.Notice("Ping machine: ", r[i].name, "ip: ", r[i].ip)
if !r[i].Ping() {
r[i].Restarter()
}
}
log.Notice("Checking machines DONE\n----------------------")
log.Notice("Starting timer")
}
//Ping IP from Linux shell
func (r *Rig) Ping() bool {
out, _ := exec.Command("ping", r.ip, "-c 3", "-i 3", "-w 10").Output()
if strings.Contains(string(out), "100% packet loss") {
log.Error("HOST NOT FOUND: ", r.name, r.ip)
return false
}
log.Notice("HOST IS ONLINE: ", r.name)
return true
}
//ForceShutDown machine
func (r *Rig) ForceShutDown() {
r.pin.Off()
time.Sleep(5 * time.Second)
r.pin.On()
}
//TurnOn machine
func (r *Rig) TurnOn() {
r.pin.Off()
time.Sleep(108 * time.Millisecond)
r.pin.On()
}
//Restarter function logic
func (r *Rig) Restarter() {
log.Warning("Restarting: ", r.name)
if Config.RemoteNotify {
if Config.Pushover {
PushoverNotify(r)
}
if Config.TgBotActivate {
//TODO: telegram notification
tgbotapi.NewMessageToChannel(Config.TgAdminUserName, "TEST MSG")
}
}
r.ForceShutDown()
time.Sleep(5 * time.Second)
r.TurnOn()
log.Warning("Machine restarted: ", r.name)
}