src/Makefile: Don't unnecessarily set plugin specific CXXFLAGS.
[collectd.git] / src / xencpu.c
1 /**
2  * collectd - src/xencpu.c
3  * Copyright (C) 2016       Pavel Rochnyak
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  *   Pavel Rochnyak <pavel2000 ngs.ru>
20  **/
21
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25
26 #include <xenctrl.h>
27
28 #ifdef XENCTRL_HAS_XC_INTERFACE
29
30 //Xen-4.1+
31 #define XC_INTERFACE_INIT_ARGS NULL,NULL,0
32 xc_interface *xc_handle;
33
34 #else /* XENCTRL_HAS_XC_INTERFACE */
35
36 //For xen-3.4/xen-4.0
37 #include <string.h>
38 #define xc_strerror(xc_interface, errcode) strerror(errcode)
39 #define XC_INTERFACE_INIT_ARGS
40 typedef int xc_interface;
41 xc_interface xc_handle = 0;
42
43 #endif /* XENCTRL_HAS_XC_INTERFACE */
44
45 uint32_t num_cpus = 0;
46 xc_cpuinfo_t *cpu_info;
47 static value_to_rate_state_t *cpu_states;
48
49 static int xencpu_init (void)
50 {
51     xc_handle = xc_interface_open(XC_INTERFACE_INIT_ARGS);
52     if (!xc_handle)
53     {
54         ERROR ("xencpu: xc_interface_open() failed");
55         return (-1);
56     };
57
58     xc_physinfo_t *physinfo;
59
60     physinfo = calloc(1, sizeof(xc_physinfo_t));
61     if (physinfo == NULL)
62     {
63         ERROR ("xencpu plugin: calloc() for physinfo failed.");
64         xc_interface_close(xc_handle);
65         return (ENOMEM);
66     }
67
68     if (xc_physinfo(xc_handle, physinfo) < 0)
69     {
70         ERROR ("xencpu plugin: xc_physinfo() failed");
71         xc_interface_close(xc_handle);
72         free(physinfo);
73         return (-1);
74     };
75
76     num_cpus = physinfo->nr_cpus;
77     free(physinfo);
78
79     INFO ("xencpu plugin: Found %"PRIu32" processors.", num_cpus);
80
81     cpu_info = calloc(num_cpus, sizeof(xc_cpuinfo_t));
82     if (cpu_info == NULL)
83     {
84         ERROR ("xencpu plugin: calloc() for num_cpus failed.");
85         xc_interface_close(xc_handle);
86         return (ENOMEM);
87     }
88
89     cpu_states = calloc (num_cpus, sizeof (value_to_rate_state_t));
90     if (cpu_states == NULL)
91     {
92         ERROR ("xencpu plugin: calloc() for cpu_states failed.");
93         xc_interface_close(xc_handle);
94         free(cpu_info);
95         return (ENOMEM);
96     }
97
98     return (0);
99 } /* static int xencpu_init */
100
101 static int xencpu_shutdown (void)
102 {
103     free(cpu_states);
104     free(cpu_info);
105     xc_interface_close(xc_handle);
106
107     return 0;
108 } /* static int xencpu_shutdown */
109
110 static void submit_value (int cpu_num, gauge_t percent)
111 {
112     value_t values[1];
113     value_list_t vl = VALUE_LIST_INIT;
114
115     values[0].gauge = percent;
116
117     vl.values = values;
118     vl.values_len = 1;
119
120     sstrncpy (vl.host, hostname_g, sizeof (vl.host));
121     sstrncpy (vl.plugin, "xencpu", sizeof (vl.plugin));
122     sstrncpy (vl.type, "percent", sizeof (vl.type));
123     sstrncpy (vl.type_instance, "load", sizeof (vl.type_instance));
124
125     if (cpu_num >= 0) {
126         ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
127                 "%i", cpu_num);
128     }
129     plugin_dispatch_values (&vl);
130 } /* static void submit_value */
131
132 static int xencpu_read (void)
133 {
134     cdtime_t now = cdtime ();
135
136     int rc, nr_cpus;
137
138     rc = xc_getcpuinfo(xc_handle, num_cpus, cpu_info, &nr_cpus);
139     if (rc < 0) {
140         ERROR ("xencpu: xc_getcpuinfo() Failed: %d %s\n", rc, xc_strerror(xc_handle,errno));
141         return (-1);
142     }
143
144     int cpu, status;
145     for (cpu = 0; cpu < nr_cpus; cpu++) {
146         gauge_t rate = NAN;
147         value_t value = {.derive = cpu_info[cpu].idletime};
148
149         status = value_to_rate (&rate, value, DS_TYPE_DERIVE, now, &cpu_states[cpu]);
150         if (status == 0) {
151             submit_value(cpu, 100 - rate/10000000);
152         }
153     }
154
155     return (0);
156 } /* static int xencpu_read */
157
158 void module_register (void)
159 {
160     plugin_register_init ("xencpu", xencpu_init);
161     plugin_register_read ("xencpu", xencpu_read);
162     plugin_register_shutdown ("xencpu", xencpu_shutdown);
163 } /* void module_register */