forked from AndreaGreco/prometeus_uwsgi_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uwsgi_parser.go
504 lines (428 loc) · 19.6 KB
/
uwsgi_parser.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net"
"strings"
"sync"
"regexp"
"io"
)
// JSON Definition
type Socket_t struct {
Name string `json:"name"`
Proto string `json:"proto"`
Queue int `json:"queue"`
MaxQueue int `json:"max_queue"`
Shared int `json:"shared"`
CanOffload int `json:"can_offload"`
}
type App_t struct {
ID int `json:"id"`
Modifier1 int `json:"modifier1"`
Mountpoint string `json:"mountpoint"`
StartupTime int `json:"startup_time"`
Requests int `json:"requests"`
Exceptions int `json:"exceptions"`
Chdir string `json:"chdir"`
}
type Caches_t struct {
Name string `json:"name"`
Hash string `json:"hash"`
Hashsize int `json:"hashsize"`
Keysize int `json:"keysize"`
MaxItems int `json:"max_items"`
Blocks int `json:"blocks"`
Blocksize int `json:"blocksize"`
Items int `json:"items"`
Hits int `json:"hits"`
Miss int `json:"miss"`
Full int `json:"full"`
LastModifiedAt int `json:"last_modified_at"`
}
type Core_t struct {
ID int `json:"id"`
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"`
Vars []interface{} `json:"vars"`
ReqInfo struct{} `json:"req_info"`
}
type Worker_t struct {
ID int `json:"id"`
Pid int `json:"pid"`
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_t `json:"apps"`
Cores []Core_t `json:"cores"`
}
type Uwsgi_json_t struct {
Version string `json:"version"`
ListenQueue int `json:"listen_queue"`
ListenQueueErrors int `json:"listen_queue_errors"`
SignalQueue int `json:"signal_queue"`
Load int `json:"load"`
Pid int `json:"pid"`
UID int `json:"uid"`
Gid int `json:"gid"`
Cwd string `json:"cwd"`
Locks []map[string]int `json:"locks"`
Cache []Caches_t `json:"caches", omitempty`
Sockets []Socket_t `json:"sockets"`
Workers []Worker_t `json:"workers"`
}
type WorkerStatus int
const (
cheap WorkerStatus = iota
pause
sig
busy
idle
)
/**
* String buffer
*/
var StrBuilder bytes.Buffer
var globalDisableHelp bool
/**
* @brief allow to enable \ref globalDisableHelp
* Help must be redered only one time, or prometheus return parsing error
* Then is disabled by render function every time is completed.busy
* This function allow main thread to enable help at fist turn in for-loop
*/
func EnableHelp() {
globalDisableHelp = true
}
/**
* @brief uwsgi prefix string
* string on head to all uwsgi metrics
*/
const uwsgi_prefix = "uwsgi_"
/**
* @brief Sanitizze string
*
* Some metrics label must be sanitizzed, it contain space, or other special charter.
* Remove:
* - " " --> "_"
*/
func SanitizeField(val string) string {
ret := strings.Replace(val, " ", "_", -1)
ret = strings.ToLower(ret)
return ret
}
/**
* @brief Write a help string in sting buffer
*/
func WriteHelp(str string, write bool) {
if write && globalDisableHelp {
StrBuilder.WriteString(str)
}
}
/**
* @brief Write metrics in string buffer
*/
func WriteMetrics(metrics string) {
StrBuilder.WriteString(metrics)
}
/**
* @brief Take uwsgi json struct and render Prometheus metrics
*/
func uWSGI_DataFormat(data Uwsgi_json_t, domain string) string {
StrBuilder.Reset()
/**
* Try respect prometheus line guide for write exporter:
* https://prometheus.io/docs/practices/naming/
*/
txt := "general_listen_queue"
WriteHelp(fmt.Sprintf("# HELP %s%s Length of uwsgi listen queue\n", uwsgi_prefix, txt), true)
WriteMetrics(fmt.Sprintf("%s%s{domain=\"%s\"} %d\n", uwsgi_prefix, txt, domain, data.ListenQueue))
txt = "general_listen_queue_errors"
WriteHelp(fmt.Sprintf("# HELP %s%s Number of uwsgi server queue error\n", uwsgi_prefix, txt), true)
WriteMetrics(fmt.Sprintf("%s%s{domain=\"%s\"} %d\n", uwsgi_prefix, txt, domain, data.ListenQueueErrors))
txt = "general_signal_queue"
WriteHelp(fmt.Sprintf("# HELP %s%s Uwsgi signal queue\n", uwsgi_prefix, txt), true)
WriteMetrics(fmt.Sprintf("%s%s{domain=\"%s\"} %d\n", uwsgi_prefix, txt, domain, data.SignalQueue))
for _, arr := range data.Locks {
for key, val := range arr {
key = SanitizeField(key)
WriteHelp((fmt.Sprintf("# HELP %s%s_%s Locks %s\n", uwsgi_prefix, "locks", key, key)), true)
WriteMetrics(fmt.Sprintf("%s%s_%s{domain=\"%s\"} %d\n", uwsgi_prefix, "locks", key, domain, val))
}
}
for iSocket, Socket := range data.Sockets {
txt := "sockets_queue"
WriteHelp(fmt.Sprintf("# HELP %s%s Sockets queue\n", uwsgi_prefix, txt), (iSocket == 0))
WriteMetrics(fmt.Sprintf("%s%s{domain=\"%s\"} %d\n", uwsgi_prefix, txt, domain, Socket.Queue))
txt = "sockets_max_queue"
WriteHelp(fmt.Sprintf("# HELP %s%s Max queue length\n", uwsgi_prefix, txt), (iSocket == 0))
WriteMetrics(fmt.Sprintf("%s%s{domain=\"%s\"} %d\n", uwsgi_prefix, txt, domain, Socket.MaxQueue))
txt = "sockets_shared"
WriteHelp(fmt.Sprintf("# HELP %s%s Sockets shared\n", uwsgi_prefix, txt), (iSocket == 0))
WriteMetrics(fmt.Sprintf("%s%s{domain=\"%s\"} %d\n", uwsgi_prefix, txt, domain, Socket.Shared))
txt = "sockets_can_off_load"
WriteHelp(fmt.Sprintf("# HELP %s%s Sockets queue\n", uwsgi_prefix, txt), (iSocket == 0))
WriteMetrics(fmt.Sprintf("%s%s{domain=\"%s\"} %d\n", uwsgi_prefix, txt, domain, Socket.CanOffload))
}
for iCache, Cache := range data.Cache {
txt := "cache_max_items"
WriteHelp(fmt.Sprintf("# HELP %s%s Max item in uwsgi cache\n", uwsgi_prefix, txt), (iCache == 0))
WriteMetrics(fmt.Sprintf("%s%s{domain=\"%s\", name=\"%s\", hash=\"%s\"} %d\n", uwsgi_prefix, txt, Cache.Name, Cache.Hash, domain, Cache.MaxItems))
txt = "cache_items"
WriteHelp(fmt.Sprintf("# HELP %s%s number current item in cache\n", uwsgi_prefix, txt), (iCache == 0))
WriteMetrics(fmt.Sprintf("%s%s{domain=\"%s\", name=\"%s\", hash=\"%s\"} %d\n", uwsgi_prefix, txt, Cache.Name, Cache.Hash, domain, Cache.Items))
txt = "cache_hits"
WriteHelp(fmt.Sprintf("# HELP %s%s cache hits\n", uwsgi_prefix, txt), (iCache == 0))
WriteMetrics(fmt.Sprintf("%s%s{domain=\"%s\", name=\"%s\", hash=\"%s\"} %d\n", uwsgi_prefix, txt, Cache.Name, Cache.Hash, domain, Cache.Hits))
txt = "cache_miss"
WriteHelp(fmt.Sprintf("# HELP %s%s Cache miss\n", uwsgi_prefix, txt), (iCache == 0))
WriteMetrics(fmt.Sprintf("%s%s{domain=\"%s\", name=\"%s\", hash=\"%s\"} %d\n", uwsgi_prefix, txt, Cache.Name, Cache.Hash, domain, Cache.Miss))
txt = "cache_full"
WriteHelp(fmt.Sprintf("# HELP %s%s Cache full\n", uwsgi_prefix, txt), (iCache == 0))
WriteMetrics(fmt.Sprintf("%s%s{domain=\"%s\", name=\"%s\", hash=\"%s\"} %d\n", uwsgi_prefix, txt, Cache.Name, Cache.Hash, domain, Cache.Full))
txt = "cache_last_modified_at"
WriteHelp(fmt.Sprintf("# HELP %s%s Last modified ithem in uwsgi cache\n", uwsgi_prefix, txt), (iCache == 0))
WriteMetrics(fmt.Sprintf("%s%s{domain=\"%s\", name=\"%s\", hash=\"%s\"} %d\n", uwsgi_prefix, txt, Cache.Name, Cache.Hash, domain, Cache.LastModifiedAt))
}
for iworker, Worker := range data.Workers {
txt := "workers_id"
WriteHelp(fmt.Sprintf("# HELP %s%s Worker id\n", uwsgi_prefix, txt), (iworker == 0))
WriteMetrics(fmt.Sprintf("%s%s{domain=\"%s\", workerenum=\"%d\"} %d\n", uwsgi_prefix, txt, domain, iworker, Worker.ID))
txt = "workers_accepting"
WriteHelp(fmt.Sprintf("# HELP %s%s Worker accepting request\n", uwsgi_prefix, txt), (iworker == 0))
WriteMetrics(fmt.Sprintf("%s%s{domain=\"%s\", workerenum=\"%d\"} %d\n", uwsgi_prefix, txt, domain, iworker, Worker.Accepting))
txt = "workers_requests"
WriteHelp(fmt.Sprintf("# HELP %s%s Worker request elaborated\n", uwsgi_prefix, txt), (iworker == 0))
WriteMetrics(fmt.Sprintf("%s%s{domain=\"%s\", workerenum=\"%d\"} %d\n", uwsgi_prefix, txt, domain, iworker, Worker.Requests))
txt = "workers_delta_requests"
WriteHelp(fmt.Sprintf("# HELP %s%s Worker delta request TODO: Check uwsgiSource\n", uwsgi_prefix, txt), (iworker == 0))
WriteMetrics(fmt.Sprintf("%s%s{domain=\"%s\", workerenum=\"%d\"} %d\n", uwsgi_prefix, txt, domain, iworker, Worker.DeltaRequests))
txt = "workers_exceptions"
WriteHelp(fmt.Sprintf("# HELP %s%s Worker exceptions counter\n", uwsgi_prefix, txt), (iworker == 0))
WriteMetrics(fmt.Sprintf("%s%s{domain=\"%s\", workerenum=\"%d\"} %d\n", uwsgi_prefix, txt, domain, iworker, Worker.Exceptions))
txt = "workers_harakiri_count"
WriteHelp(fmt.Sprintf("# HELP %s%s Worker harakiri count\n", uwsgi_prefix, txt), (iworker == 0))
WriteMetrics(fmt.Sprintf("%s%s{domain=\"%s\", workerenum=\"%d\"} %d\n", uwsgi_prefix, txt, domain, iworker, Worker.HarakiriCount))
txt = "workers_signals"
WriteHelp(fmt.Sprintf("# HELP %s%s Worker signal elaborated count\n", uwsgi_prefix, txt), (iworker == 0))
WriteMetrics(fmt.Sprintf("%s%s{domain=\"%s\", workerenum=\"%d\"} %d\n", uwsgi_prefix, txt, domain, iworker, Worker.Signals))
txt = "workers_signal_queue"
WriteHelp(fmt.Sprintf("# HELP %s%s Worker signal queue\n", uwsgi_prefix, txt), (iworker == 0))
WriteMetrics(fmt.Sprintf("%s%s{domain=\"%s\", workerenum=\"%d\"} %d\n", uwsgi_prefix, txt, domain, iworker, Worker.SignalQueue))
WriteHelp(fmt.Sprintf("# HELP %s%s Worker status %d cheap %d pause %d sig %d busy %d idle\n", uwsgi_prefix, "workers_status", cheap, pause, sig, busy, idle), (iworker == 0))
// Better return number
switch Worker.Status {
case "cheap":
WriteMetrics(fmt.Sprintf("%s%s{domain=\"%s\", workerenum=\"%d\"} %d\n", uwsgi_prefix, "workers_status", domain, iworker, cheap))
case "pause":
WriteMetrics(fmt.Sprintf("%s%s{domain=\"%s\", workerenum=\"%d\"} %d\n", uwsgi_prefix, "workers_status", domain, iworker, pause))
case "sig":
WriteMetrics(fmt.Sprintf("%s%s{domain=\"%s\", workerenum=\"%d\"} %d\n", uwsgi_prefix, "workers_status", domain, iworker, sig))
case "busy":
WriteMetrics(fmt.Sprintf("%s%s{domain=\"%s\", workerenum=\"%d\"} %d\n", uwsgi_prefix, "workers_status", domain, iworker, busy))
case "idle":
WriteMetrics(fmt.Sprintf("%s%s{domain=\"%s\", workerenum=\"%d\"} %d\n", uwsgi_prefix, "workers_status", domain, iworker, idle))
}
txt = "workers_rss"
WriteHelp(fmt.Sprintf("# HELP %s%s Worker resident set size\n", uwsgi_prefix, txt), (iworker == 0))
WriteMetrics(fmt.Sprintf("%s%s{domain=\"%s\", workerenum=\"%d\"} %d\n", uwsgi_prefix, txt, domain, iworker, Worker.Rss))
txt = "workers_vsz"
WriteHelp(fmt.Sprintf("# HELP %s%s Worker virtual memory sizen", uwsgi_prefix, txt), (iworker == 0))
WriteMetrics(fmt.Sprintf("%s%s{domain=\"%s\", workerenum=\"%d\"} %d\n", uwsgi_prefix, txt, domain, iworker, Worker.Vsz))
txt = "workers_running_time"
WriteHelp(fmt.Sprintf("# HELP %s%s Worker Worker running time\n", uwsgi_prefix, txt), (iworker == 0))
WriteMetrics(fmt.Sprintf("%s%s{domain=\"%s\", workerenum=\"%d\"} %d\n", uwsgi_prefix, txt, domain, iworker, Worker.RunningTime))
txt = "workers_last_spawn"
WriteHelp(fmt.Sprintf("# HELP %s%s Last time stamp spawn\n", uwsgi_prefix, txt), (iworker == 0))
WriteMetrics(fmt.Sprintf("%s%s{domain=\"%s\", workerenum=\"%d\"} %d\n", uwsgi_prefix, txt, domain, iworker, Worker.LastSpawn))
txt = "workers_respawn_count"
WriteHelp(fmt.Sprintf("# HELP %s%s Worker respawn count\n", uwsgi_prefix, txt), (iworker == 0))
WriteMetrics(fmt.Sprintf("%s%s{domain=\"%s\", workerenum=\"%d\"} %d\n", uwsgi_prefix, txt, domain, iworker, Worker.RespawnCount))
txt = "workers_tx"
WriteHelp(fmt.Sprintf("# HELP %s%s Worker byte trasmitted\n", uwsgi_prefix, txt), (iworker == 0))
WriteMetrics(fmt.Sprintf("%s%s{domain=\"%s\", workerenum=\"%d\"} %d\n", uwsgi_prefix, txt, domain, iworker, Worker.Tx))
txt = "workers_avg_rt"
WriteHelp(fmt.Sprintf("# HELP %s%s Worker average request\n", uwsgi_prefix, txt), (iworker == 0))
WriteMetrics(fmt.Sprintf("%s%s{domain=\"%s\", workerenum=\"%d\"} %d\n", uwsgi_prefix, txt, domain, iworker, Worker.AvgRt))
for _, App := range Worker.Apps {
txt := "apps_modifier1"
WriteHelp(fmt.Sprintf("# HELP %s%s Apps modifier1 wsgi protocol\n", uwsgi_prefix, txt), (iworker == 0))
WriteMetrics(fmt.Sprintf("%s%s{domain=\"%s\", workerenum=\"%d\", app=\"%d\"} %d\n", uwsgi_prefix, txt, domain, iworker, App.ID, App.Modifier1))
txt = "apps_startup_time"
WriteHelp(fmt.Sprintf("# HELP %s%s Apps start up time\n", uwsgi_prefix, txt), (iworker == 0))
WriteMetrics(fmt.Sprintf("%s%s{domain=\"%s\", workerenum=\"%d\", app=\"%d\"} %d\n", uwsgi_prefix, txt, domain, iworker, App.ID, App.StartupTime))
txt = "apps_requests"
WriteHelp(fmt.Sprintf("# HELP %s%s App request\n", uwsgi_prefix, txt), (iworker == 0))
WriteMetrics(fmt.Sprintf("%s%s{domain=\"%s\", workerenum=\"%d\", app=\"%d\"} %d\n", uwsgi_prefix, txt, domain, iworker, App.ID, App.Requests))
txt = "apps_exceptions"
WriteHelp(fmt.Sprintf("# HELP %s%s App execeptions\n", uwsgi_prefix, txt), (iworker == 0))
WriteMetrics(fmt.Sprintf("%s%s{domain=\"%s\", workerenum=\"%d\", app=\"%d\"} %d\n", uwsgi_prefix, txt, domain, iworker, App.ID, App.Exceptions))
}
for _, Core := range Worker.Cores {
txt := "cores_requests"
WriteHelp(fmt.Sprintf("# HELP %s%s Cores requests\n", uwsgi_prefix, txt), (iworker == 0))
WriteMetrics(fmt.Sprintf("%s%s{domain=\"%s\", workerenum=\"%d\", core=\"%d\"} %d\n", uwsgi_prefix, "cores_requests", domain, iworker, Core.ID, Core.Requests))
txt = "cores_static_requests"
WriteHelp(fmt.Sprintf("# HELP %s%s Cores static resource request count\n", uwsgi_prefix, txt), (iworker == 0))
WriteMetrics(fmt.Sprintf("%s%s{domain=\"%s\", workerenum=\"%d\", core=\"%d\"} %d\n", uwsgi_prefix, "cores_static_requests", domain, iworker, Core.ID, Core.StaticRequests))
txt = "cores_routed_requests"
WriteHelp(fmt.Sprintf("# HELP %s%s Cores routed requests\n", uwsgi_prefix, txt), (iworker == 0))
WriteMetrics(fmt.Sprintf("%s%s{domain=\"%s\", workerenum=\"%d\", core=\"%d\"} %d\n", uwsgi_prefix, "cores_routed_requests", domain, iworker, Core.ID, Core.RoutedRequests))
txt = "cores_offloaded_requests"
WriteHelp(fmt.Sprintf("# HELP %s%s Cores offloaded requests\n", uwsgi_prefix, txt), (iworker == 0))
WriteMetrics(fmt.Sprintf("%s%s{domain=\"%s\", workerenum=\"%d\", core=\"%d\"} %d\n", uwsgi_prefix, "cores_offloaded_requests", domain, iworker, Core.ID, Core.OffloadedRequests))
txt = "cores_write_errors"
WriteHelp(fmt.Sprintf("# HELP %s%s Cores write errors\n", uwsgi_prefix, txt), (iworker == 0))
WriteMetrics(fmt.Sprintf("%s%s{domain=\"%s\", workerenum=\"%d\", core=\"%d\"} %d\n", uwsgi_prefix, "cores_write_errors", domain, iworker, Core.ID, Core.WriteErrors))
txt = "cores_read_errors"
WriteHelp(fmt.Sprintf("# HELP %s%s Cores read errors\n", uwsgi_prefix, txt), (iworker == 0))
WriteMetrics(fmt.Sprintf("%s%s{domain=\"%s\", workerenum=\"%d\", core=\"%d\"} %d\n", uwsgi_prefix, "cores_read_errors", domain, iworker, Core.ID, Core.ReadErrors))
txt = "cores_in_request"
WriteHelp(fmt.Sprintf("# HELP %s%s Cores in request\n", uwsgi_prefix, txt), (iworker == 0))
WriteMetrics(fmt.Sprintf("%s%s{domain=\"%s\", workerenum=\"%d\", core=\"%d\"} %d\n", uwsgi_prefix, "cores_in_request", domain, iworker, Core.ID, Core.InRequest))
}
}
// Disable help must be write only fist time or prometeus return error
globalDisableHelp = false
return StrBuilder.String()
}
/**
* @brief Get json text from File DEBUG
*/
func ProvideJsonTextFile(path string) ([]byte, error) {
b, err := ioutil.ReadFile(path)
if err != nil {
log.Criticalf("Impossible read file:%v\n", err)
}
return b, err
}
/**
* @brief Get json text from Unix Socket
*/
func ProvideJsonTextFromUnixSocket(FullPath string, connect_type string) ([]byte, error) {
if CheckUnixSocket(FullPath) {
log.Errorf("Impossible open UnixSocket %s\r\n", FullPath)
return nil, nil
}
c, err := net.Dial(connect_type, FullPath)
if err != nil {
return nil, err
}
buf := make([]byte, 1024*100)
var _nbyte int
//initial := make([]byte, 1024*100)
//nbyte, err := c.Read(buf)
//fmt.Println(string(buf[:nbyte]))
//fmt.Println(bytes.IndexByte(buf, 0))
//nbyte_, err := c.Read(tmp)
//fmt.Println(string(buf[:nbyte_]))
//fmt.Println(bytes.IndexByte(tmp, 0))
////buf = append(buf, tmp[:nbyte_]...)
//c.Close()
////fmt.Println(bytes.IndexByte(buf, 0))
//return buf[:nbyte], nil
for {
tmp := make([]byte, 1024*40)
nbyte, err := c.Read(tmp)
if err != nil {
if err != io.EOF {
log.Errorf("Cannot read socket:%v", err)
}
break
}
//fmt.Println("got", n, "bytes.")
//fmt.Println(bytes.IndexByte(tmp, 0))
//initial = tmp
//fmt.Println(nbyte)
buf = append(buf, tmp[:nbyte]...)
_nbyte += nbyte
//fmt.Println(bytes.IndexByte(buf, 0))
}
//fmt.Println("test",string(buf), len(buf) , _nbyte)
//fmt.Println("test",string(initial), len(buf) , _nbyte)
buf = bytes.Trim(buf, "\x00")
return buf[:_nbyte], nil
//fmt.Println(bytes.IndexByte(buf, 0))
//fmt.Println(string(buf[:bytes.IndexByte(buf, 0)]))
}
type uWSGI_BufferCollector struct {
buffer bytes.Buffer
mutex sync.Mutex
}
func findIP(input string) string {
numBlock := "(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])"
regexPattern := numBlock + "\\." + numBlock + "\\." + numBlock + "\\." + numBlock
regEx := regexp.MustCompile(regexPattern)
return regEx.FindString(input)
}
/**
* @brief Perform reading of uwsgi stast socket
*/
func ReadStatsSocket_uWSGI() []byte {
/*
* Worning, this function use go subroutine for parallel reading of socket.
* Function is splitted this part:
* - Create comunication channel
* - Create all go-routing with for loop
* - Read all unix socket
* - Concatenate string
* - close channel
* This part is sheduled by internal go scheduler.
*/
var wg sync.WaitGroup
var AllMetrics uWSGI_BufferCollector
AllMetrics.buffer.Reset()
log.Debugf("Map len:%d\n", len(FileMap))
// Enable help fist time
EnableHelp()
wg.Add(len(FileMap))
for Domain, FullPath := range FileMap {
go func(FullPath string, CurretDomain string) {
var connect_type string
Curret_uWSGI_Data := new(Uwsgi_json_t)
if len(findIP(FullPath)) > 0 {
connect_type = "tcp"
} else {
connect_type = "unix"
}
text, err := ProvideJsonTextFromUnixSocket(FullPath, connect_type)
if err != nil {
log.Errorf("Cannot read socket:%v", err)
wg.Done()
return
}
err = json.Unmarshal(text, Curret_uWSGI_Data)
if err != nil {
log.Errorf("Cannon Unmarshal json:%v", err)
wg.Done()
return
}
// Formatting routine is not thread safe group all in global, then look here
AllMetrics.mutex.Lock()
value := uWSGI_DataFormat(*Curret_uWSGI_Data, CurretDomain)
AllMetrics.buffer.WriteString(value)
AllMetrics.mutex.Unlock()
wg.Done()
}(FullPath, Domain)
}
wg.Wait()
return AllMetrics.buffer.Bytes()
}