-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
157 lines (127 loc) · 3.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
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
package main
import (
"fmt"
"log"
"net/http"
"os"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/shirou/gopsutil/v3/process"
)
func main() {
prometheus.MustRegister(&metrics{})
http.Handle("/metrics", promhttp.Handler())
port := os.Getenv("PORT")
if port == "" {
port = "19002"
}
http.ListenAndServe("localhost:"+port, nil)
}
type metrics struct{}
func (m metrics) Describe(ch chan<- *prometheus.Desc) {
ch <- prometheus.NewDesc("mac_process_cpu_usage_total", "mac_process_cpu_usage", []string{"name", "parent"}, nil)
ch <- prometheus.NewDesc("mac_process_mem_usage", "mac_process_mem_usage", []string{"name", "parent"}, nil)
}
func (m *metrics) Collect(ch chan<- prometheus.Metric) {
fmt.Println("Collecting metrics")
procs, err := process.Processes()
if err != nil {
log.Fatal(err)
}
fmt.Printf("procs: %d\n", len(procs))
type resultKey struct {
name string
parent string
// cmdline string
}
type result struct {
resultKey
cpuTotal float64
mem float32
}
var results = make(map[resultKey]result)
for _, proc := range procs {
var name, parentName string
{
name, err = proc.Name()
if err != nil {
log.Println("error getting process name:", err)
continue
}
}
{
parentName, err = findParent(name, proc)
if err != nil {
log.Println("error getting parent process name:", err)
continue
}
}
key := resultKey{name, parentName}
if _, ok := results[key]; !ok {
results[key] = result{resultKey: key}
}
r := results[key]
{
times, err := proc.Times()
if err != nil {
log.Printf("error getting process cpu for %s: %v", name, err)
continue
}
// Other values are available, but only these are filled in
totalCPU := times.User + times.System
r.cpuTotal += totalCPU
}
{
mem, err := proc.MemoryPercent()
if err != nil {
log.Printf("error getting process memory for %s: %v", name, err)
continue
}
r.mem += mem
}
results[key] = r
}
var orderedResults []result
for _, v := range results {
orderedResults = append(orderedResults, v)
}
for _, r := range orderedResults {
fmt.Println("name:", r.name, "pct:", r.cpuTotal, "mem:", r.mem)
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc("mac_process_cpu_usage_total", "process cpu usage", []string{"name", "parent"}, nil),
prometheus.CounterValue,
r.cpuTotal,
r.name, r.parent,
)
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc("mac_process_mem_usage", "process memory usage", []string{"name", "parent"}, nil),
prometheus.GaugeValue,
float64(r.mem),
r.name, r.parent,
)
}
fmt.Println("done")
}
func findParent(name string, proc *process.Process) (string, error) {
var err error
for {
proc, err = proc.Parent()
if err != nil {
return "", err
}
// We've reached the top of the tree
if proc == nil {
return name, nil
}
parentName, err := proc.Name()
if err != nil {
return "", err
}
// We've reached the top of the tree, but launchd is in charge of
// everything so we return the last child name
if parentName == "launchd" {
return name, nil
}
name = parentName
}
}