aquaero: Fix sensor names
[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, double value)
69 {
70         const char *instance = conf_device?conf_device:"default";
71         value_t values[1];
72         value_list_t vl = VALUE_LIST_INIT;
73
74         /* Don't report undefined values. */
75         if (value == AQ5_FLOAT_UNDEF)
76                 return;
77
78         values[0].gauge = value;
79
80         vl.values = values;
81         vl.values_len = 1;
82
83         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
84         sstrncpy (vl.plugin, "aquaero", sizeof (vl.plugin));
85         sstrncpy (vl.plugin_instance, instance, sizeof (vl.plugin_instance));
86         sstrncpy (vl.type, type, sizeof (vl.type));
87         sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
88
89         plugin_dispatch_values (&vl);
90 } /* int aquaero_submit */
91
92 /* aquaero_submit_array submits every value of a given array of values */
93 static void aquaero_submit_array (const char *type, const char *type_instance_prefix, double *value_array, int len)
94 {
95         char type_instance[DATA_MAX_NAME_LEN];
96         int i;
97
98         for (i = 0; i < len; i++)
99                 if (value_array[i] != AQ5_FLOAT_UNDEF)
100                 {
101                         snprintf(type_instance, sizeof(type_instance), "%s%d", type_instance_prefix, i+1);
102                         aquaero_submit(type, type_instance, value_array[i]);
103                 }
104 }
105
106 static int aquaero_read (void)
107 {
108         aq5_data_t aq_data;
109         aq5_settings_t aq_sett;
110         char *err_msg = NULL;
111         char type_instance[DATA_MAX_NAME_LEN];
112         char errbuf[1024];
113         int i;
114
115         if (libaquaero5_poll(conf_device, &aq_data, &err_msg) < 0)
116         {
117                 sstrerror(errno, errbuf, sizeof (errbuf));
118                 ERROR ("Failed to poll device '%s': %s (%s)",
119                                 conf_device?conf_device:"default", err_msg, errbuf);
120                 return (-1);
121         }
122
123         if (libaquaero5_getsettings(conf_device, &aq_sett, &err_msg) < 0)
124         {
125                 sstrerror(errno, errbuf, sizeof (errbuf));
126                 ERROR ("Failed to get settings for device '%s': %s (%s)\n",
127                                 conf_device?conf_device:"default", err_msg, errbuf);
128                 return (-1);
129         }
130
131         /* CPU Temperature sensor */
132         aquaero_submit("temperature", "cpu", aq_data.cpu_temp[0]);
133
134         /* Temperature sensors */
135         aquaero_submit_array("temperature", "temp", aq_data.temp, AQ5_NUM_TEMP);
136
137         /* Virtual temperature sensors */
138         aquaero_submit_array("temperature", "virttemp", aq_data.vtemp, AQ5_NUM_VIRT_SENSORS);
139
140         /* Software temperature sensors */
141         aquaero_submit_array("temperature", "softtemp", aq_data.stemp, AQ5_NUM_SOFT_SENSORS);
142
143         /* Other temperature sensors */
144         aquaero_submit_array("temperature", "othertemp", aq_data.otemp, AQ5_NUM_OTHER_SENSORS);
145
146         /* Fans */
147         for (i = 0; i < AQ5_NUM_FAN; i++)
148         {
149                 if ((aq_sett.fan_data_source[i] == NONE) || (aq_data.fan_vrm_temp[i] != AQ5_FLOAT_UNDEF))
150                         continue;
151
152                 /* Always report fanthe fan VRM (Voltage Regulator) temp if a regulator is installed. */
153                 snprintf(type_instance, sizeof(type_instance), "fan%d", i+1);
154                 aquaero_submit("fanspeed", type_instance, aq_data.fan_rpm[i]);
155                 snprintf(type_instance, sizeof(type_instance), "fan-vrm%d", i+1);
156                 aquaero_submit("temperature", type_instance, aq_data.fan_vrm_temp[i]);
157
158                 snprintf(type_instance, sizeof(type_instance), "fan%d", i+1);
159                 aquaero_submit("percentage", type_instance, aq_data.fan_duty[i]);
160
161                 snprintf(type_instance, sizeof(type_instance), "fan%d", i+1);
162                 aquaero_submit("voltage", type_instance, aq_data.fan_voltage[i]);
163                 snprintf(type_instance, sizeof(type_instance), "fan%d", i+1);
164                 aquaero_submit("current", type_instance, aq_data.fan_current[i]);
165         }
166
167         /* Flow sensors */
168         aquaero_submit_array("flow", "flow", aq_data.flow, AQ5_NUM_FLOW);
169
170         /* Liquid level */
171         aquaero_submit_array("percentage", "waterlevel", aq_data.level, AQ5_NUM_LEVEL);
172
173         return (0);
174 }
175
176 void module_register (void)
177 {
178         plugin_register_config ("aquaero", aquaero_config, config_keys,
179                         config_keys_num);
180         plugin_register_read ("aquaero", aquaero_read);
181         plugin_register_shutdown ("aquaero", aquaero_shutdown);
182 } /* void module_register */