2 * collectd - src/wpar.c
3 * Copyright (C) 2010 Manuel Sanmartin
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.
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.
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
19 * Manuel Sanmartin <manuel.luis at gmail.com>
27 # error "No applicable input method."
30 #include <sys/proc.h> /* AIX 5 */
31 #include <sys/protosw.h>
32 #include <libperfstat.h>
35 static int wpar_total_num;
36 static perfstat_wpar_total_t *wpar_total = NULL;
38 static int wpar_init(void) /* {{{ */
40 pagesize = getpagesize ();
42 } /* }}} int wpar_init */
44 static void memory_submit (const char *plugin_instance, const char *type_instance, gauge_t value) /* {{{ */
47 value_list_t vl = VALUE_LIST_INIT;
49 values[0].gauge = value;
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));
59 plugin_dispatch_values (&vl);
60 } /* }}} void memory_submit */
62 static void cpu_submit (const char *plugin_instance, const char *type_instance, counter_t value) /* {{{ */
65 value_list_t vl = VALUE_LIST_INIT;
67 values[0].counter = value;
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));
77 plugin_dispatch_values (&vl);
78 } /* }}} void cpu_submit */
80 static void load_submit (const char *plugin_instance, gauge_t snum, gauge_t mnum, gauge_t lnum) /* {{{ */
83 value_list_t vl = VALUE_LIST_INIT;
85 values[0].gauge = snum;
86 values[1].gauge = mnum;
87 values[2].gauge = lnum;
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));
96 plugin_dispatch_values (&vl);
97 } /* }}} void load_submit */
99 static int wpar_read (void) /* {{{ */
103 perfstat_id_wpar_t id_wpar;
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),
113 WARNING ("wpar plugin: perfstat_wpar_total failed: %s",
114 sstrerror (errno, errbuf, sizeof (errbuf)));
119 /* Avoid "realloc returned NULL" messages */
120 INFO ("wpar plugin: perfstat_wpar_total returned zero.");
124 /* If necessary, reallocate the "wpar_total" memory. */
125 if ((wpar_total_num != nwpar) || (wpar_total == NULL))
127 perfstat_wpar_total_t *tmp;
129 tmp = realloc (wpar_total, nwpar * sizeof (*wpar_total));
132 ERROR ("wpar plugin: realloc failed.");
137 wpar_total_num = nwpar;
139 memset (&id_wpar, 0, sizeof (id_wpar));
140 id_wpar.spec = WPARID;
141 id_wpar.u.wpar_id = FIRST_WPARID;
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);
151 WARNING ("wpar plugin: perfstat_wpar_total failed: %s",
152 sstrerror (errno, errbuf, sizeof (errbuf)));
155 else if (nwpar > wpar_total_num)
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;
162 /* Iterate over all WPARs and dispatch information */
163 for (i = 0; i < nwpar; i++)
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;
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;
179 status = perfstat_memory_total_wpar(/* id = */ &id_wpar,
180 /* (out) */ &wmemory,
181 /* size = */ sizeof(wmemory), /* nmemb = */ 1);
185 WARNING ("wpar plugin: perfstat_memory_total_wpar(%s) failed: %s",
186 wname, sstrerror (errno, errbuf, sizeof (errbuf)));
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);
197 status = perfstat_cpu_total_wpar(/* id = */ &id_wpar,
199 /* size = */ sizeof(wcpu), /* nmemb = */ 1);
203 WARNING ("wpar plugin: perfstat_cpu_total_wpar(%s) failed: %s",
204 wname, sstrerror (errno, errbuf, sizeof (errbuf)));
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;
213 load_submit (wname, snum, mnum, lnum);
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) */
222 } /* }}} int wpar_read */
224 void module_register (void)
226 plugin_register_init ("wpar", wpar_init);
227 plugin_register_read ("wpar", wpar_read);
230 /* vim: set sw=2 sts=2 et fdm=marker : */