new plugin: AIX WPAR
[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 int nwpar = -1;
36 int pwpar;
37 static perfstat_wpar_total_t *wpar_total;
38 static perfstat_memory_total_wpar_t wmemory;
39 static perfstat_cpu_total_wpar_t wcpu;
40
41 static int wpar_init(void)
42 {
43   pagesize = getpagesize ();
44   return (0);
45 }
46
47 static void memory_submit (const char *plugin_instance, const char *type_instance, gauge_t value)
48 {
49   value_t values[1];
50   value_list_t vl = VALUE_LIST_INIT;
51
52   values[0].gauge = value;
53
54   vl.values = values;
55   vl.values_len = 1;
56   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
57   sstrncpy (vl.plugin, "wpar", sizeof (vl.plugin));
58   sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
59   sstrncpy (vl.type, "memory", sizeof (vl.type));
60   sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
61
62   plugin_dispatch_values (&vl);
63 }
64
65 static void cpu_submit (const char *plugin_instance, const char *type_instance, counter_t value)
66 {
67   value_t values[1];
68   value_list_t vl = VALUE_LIST_INIT;
69
70   values[0].counter = value;
71
72   vl.values = values;
73   vl.values_len = 1;
74   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
75   sstrncpy (vl.plugin, "wpar", sizeof (vl.plugin));
76   sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
77   sstrncpy (vl.type, "cpu", sizeof (vl.type));
78   sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
79
80   plugin_dispatch_values (&vl);
81 }
82
83 static void load_submit (const char *plugin_instance, gauge_t snum, gauge_t mnum, gauge_t lnum)
84 {
85   value_t values[3];
86   value_list_t vl = VALUE_LIST_INIT;
87
88   values[0].gauge = snum;
89   values[1].gauge = mnum;
90   values[2].gauge = lnum;
91
92   vl.values = values;
93   vl.values_len = STATIC_ARRAY_SIZE (values);
94   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
95   sstrncpy (vl.plugin, "load", sizeof (vl.plugin));
96   sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
97   sstrncpy (vl.type, "load", sizeof (vl.type));
98
99   plugin_dispatch_values (&vl);
100 }
101
102 static int wpar_read (void)
103 {
104   int i,wpars;
105   float snum, mnum, lnum;
106   perfstat_id_wpar_t id_wpar;
107
108   nwpar = perfstat_wpar_total(NULL, NULL, sizeof(perfstat_wpar_total_t), 0);
109   if (nwpar == -1)
110   {
111     char errbuf[1024];
112     WARNING ("wpar plugin: perfstat_wpar_total: %s",
113         sstrerror (errno, errbuf, sizeof (errbuf)));
114     return (-1);
115   }
116
117   if (pwpar != nwpar ||  wpar_total == NULL)
118   {
119     if (wpar_total != NULL)
120       free(wpar_total);
121     wpar_total = malloc(nwpar * sizeof(perfstat_wpar_total_t));
122   }
123   pwpar = nwpar;
124
125   bzero(&id_wpar, sizeof(perfstat_id_wpar_t));
126   id_wpar.spec = WPARID;
127   id_wpar.u.wpar_id = FIRST_WPARID;
128   if ((wpars = perfstat_wpar_total(&id_wpar, wpar_total, sizeof(perfstat_wpar_total_t), nwpar)) < 0)
129   {
130     char errbuf[1024];
131     WARNING ("cpu plugin: perfstat_wpar_total: %s",
132         sstrerror (errno, errbuf, sizeof (errbuf)));
133     return (-1);
134   }
135
136   for (i = 0; i < wpars; i++)
137   {
138     char *wname = wpar_total[i].name;
139
140     bzero(&id_wpar, sizeof(perfstat_id_wpar_t));
141     id_wpar.spec = WPARID;
142     id_wpar.u.wpar_id = wpar_total[i].wpar_id;
143
144     if (perfstat_memory_total_wpar(&id_wpar, &wmemory, sizeof(perfstat_memory_total_wpar_t), 1) < 0)
145     {
146       char errbuf[1024];
147       WARNING ("memory plugin: perfstat_memory_total_wpar failed: %s",
148           sstrerror (errno, errbuf, sizeof (errbuf)));
149       return (-1);
150     }
151     memory_submit (wname, "used",   wmemory.real_inuse * pagesize);
152     memory_submit (wname, "free",   wmemory.real_free * pagesize);
153     memory_submit (wname, "cached", wmemory.numperm * pagesize);
154     memory_submit (wname, "total",  wmemory.real_total * pagesize);
155
156
157     if (perfstat_cpu_total_wpar(&id_wpar, &wcpu, sizeof(perfstat_cpu_total_wpar_t), 1) < 0)
158     {
159       char errbuf[1024];
160       WARNING ("memory plugin: perfstat_cpu_total_wpar failed: %s",
161           sstrerror (errno, errbuf, sizeof (errbuf)));
162       return (-1);
163     }
164
165     snum = (float)wcpu.loadavg[0]/(float)(1<<SBITS);
166     mnum = (float)wcpu.loadavg[1]/(float)(1<<SBITS);
167     lnum = (float)wcpu.loadavg[2]/(float)(1<<SBITS);
168
169     load_submit (wname, snum, mnum, lnum);
170
171     cpu_submit (wname, "idle",   (counter_t) wcpu.pidle);
172     cpu_submit (wname, "system", (counter_t) wcpu.psys);
173     cpu_submit (wname, "user",   (counter_t) wcpu.puser);
174     cpu_submit (wname, "wait",   (counter_t) wcpu.pwait);
175   }
176
177   return (0);
178 }
179
180 void module_register (void)
181 {
182   plugin_register_init ("wpar", wpar_init);
183   plugin_register_read ("wpar", wpar_read);
184 }