Change IPC plugin to use type_instances.
[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 *plugin_instance,
93                           const char *type,
94                           const char *type_instance,
95                           gauge_t value) /* {{{ */
96 {
97   value_t values[1];
98   value_list_t vl = VALUE_LIST_INIT;
99
100   values[0].gauge = value;
101
102   vl.values = values;
103   vl.values_len = 1;
104   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
105   sstrncpy (vl.plugin, "ipc", sizeof (vl.plugin));
106   sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
107   sstrncpy (vl.type, type, sizeof (vl.type));
108   if (type_instance != NULL)
109     sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
110
111   plugin_dispatch_values (&vl);
112 } /* }}} */
113
114 #if KERNEL_AIX
115 static caddr_t ipc_get_info (cid_t cid, int cmd, int version, int stsize, int *nmemb) /* {{{ */
116 {
117   int size = 0;
118   caddr_t buff = NULL;
119
120   if (get_ipc_info(cid, cmd, version, buff, &size) < 0)
121   {
122     if (errno != ENOSPC) {
123       char errbuf[1024];
124       WARNING ("ipc plugin: get_ipc_info: %s",
125         sstrerror (errno, errbuf, sizeof (errbuf)));
126       return (NULL);
127     }
128   }
129
130   if (size == 0)
131     return NULL;
132
133   if (size % stsize) {
134     ERROR ("ipc plugin: ipc_get_info: missmatch struct size and buffer size");
135     return (NULL);
136   }
137
138   *nmemb = size / stsize;
139
140   buff = (caddr_t)malloc (size);
141   if (buff == NULL)  {
142     ERROR ("ipc plugin: ipc_get_info malloc failed.");
143     return (NULL);
144   }
145
146   if (get_ipc_info(cid, cmd, version, buff, &size) < 0)
147   {
148     char errbuf[1024];
149     WARNING ("ipc plugin: get_ipc_info: %s",
150       sstrerror (errno, errbuf, sizeof (errbuf)));
151     free(buff);
152     return (NULL);
153   }
154
155   return buff;
156 } /* }}} */
157 #endif /* KERNEL_AIX */
158
159 static int ipc_read_sem (void) /* {{{ */
160 {
161 #if KERNEL_LINUX
162   struct seminfo seminfo;
163   union semun arg;
164
165   arg.array = (ushort *) (void *) &seminfo;
166
167   if ( semctl(0, 0, SEM_INFO, arg) < 0 )
168   {
169     ERROR("Kernel is not configured for semaphores");
170     return (-1);
171   }
172
173   ipc_submit_g("sem", "count", "arrays", seminfo.semusz);
174   ipc_submit_g("sem", "count", "total", seminfo.semaem);
175
176 /* #endif KERNEL_LINUX */
177 #elif KERNEL_AIX
178   ipcinfo_sem_t *ipcinfo_sem;
179   unsigned short sem_nsems=0;
180   unsigned short sems=0;
181   int i,n;
182
183   ipcinfo_sem = (ipcinfo_sem_t *)ipc_get_info(0,
184     GET_IPCINFO_SEM_ALL, IPCINFO_SEM_VERSION, sizeof(ipcinfo_sem_t), &n);
185   if (ipcinfo_sem == NULL)
186     return -1;
187
188   for (i=0; i<n; i++) {
189     sem_nsems += ipcinfo_sem[i].sem_nsems;
190     sems++;
191   }
192   free(ipcinfo_sem);
193
194   ipc_submit_g("sem", "count", "arrays", sem_nsems);
195   ipc_submit_g("sem", "count", "total", sems);
196 #endif /* KERNEL_AIX */
197
198   return (0);
199 }
200 /* }}} */
201
202 static int ipc_read_shm (void) /* {{{ */
203 {
204 #if KERNEL_LINUX
205   struct shm_info shm_info;
206   pagesize_g = sysconf(_SC_PAGESIZE);
207
208   if ( shmctl(0, SHM_INFO, (struct shmid_ds *) (void *) &shm_info) < 0 )
209   {
210     ERROR("Kernel is not configured for shared memory");
211     return (-1);
212   }
213   ipc_submit_g("shm", "segments", NULL, shm_info.used_ids);
214   ipc_submit_g("shm", "bytes", "total", shm_info.shm_tot * pagesize_g);
215   ipc_submit_g("shm", "bytes", "rss", shm_info.shm_rss * pagesize_g);
216   ipc_submit_g("shm", "bytes", "swapped", shm_info.shm_swp * pagesize_g);
217 /* #endif KERNEL_LINUX */
218 #elif KERNEL_AIX
219   ipcinfo_shm_t *ipcinfo_shm;
220   ipcinfo_shm_t *pshm;
221   unsigned int shm_segments=0;
222   size64_t shm_bytes=0;
223   int i,n;
224
225   ipcinfo_shm = (ipcinfo_shm_t *)ipc_get_info(0,
226     GET_IPCINFO_SHM_ALL, IPCINFO_SHM_VERSION, sizeof(ipcinfo_shm_t), &n);
227   if (ipcinfo_shm == NULL)
228     return -1;
229
230   for (i=0, pshm=ipcinfo_shm; i<n; i++, pshm++) {
231     shm_segments++;
232     shm_bytes += pshm->shm_segsz;
233   }
234   free(ipcinfo_shm);
235
236   ipc_submit_g("shm", "segments", NULL, shm_segments);
237   ipc_submit_g("shm", "bytes", "total", shm_bytes);
238
239 #endif /* KERNEL_AIX */
240   return (0);
241 }
242 /* }}} */
243
244 static int ipc_read_msg (void) /* {{{ */
245 {
246 #if KERNEL_LINUX
247   struct msginfo msginfo;
248
249   if ( msgctl(0, MSG_INFO, (struct msqid_ds *) (void *) &msginfo) < 0 )
250   {
251     ERROR("Kernel is not configured for message queues");
252     return (-1);
253   }
254   ipc_submit_g("msg", "count", "queues", msginfo.msgmni);
255   ipc_submit_g("msg", "count", "headers", msginfo.msgmap);
256   ipc_submit_g("msg", "count", "space", msginfo.msgtql);
257 /* #endif KERNEL_LINUX */
258 #elif KERNEL_AIX
259   ipcinfo_msg_t *ipcinfo_msg;
260   uint32_t msg_used_space=0;
261   uint32_t msg_alloc_queues=0;
262   msgqnum32_t msg_qnum=0;
263   int i,n;
264
265   ipcinfo_msg = (ipcinfo_msg_t *)ipc_get_info(0,
266     GET_IPCINFO_MSG_ALL, IPCINFO_MSG_VERSION, sizeof(ipcinfo_msg_t), &n);
267   if (ipcinfo_msg == NULL)
268     return -1;
269
270   for (i=0; i<n; i++) {
271     msg_alloc_queues++;
272     msg_used_space += ipcinfo_msg[i].msg_cbytes;
273     msg_qnum += ipcinfo_msg[i].msg_qnum;
274   }
275   free(ipcinfo_msg);
276
277   ipc_submit_g("msg", "count", "queues", msg_alloc_queues);
278   ipc_submit_g("msg", "count", "headers", msg_qnum);
279   ipc_submit_g("msg", "count", "space", msg_used_space);
280 #endif /* KERNEL_AIX */
281   return (0);
282 }
283 /* }}} */
284
285 static int ipc_read (void) /* {{{ */
286 {
287   int x = 0;
288   x |= ipc_read_shm();
289   x |= ipc_read_sem();
290   x |= ipc_read_msg();
291
292   return (x);
293 }
294 /* }}} */
295
296 #ifdef KERNEL_LINUX
297 static int ipc_init (void) /* {{{ */
298 {
299   pagesize_g = sysconf(_SC_PAGESIZE);
300   return (0);
301 }
302 /* }}} */
303 #endif /* KERNEL_LINUX */
304
305 void module_register (void) /* {{{ */
306 {
307 #ifdef KERNEL_LINUX
308   plugin_register_init ("ipc", ipc_init);
309 #endif
310   plugin_register_read ("ipc", ipc_read);
311 }
312 /* }}} */
313
314 /* vim: set sw=2 sts=2 et fdm=marker : */