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