/
archstatus.go
76 lines (58 loc) · 1.62 KB
/
archstatus.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
package main
import (
"bytes"
"fmt"
"os/exec"
"github.com/mgutz/ansi"
"github.com/stevedomin/termtable"
"strings"
)
func fmtString(color, str, reset string) string {
return fmt.Sprintf("%s%s%s", color, str, reset)
}
func main() {
services := []string{
"cronie.service",
"httpd.service",
"mysqld.service",
"ntpd.service",
"postfix.service",
"sshd.service",
"home.mount",
"mnt-extra.mount",
"tmp.mount",
"var-lib-mysqltmp.mount",
"var.mount",
}
t := termtable.NewTable(nil, nil)
t.SetHeader([]string{"SERVICE", "STATUS"})
for _, service := range services {
// The systemctl command.
syscommand := exec.Command("systemctl", "status", service)
// The grep command.
grepcommand := exec.Command("grep", "Active:")
// Pipe the stdout of syscommand to the stdin of grepcommand.
grepcommand.Stdin, _ = syscommand.StdoutPipe()
// Create a buffer of bytes.
var b bytes.Buffer
// Assign the address of our buffer to grepcommand.Stdout.
grepcommand.Stdout = &b
// Start grepcommand.
_ = grepcommand.Start()
// Run syscommand
_ = syscommand.Run()
// Wait for grepcommand to exit.
_ = grepcommand.Wait()
s := fmt.Sprintf("%s", &b)
if strings.Contains(s, "active (running)") || strings.Contains(s, "active (mounted)") {
color := ansi.ColorCode("green+h:black")
reset := ansi.ColorCode("reset")
t.AddRow([]string{fmtString(color, service, reset), fmtString(color, s, reset)})
} else {
color := ansi.ColorCode("red+h:black")
reset := ansi.ColorCode("reset")
t.AddRow([]string{fmtString(color, service, reset), fmtString(color, s, reset)})
}
}
fmt.Println(t.Render())
}