forked from collectd/collectd
/
ipc.c
327 lines (275 loc) · 8.36 KB
/
ipc.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
/**
* collectd - src/ipc.c, based on src/memcached.c
* Copyright (C) 2010 Andres J. Diaz <ajdiaz@connectical.com>
* Copyright (C) 2010 Manuel L. Sanmartin <manuel.luis@gmail.com>
*
* 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 St, Fifth Floor, Boston, MA 02110-1301 USA
*
* Authors:
* Andres J. Diaz <ajdiaz@connectical.com>
* Manuel L. Sanmartin <manuel.luis@gmail>
**/
/* Many of this code is based on busybox ipc implementation, which is:
* (C) Rodney Radford <rradford@mindspring.com> and distributed under GPLv2.
*/
#include "collectd.h"
#include "common.h"
#include "plugin.h"
#if KERNEL_LINUX
/* _GNU_SOURCE is needed for struct shm_info.used_ids on musl libc */
#define _GNU_SOURCE
/* X/OPEN tells us to use <sys/{types,ipc,sem}.h> for semctl() */
/* X/OPEN tells us to use <sys/{types,ipc,msg}.h> for msgctl() */
/* X/OPEN tells us to use <sys/{types,ipc,shm}.h> for shmctl() */
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <sys/types.h>
/* For older kernels the same holds for the defines below */
#ifndef MSG_STAT
#define MSG_STAT 11
#define MSG_INFO 12
#endif
#ifndef SHM_STAT
#define SHM_STAT 13
#define SHM_INFO 14
struct shm_info {
int used_ids;
ulong shm_tot; /* total allocated shm */
ulong shm_rss; /* total resident shm */
ulong shm_swp; /* total swapped shm */
ulong swap_attempts;
ulong swap_successes;
};
#endif
#ifndef SEM_STAT
#define SEM_STAT 18
#define SEM_INFO 19
#endif
/* The last arg of semctl is a union semun, but where is it defined?
X/OPEN tells us to define it ourselves, but until recently
Linux include files would also define it. */
#if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
/* union semun is defined by including <sys/sem.h> */
#else
/* according to X/OPEN we have to define it ourselves */
union semun {
int val;
struct semid_ds *buf;
unsigned short *array;
struct seminfo *__buf;
};
#endif
static long pagesize_g;
/* #endif KERNEL_LINUX */
#elif KERNEL_AIX
#include <sys/ipc_info.h>
/* #endif KERNEL_AIX */
#else
#error "No applicable input method."
#endif
__attribute__((nonnull(1))) static void
ipc_submit_g(const char *plugin_instance, const char *type,
const char *type_instance, gauge_t value) /* {{{ */
{
value_t values[1];
value_list_t vl = VALUE_LIST_INIT;
values[0].gauge = value;
vl.values = values;
vl.values_len = 1;
sstrncpy(vl.host, hostname_g, sizeof(vl.host));
sstrncpy(vl.plugin, "ipc", sizeof(vl.plugin));
sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
sstrncpy(vl.type, type, sizeof(vl.type));
if (type_instance != NULL)
sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
plugin_dispatch_values(&vl);
} /* }}} */
#if KERNEL_LINUX
static int ipc_read_sem(void) /* {{{ */
{
struct seminfo seminfo;
union semun arg;
int status;
arg.array = (void *)&seminfo;
status = semctl(/* id = */ 0, /* num = */ 0, SEM_INFO, arg);
if (status == -1) {
char errbuf[1024];
ERROR("ipc plugin: semctl(2) failed: %s. "
"Maybe the kernel is not configured for semaphores?",
sstrerror(errno, errbuf, sizeof(errbuf)));
return (-1);
}
ipc_submit_g("sem", "count", "arrays", seminfo.semusz);
ipc_submit_g("sem", "count", "total", seminfo.semaem);
return (0);
} /* }}} int ipc_read_sem */
static int ipc_read_shm(void) /* {{{ */
{
struct shm_info shm_info;
int status;
status = shmctl(/* id = */ 0, SHM_INFO, (void *)&shm_info);
if (status == -1) {
char errbuf[1024];
ERROR("ipc plugin: shmctl(2) failed: %s. "
"Maybe the kernel is not configured for shared memory?",
sstrerror(errno, errbuf, sizeof(errbuf)));
return (-1);
}
ipc_submit_g("shm", "segments", NULL, shm_info.used_ids);
ipc_submit_g("shm", "bytes", "total", shm_info.shm_tot * pagesize_g);
ipc_submit_g("shm", "bytes", "rss", shm_info.shm_rss * pagesize_g);
ipc_submit_g("shm", "bytes", "swapped", shm_info.shm_swp * pagesize_g);
return (0);
}
/* }}} int ipc_read_shm */
static int ipc_read_msg(void) /* {{{ */
{
struct msginfo msginfo;
if (msgctl(0, MSG_INFO, (struct msqid_ds *)(void *)&msginfo) < 0) {
ERROR("Kernel is not configured for message queues");
return (-1);
}
ipc_submit_g("msg", "count", "queues", msginfo.msgmni);
ipc_submit_g("msg", "count", "headers", msginfo.msgmap);
ipc_submit_g("msg", "count", "space", msginfo.msgtql);
return (0);
}
/* }}} int ipc_read_msg */
static int ipc_init(void) /* {{{ */
{
pagesize_g = sysconf(_SC_PAGESIZE);
return (0);
}
/* }}} */
/* #endif KERNEL_LINUX */
#elif KERNEL_AIX
static caddr_t ipc_get_info(cid_t cid, int cmd, int version, int stsize,
int *nmemb) /* {{{ */
{
int size = 0;
caddr_t buff = NULL;
if (get_ipc_info(cid, cmd, version, buff, &size) < 0) {
if (errno != ENOSPC) {
char errbuf[1024];
WARNING("ipc plugin: get_ipc_info: %s",
sstrerror(errno, errbuf, sizeof(errbuf)));
return (NULL);
}
}
if (size == 0)
return NULL;
if (size % stsize) {
ERROR("ipc plugin: ipc_get_info: missmatch struct size and buffer size");
return (NULL);
}
*nmemb = size / stsize;
buff = malloc(size);
if (buff == NULL) {
ERROR("ipc plugin: ipc_get_info malloc failed.");
return (NULL);
}
if (get_ipc_info(cid, cmd, version, buff, &size) < 0) {
char errbuf[1024];
WARNING("ipc plugin: get_ipc_info: %s",
sstrerror(errno, errbuf, sizeof(errbuf)));
free(buff);
return (NULL);
}
return buff;
} /* }}} */
static int ipc_read_sem(void) /* {{{ */
{
ipcinfo_sem_t *ipcinfo_sem;
unsigned short sem_nsems = 0;
unsigned short sems = 0;
int n;
ipcinfo_sem = (ipcinfo_sem_t *)ipc_get_info(
0, GET_IPCINFO_SEM_ALL, IPCINFO_SEM_VERSION, sizeof(ipcinfo_sem_t), &n);
if (ipcinfo_sem == NULL)
return -1;
for (int i = 0; i < n; i++) {
sem_nsems += ipcinfo_sem[i].sem_nsems;
sems++;
}
free(ipcinfo_sem);
ipc_submit_g("sem", "count", "arrays", sem_nsems);
ipc_submit_g("sem", "count", "total", sems);
return (0);
} /* }}} int ipc_read_sem */
static int ipc_read_shm(void) /* {{{ */
{
ipcinfo_shm_t *ipcinfo_shm;
ipcinfo_shm_t *pshm;
unsigned int shm_segments = 0;
size64_t shm_bytes = 0;
int i, n;
ipcinfo_shm = (ipcinfo_shm_t *)ipc_get_info(
0, GET_IPCINFO_SHM_ALL, IPCINFO_SHM_VERSION, sizeof(ipcinfo_shm_t), &n);
if (ipcinfo_shm == NULL)
return -1;
for (i = 0, pshm = ipcinfo_shm; i < n; i++, pshm++) {
shm_segments++;
shm_bytes += pshm->shm_segsz;
}
free(ipcinfo_shm);
ipc_submit_g("shm", "segments", NULL, shm_segments);
ipc_submit_g("shm", "bytes", "total", shm_bytes);
return (0);
}
/* }}} int ipc_read_shm */
static int ipc_read_msg(void) /* {{{ */
{
ipcinfo_msg_t *ipcinfo_msg;
uint32_t msg_used_space = 0;
uint32_t msg_alloc_queues = 0;
msgqnum32_t msg_qnum = 0;
int n;
ipcinfo_msg = (ipcinfo_msg_t *)ipc_get_info(
0, GET_IPCINFO_MSG_ALL, IPCINFO_MSG_VERSION, sizeof(ipcinfo_msg_t), &n);
if (ipcinfo_msg == NULL)
return -1;
for (int i = 0; i < n; i++) {
msg_alloc_queues++;
msg_used_space += ipcinfo_msg[i].msg_cbytes;
msg_qnum += ipcinfo_msg[i].msg_qnum;
}
free(ipcinfo_msg);
ipc_submit_g("msg", "count", "queues", msg_alloc_queues);
ipc_submit_g("msg", "count", "headers", msg_qnum);
ipc_submit_g("msg", "count", "space", msg_used_space);
return (0);
}
/* }}} */
#endif /* KERNEL_AIX */
static int ipc_read(void) /* {{{ */
{
int x = 0;
x |= ipc_read_shm();
x |= ipc_read_sem();
x |= ipc_read_msg();
return (x);
}
/* }}} */
void module_register(void) /* {{{ */
{
#ifdef KERNEL_LINUX
plugin_register_init("ipc", ipc_init);
#endif
plugin_register_read("ipc", ipc_read);
}
/* }}} */
/* vim: set sw=2 sts=2 et fdm=marker : */