/
nginx_counter.go
181 lines (153 loc) · 4.5 KB
/
nginx_counter.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
/**
* Copyright (c) F5, Inc.
*
* This source code is licensed under the Apache License, Version 2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
package plugins
import (
"context"
"encoding/json"
"net"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"time"
log "github.com/sirupsen/logrus"
agent_config "github.com/nginx/agent/sdk/v2/agent/config"
"github.com/nginx/agent/sdk/v2/proto"
"github.com/nginx/agent/v2/src/core"
"github.com/nginx/agent/v2/src/core/config"
"go.uber.org/atomic"
)
type NginxCounter struct {
ctx context.Context
conf *config.Config
signalChannel chan os.Signal
nginxBinary core.NginxBinary
env core.Environment
lastUpdated int64
serverAddress []string
connected *atomic.Bool
socketListener net.Listener
processMutex sync.RWMutex
nginxes map[string]*proto.NginxDetails
}
type Payload struct {
LastUpdated int64 `json:",string"`
}
func NewNginxCounter(conf *config.Config, nginxBinary core.NginxBinary, env core.Environment) *NginxCounter {
return &NginxCounter{
conf: conf,
signalChannel: make(chan os.Signal, 1),
nginxBinary: nginxBinary,
env: env,
lastUpdated: time.Now().Unix(),
connected: atomic.NewBool(false),
}
}
func (nc *NginxCounter) Init(pipeline core.MessagePipeInterface) {
log.Infof("NGINX Counter initializing %v", nc.conf.Nginx)
nc.serverAddress = strings.Split(nc.conf.Nginx.NginxCountingSocket, ":")
nc.ctx = pipeline.Context()
if nc.serverAddress[0] == "unix" {
if err := os.RemoveAll(nc.serverAddress[1]); err != nil {
log.Warn("Can not parse server socket address, is the configuration correct?")
}
}
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
nc.agentServer(nc.serverAddress)
}()
wg.Wait()
}
func (nc *NginxCounter) agentServer(serverAddress []string) {
var err error
signal.Notify(nc.signalChannel, syscall.SIGINT, os.Interrupt, syscall.SIGTERM)
nc.socketListener, err = net.Listen(serverAddress[0], serverAddress[1])
if err != nil {
log.Warn("failed to start NGINX counter listener")
}
err = core.EnableWritePermissionForSocket(serverAddress[1])
if err != nil {
log.Warn("unable to set correct write permissions for NGINX counter socket")
}
if nc.socketListener != nil {
go nc.recieveSocketMessage(nc.socketListener)
}
}
func (nc *NginxCounter) recieveSocketMessage(socketListener net.Listener) {
for {
connectionResponse, err := socketListener.Accept()
if err != nil {
log.Warnf("Unable to accept from NGINX counter socket")
break
}
go nc.handleResponse(connectionResponse)
}
}
func (nc *NginxCounter) Close() {
if nc.socketListener != nil {
nc.socketListener.Close()
}
log.Info("NGINX Counter is wrapping up")
if err := os.RemoveAll(nc.serverAddress[1]); err != nil {
log.Warn("Error removing socket")
}
}
func (nc *NginxCounter) Info() *core.Info {
return core.NewInfo(agent_config.FeatureNginxCounting, "v0.0.1")
}
func (nc *NginxCounter) Process(msg *core.Message) {
switch {
case msg.Exact(core.AgentConnected):
nc.connected.Toggle()
case msg.Exact(core.NginxDetailProcUpdate):
// get the processes from this payload
nc.processMutex.Lock()
processes := msg.Data().([]*core.Process)
nc.nginxBinary.UpdateNginxDetailsFromProcesses(processes)
nc.nginxes = nc.nginxBinary.GetNginxDetailsMapFromProcesses(processes)
defer nc.processMutex.Unlock()
}
}
func (nc *NginxCounter) Subscriptions() []string {
return []string{core.NginxDetailProcUpdate, core.AgentConnected}
}
func (nc *NginxCounter) handleResponse(connection net.Conn) {
log.Trace("Receiving Server")
for {
buf := make([]byte, 512)
responseBytes, err := connection.Read(buf)
if err != nil {
return
}
bufferedResponse := buf[0:responseBytes]
log.Tracef("Socket Server handleResponse: %s", string(bufferedResponse))
if nc.connected.Load() {
nc.processMutex.RLock()
nginxes := nc.nginxes
nc.processMutex.RUnlock()
log.Tracef("Reporting the following nginx instances: %v", nginxes)
for _, nginx := range nginxes {
if nginx.Plus.Enabled {
nc.lastUpdated = time.Now().Unix()
log.Debugf("lastUpdated %d nginxId: %s", nc.lastUpdated, nginx.NginxId)
payload := Payload{LastUpdated: nc.lastUpdated}
data, err := json.Marshal(payload)
if err != nil {
log.Warnf("err trying to unmarshal payload %v", err)
}
_, err = connection.Write(data)
if err != nil {
log.Warn("Could not write to NGINX counter socket")
}
}
}
}
}
}