-
Notifications
You must be signed in to change notification settings - Fork 4
/
util.go
108 lines (87 loc) · 2.98 KB
/
util.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
package main
import (
"fmt"
"os/exec"
"strings"
"syscall"
"time"
st "github.com/meinside/rpi-tools/status"
)
// calculates uptime of this bot
func uptimeSince(launched time.Time) (uptime string) {
now := time.Now()
gap := now.Sub(launched)
uptimeSeconds := int(gap.Seconds())
numDays := uptimeSeconds / (60 * 60 * 24)
numHours := (uptimeSeconds % (60 * 60 * 24)) / (60 * 60)
return fmt.Sprintf("*%d* day(s) *%d* hour(s)", numDays, numHours)
}
// calculates memory usage of this bot
func memoryUsage() (usage string) {
sys, heap := st.MemoryUsage()
return fmt.Sprintf("sys *%.1f MB*, heap *%.1f MB*", float32(sys)/1024/1024, float32(heap)/1024/1024)
}
// calculates disk usage of the system (https://gist.github.com/lunny/9828326)
func diskUsage(additionalPaths []string) (usage string) {
paths := []string{"/"}
paths = append(paths, additionalPaths...)
var lines []string
for _, p := range paths {
fs := syscall.Statfs_t{}
if err := syscall.Statfs(p, &fs); err == nil {
all := fs.Blocks * uint64(fs.Bsize)
free := fs.Bfree * uint64(fs.Bsize)
used := all - free
lines = append(lines, fmt.Sprintf(
" %s all *%.2f GB*, used *%.2f GB*, free *%.2f GB*",
p,
float64(all)/1024/1024/1024,
float64(used)/1024/1024/1024,
float64(free)/1024/1024/1024,
))
} else {
lines = append(lines, fmt.Sprintf("%s: %s", p, err))
}
}
return strings.Join(lines, "\n")
}
// removes markdown characters for avoiding
// 'Bad Request: Can't parse message text: Can't find end of the entity starting at byte offset ...' errors
// from the server
func removeMarkdownChars(original, replaceWith string) string {
removed := strings.Replace(original, "*", replaceWith, -1)
removed = strings.Replace(removed, "_", replaceWith, -1)
removed = strings.Replace(removed, "`", replaceWith, -1)
return removed
}
// `systemctl status is-active`
func systemctlStatus(services []string) (statuses map[string]string, success bool) {
statuses = make(map[string]string)
args := []string{"systemctl", "is-active"}
args = append(args, services...)
output, _ := sudoRunCmd(args)
for i, status := range strings.Split(output, "\n") {
statuses[services[i]] = status
}
return statuses, true
}
// `systemctl start [service]`
func systemctlStart(service string) (message string, err error) {
return sudoRunCmd([]string{"systemctl", "start", service})
}
// `systemctl stop [service]`
func systemctlStop(service string) (message string, err error) {
return sudoRunCmd([]string{"systemctl", "stop", service})
}
// `systemctl restart [service]`
func systemctlRestart(service string) (message string, err error) {
return sudoRunCmd([]string{"systemctl", "restart", service})
}
// sudo run given command with parameters and return combined output
func sudoRunCmd(cmdAndParams []string) (string, error) {
if len(cmdAndParams) < 1 {
return "", fmt.Errorf("no command provided")
}
output, err := exec.Command("sudo", cmdAndParams...).CombinedOutput()
return strings.TrimRight(string(output), "\n"), err
}