Merge branch 'collectd-5.5'
[collectd.git] / src / ipc.c
1 /**
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>
5  *
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.
10  *
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.
15  *
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
19  *
20  * Authors:
21  *   Andres J. Diaz <ajdiaz@connectical.com>
22  *   Manuel L. Sanmartin <manuel.luis@gmail>
23  **/
24
25 /* Many of this code is based on busybox ipc implementation, which is:
26  *   (C) Rodney Radford <rradford@mindspring.com> and distributed under GPLv2.
27  */
28
29 #include "collectd.h"
30
31 #include "common.h"
32 #include "plugin.h"
33 #include "configfile.h"
34
35 #if KERNEL_LINUX
36   /* _GNU_SOURCE is needed for struct shm_info.used_ids on musl libc */
37 # define _GNU_SOURCE
38
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>
43 # include <sys/ipc.h>
44 # include <sys/sem.h>
45 # include <sys/msg.h>
46 # include <sys/shm.h>
47
48   /* For older kernels the same holds for the defines below */
49 # ifndef MSG_STAT
50 #  define MSG_STAT    11
51 #  define MSG_INFO    12
52 # endif
53
54 # ifndef SHM_STAT
55 #   define SHM_STAT        13
56 #   define SHM_INFO        14
57     struct shm_info {
58         int used_ids;
59         ulong shm_tot;      /* total allocated shm */
60         ulong shm_rss;      /* total resident shm */
61         ulong shm_swp;      /* total swapped shm */
62         ulong swap_attempts;
63         ulong swap_successes;
64     };
65 # endif
66
67 # ifndef SEM_STAT
68 #  define SEM_STAT    18
69 #  define SEM_INFO    19
70 # endif
71
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> */
77 # else
78     /* according to X/OPEN we have to define it ourselves */
79     union semun {
80       int val;
81       struct semid_ds *buf;
82       unsigned short *array;
83       struct seminfo *__buf;
84     };
85 # endif
86 static long pagesize_g;
87 /* #endif  KERNEL_LINUX */
88 #elif KERNEL_AIX
89 # include <sys/ipc_info.h>
90 /* #endif KERNEL_AIX */
91 #else
92 # error "No applicable input method."
93 #endif
94
95 __attribute__ ((nonnull(1)))
96 static void ipc_submit_g (const char *plugin_instance,
97                           const char *type,
98                           const char *type_instance,
99                           gauge_t value) /* {{{ */
100 {
101   value_t values[1];
102   value_list_t vl = VALUE_LIST_INIT;
103
104   values[0].gauge = value;
105
106   vl.values = values;
107   vl.values_len = 1;
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));
114
115   plugin_dispatch_values (&vl);
116 } /* }}} */
117
118 #if KERNEL_LINUX
119 static int ipc_read_sem (void) /* {{{ */
120 {
121   struct seminfo seminfo;
122   union semun arg;
123   int status;
124
125   arg.array = (void *) &seminfo;
126
127   status = semctl (/* id = */ 0, /* num = */ 0, SEM_INFO, arg);
128   if (status == -1)
129   {
130     char errbuf[1024];
131     ERROR("ipc plugin: semctl(2) failed: %s. "
132         "Maybe the kernel is not configured for semaphores?",
133         sstrerror (errno, errbuf, sizeof (errbuf)));
134     return (-1);
135   }
136
137   ipc_submit_g("sem", "count", "arrays", seminfo.semusz);
138   ipc_submit_g("sem", "count", "total", seminfo.semaem);
139
140   return (0);
141 } /* }}} int ipc_read_sem */
142
143 static int ipc_read_shm (void) /* {{{ */
144 {
145   struct shm_info shm_info;
146   int status;
147
148   status = shmctl (/* id = */ 0, SHM_INFO, (void *) &shm_info);
149   if (status == -1)
150   {
151     char errbuf[1024];
152     ERROR("ipc plugin: shmctl(2) failed: %s. "
153         "Maybe the kernel is not configured for shared memory?",
154         sstrerror (errno, errbuf, sizeof (errbuf)));
155     return (-1);
156   }
157
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);
162   return (0);
163 }
164 /* }}} int ipc_read_shm */
165
166 static int ipc_read_msg (void) /* {{{ */
167 {
168   struct msginfo msginfo;
169
170   if ( msgctl(0, MSG_INFO, (struct msqid_ds *) (void *) &msginfo) < 0 )
171   {
172     ERROR("Kernel is not configured for message queues");
173     return (-1);
174   }
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);
178
179   return (0);
180 }
181 /* }}} int ipc_read_msg */
182
183 static int ipc_init (void) /* {{{ */
184 {
185   pagesize_g = sysconf(_SC_PAGESIZE);
186   return (0);
187 }
188 /* }}} */
189 /* #endif KERNEL_LINUX */
190
191 #elif KERNEL_AIX
192 static caddr_t ipc_get_info (cid_t cid, int cmd, int version, int stsize, int *nmemb) /* {{{ */
193 {
194   int size = 0;
195   caddr_t buff = NULL;
196
197   if (get_ipc_info(cid, cmd, version, buff, &size) < 0)
198   {
199     if (errno != ENOSPC) {
200       char errbuf[1024];
201       WARNING ("ipc plugin: get_ipc_info: %s",
202         sstrerror (errno, errbuf, sizeof (errbuf)));
203       return (NULL);
204     }
205   }
206
207   if (size == 0)
208     return NULL;
209
210   if (size % stsize) {
211     ERROR ("ipc plugin: ipc_get_info: missmatch struct size and buffer size");
212     return (NULL);
213   }
214
215   *nmemb = size / stsize;
216
217   buff = malloc (size);
218   if (buff == NULL)  {
219     ERROR ("ipc plugin: ipc_get_info malloc failed.");
220     return (NULL);
221   }
222
223   if (get_ipc_info(cid, cmd, version, buff, &size) < 0)
224   {
225     char errbuf[1024];
226     WARNING ("ipc plugin: get_ipc_info: %s",
227       sstrerror (errno, errbuf, sizeof (errbuf)));
228     free(buff);
229     return (NULL);
230   }
231
232   return buff;
233 } /* }}} */
234
235 static int ipc_read_sem (void) /* {{{ */
236 {
237   ipcinfo_sem_t *ipcinfo_sem;
238   unsigned short sem_nsems=0;
239   unsigned short sems=0;
240   int n;
241
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)
245     return -1;
246
247   for (int i=0; i<n; i++) {
248     sem_nsems += ipcinfo_sem[i].sem_nsems;
249     sems++;
250   }
251   free(ipcinfo_sem);
252
253   ipc_submit_g("sem", "count", "arrays", sem_nsems);
254   ipc_submit_g("sem", "count", "total", sems);
255
256   return (0);
257 } /* }}} int ipc_read_sem */
258
259 static int ipc_read_shm (void) /* {{{ */
260 {
261   ipcinfo_shm_t *ipcinfo_shm;
262   ipcinfo_shm_t *pshm;
263   unsigned int shm_segments=0;
264   size64_t shm_bytes=0;
265   int n;
266
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)
270     return -1;
271
272   for (int i=0, pshm=ipcinfo_shm; i<n; i++, pshm++) {
273     shm_segments++;
274     shm_bytes += pshm->shm_segsz;
275   }
276   free(ipcinfo_shm);
277
278   ipc_submit_g("shm", "segments", NULL, shm_segments);
279   ipc_submit_g("shm", "bytes", "total", shm_bytes);
280
281   return (0);
282 }
283 /* }}} int ipc_read_shm */
284
285 static int ipc_read_msg (void) /* {{{ */
286 {
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;
291   int n;
292
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)
296     return -1;
297
298   for (int i=0; i<n; i++) {
299     msg_alloc_queues++;
300     msg_used_space += ipcinfo_msg[i].msg_cbytes;
301     msg_qnum += ipcinfo_msg[i].msg_qnum;
302   }
303   free(ipcinfo_msg);
304
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);
308
309   return (0);
310 }
311 /* }}} */
312 #endif /* KERNEL_AIX */
313
314 static int ipc_read (void) /* {{{ */
315 {
316   int x = 0;
317   x |= ipc_read_shm();
318   x |= ipc_read_sem();
319   x |= ipc_read_msg();
320
321   return (x);
322 }
323 /* }}} */
324
325 void module_register (void) /* {{{ */
326 {
327 #ifdef KERNEL_LINUX
328   plugin_register_init ("ipc", ipc_init);
329 #endif
330   plugin_register_read ("ipc", ipc_read);
331 }
332 /* }}} */
333
334 /* vim: set sw=2 sts=2 et fdm=marker : */