This repository has been archived by the owner on Jul 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
corestats.go
76 lines (68 loc) · 2.39 KB
/
corestats.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 (
"fmt"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/disk"
"github.com/shirou/gopsutil/host"
"github.com/shirou/gopsutil/load"
"github.com/shirou/gopsutil/mem"
"github.com/shirou/gopsutil/net"
)
type Stats struct{}
func GetMemStats() string {
v, err := mem.VirtualMemory()
format := "total=%d available=%d used=%d used_percent=%f"
if err == nil {
return fmt.Sprintf(format, v.Total, v.Available, v.Used, v.UsedPercent)
} else {
return fmt.Sprintf(format, 0, 0, 0, 0)
}
}
func GetLoadStats() string {
l, err := load.Avg()
format := "last1=%f last5=%f last15=%f"
if err == nil {
return fmt.Sprintf(format, l.Load1, l.Load5, l.Load15)
} else {
return fmt.Sprintf(format, 0, 0, 0)
}
}
func GetDiskStats() string {
d, err := disk.Usage("/")
format := "fstype=%s total=%d free=%d used=%d used_percent=%f inodes_total=%d inodes_used=%d inodes_free=%d inodes_used_percent=%f"
if err == nil {
return fmt.Sprintf(format,
d.Fstype, d.Total, d.Free, d.Used, d.UsedPercent, d.InodesTotal, d.InodesUsed, d.InodesFree, d.InodesUsedPercent)
} else {
return fmt.Sprintf(format, 0, 0, 0, 0, 0, 0, 0, 0, 0)
}
}
func GetCpuStats() string {
v, err := cpu.Times(false)
format := "user=%f system=%f idle=%f nice=%f iowait=%f irq=%f soft_irq=%f steal=%f guest=%f guest_nice=%f stolen=%f"
if err == nil {
return fmt.Sprintf(format,
v[0].User, v[0].System, v[0].Idle, v[0].Nice, v[0].Iowait, v[0].Irq, v[0].Softirq, v[0].Steal,
v[0].Guest, v[0].GuestNice, v[0].Stolen)
} else {
return fmt.Sprintf(format, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
}
}
func GetHostStats() string {
v, err := host.Info()
format := "uptime=%d procs=%d os=%s platform=%s platform_family=%s platform_version=%s virtualization_system=%s virtualization_role=%s"
if err == nil {
return fmt.Sprintf(format, v.Uptime, v.Procs, v.OS, v.Platform, v.PlatformFamily, v.PlatformVersion, v.VirtualizationSystem, v.VirtualizationRole)
} else {
return fmt.Sprintf(format, 0, 0, 0, 0, 0, 0, 0, 0)
}
}
func GetNetStats() string {
v, err := net.IOCounters(false)
format := "bytes_sent=%d bytes_recv=%d packets_sent=%d packets_recv=%d err_in=%d err_out=%d drop_in=%d drop_out=%d"
if err == nil {
return fmt.Sprintf(format, v[0].BytesSent, v[0].BytesRecv, v[0].PacketsSent, v[0].PacketsRecv, v[0].Errin, v[0].Errout, v[0].Dropin, v[0].Dropout)
} else {
return fmt.Sprintf(format, 0, 0, 0, 0, 0, 0, 0, 0)
}
}