16d4d33f4dcb3cd0e836ccbaa4992f32b6321c85
[collectd.git] / src / wpar.c
1 /**
2  * collectd - src/wpar.c
3  * Copyright (C) 2010  Manuel Sanmartin
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Authors:
19  *   Manuel Sanmartin <manuel.luis at gmail.com>
20  **/
21
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25
26 #if !HAVE_PERFSTAT
27 # error "No applicable input method."
28 #endif
29
30 #include <sys/proc.h> /* AIX 5 */
31 #include <sys/protosw.h>
32 #include <libperfstat.h>
33
34 static int pagesize;
35 static int wpar_total_num;
36 static perfstat_wpar_total_t *wpar_total = NULL;
37
38 static int wpar_init(void) /* {{{ */
39 {
40   pagesize = getpagesize ();
41   return (0);
42 } /* }}} int wpar_init */
43
44 static void memory_submit (const char *plugin_instance, const char *type_instance, gauge_t value) /* {{{ */
45 {
46   value_t values[1];
47   value_list_t vl = VALUE_LIST_INIT;
48
49   values[0].gauge = value;
50
51   vl.values = values;
52   vl.values_len = 1;
53   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
54   sstrncpy (vl.plugin, "wpar", sizeof (vl.plugin));
55   sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
56   sstrncpy (vl.type, "memory", sizeof (vl.type));
57   sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
58
59   plugin_dispatch_values (&vl);
60 } /* }}} void memory_submit */
61
62 static void cpu_submit (const char *plugin_instance, const char *type_instance, counter_t value) /* {{{ */
63 {
64   value_t values[1];
65   value_list_t vl = VALUE_LIST_INIT;
66
67   values[0].counter = value;
68
69   vl.values = values;
70   vl.values_len = 1;
71   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
72   sstrncpy (vl.plugin, "wpar", sizeof (vl.plugin));
73   sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
74   sstrncpy (vl.type, "cpu", sizeof (vl.type));
75   sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
76
77   plugin_dispatch_values (&vl);
78 } /* }}} void cpu_submit */
79
80 static void load_submit (const char *plugin_instance, gauge_t snum, gauge_t mnum, gauge_t lnum) /* {{{ */
81 {
82   value_t values[3];
83   value_list_t vl = VALUE_LIST_INIT;
84
85   values[0].gauge = snum;
86   values[1].gauge = mnum;
87   values[2].gauge = lnum;
88
89   vl.values = values;
90   vl.values_len = STATIC_ARRAY_SIZE (values);
91   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
92   sstrncpy (vl.plugin, "wpar", sizeof (vl.plugin));
93   sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
94   sstrncpy (vl.type, "load", sizeof (vl.type));
95
96   plugin_dispatch_values (&vl);
97 } /* }}} void load_submit */
98
99 static int wpar_read (void) /* {{{ */
100 {
101   int i;
102   int nwpar;
103   perfstat_id_wpar_t id_wpar;
104
105   /* Read the number of partitions */
106   nwpar = perfstat_wpar_total (/* id = */ NULL,
107       /* (out) wpar_total */ NULL,
108       /* size = */ sizeof (perfstat_wpar_total_t),
109       /* nmemb = */ 0);
110   if (nwpar < 0)
111   {
112     char errbuf[1024];
113     WARNING ("wpar plugin: perfstat_wpar_total failed: %s",
114         sstrerror (errno, errbuf, sizeof (errbuf)));
115     return (-1);
116   }
117   else if (nwpar == 0)
118   {
119     /* Avoid "realloc returned NULL" messages */
120     INFO ("wpar plugin: perfstat_wpar_total returned zero.");
121     return (0);
122   }
123
124   /* If necessary, reallocate the "wpar_total" memory. */
125   if ((wpar_total_num != nwpar) || (wpar_total == NULL))
126   {
127     perfstat_wpar_total_t *tmp;
128
129     tmp = realloc (wpar_total, nwpar * sizeof (*wpar_total));
130     if (tmp == NULL)
131     {
132       ERROR ("wpar plugin: realloc failed.");
133       return (ENOMEM);
134     }
135     wpar_total = tmp;
136   }
137   wpar_total_num = nwpar;
138
139   memset (&id_wpar, 0, sizeof (id_wpar));
140   id_wpar.spec = WPARID;
141   id_wpar.u.wpar_id = FIRST_WPARID;
142
143   /* Now actually query the data */
144   nwpar = perfstat_wpar_total (/* id = */ &id_wpar,
145       /* (out) */ wpar_total,
146       /* size = */ sizeof(perfstat_wpar_total_t),
147       /* nmemb = */ wpar_total_num);
148   if (nwpar < 0)
149   {
150     char errbuf[1024];
151     WARNING ("wpar plugin: perfstat_wpar_total failed: %s",
152         sstrerror (errno, errbuf, sizeof (errbuf)));
153     return (-1);
154   }
155   else if (nwpar > wpar_total_num)
156   {
157     INFO ("wpar plugin: Number of WPARs increased during allocation. "
158         "Will ignore %i WPAR(s).", nwpar - wpar_total_num);
159     nwpar = wpar_total_num;
160   }
161
162   /* Iterate over all WPARs and dispatch information */
163   for (i = 0; i < nwpar; i++)
164   {
165     const char *wname = wpar_total[i].name;
166     perfstat_memory_total_wpar_t wmemory;
167     perfstat_cpu_total_wpar_t wcpu;
168     float factor, snum, mnum, lnum;
169     int status;
170
171     /* Update the ID structure */
172     memset (&id_wpar, 0, sizeof (id_wpar));
173     id_wpar.spec = WPARID;
174     id_wpar.u.wpar_id = wpar_total[i].wpar_id;
175
176     /*
177      * Memory
178      */
179     status = perfstat_memory_total_wpar(/* id = */ &id_wpar,
180         /* (out) */ &wmemory,
181         /* size = */ sizeof(wmemory), /* nmemb = */ 1);
182     if (status < 0)
183     {
184       char errbuf[1024];
185       WARNING ("wpar plugin: perfstat_memory_total_wpar(%s) failed: %s",
186           wname, sstrerror (errno, errbuf, sizeof (errbuf)));
187       continue;
188     }
189     memory_submit (wname, "used",   wmemory.real_inuse * pagesize);
190     memory_submit (wname, "free",   wmemory.real_free * pagesize);
191     memory_submit (wname, "cached", wmemory.numperm * pagesize);
192     memory_submit (wname, "total",  wmemory.real_total * pagesize);
193
194     /*
195      * CPU and load
196      */
197     status = perfstat_cpu_total_wpar(/* id = */ &id_wpar,
198         /* (out) */ &wcpu,
199         /* size = */ sizeof(wcpu), /* nmemb = */ 1);
200     if (status < 0)
201     {
202       char errbuf[1024];
203       WARNING ("wpar plugin: perfstat_cpu_total_wpar(%s) failed: %s",
204           wname, sstrerror (errno, errbuf, sizeof (errbuf)));
205       continue;
206     }
207
208     factor = 1.0 / ((gauge_t) (1 << SBITS));
209     snum = ((gauge_t) wcpu.loadavg[0]) * factor;
210     mnum = ((gauge_t) wcpu.loadavg[1]) * factor;
211     lnum = ((gauge_t) wcpu.loadavg[2]) * factor;
212
213     load_submit (wname, snum, mnum, lnum);
214
215     cpu_submit (wname, "idle",   (counter_t) wcpu.pidle);
216     cpu_submit (wname, "system", (counter_t) wcpu.psys);
217     cpu_submit (wname, "user",   (counter_t) wcpu.puser);
218     cpu_submit (wname, "wait",   (counter_t) wcpu.pwait);
219   } /* for (i = 0 ... nwpar) */
220
221   return (0);
222 } /* }}} int wpar_read */
223
224 void module_register (void)
225 {
226   plugin_register_init ("wpar", wpar_init);
227   plugin_register_read ("wpar", wpar_read);
228 }
229
230 /* vim: set sw=2 sts=2 et fdm=marker : */