-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
124 lines (102 loc) · 2.17 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
package main
import (
"log"
"errors"
"fmt"
"net"
"os"
"encoding/json"
linuxproc "github.com/c9s/goprocinfo/linux"
)
func cpu_usage() uint64{
stat, err := linuxproc.ReadStat("/proc/stat")
if err != nil {
log.Fatal("stat read fail")
}
s := stat.CPUStatAll
a := s.User + s.Nice + s.System + s.IOWait + s.IRQ + s.SoftIRQ + s.Steal + s.Guest + s.GuestNice + s.Idle
c := s.User + s.Nice + s.System + s.IOWait + s.IRQ + s.SoftIRQ + s.Steal + s.Guest + s.GuestNice
d := c*100/a
//fmt.Println(d)
return d
}
func mem_usage() uint64{
MemInfo, err := linuxproc.ReadMemInfo("/proc/meminfo")
if err != nil {
log.Fatal("meminfo read fail")
}
memtotal := MemInfo.MemTotal
memactive := MemInfo.Active
usage_util := memactive*100/memtotal
//fmt.Println(usage_util)
return usage_util
}
func externalIP() (string, error) {
ifaces, err := net.Interfaces()
if err != nil {
return "", err
}
for _, iface := range ifaces {
if iface.Flags&net.FlagUp == 0 {
continue // interface down
}
if iface.Flags&net.FlagLoopback != 0 {
continue // loopback interface
}
addrs, err := iface.Addrs()
if err != nil {
return "", err
}
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
if ip == nil || ip.IsLoopback() {
continue
}
ip = ip.To4()
if ip == nil {
continue // not an ipv4 address
}
return ip.String(), nil
}
}
return "", errors.New("are you connected to the network?")
}
func main() {
//cpu_usage()
//mem_usage()
ip, err := externalIP()
if err != nil {
fmt.Println(err)
}
//fmt.Println("ip_address : ", ip)
hostname, err := os.Hostname()
if err != nil {
panic(err)
}
cpu_usages := cpu_usage()
mem_usages := mem_usage()
//fmt.Println("hostname : ", hostname)
type AMonS struct {
Hoetname string
IP string
MemUsage uint64
CPUUsage uint64
}
mons := AMonS{
Hoetname: hostname,
IP: ip,
MemUsage: mem_usages,
CPUUsage: cpu_usages,
}
b, err := json.MarshalIndent(mons, "", " ")
if err != nil {
fmt.Println("error:", err)
}
os.Stdout.Write(b)
}