-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
173 lines (151 loc) · 3.75 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package main
import (
"fmt"
"io/ioutil"
"log"
//"os/exec"
tb "gopkg.in/tucnak/telebot.v2"
"net/http"
"strconv"
"strings"
"time"
)
func main() {
// read token from file
tok, err := ioutil.ReadFile("token.txt")
if err != nil {
fmt.Print(err)
}
token := strings.TrimSpace(string(tok))
// connect with telegram bot api
b, err := tb.NewBot(tb.Settings{
Token: token,
Poller: &tb.LongPoller{Timeout: 10 * time.Second},
})
if err != nil {
log.Fatal(err)
return
}
user := &tb.User{}
b.Handle("hello", func(m *tb.Message) {
user = m.Sender
b.Send(m.Sender, fmt.Sprintf("hello %s! I will send you alerts now.", user.Username))
})
b.Handle("ip", func(m *tb.Message) {
myExternalIp := getExternalIp()
b.Send(m.Sender, myExternalIp)
})
b.Handle("cpu", func(m *tb.Message) {
cpuUsage := getCPUUsage()
b.Send(m.Sender, cpuUsage)
})
b.Handle("memory", func(m *tb.Message) {
memoryUsage := getMemoryUsage()
//if m.Sender.Username {
fmt.Printf(m.Sender.Username)
//}
b.Send(m.Sender, memoryUsage)
})
// catchall for unknow commands
b.Handle(tb.OnText, func(m *tb.Message) {
b.Send(m.Sender, "??")
})
ticker := time.NewTicker(5 * time.Second)
quit := make(chan struct{})
go func() {
for {
select {
case <-ticker.C:
b.Send(user, "example alert", &tb.SendOptions{})
doWebMonitorCheck()
case <-quit:
ticker.Stop()
return
}
}
}()
// start telegram bot after ticker
b.Start()
}
func doWebMonitorCheck() {
fmt.Println("checking...")
}
func getExternalIp() string {
bodyString := "(empty)"
resp, err := http.Get("http://ipinfo.io/ip")
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
bodyBytes, err2 := ioutil.ReadAll(resp.Body)
if err2 != nil {
log.Fatal(err2)
}
bodyString = string(bodyBytes)
return bodyString
}
return bodyString
}
func getCPUSample() (idle, total uint64) {
contents, err := ioutil.ReadFile("/proc/stat")
if err != nil {
return
}
lines := strings.Split(string(contents), "\n")
for _, line := range lines {
fields := strings.Fields(line)
if fields[0] == "cpu" {
numFields := len(fields)
for i := 1; i < numFields; i++ {
val, err := strconv.ParseUint(fields[i], 10, 64)
if err != nil {
fmt.Println("Error: ", i, fields[i], err)
}
total += val // tally up all the numbers to get total ticks
if i == 4 { // idle is the 5th field in the cpu line
idle = val
}
}
return
}
}
return
}
func getCPUUsage() string {
idle0, total0 := getCPUSample()
time.Sleep(3 * time.Second)
idle1, total1 := getCPUSample()
idleTicks := float64(idle1 - idle0)
totalTicks := float64(total1 - total0)
cpuUsage := 100 * (totalTicks - idleTicks) / totalTicks
return fmt.Sprintf("CPU usage is %f%% [busy: %f, total: %f]\n", cpuUsage, totalTicks-idleTicks, totalTicks)
}
func getMemoryUsage() string {
meminfoContents, err := ioutil.ReadFile("/proc/meminfo")
if err != nil {
errMsg := "error reading '/proc/meminfo'"
return errMsg
}
lines := strings.Split(string(meminfoContents), "\n")
MemTotal := 0
MemFree := 0
for _, line := range lines {
if strings.Split(string(line), ":")[0] == "MemTotal" {
memtotal, err := strconv.Atoi(strings.TrimSpace(strings.Replace((strings.Split(string(line), ":")[1]), "kB", "", -1)))
if err != nil {
fmt.Println(err)
}
MemTotal = memtotal
}
if strings.Split(string(line), ":")[0] == "MemFree" {
memfree, err := strconv.Atoi(strings.TrimSpace(strings.Replace((strings.Split(string(line), ":")[1]), "kB", "", -1)))
if err != nil {
fmt.Println(err)
}
MemFree = memfree
}
}
MemUsage := strconv.FormatFloat((float64(MemTotal)-float64(MemFree))/float64(MemTotal), 'f', 6, 64)
return fmt.Sprintf("%s%% of memory used", MemUsage)
}