/
stats.go
155 lines (130 loc) · 3.41 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
package controllers
import (
"bufio"
"encoding/json"
"errors"
"fmt"
"net/http"
"regexp"
"strconv"
"strings"
"github.com/rs/zerolog/hlog"
)
// StatsResponse represents the json response of a /stats endpoint.
// It represents the statistics about the scan queue,
// contents of scan queue, and memory usage.
type StatsResponse struct {
Pools int `json:"pools"`
State string `json:"state"`
Threads string `json:"threads"`
Queue string `json:"queue"`
Memstats string `json:"memstats"`
}
var ErrParsingStats = errors.New("error while parsing 'stats'")
func (h *Handler) Stats(w http.ResponseWriter, r *http.Request) {
// Get request id for logging purposes
req_id, _ := hlog.IDFromCtx(r.Context())
ctx := r.Context()
stats, err := h.Clamav.Stats(ctx)
if err != nil {
h.Logger.Error().Str("req_id", req_id.String()).Msgf("error while sending stats command: %v", err)
SetErrorResponse(w, err)
return
}
h.Logger.Debug().Str("req_id", req_id.String()).Msg("stats command sent successfully")
s, err := statsMarshall(string(stats))
if err != nil {
h.Logger.Error().Str("req_id", req_id.String()).Msgf("error while marshalling stats: %v", err)
SetErrorResponse(w, err)
return
}
h.Logger.Debug().Str("req_id", req_id.String()).Msg("stats marshalled successfully")
resp, err := json.Marshal(&s)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Add("Content-Type", ContentTypeApplicationJSON)
w.WriteHeader(http.StatusOK)
w.Write(resp)
}
// statsMarshall will marshall the string s
// into a *StatsResponse.
//
// Example of output for a 'STATS' command:
//
// POOLS: 1
//
// STATE: VALID PRIMARY
// THREADS: live 1 idle 0 max 10 idle-timeout 30
// QUEUE: 0 items
//
// STATS 0.000111
//
// MEMSTATS: heap N/A mmap N/A used N/A free N/A releasable N/A pools 1 pools_used 713.137M pools_total 713.226M
// END
//
// It returns any error encountered.
func statsMarshall(s string) (*StatsResponse, error) {
if len(s) == 0 {
return nil, fmt.Errorf("error: empty string")
}
scanner := bufio.NewScanner(strings.NewReader(s))
scanner.Split(bufio.ScanLines)
var pools int
var state string
var threads string
var queue string
var memstats string
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "POOLS: ") {
var err error
s := strings.SplitAfter(line, ": ")
if len(s) != 2 {
return nil, ErrParsingStats
}
pools, err = strconv.Atoi(s[1])
if err != nil {
return nil, ErrParsingStats
}
}
if strings.HasPrefix(line, "STATE: ") {
s := strings.SplitAfter(line, ": ")
if len(s) != 2 {
return nil, ErrParsingStats
}
state = s[1]
}
if strings.HasPrefix(line, "THREADS: ") {
s := strings.SplitAfter(line, ": ")
if len(s) != 2 {
return nil, ErrParsingStats
}
threads = s[1]
}
// "QUEUE" is a multi-line string until the "MEMSTATS" section
if strings.HasPrefix(line, "QUEUE: ") {
re := regexp.MustCompile(`(?ms)QUEUE: (.*?)\sMEMSTATS`)
matches := re.FindStringSubmatch(s)
if len(matches) != 2 {
return nil, ErrParsingStats
}
queue = strings.TrimSuffix(matches[1], "\n")
}
if strings.HasPrefix(line, "MEMSTATS: ") {
s := strings.SplitAfter(line, ": ")
if len(s) != 2 {
return nil, ErrParsingStats
}
memstats = s[1]
}
}
return &StatsResponse{
Pools: pools,
State: state,
Threads: threads,
Queue: queue,
Memstats: memstats,
}, nil
}