forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 1
/
uwsgi.go
295 lines (258 loc) · 7.02 KB
/
uwsgi.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// Package uwsgi implements a telegraf plugin for collecting uwsgi stats from
// the uwsgi stats server.
package uwsgi
import (
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"net/url"
"os"
"strconv"
"sync"
"time"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/plugins/inputs"
)
// Uwsgi server struct
type Uwsgi struct {
Servers []string `toml:"servers"`
Timeout internal.Duration `toml:"timeout"`
client *http.Client
}
// Description returns the plugin description
func (u *Uwsgi) Description() string {
return "Read uWSGI metrics."
}
// SampleConfig returns the sample configuration
func (u *Uwsgi) SampleConfig() string {
return `
## List with urls of uWSGI Stats servers. URL must match pattern:
## scheme://address[:port]
##
## For example:
## servers = ["tcp://localhost:5050", "http://localhost:1717", "unix:///tmp/statsock"]
servers = ["tcp://127.0.0.1:1717"]
## General connection timout
# timeout = "5s"
`
}
// Gather collect data from uWSGI Server
func (u *Uwsgi) Gather(acc telegraf.Accumulator) error {
if u.client == nil {
u.client = &http.Client{
Timeout: u.Timeout.Duration,
}
}
wg := &sync.WaitGroup{}
for _, s := range u.Servers {
wg.Add(1)
go func(s string) {
defer wg.Done()
n, err := url.Parse(s)
if err != nil {
acc.AddError(fmt.Errorf("could not parse uWSGI Stats Server url '%s': %s", s, err.Error()))
return
}
if err := u.gatherServer(acc, n); err != nil {
acc.AddError(err)
return
}
}(s)
}
wg.Wait()
return nil
}
func (u *Uwsgi) gatherServer(acc telegraf.Accumulator, url *url.URL) error {
var err error
var r io.ReadCloser
var s StatsServer
switch url.Scheme {
case "tcp":
r, err = net.DialTimeout(url.Scheme, url.Host, u.Timeout.Duration)
if err != nil {
return err
}
s.source = url.Host
case "unix":
r, err = net.DialTimeout(url.Scheme, url.Path, u.Timeout.Duration)
if err != nil {
return err
}
s.source, err = os.Hostname()
if err != nil {
s.source = ""
}
case "http":
resp, err := u.client.Get(url.String())
if err != nil {
return err
}
r = resp.Body
s.source = url.Host
default:
return fmt.Errorf("'%s' is not a supported scheme", url.Scheme)
}
defer r.Close()
if err := json.NewDecoder(r).Decode(&s); err != nil {
return fmt.Errorf("failed to decode json payload from '%s': %s", url.String(), err.Error())
}
u.gatherStatServer(acc, &s)
return err
}
func (u *Uwsgi) gatherStatServer(acc telegraf.Accumulator, s *StatsServer) {
fields := map[string]interface{}{
"listen_queue": s.ListenQueue,
"listen_queue_errors": s.ListenQueueErrors,
"signal_queue": s.SignalQueue,
"load": s.Load,
"pid": s.PID,
}
tags := map[string]string{
"source": s.source,
"uid": strconv.Itoa(s.UID),
"gid": strconv.Itoa(s.GID),
"version": s.Version,
}
acc.AddFields("uwsgi_overview", fields, tags)
u.gatherWorkers(acc, s)
u.gatherApps(acc, s)
u.gatherCores(acc, s)
}
func (u *Uwsgi) gatherWorkers(acc telegraf.Accumulator, s *StatsServer) {
for _, w := range s.Workers {
fields := map[string]interface{}{
"requests": w.Requests,
"accepting": w.Accepting,
"delta_request": w.DeltaRequests,
"exceptions": w.Exceptions,
"harakiri_count": w.HarakiriCount,
"pid": w.PID,
"signals": w.Signals,
"signal_queue": w.SignalQueue,
"status": w.Status,
"rss": w.Rss,
"vsz": w.Vsz,
"running_time": w.RunningTime,
"last_spawn": w.LastSpawn,
"respawn_count": w.RespawnCount,
"tx": w.Tx,
"avg_rt": w.AvgRt,
}
tags := map[string]string{
"worker_id": strconv.Itoa(w.WorkerID),
"source": s.source,
}
acc.AddFields("uwsgi_workers", fields, tags)
}
}
func (u *Uwsgi) gatherApps(acc telegraf.Accumulator, s *StatsServer) {
for _, w := range s.Workers {
for _, a := range w.Apps {
fields := map[string]interface{}{
"modifier1": a.Modifier1,
"requests": a.Requests,
"startup_time": a.StartupTime,
"exceptions": a.Exceptions,
}
tags := map[string]string{
"app_id": strconv.Itoa(a.AppID),
"worker_id": strconv.Itoa(w.WorkerID),
"source": s.source,
}
acc.AddFields("uwsgi_apps", fields, tags)
}
}
}
func (u *Uwsgi) gatherCores(acc telegraf.Accumulator, s *StatsServer) {
for _, w := range s.Workers {
for _, c := range w.Cores {
fields := map[string]interface{}{
"requests": c.Requests,
"static_requests": c.StaticRequests,
"routed_requests": c.RoutedRequests,
"offloaded_requests": c.OffloadedRequests,
"write_errors": c.WriteErrors,
"read_errors": c.ReadErrors,
"in_request": c.InRequest,
}
tags := map[string]string{
"core_id": strconv.Itoa(c.CoreID),
"worker_id": strconv.Itoa(w.WorkerID),
"source": s.source,
}
acc.AddFields("uwsgi_cores", fields, tags)
}
}
}
func init() {
inputs.Add("uwsgi", func() telegraf.Input {
return &Uwsgi{
Timeout: internal.Duration{Duration: 5 * time.Second},
}
})
}
// StatsServer defines the stats server structure.
type StatsServer struct {
// Tags
source string
PID int `json:"pid"`
UID int `json:"uid"`
GID int `json:"gid"`
Version string `json:"version"`
// Fields
ListenQueue int `json:"listen_queue"`
ListenQueueErrors int `json:"listen_queue_errors"`
SignalQueue int `json:"signal_queue"`
Load int `json:"load"`
Workers []*Worker `json:"workers"`
}
// Worker defines the worker metric structure.
type Worker struct {
// Tags
WorkerID int `json:"id"`
PID int `json:"pid"`
// Fields
Accepting int `json:"accepting"`
Requests int `json:"requests"`
DeltaRequests int `json:"delta_requests"`
Exceptions int `json:"exceptions"`
HarakiriCount int `json:"harakiri_count"`
Signals int `json:"signals"`
SignalQueue int `json:"signal_queue"`
Status string `json:"status"`
Rss int `json:"rss"`
Vsz int `json:"vsz"`
RunningTime int `json:"running_time"`
LastSpawn int `json:"last_spawn"`
RespawnCount int `json:"respawn_count"`
Tx int `json:"tx"`
AvgRt int `json:"avg_rt"`
Apps []*App `json:"apps"`
Cores []*Core `json:"cores"`
}
// App defines the app metric structure.
type App struct {
// Tags
AppID int `json:"id"`
// Fields
Modifier1 int `json:"modifier1"`
Requests int `json:"requests"`
StartupTime int `json:"startup_time"`
Exceptions int `json:"exceptions"`
}
// Core defines the core metric structure.
type Core struct {
// Tags
CoreID int `json:"id"`
// Fields
Requests int `json:"requests"`
StaticRequests int `json:"static_requests"`
RoutedRequests int `json:"routed_requests"`
OffloadedRequests int `json:"offloaded_requests"`
WriteErrors int `json:"write_errors"`
ReadErrors int `json:"read_errors"`
InRequest int `json:"in_request"`
}