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