Merge pull request #3339 from jkohen/patch-1
[collectd.git] / src / aquaero.c
1 /**
2  * collectd - src/aquaero.c
3  * Copyright (C) 2013  Alex Deymo
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  *   Alex Deymo
20  **/
21
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25
26 #include <libaquaero5.h>
27
28 /*
29  * Private variables
30  */
31 /* Default values for contacting daemon */
32 static char *conf_device = NULL;
33
34 static int aquaero_config (oconfig_item_t *ci)
35 {
36         int i;
37
38         for (i = 0; i < ci->children_num; i++)
39         {
40                 oconfig_item_t *child = ci->children + i;
41
42                 if (strcasecmp ("Device", child->key))
43                         cf_util_get_string (child, &conf_device);
44                 else
45                 {
46                         ERROR ("aquaero plugin: Unknown config option \"%s\".",
47                                         child->key);
48                 }
49         }
50
51         return (0);
52 }
53
54 static int aquaero_shutdown (void)
55 {
56         libaquaero5_exit();
57         return (0);
58 } /* int aquaero_shutdown */
59
60 static void aquaero_submit (const char *type, const char *type_instance,
61                 double value)
62 {
63         const char *instance = conf_device?conf_device:"default";
64         value_t values[1];
65         value_list_t vl = VALUE_LIST_INIT;
66
67         /* Don't report undefined values. */
68         if (value == AQ5_FLOAT_UNDEF)
69                 return;
70
71         values[0].gauge = value;
72
73         vl.values = values;
74         vl.values_len = 1;
75
76         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
77         sstrncpy (vl.plugin, "aquaero", sizeof (vl.plugin));
78         sstrncpy (vl.plugin_instance, instance, sizeof (vl.plugin_instance));
79         sstrncpy (vl.type, type, sizeof (vl.type));
80         sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
81
82         plugin_dispatch_values (&vl);
83 } /* int aquaero_submit */
84
85 /* aquaero_submit_array submits every value of a given array of values */
86 static void aquaero_submit_array (const char *type,
87                 const char *type_instance_prefix, double *value_array, int len)
88 {
89         char type_instance[DATA_MAX_NAME_LEN];
90         int i;
91
92         for (i = 0; i < len; i++)
93         {
94                 if (value_array[i] == AQ5_FLOAT_UNDEF)
95                         continue;
96
97                 snprintf (type_instance, sizeof (type_instance), "%s%d",
98                                 type_instance_prefix, i + 1);
99                 aquaero_submit (type, type_instance, value_array[i]);
100         }
101 }
102
103 static int aquaero_read (void)
104 {
105         aq5_data_t aq_data;
106         aq5_settings_t aq_sett;
107         char *err_msg = NULL;
108         char type_instance[DATA_MAX_NAME_LEN];
109         int i;
110
111         if (libaquaero5_poll(conf_device, &aq_data, &err_msg) < 0)
112         {
113                 char errbuf[1024];
114                 ERROR ("aquaero plugin: Failed to poll device \"%s\": %s (%s)",
115                                 conf_device ? conf_device : "default", err_msg,
116                                 sstrerror (errno, errbuf, sizeof (errbuf)));
117                 return (-1);
118         }
119
120         if (libaquaero5_getsettings(conf_device, &aq_sett, &err_msg) < 0)
121         {
122                 char errbuf[1024];
123                 ERROR ("aquaero plugin: Failed to get settings "
124                                 "for device \"%s\": %s (%s)",
125                                 conf_device ? conf_device : "default", err_msg,
126                                 sstrerror (errno, errbuf, sizeof (errbuf)));
127                 return (-1);
128         }
129
130         /* CPU Temperature sensor */
131         aquaero_submit("temperature", "cpu", aq_data.cpu_temp[0]);
132
133         /* Temperature sensors */
134         aquaero_submit_array("temperature", "sensor", aq_data.temp,
135                         AQ5_NUM_TEMP);
136
137         /* Virtual temperature sensors */
138         aquaero_submit_array("temperature", "virtual", aq_data.vtemp,
139                         AQ5_NUM_VIRT_SENSORS);
140
141         /* Software temperature sensors */
142         aquaero_submit_array("temperature", "software", aq_data.stemp,
143                         AQ5_NUM_SOFT_SENSORS);
144
145         /* Other temperature sensors */
146         aquaero_submit_array("temperature", "other", aq_data.otemp,
147                         AQ5_NUM_OTHER_SENSORS);
148
149         /* Fans */
150         for (i = 0; i < AQ5_NUM_FAN; i++)
151         {
152                 if ((aq_sett.fan_data_source[i] == NONE)
153                                 || (aq_data.fan_vrm_temp[i] != AQ5_FLOAT_UNDEF))
154                         continue;
155
156                 snprintf (type_instance, sizeof (type_instance),
157                                 "fan%d", i + 1);
158
159                 aquaero_submit ("fanspeed", type_instance,
160                                 aq_data.fan_rpm[i]);
161                 aquaero_submit ("percent", type_instance,
162                                 aq_data.fan_duty[i]);
163                 aquaero_submit ("voltage", type_instance,
164                                 aq_data.fan_voltage[i]);
165                 aquaero_submit ("current", type_instance,
166                                 aq_data.fan_current[i]);
167
168                 /* Report the voltage reglator module (VRM) temperature with a
169                  * different type instance. */
170                 snprintf (type_instance, sizeof (type_instance),
171                                 "fan%d-vrm", i + 1);
172                 aquaero_submit ("temperature", type_instance,
173                                 aq_data.fan_vrm_temp[i]);
174         }
175
176         /* Flow sensors */
177         aquaero_submit_array("flow", "sensor", aq_data.flow, AQ5_NUM_FLOW);
178
179         /* Liquid level */
180         aquaero_submit_array("percent", "waterlevel",
181                         aq_data.level, AQ5_NUM_LEVEL);
182
183         return (0);
184 }
185
186 void module_register (void)
187 {
188         plugin_register_complex_config ("aquaero", aquaero_config);
189         plugin_register_read ("aquaero", aquaero_read);
190         plugin_register_shutdown ("aquaero", aquaero_shutdown);
191 } /* void module_register */
192
193 /* vim: set sw=8 sts=8 noet : */