2 * collectd - src/ipc.c, based on src/memcached.c
3 * Copyright (C) 2010 Andres J. Diaz <ajdiaz@connectical.com>
4 * Copyright (C) 2010 Manuel L. Sanmartin <manuel.luis@gmail.com>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 * Andres J. Diaz <ajdiaz@connectical.com>
22 * Manuel L. Sanmartin <manuel.luis@gmail>
25 /* Many of this code is based on busybox ipc implementation, which is:
26 * (C) Rodney Radford <rradford@mindspring.com> and distributed under GPLv2.
33 #include "configfile.h"
36 /* _GNU_SOURCE is needed for struct shm_info.used_ids on musl libc */
39 /* X/OPEN tells us to use <sys/{types,ipc,sem}.h> for semctl() */
40 /* X/OPEN tells us to use <sys/{types,ipc,msg}.h> for msgctl() */
41 /* X/OPEN tells us to use <sys/{types,ipc,shm}.h> for shmctl() */
42 # include <sys/types.h>
48 /* For older kernels the same holds for the defines below */
59 ulong shm_tot; /* total allocated shm */
60 ulong shm_rss; /* total resident shm */
61 ulong shm_swp; /* total swapped shm */
72 /* The last arg of semctl is a union semun, but where is it defined?
73 X/OPEN tells us to define it ourselves, but until recently
74 Linux include files would also define it. */
75 # if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
76 /* union semun is defined by including <sys/sem.h> */
78 /* according to X/OPEN we have to define it ourselves */
82 unsigned short *array;
83 struct seminfo *__buf;
86 static long pagesize_g;
87 /* #endif KERNEL_LINUX */
89 # include <sys/ipc_info.h>
90 /* #endif KERNEL_AIX */
92 # error "No applicable input method."
95 __attribute__ ((nonnull(1)))
96 static void ipc_submit_g (const char *plugin_instance,
98 const char *type_instance,
99 gauge_t value) /* {{{ */
102 value_list_t vl = VALUE_LIST_INIT;
104 values[0].gauge = value;
108 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
109 sstrncpy (vl.plugin, "ipc", sizeof (vl.plugin));
110 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
111 sstrncpy (vl.type, type, sizeof (vl.type));
112 if (type_instance != NULL)
113 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
115 plugin_dispatch_values (&vl);
119 static int ipc_read_sem (void) /* {{{ */
121 struct seminfo seminfo;
125 arg.array = (void *) &seminfo;
127 status = semctl (/* id = */ 0, /* num = */ 0, SEM_INFO, arg);
131 ERROR("ipc plugin: semctl(2) failed: %s. "
132 "Maybe the kernel is not configured for semaphores?",
133 sstrerror (errno, errbuf, sizeof (errbuf)));
137 ipc_submit_g("sem", "count", "arrays", seminfo.semusz);
138 ipc_submit_g("sem", "count", "total", seminfo.semaem);
141 } /* }}} int ipc_read_sem */
143 static int ipc_read_shm (void) /* {{{ */
145 struct shm_info shm_info;
148 status = shmctl (/* id = */ 0, SHM_INFO, (void *) &shm_info);
152 ERROR("ipc plugin: shmctl(2) failed: %s. "
153 "Maybe the kernel is not configured for shared memory?",
154 sstrerror (errno, errbuf, sizeof (errbuf)));
158 ipc_submit_g("shm", "segments", NULL, shm_info.used_ids);
159 ipc_submit_g("shm", "bytes", "total", shm_info.shm_tot * pagesize_g);
160 ipc_submit_g("shm", "bytes", "rss", shm_info.shm_rss * pagesize_g);
161 ipc_submit_g("shm", "bytes", "swapped", shm_info.shm_swp * pagesize_g);
164 /* }}} int ipc_read_shm */
166 static int ipc_read_msg (void) /* {{{ */
168 struct msginfo msginfo;
170 if ( msgctl(0, MSG_INFO, (struct msqid_ds *) (void *) &msginfo) < 0 )
172 ERROR("Kernel is not configured for message queues");
175 ipc_submit_g("msg", "count", "queues", msginfo.msgmni);
176 ipc_submit_g("msg", "count", "headers", msginfo.msgmap);
177 ipc_submit_g("msg", "count", "space", msginfo.msgtql);
181 /* }}} int ipc_read_msg */
183 static int ipc_init (void) /* {{{ */
185 pagesize_g = sysconf(_SC_PAGESIZE);
189 /* #endif KERNEL_LINUX */
192 static caddr_t ipc_get_info (cid_t cid, int cmd, int version, int stsize, int *nmemb) /* {{{ */
197 if (get_ipc_info(cid, cmd, version, buff, &size) < 0)
199 if (errno != ENOSPC) {
201 WARNING ("ipc plugin: get_ipc_info: %s",
202 sstrerror (errno, errbuf, sizeof (errbuf)));
211 ERROR ("ipc plugin: ipc_get_info: missmatch struct size and buffer size");
215 *nmemb = size / stsize;
217 buff = malloc (size);
219 ERROR ("ipc plugin: ipc_get_info malloc failed.");
223 if (get_ipc_info(cid, cmd, version, buff, &size) < 0)
226 WARNING ("ipc plugin: get_ipc_info: %s",
227 sstrerror (errno, errbuf, sizeof (errbuf)));
235 static int ipc_read_sem (void) /* {{{ */
237 ipcinfo_sem_t *ipcinfo_sem;
238 unsigned short sem_nsems=0;
239 unsigned short sems=0;
242 ipcinfo_sem = (ipcinfo_sem_t *)ipc_get_info(0,
243 GET_IPCINFO_SEM_ALL, IPCINFO_SEM_VERSION, sizeof(ipcinfo_sem_t), &n);
244 if (ipcinfo_sem == NULL)
247 for (int i=0; i<n; i++) {
248 sem_nsems += ipcinfo_sem[i].sem_nsems;
253 ipc_submit_g("sem", "count", "arrays", sem_nsems);
254 ipc_submit_g("sem", "count", "total", sems);
257 } /* }}} int ipc_read_sem */
259 static int ipc_read_shm (void) /* {{{ */
261 ipcinfo_shm_t *ipcinfo_shm;
263 unsigned int shm_segments=0;
264 size64_t shm_bytes=0;
267 ipcinfo_shm = (ipcinfo_shm_t *)ipc_get_info(0,
268 GET_IPCINFO_SHM_ALL, IPCINFO_SHM_VERSION, sizeof(ipcinfo_shm_t), &n);
269 if (ipcinfo_shm == NULL)
272 for (int i=0, pshm=ipcinfo_shm; i<n; i++, pshm++) {
274 shm_bytes += pshm->shm_segsz;
278 ipc_submit_g("shm", "segments", NULL, shm_segments);
279 ipc_submit_g("shm", "bytes", "total", shm_bytes);
283 /* }}} int ipc_read_shm */
285 static int ipc_read_msg (void) /* {{{ */
287 ipcinfo_msg_t *ipcinfo_msg;
288 uint32_t msg_used_space=0;
289 uint32_t msg_alloc_queues=0;
290 msgqnum32_t msg_qnum=0;
293 ipcinfo_msg = (ipcinfo_msg_t *)ipc_get_info(0,
294 GET_IPCINFO_MSG_ALL, IPCINFO_MSG_VERSION, sizeof(ipcinfo_msg_t), &n);
295 if (ipcinfo_msg == NULL)
298 for (int i=0; i<n; i++) {
300 msg_used_space += ipcinfo_msg[i].msg_cbytes;
301 msg_qnum += ipcinfo_msg[i].msg_qnum;
305 ipc_submit_g("msg", "count", "queues", msg_alloc_queues);
306 ipc_submit_g("msg", "count", "headers", msg_qnum);
307 ipc_submit_g("msg", "count", "space", msg_used_space);
312 #endif /* KERNEL_AIX */
314 static int ipc_read (void) /* {{{ */
325 void module_register (void) /* {{{ */
328 plugin_register_init ("ipc", ipc_init);
330 plugin_register_read ("ipc", ipc_read);
334 /* vim: set sw=2 sts=2 et fdm=marker : */