-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
84 lines (66 loc) · 2.79 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
package main
import (
"fmt"
"io/ioutil"
"runtime"
"strconv"
"strings"
"time"
"github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/disk"
"github.com/shirou/gopsutil/v3/host"
"github.com/shirou/gopsutil/v3/mem"
)
func handle(err error) {
if err != nil {
fmt.Println(err)
}
}
func getHardwareData() string {
runtimeOS := runtime.GOOS
// memory
vmStat, err := mem.VirtualMemory()
handle(err)
// disk - start from "/" mount point for Linux
// might have to change for Windows!!
// don't have a Window to test this out, if detect OS == windows
// then use "\" instead of "/"
diskStat, err := disk.Usage("/")
handle(err)
percentage, err := cpu.Percent(time.Second, true)
total, err := cpu.Percent(time.Second, false)
handle(err)
// host or machine kernel, uptime, platform Info
hostStat, err := host.Info()
handle(err)
var r strings.Builder
r.WriteString("OS : " + runtimeOS + "\n")
r.WriteString("Total memory: " + strconv.FormatUint(vmStat.Total, 10) + " bytes \n")
r.WriteString("Free memory: " + strconv.FormatUint(vmStat.Free, 10) + " bytes\n")
r.WriteString("Percentage used memory: " + strconv.FormatFloat(vmStat.UsedPercent, 'f', 2, 64) + "%\n")
r.WriteString("Total disk space: " + strconv.FormatUint(diskStat.Total, 10) + " bytes \n")
r.WriteString("Used disk space: " + strconv.FormatUint(diskStat.Used, 10) + " bytes\n")
r.WriteString("Free disk space: " + strconv.FormatUint(diskStat.Free, 10) + " bytes\n")
r.WriteString("Percentage disk space usage: " + strconv.FormatFloat(diskStat.UsedPercent, 'f', 2, 64) + "%\n")
// r.WriteString("CPU index number: " + strconv.FormatInt(int64(cpuStat[0].CPU), 10) + "\n")
// r.WriteString("VendorID: " + cpuStat[0].VendorID + "\n")
// r.WriteString("Family: " + cpuStat[0].Family + "\n")
// r.WriteString("Number of cores: " + strconv.FormatInt(int64(cpuStat[0].Cores), 10) + "\n")
// r.WriteString("Model Name: " + cpuStat[0].ModelName + "\n")
// r.WriteString("Speed: " + strconv.FormatFloat(cpuStat[0].Mhz, 'f', 2, 64) + " MHz \n")
r.WriteString("Total CPU utilization: " + strconv.FormatFloat(total[0], 'f', 2, 64) + "%\n")
for idx, cpupercent := range percentage {
r.WriteString("Current CPU utilization: [" + strconv.Itoa(idx) + "] " + strconv.FormatFloat(cpupercent, 'f', 2, 64) + "%\n")
}
r.WriteString("Hostname: " + hostStat.Hostname + "\n")
r.WriteString("Uptime: " + strconv.FormatUint(hostStat.Uptime, 10) + "\n")
r.WriteString("Number of processes running: " + strconv.FormatUint(hostStat.Procs, 10) + "\n")
return r.String()
}
func main() {
fmt.Println(getHardwareData())
ioutil.WriteFile(time.Now().Format("20060102_150405.txt"), []byte(getHardwareData()), 0644)
for range time.Tick(time.Minute * 10) {
ioutil.WriteFile(time.Now().Format("20060102_150405.txt"), []byte(getHardwareData()), 0644)
}
}