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