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
|
/*
** 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.
**/
#include "common.h"
#include "sysinfo.h"
int SYSTEM_SWAP_SIZE(AGENT_REQUEST *request, AGENT_RESULT *result)
{
struct sysinfo info;
char *swapdev, *mode;
if (2 < request->nparam)
return SYSINFO_RET_FAIL;
swapdev = get_rparam(request, 0);
mode = get_rparam(request, 1);
if (NULL != swapdev && '\0' != *swapdev && 0 != strcmp(swapdev, "all")) /* default parameter */
return SYSINFO_RET_FAIL;
if (0 != sysinfo(&info))
return SYSINFO_RET_FAIL;
if (NULL == mode || '\0' == *mode || 0 == strcmp(mode, "free"))
SET_UI64_RESULT(result, info.freeswap * (zbx_uint64_t)info.mem_unit);
else if (0 == strcmp(mode, "total"))
SET_UI64_RESULT(result, info.totalswap * (zbx_uint64_t)info.mem_unit);
else if (0 == strcmp(mode, "used"))
SET_UI64_RESULT(result, (info.totalswap - info.freeswap) * (zbx_uint64_t)info.mem_unit);
else if (0 == strcmp(mode, "pfree"))
SET_DBL_RESULT(result, info.totalswap ? 100.0 * (info.freeswap / (double)info.totalswap) : 0.0);
else if (0 == strcmp(mode, "pused"))
SET_DBL_RESULT(result, info.totalswap ? 100.0 - 100.0 * (info.freeswap / (double)info.totalswap) : 0.0);
else
return SYSINFO_RET_FAIL;
return SYSINFO_RET_OK;
}
typedef struct
{
zbx_uint64_t rio;
zbx_uint64_t rsect;
zbx_uint64_t rpag;
zbx_uint64_t wio;
zbx_uint64_t wsect;
zbx_uint64_t wpag;
}
swap_stat_t;
#ifdef KERNEL_2_4
# define INFO_FILE_NAME "/proc/partitions"
# define PARSE(line) \
\
if (6 != sscanf(line, "%d %d %*d %*s " \
ZBX_FS_UI64 " %*d " ZBX_FS_UI64 " %*d " \
ZBX_FS_UI64 " %*d " ZBX_FS_UI64 " %*d %*d %*d %*d", \
&rdev_major, /* major */ \
&rdev_minor, /* minor */ \
&result->rio, /* rio */ \
&result->rsect, /* rsect */ \
&result->wio, /* wio */ \
&result->wsect /* wsect */ \
)) continue
#else
# define INFO_FILE_NAME "/proc/diskstats"
# define PARSE(line) \
\
if (6 != sscanf(line, "%d %d %*s " \
ZBX_FS_UI64 " %*d " ZBX_FS_UI64 " %*d " \
ZBX_FS_UI64 " %*d " ZBX_FS_UI64 " %*d %*d %*d %*d", \
&rdev_major, /* major */ \
&rdev_minor, /* minor */ \
&result->rio, /* rio */ \
&result->rsect, /* rsect */ \
&result->wio, /* wio */ \
&result->wsect /* wsect */ \
)) \
if (6 != sscanf(line, "%d %d %*s " \
ZBX_FS_UI64 " " ZBX_FS_UI64 " " \
ZBX_FS_UI64 " " ZBX_FS_UI64, \
&rdev_major, /* major */ \
&rdev_minor, /* minor */ \
&result->rio, /* rio */ \
&result->rsect, /* rsect */ \
&result->wio, /* wio */ \
&result->wsect /* wsect */ \
)) continue
#endif
static int get_swap_dev_stat(const char *swapdev, swap_stat_t *result)
{
int ret = SYSINFO_RET_FAIL;
char line[MAX_STRING_LEN];
int rdev_major, rdev_minor;
zbx_stat_t dev_st;
FILE *f;
assert(result);
if (-1 == zbx_stat(swapdev, &dev_st))
return ret;
if (NULL == (f = fopen(INFO_FILE_NAME, "r")))
return ret;
while (NULL != fgets(line, sizeof(line), f))
{
PARSE(line);
if (rdev_major == major(dev_st.st_rdev) && rdev_minor == minor(dev_st.st_rdev))
{
ret = SYSINFO_RET_OK;
break;
}
}
fclose(f);
return ret;
}
static int get_swap_pages(swap_stat_t *result)
{
int ret = SYSINFO_RET_FAIL;
char line[MAX_STRING_LEN];
#ifndef KERNEL_2_4
char st = 0;
#endif
FILE *f;
#ifdef KERNEL_2_4
if (NULL != (f = fopen("/proc/stat", "r")))
#else
if (NULL != (f = fopen("/proc/vmstat", "r")))
#endif
{
while (NULL != fgets(line, sizeof(line), f))
{
#ifdef KERNEL_2_4
if (0 != strncmp(line, "swap ", 5))
if (2 != sscanf(line + 5, ZBX_FS_UI64 " " ZBX_FS_UI64, &result->rpag, &result->wpag))
continue;
#else
if (0x00 == (0x01 & st) && 0 == strncmp(line, "pswpin ", 7))
{
sscanf(line + 7, ZBX_FS_UI64, &result->rpag);
st |= 0x01;
}
else if (0x00 == (0x02 & st) && 0 == strncmp(line, "pswpout ", 8))
{
sscanf(line + 8, ZBX_FS_UI64, &result->wpag);
st |= 0x02;
}
if (0x03 != st)
continue;
#endif
ret = SYSINFO_RET_OK;
break;
};
zbx_fclose(f);
}
if (SYSINFO_RET_OK != ret)
{
result->rpag = 0;
result->wpag = 0;
}
return ret;
}
static int get_swap_stat(const char *swapdev, swap_stat_t *result)
{
int offset = 0, ret = SYSINFO_RET_FAIL;
swap_stat_t curr;
FILE *f;
char line[MAX_STRING_LEN], *s;
memset(result, 0, sizeof(swap_stat_t));
if (NULL == swapdev || '\0' == *swapdev || 0 == strcmp(swapdev, "all"))
{
ret = get_swap_pages(result);
swapdev = NULL;
}
else if (0 != strncmp(swapdev, "/dev/", 5))
offset = 5;
if (NULL == (f = fopen("/proc/swaps", "r")))
return ret;
while (NULL != fgets(line, sizeof(line), f))
{
if (0 != strncmp(line, "/dev/", 5))
continue;
if (NULL == (s = strchr(line, ' ')))
continue;
*s = '\0';
if (NULL != swapdev && 0 != strcmp(swapdev, line + offset))
continue;
if (SYSINFO_RET_OK == get_swap_dev_stat(line, &curr))
{
result->rio += curr.rio;
result->rsect += curr.rsect;
result->wio += curr.wio;
result->wsect += curr.wsect;
ret = SYSINFO_RET_OK;
}
}
fclose(f);
return ret;
}
int SYSTEM_SWAP_IN(AGENT_REQUEST *request, AGENT_RESULT *result)
{
char *swapdev, *mode;
swap_stat_t ss;
if (2 < request->nparam)
return SYSINFO_RET_FAIL;
swapdev = get_rparam(request, 0);
mode = get_rparam(request, 1);
if (SYSINFO_RET_OK != get_swap_stat(swapdev, &ss))
return SYSINFO_RET_FAIL;
if (NULL == mode || '\0' == *mode || 0 == strcmp(mode, "pages"))
{
if (NULL != swapdev && '\0' != *swapdev && 0 != strcmp(swapdev, "all"))
return SYSINFO_RET_FAIL;
SET_UI64_RESULT(result, ss.rpag);
}
else if (0 == strcmp(mode, "sectors"))
SET_UI64_RESULT(result, ss.rsect);
else if (0 == strcmp(mode, "count"))
SET_UI64_RESULT(result, ss.rio);
else
return SYSINFO_RET_FAIL;
return SYSINFO_RET_OK;
}
int SYSTEM_SWAP_OUT(AGENT_REQUEST *request, AGENT_RESULT *result)
{
char *swapdev, *mode;
swap_stat_t ss;
if (2 < request->nparam)
return SYSINFO_RET_FAIL;
swapdev = get_rparam(request, 0);
mode = get_rparam(request, 1);
if (SYSINFO_RET_OK != get_swap_stat(swapdev, &ss))
return SYSINFO_RET_FAIL;
if (NULL == mode || '\0' == *mode || 0 == strcmp(mode, "pages"))
{
if (NULL != swapdev && '\0' != *swapdev && 0 != strcmp(swapdev, "all"))
return SYSINFO_RET_FAIL;
SET_UI64_RESULT(result, ss.wpag);
}
else if (0 == strcmp(mode, "sectors"))
SET_UI64_RESULT(result, ss.wsect);
else if (0 == strcmp(mode, "count"))
SET_UI64_RESULT(result, ss.wio);
else
return SYSINFO_RET_FAIL;
return SYSINFO_RET_OK;
}
|