forked from SesquipedalianDefenestrator/zabbix-module-ipvs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathipvs.c
334 lines (308 loc) · 11.7 KB
/
ipvs.c
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
/*
** Zabbix
** Copyright (C) 2001-2014 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**
** Author: Cao Qingshan <[email protected]>
**
** Version: 1.6 Last update: 2015-08-09 15:00
**
**/
#include "common.h"
#include "sysinc.h"
#include "module.h"
#include "zbxjson.h"
#include "log.h"
/* the variable keeps timeout setting for item processing */
static int item_timeout = 0;
int zbx_module_ipvs_stats(AGENT_REQUEST *request, AGENT_RESULT *result);
int zbx_module_ipvs_vip_discovery(AGENT_REQUEST *request, AGENT_RESULT *result);
int zbx_module_ipvs_vip_conns(AGENT_REQUEST *request, AGENT_RESULT *result);
static ZBX_METRIC keys[] =
/* KEY FLAG FUNCTION TEST PARAMETERS */
{
{"ipvs.stats", CF_HAVEPARAMS, zbx_module_ipvs_stats, NULL},
{"ipvs.vipdiscovery", 0, zbx_module_ipvs_vip_discovery, NULL},
{"ipvs.vipconns", CF_HAVEPARAMS, zbx_module_ipvs_vip_conns, NULL},
{NULL}
};
/******************************************************************************
* *
* Function: zbx_module_api_version *
* *
* Purpose: returns version number of the module interface *
* *
* Return value: ZBX_MODULE_API_VERSION_ONE - the only version supported by *
* Zabbix currently *
* *
******************************************************************************/
int zbx_module_api_version()
{
return ZBX_MODULE_API_VERSION_ONE;
}
/******************************************************************************
* *
* Function: zbx_module_item_timeout *
* *
* Purpose: set timeout value for processing of items *
* *
* Parameters: timeout - timeout in seconds, 0 - no timeout set *
* *
******************************************************************************/
void zbx_module_item_timeout(int timeout)
{
item_timeout = timeout;
}
/******************************************************************************
* *
* Function: zbx_module_item_list *
* *
* Purpose: returns list of item keys supported by the module *
* *
* Return value: list of item keys *
* *
******************************************************************************/
ZBX_METRIC *zbx_module_item_list()
{
return keys;
}
int zbx_module_ipvs_vip_conns(AGENT_REQUEST *request, AGENT_RESULT *result)
{
int ret = SYSINFO_RET_FAIL;
char line[MAX_STRING_LEN];
FILE *f;
int vip_matches = 0;
char *vipstring;
unsigned int total_conns = 0;
unsigned int requested_ip_int;
unsigned int requested_port_int;
unsigned int quad1, quad2, quad3, quad4;
if (request->nparam != 1)
{
/* set optional error message */
SET_MSG_RESULT(result, strdup("Invalid number of parameters"));
return ret;
}
vipstring = get_rparam(request, 0);
sscanf(vipstring, "%u.%u.%u.%u:%u", &quad1,&quad2,&quad3,&quad4,&requested_port_int);
requested_ip_int = (quad1 << 24) + (quad2 << 16) + (quad3 << 8) + quad4;
if (NULL != (f = fopen("/proc/net/ip_vs", "r")))
{
while (NULL != fgets(line, sizeof(line), f))
{
//TODO: actually find our VIP and parse out connections
if (0 == strncmp(line, "IP Virtual Server", 18) || 0 == strncmp(line, "Prot LocalAddress:Port", 23) || 0 == strncmp(line, " -> RemoteAddress:Port", 24))
{
zabbix_log(LOG_LEVEL_WARNING,"Skipping line: %s", line);
continue;
}
if (0 == strncmp(line, "TCP", 3) || 0 == strncmp(line, "UDP", 3))
{
zabbix_log(LOG_LEVEL_WARNING,"Found VIP line: %s", line);
unsigned int line_ip_int;
unsigned int line_port_int;
sscanf(line, "%*s %x:%x %*s", &line_ip_int, &line_port_int);
if (requested_ip_int == line_ip_int && requested_port_int == line_port_int)
{
zabbix_log(LOG_LEVEL_WARNING,"VIP line matches");
vip_matches = 1;
continue;
}
else
{
zabbix_log(LOG_LEVEL_WARNING,"VIP line does not match");
vip_matches = 0;
continue;
}
}
if (1 == vip_matches && 0 == strncmp(line, " -> ", 5))
{
zabbix_log(LOG_LEVEL_WARNING,"Found server line for matching VIP");
int activeconns = 0;
sscanf(line,"%*s %*s %*s %*s %u", &activeconns);
total_conns += activeconns;
ret = SYSINFO_RET_OK;
}
if (0 != strncmp(line, " -> ", 5))
{
// VIP line of unknown type
vip_matches = 0;
}
}
SET_UI64_RESULT(result, (zbx_uint64_t)total_conns);
zbx_fclose(f);
}
else
{
SET_MSG_RESULT(result, zbx_strdup(NULL, "Reading /proc/net/ip_vs failed"));
}
return ret;
}
int zbx_module_ipvs_vip_discovery(AGENT_REQUEST *request, AGENT_RESULT *result)
{
int ret = SYSINFO_RET_FAIL;
char line[MAX_STRING_LEN];
FILE *f;
if (NULL != (f = fopen("/proc/net/ip_vs", "r")))
{
char *buffer = (char *)malloc(sizeof(char)*10);
zbx_strlcpy(buffer, "{\"data\":[", 10);
int vipcount=0;
while (NULL != fgets(line, sizeof(line), f))
{
if (0 == strncmp(line, "TCP", 3) || 0 == strncmp(line, "UDP", 3))
{
// VIP definition line, add it to the list
unsigned int ip_int;
unsigned int port_int = 0;
char dottedquad_ip_port[22];
char tempbuff [sizeof(dottedquad_ip_port) + sizeof(char)*15];
sscanf(line, "%*s %x:%x %*s", &ip_int, &port_int);
zbx_snprintf(dottedquad_ip_port,sizeof(dottedquad_ip_port), "%d.%d.%d.%d:%d", ip_int >> 24, (ip_int >> 16) & 0xFF, (ip_int >> 8) & 0xFF, ip_int & 0xFF, port_int);
if (vipcount==0) {
zbx_snprintf(tempbuff, sizeof(tempbuff), "{\"{#VIP}\":\"%s\"}", dottedquad_ip_port);
}
else
{
zbx_snprintf(tempbuff, sizeof(tempbuff), ",{\"{#VIP}\":\"%s\"}", dottedquad_ip_port);
}
buffer = (char*)realloc(buffer, sizeof(char)*(strlen(buffer) + strlen(tempbuff) + 1));
strcat(buffer, tempbuff);
vipcount++;
}
else
{
// only care about VIPs for discovery, ignore all other lines
continue;
}
}
buffer = (char*)realloc(buffer, sizeof(char)*(strlen(buffer) + 3));
strcat(buffer, "]}");
SET_STR_RESULT(result, strdup(buffer));
free(buffer);
zbx_fclose(f);
ret = SYSINFO_RET_OK;
}
else
{
SET_MSG_RESULT(result, zbx_strdup(NULL, "Reading /proc/net/ip_vs failed"));
}
return ret;
}
int zbx_module_ipvs_stats(AGENT_REQUEST *request, AGENT_RESULT *result)
{
int ret = SYSINFO_RET_FAIL;
char *key;
char line[MAX_STRING_LEN];
FILE *f;
int cps, inpps, outpps, inbps, outbps;
if (request->nparam != 1)
{
/* set optional error message */
SET_MSG_RESULT(result, strdup("Invalid number of parameters"));
return ret;
}
key = get_rparam(request, 0);
if (NULL != (f = fopen("/proc/net/ip_vs_stats", "r")))
{
while (NULL != fgets(line, sizeof(line), f))
{
if (0 != strncmp(line, " Conns/s", 8))
continue;
if(NULL != fgets(line, sizeof(line), f))
{
zbx_rtrim(line, "\n");
/* for dev
zabbix_log(LOG_LEVEL_INFORMATION, "module [lvs], func [zbx_module_ipvs_stats], line:[%s]", line);
*/
if(5 == sscanf(line, "%8X %8X %8X %16X %16X", &cps, &inpps, &outpps, &inbps, &outbps))
{
/* for dev
zabbix_log(LOG_LEVEL_INFORMATION, "module [lvs], func [zbx_module_ipvs_stats], cps:[%d] inpps:[%d] outpps:[%d] inbps:[%d] outbps:[%d]", cps, inpps, outpps, inbps, outbps);
*/
}
}
if (0 == strcmp(key, "cps"))
{
SET_UI64_RESULT(result, (zbx_uint64_t)cps);
ret = SYSINFO_RET_OK;
}
else if (0 == strcmp(key, "inpps"))
{
SET_UI64_RESULT(result, (zbx_uint64_t)inpps);
ret = SYSINFO_RET_OK;
}
else if (0 == strcmp(key, "outpps"))
{
SET_UI64_RESULT(result, (zbx_uint64_t)outpps);
ret = SYSINFO_RET_OK;
}
else if (0 == strcmp(key, "inbps"))
{
SET_UI64_RESULT(result, (zbx_uint64_t)inbps);
ret = SYSINFO_RET_OK;
}
else if (0 == strcmp(key, "outbps"))
{
SET_UI64_RESULT(result, (zbx_uint64_t)outbps);
ret = SYSINFO_RET_OK;
}
else
{
zabbix_log(LOG_LEVEL_WARNING, "module [ipvs], func [zbx_module_ipvs_stats], can't find key [%s]", key);
SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Not supported key [%s]", key));
}
}
zbx_fclose(f);
}
else
{
SET_MSG_RESULT(result, zbx_strdup(NULL, "Parse proc file failed"));
}
return ret;
}
/******************************************************************************
* *
* Function: zbx_module_init *
* *
* Purpose: the function is called on agent startup *
* It should be used to call any initialization routines *
* *
* Return value: ZBX_MODULE_OK - success *
* ZBX_MODULE_FAIL - module initialization failed *
* *
* Comment: the module won't be loaded in case of ZBX_MODULE_FAIL *
* *
******************************************************************************/
int zbx_module_init()
{
return ZBX_MODULE_OK;
}
/******************************************************************************
* *
* Function: zbx_module_uninit *
* *
* Purpose: the function is called on agent shutdown *
* It should be used to cleanup used resources if there are any *
* *
* Return value: ZBX_MODULE_OK - success *
* ZBX_MODULE_FAIL - function failed *
* *
******************************************************************************/
int zbx_module_uninit()
{
return ZBX_MODULE_OK;
}