-
Notifications
You must be signed in to change notification settings - Fork 4
/
stats.go
161 lines (154 loc) · 3.66 KB
/
stats.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
package clamd
import (
"bufio"
"strconv"
"strings"
)
type Stats struct {
Pools int
State string
Threads Thread
Memstats MemStat
Queue Queue
}
type MemStat struct {
Heap float32
Mmap float32
Used float32
Free float32
Releasable float32
Pools int
PoolsUsed float32
PoolsTotal float32
}
type Thread struct {
Live int
Idle int
Max int
IdleTimeout int
}
type Queue struct {
Items int
Stats float32
}
func parseStats(res string) (*Stats, error) {
stats := Stats{}
scanner := bufio.NewScanner(strings.NewReader(res))
var (
text string
err error
)
queue := false
for scanner.Scan() {
text = scanner.Text()
if strings.HasPrefix(text, "POOLS") {
stats.Pools, err = strconv.Atoi(strings.TrimPrefix(text, "POOLS: "))
if err != nil {
return nil, err
}
} else if strings.HasPrefix(text, "STATE") {
stats.State = strings.TrimPrefix(text, "STATE: ")
} else if strings.HasPrefix(text, "THREADS") {
text = strings.TrimPrefix(text, "THREADS: ")
split := strings.Split(text, " ")
prev := ""
for _, s := range split {
switch prev {
case "live":
stats.Threads.Live, err = strconv.Atoi(s)
if err != nil {
return nil, err
}
case "idle":
stats.Threads.Idle, err = strconv.Atoi(s)
if err != nil {
return nil, err
}
case "max":
stats.Threads.Max, err = strconv.Atoi(s)
if err != nil {
return nil, err
}
case "idle-timeout":
stats.Threads.IdleTimeout, err = strconv.Atoi(s)
if err != nil {
return nil, err
}
}
prev = s
}
} else if strings.HasPrefix(text, "QUEUE") {
queue = true
text = strings.TrimPrefix(strings.Trim(text, "\n\t"), "QUEUE: ")
stats.Queue.Items, err = strconv.Atoi(strings.Trim(text[:1], " "))
if err != nil {
return nil, err
}
} else if strings.HasPrefix(text, "\t") && queue {
text = strings.TrimPrefix(strings.Trim(text, "\t"), "STATS ")
f, err := strconv.ParseFloat(strings.Trim(text, " "), 32)
if err != nil {
return nil, err
}
stats.Queue.Stats = float32(f)
} else if strings.HasPrefix(text, "MEMSTATS:") && queue {
text = strings.TrimPrefix(text, "MEMSTATS: ")
split := strings.Split(text, " ")
prev := ""
for _, s := range split {
switch prev {
case "heap":
f, err := strconv.ParseFloat(strings.Trim(s, "M"), 32)
if err != nil {
return nil, err
}
stats.Memstats.Heap = float32(f)
case "mmap":
f, err := strconv.ParseFloat(strings.Trim(s, "M"), 32)
if err != nil {
return nil, err
}
stats.Memstats.Mmap = float32(f)
case "used":
f, err := strconv.ParseFloat(strings.Trim(s, "M"), 32)
if err != nil {
return nil, err
}
stats.Memstats.Used = float32(f)
case "free":
f, err := strconv.ParseFloat(strings.Trim(s, "M"), 32)
if err != nil {
return nil, err
}
stats.Memstats.Free = float32(f)
case "releasable":
f, err := strconv.ParseFloat(strings.Trim(s, "M"), 32)
if err != nil {
return nil, err
}
stats.Memstats.Releasable = float32(f)
case "pools":
f, err := strconv.Atoi(strings.Trim(s, "M"))
if err != nil {
return nil, err
}
stats.Memstats.Pools = f
case "pools_used":
f, err := strconv.ParseFloat(strings.Trim(s, "M"), 32)
if err != nil {
return nil, err
}
stats.Memstats.PoolsUsed = float32(f)
case "pools_total":
f, err := strconv.ParseFloat(strings.Trim(s, "M"), 32)
if err != nil {
return nil, err
}
stats.Memstats.PoolsTotal = float32(f)
}
prev = s
}
}
}
return &stats, nil
}