-
Notifications
You must be signed in to change notification settings - Fork 0
/
metric.go
177 lines (158 loc) · 3.64 KB
/
metric.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package psmetrics
import (
"github.com/gin-gonic/gin"
psDisk "github.com/shirou/gopsutil/disk"
"github.com/shirou/gopsutil/host"
psMem "github.com/shirou/gopsutil/mem"
psNet "github.com/shirou/gopsutil/net"
"github.com/shirou/gopsutil/v3/cpu"
"log"
"net/http"
"os/exec"
"strconv"
"strings"
)
func HandleDisk(c *gin.Context) {
partitions, err := psDisk.Partitions(false)
if err != nil {
log.Println("err", err)
}
c.JSON(http.StatusOK, partitions)
}
func HandleDiskUsage(c *gin.Context) {
path := c.GetHeader("path")
stats, err := psDisk.Usage(path)
if err != nil {
log.Println("err", err, " path = ", path)
}
c.JSON(http.StatusOK, stats)
}
func HandleDiskIOCounter(c *gin.Context) {
path := c.GetHeader("path")
stats, err := psDisk.IOCounters(path)
if err != nil {
log.Println("err", err)
}
c.JSON(http.StatusOK, stats)
}
func HandleNetIOCounter(c *gin.Context) {
stats, err := psNet.IOCounters(true)
if err != nil {
log.Println("err", err)
}
c.JSON(http.StatusOK, stats)
}
func HandleNetIP(c *gin.Context) {
stats := getDevWithIP()
c.JSON(http.StatusOK, stats)
}
func HandleSensorsTemperatures(c *gin.Context) {
sensors, err := host.SensorsTemperatures()
if err != nil {
log.Println("err", err)
}
c.JSON(http.StatusOK, sensors)
}
func HandleCpuCount(c *gin.Context) {
cpuCount, err := cpu.Counts(false)
if err != nil {
log.Println("err", err)
}
if cpuCount == 0 {
is, err := cpu.Info()
if err != nil {
log.Println("err", err)
}
if is[0].Cores > 0 {
c.JSON(http.StatusOK, len(is)/2)
return
}
c.JSON(http.StatusOK, len(is))
return
}
c.JSON(http.StatusOK, cpuCount)
return
}
func HandleCpuPercent(c *gin.Context) {
percpu := c.GetHeader("percpu")
var per bool
if percpu != "" {
per = true
}
sensors, err := cpu.Percent(0, per)
if err != nil {
log.Println("err", err)
}
c.JSON(http.StatusOK, sensors)
}
func HandleMem(c *gin.Context) {
mem, err := psMem.VirtualMemory()
if err != nil {
log.Println("err", err)
}
c.JSON(http.StatusOK, mem)
}
type Proc struct {
Pid int
CommandName string
FullCommand string
CPU float64
Mem float64
}
func Handleprocs(c *gin.Context) {
output, err := exec.Command("ps", "-axo", "pid:10,comm:50,pcpu:5,pmem:5,args").Output()
if err != nil {
log.Println("err", err)
return
}
// converts to []string, removing trailing newline and header
linesOfProcStrings := strings.Split(strings.TrimSuffix(string(output), "\n"), "\n")[1:]
procs := []Proc{}
for _, line := range linesOfProcStrings {
pid, err := strconv.Atoi(strings.TrimSpace(line[0:10]))
if err != nil {
log.Println("err", err)
}
cpu, err := strconv.ParseFloat(strings.TrimSpace(line[63:68]), 64)
if err != nil {
log.Println("err", err)
}
mem, err := strconv.ParseFloat(strings.TrimSpace(line[69:74]), 64)
if err != nil {
log.Println("err", err)
}
proc := Proc{
Pid: pid,
CommandName: strings.TrimSpace(line[11:61]),
FullCommand: line[74:],
CPU: cpu,
Mem: mem,
}
procs = append(procs, proc)
}
c.JSON(http.StatusOK, procs)
}
func HandleDevices(c *gin.Context) {
sensors, err := host.SensorsTemperatures()
if err != nil {
log.Println("err", err)
}
c.JSON(http.StatusOK, sensors)
}
func HandleMemSwap(c *gin.Context) {
memory, err := psMem.SwapMemory()
if err != nil {
log.Println("err", err)
}
c.JSON(http.StatusOK, memory)
}
func HandleNvidiaInfo(c *gin.Context) {
bs, err := exec.Command(
"nvidia-smi",
"--query-gpu=name,index,temperature.gpu,utilization.gpu,memory.total,memory.used",
"--format=csv,noheader,nounits").Output()
if err != nil {
log.Println("err", err)
}
c.JSON(http.StatusOK, bs)
}