Merge branch 'collectd-5.6'
[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
34 #if KERNEL_LINUX
35   /* _GNU_SOURCE is needed for struct shm_info.used_ids on musl libc */
36 # define _GNU_SOURCE
37
38   /* X/OPEN tells us to use <sys/{types,ipc,sem}.h> for semctl() */
39   /* X/OPEN tells us to use <sys/{types,ipc,msg}.h> for msgctl() */
40   /* X/OPEN tells us to use <sys/{types,ipc,shm}.h> for shmctl() */
41 # include <sys/types.h>
42 # include <sys/ipc.h>
43 # include <sys/sem.h>
44 # include <sys/msg.h>
45 # include <sys/shm.h>
46
47   /* For older kernels the same holds for the defines below */
48 # ifndef MSG_STAT
49 #  define MSG_STAT    11
50 #  define MSG_INFO    12
51 # endif
52
53 # ifndef SHM_STAT
54 #   define SHM_STAT        13
55 #   define SHM_INFO        14
56     struct shm_info {
57         int used_ids;
58         ulong shm_tot;      /* total allocated shm */
59         ulong shm_rss;      /* total resident shm */
60         ulong shm_swp;      /* total swapped shm */
61         ulong swap_attempts;
62         ulong swap_successes;
63     };
64 # endif
65
66 # ifndef SEM_STAT
67 #  define SEM_STAT    18
68 #  define SEM_INFO    19
69 # endif
70
71   /* The last arg of semctl is a union semun, but where is it defined?
72      X/OPEN tells us to define it ourselves, but until recently
73      Linux include files would also define it. */
74 # if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
75     /* union semun is defined by including <sys/sem.h> */
76 # else
77     /* according to X/OPEN we have to define it ourselves */
78     union semun {
79       int val;
80       struct semid_ds *buf;
81       unsigned short *array;
82       struct seminfo *__buf;
83     };
84 # endif
85 static long pagesize_g;
86 /* #endif  KERNEL_LINUX */
87 #elif KERNEL_AIX
88 # include <sys/ipc_info.h>
89 /* #endif KERNEL_AIX */
90 #else
91 # error "No applicable input method."
92 #endif
93
94 __attribute__ ((nonnull(1)))
95 static void ipc_submit_g (const char *plugin_instance,
96                           const char *type,
97                           const char *type_instance,
98                           gauge_t value) /* {{{ */
99 {
100   value_list_t vl = VALUE_LIST_INIT;
101
102   vl.values = &(value_t) { .gauge = value };
103   vl.values_len = 1;
104   sstrncpy (vl.plugin, "ipc", sizeof (vl.plugin));
105   sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
106   sstrncpy (vl.type, type, sizeof (vl.type));
107   if (type_instance != NULL)
108     sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
109
110   plugin_dispatch_values (&vl);
111 } /* }}} */
112
113 #if KERNEL_LINUX
114 static int ipc_read_sem (void) /* {{{ */
115 {
116   struct seminfo seminfo;
117   union semun arg;
118   int status;
119
120   arg.array = (void *) &seminfo;
121
122   status = semctl (/* id = */ 0, /* num = */ 0, SEM_INFO, arg);
123   if (status == -1)
124   {
125     char errbuf[1024];
126     ERROR("ipc plugin: semctl(2) failed: %s. "
127         "Maybe the kernel is not configured for semaphores?",
128         sstrerror (errno, errbuf, sizeof (errbuf)));
129     return (-1);
130   }
131
132   ipc_submit_g("sem", "count", "arrays", seminfo.semusz);
133   ipc_submit_g("sem", "count", "total", seminfo.semaem);
134
135   return (0);
136 } /* }}} int ipc_read_sem */
137
138 static int ipc_read_shm (void) /* {{{ */
139 {
140   struct shm_info shm_info;
141   int status;
142
143   status = shmctl (/* id = */ 0, SHM_INFO, (void *) &shm_info);
144   if (status == -1)
145   {
146     char errbuf[1024];
147     ERROR("ipc plugin: shmctl(2) failed: %s. "
148         "Maybe the kernel is not configured for shared memory?",
149         sstrerror (errno, errbuf, sizeof (errbuf)));
150     return (-1);
151   }
152
153   ipc_submit_g("shm", "segments", NULL, shm_info.used_ids);
154   ipc_submit_g("shm", "bytes", "total", shm_info.shm_tot * pagesize_g);
155   ipc_submit_g("shm", "bytes", "rss", shm_info.shm_rss * pagesize_g);
156   ipc_submit_g("shm", "bytes", "swapped", shm_info.shm_swp * pagesize_g);
157   return (0);
158 }
159 /* }}} int ipc_read_shm */
160
161 static int ipc_read_msg (void) /* {{{ */
162 {
163   struct msginfo msginfo;
164
165   if ( msgctl(0, MSG_INFO, (struct msqid_ds *) (void *) &msginfo) < 0 )
166   {
167     ERROR("Kernel is not configured for message queues");
168     return (-1);
169   }
170   ipc_submit_g("msg", "count", "queues", msginfo.msgmni);
171   ipc_submit_g("msg", "count", "headers", msginfo.msgmap);
172   ipc_submit_g("msg", "count", "space", msginfo.msgtql);
173
174   return (0);
175 }
176 /* }}} int ipc_read_msg */
177
178 static int ipc_init (void) /* {{{ */
179 {
180   pagesize_g = sysconf(_SC_PAGESIZE);
181   return (0);
182 }
183 /* }}} */
184 /* #endif KERNEL_LINUX */
185
186 #elif KERNEL_AIX
187 static caddr_t ipc_get_info (cid_t cid, int cmd, int version, int stsize, int *nmemb) /* {{{ */
188 {
189   int size = 0;
190   caddr_t buff = NULL;
191
192   if (get_ipc_info(cid, cmd, version, buff, &size) < 0)
193   {
194     if (errno != ENOSPC) {
195       char errbuf[1024];
196       WARNING ("ipc plugin: get_ipc_info: %s",
197         sstrerror (errno, errbuf, sizeof (errbuf)));
198       return (NULL);
199     }
200   }
201
202   if (size == 0)
203     return NULL;
204
205   if (size % stsize) {
206     ERROR ("ipc plugin: ipc_get_info: missmatch struct size and buffer size");
207     return (NULL);
208   }
209
210   *nmemb = size / stsize;
211
212   buff = malloc (size);
213   if (buff == NULL)  {
214     ERROR ("ipc plugin: ipc_get_info malloc failed.");
215     return (NULL);
216   }
217
218   if (get_ipc_info(cid, cmd, version, buff, &size) < 0)
219   {
220     char errbuf[1024];
221     WARNING ("ipc plugin: get_ipc_info: %s",
222       sstrerror (errno, errbuf, sizeof (errbuf)));
223     free(buff);
224     return (NULL);
225   }
226
227   return buff;
228 } /* }}} */
229
230 static int ipc_read_sem (void) /* {{{ */
231 {
232   ipcinfo_sem_t *ipcinfo_sem;
233   unsigned short sem_nsems=0;
234   unsigned short sems=0;
235   int n;
236
237   ipcinfo_sem = (ipcinfo_sem_t *)ipc_get_info(0,
238     GET_IPCINFO_SEM_ALL, IPCINFO_SEM_VERSION, sizeof(ipcinfo_sem_t), &n);
239   if (ipcinfo_sem == NULL)
240     return -1;
241
242   for (int i=0; i<n; i++) {
243     sem_nsems += ipcinfo_sem[i].sem_nsems;
244     sems++;
245   }
246   free(ipcinfo_sem);
247
248   ipc_submit_g("sem", "count", "arrays", sem_nsems);
249   ipc_submit_g("sem", "count", "total", sems);
250
251   return (0);
252 } /* }}} int ipc_read_sem */
253
254 static int ipc_read_shm (void) /* {{{ */
255 {
256   ipcinfo_shm_t *ipcinfo_shm;
257   ipcinfo_shm_t *pshm;
258   unsigned int shm_segments=0;
259   size64_t shm_bytes=0;
260   int n;
261
262   ipcinfo_shm = (ipcinfo_shm_t *)ipc_get_info(0,
263     GET_IPCINFO_SHM_ALL, IPCINFO_SHM_VERSION, sizeof(ipcinfo_shm_t), &n);
264   if (ipcinfo_shm == NULL)
265     return -1;
266
267   for (int i=0, pshm=ipcinfo_shm; i<n; i++, pshm++) {
268     shm_segments++;
269     shm_bytes += pshm->shm_segsz;
270   }
271   free(ipcinfo_shm);
272
273   ipc_submit_g("shm", "segments", NULL, shm_segments);
274   ipc_submit_g("shm", "bytes", "total", shm_bytes);
275
276   return (0);
277 }
278 /* }}} int ipc_read_shm */
279
280 static int ipc_read_msg (void) /* {{{ */
281 {
282   ipcinfo_msg_t *ipcinfo_msg;
283   uint32_t msg_used_space=0;
284   uint32_t msg_alloc_queues=0;
285   msgqnum32_t msg_qnum=0;
286   int n;
287
288   ipcinfo_msg = (ipcinfo_msg_t *)ipc_get_info(0,
289     GET_IPCINFO_MSG_ALL, IPCINFO_MSG_VERSION, sizeof(ipcinfo_msg_t), &n);
290   if (ipcinfo_msg == NULL)
291     return -1;
292
293   for (int i=0; i<n; i++) {
294     msg_alloc_queues++;
295     msg_used_space += ipcinfo_msg[i].msg_cbytes;
296     msg_qnum += ipcinfo_msg[i].msg_qnum;
297   }
298   free(ipcinfo_msg);
299
300   ipc_submit_g("msg", "count", "queues", msg_alloc_queues);
301   ipc_submit_g("msg", "count", "headers", msg_qnum);
302   ipc_submit_g("msg", "count", "space", msg_used_space);
303
304   return (0);
305 }
306 /* }}} */
307 #endif /* KERNEL_AIX */
308
309 static int ipc_read (void) /* {{{ */
310 {
311   int x = 0;
312   x |= ipc_read_shm();
313   x |= ipc_read_sem();
314   x |= ipc_read_msg();
315
316   return (x);
317 }
318 /* }}} */
319
320 void module_register (void) /* {{{ */
321 {
322 #ifdef KERNEL_LINUX
323   plugin_register_init ("ipc", ipc_init);
324 #endif
325   plugin_register_read ("ipc", ipc_read);
326 }
327 /* }}} */
328
329 /* vim: set sw=2 sts=2 et fdm=marker : */