forked from pereiro/smtprelay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats.go
149 lines (126 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
package main
import (
"encoding/json"
"net/http"
)
func GetErrorQueueLength() int64 {
return int64(len(ErrorChannel))
}
func GetMailQueueLength() int64 {
return int64(len(MailChannel))
}
const (
STATISTICS_CHANNELS_SIZE = 100000
)
var (
MailSendersCounter int64
MailHandlersCounter int64
MaxQueueCounter int64
MailSentCounter int64
MailDroppedCounter int64
MailSentChannel chan int
MailDroppedChannel chan int
MailMaxQueueChannel chan int
MailHandlersChannel chan int
MailSendersChannel chan int
)
func InitStatistics() {
MailSentChannel = make(chan int)
MailDroppedChannel = make(chan int, STATISTICS_CHANNELS_SIZE)
MailMaxQueueChannel = make(chan int, STATISTICS_CHANNELS_SIZE)
MailHandlersChannel = make(chan int, STATISTICS_CHANNELS_SIZE)
MailSendersChannel = make(chan int, STATISTICS_CHANNELS_SIZE)
go func() {
for val := range MailSentChannel {
MailSentCounter += int64(val)
}
}()
go func() {
for val := range MailDroppedChannel {
MailDroppedCounter += int64(val)
}
}()
go func() {
for val := range MailMaxQueueChannel {
if int64(val) > MaxQueueCounter {
MaxQueueCounter = int64(val)
}
}
}()
go func() {
for val := range MailHandlersChannel {
MailHandlersCounter += int64(val)
}
}()
go func() {
for val := range MailSendersChannel {
MailSendersCounter += int64(val)
}
}()
}
type QueueStats struct {
OverallCounter int64
ErrorBufferCounter int64
MailBufferCounter int64
OutboundSMTPConnects int64
InboundTCPHandlers int64
InboundTCPConnects int64
InboundSMTPConnects int64
MaxQueueSizeSinceLastRestart int64
MailSentSinceLastRestart int64
MailDroppedSinceLastRestart int64
Configuration *Conf
}
func GetStatistics() (data []byte, err error) {
var stats QueueStats
stats.OutboundSMTPConnects = MailSendersCounter
stats.InboundSMTPConnects = MailHandlersCounter
stats.ErrorBufferCounter = int64(len(ErrorChannel))
stats.MailBufferCounter = int64(len(MailChannel))
stats.InboundTCPHandlers = int64(len(TCPHandlersLimiter))
stats.InboundTCPConnects = int64(len(TCPConnectionsLimiter))
stats.OverallCounter = stats.ErrorBufferCounter + stats.MailBufferCounter
stats.MaxQueueSizeSinceLastRestart = MaxQueueCounter
stats.MailSentSinceLastRestart = MailSentCounter
stats.MailDroppedSinceLastRestart = MailDroppedCounter
stats.Configuration = conf
data, err = json.Marshal(stats)
if err != nil {
return data, err
}
return data, err
}
func MailSentIncreaseCounter(count int) {
MailSentChannel <- count
}
func MailDroppedIncreaseCounter(count int) {
MailDroppedChannel <- count
}
func MailQueueCheckMax() {
MailMaxQueueChannel <- int(GetErrorQueueLength() + GetMailQueueLength())
}
func MailSendersIncreaseCounter(count int) {
MailSendersChannel <- count
}
func MailSendersDecreaseCounter(count int) {
MailSendersChannel <- -count
}
func MailHandlersIncreaseCounter(count int) {
MailHandlersChannel <- count
}
func MailHandlersDecreaseCounter(count int) {
MailHandlersChannel <- -count
}
func StatisticHandler(w http.ResponseWriter, r *http.Request) {
js, err := GetStatistics()
if err != nil {
log.Error("can't start statistics server:%s", err)
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}
func StartStatisticServer() {
InitStatistics()
http.HandleFunc("/", StatisticHandler)
http.ListenAndServe(":"+conf.StatisticPort, nil)
}