aquaero: New Aquaero sensors plugin
[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 #define _BSD_SOURCE
23
24 #include "collectd.h"
25 #include "common.h"
26 #include "plugin.h"
27
28 #include <libaquaero5.h>
29
30 /*
31  * Private variables
32  */
33 /* Default values for contacting daemon */
34 static char *conf_device = NULL;
35
36 static const char *config_keys[] =
37 {
38         "Device",
39 };
40 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
41
42
43 static int aquaero_config (const char *key, const char *value)
44 {
45         if (strcasecmp (key, "Device") == 0)
46         {
47                 if (conf_device != NULL)
48                 {
49                         free (conf_device);
50                         conf_device = NULL;
51                 }
52                 if (value[0] == '\0')
53                         return (0);
54                 if ((conf_device = strdup (value)) == NULL)
55                         return (1);
56         }
57         else
58         {
59                 return (-1);
60         }
61         return (0);
62 } /* int aquaero_config */
63
64 static int aquaero_shutdown (void)
65 {
66         libaquaero5_exit();
67         return (0);
68 } /* int aquaero_shutdown */
69
70 static void aquaero_submit (const char *type, const char *type_instance, double value)
71 {
72         const char *instance = conf_device?conf_device:"default";
73         value_t values[1];
74         value_list_t vl = VALUE_LIST_INIT;
75
76         /* Don't report undefined values. */
77         if (value == AQ5_FLOAT_UNDEF)
78                 return;
79
80         values[0].gauge = value;
81
82         vl.values = values;
83         vl.values_len = 1;
84
85         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
86         sstrncpy (vl.plugin, "aquaero", sizeof (vl.plugin));
87         sstrncpy (vl.plugin_instance, instance, sizeof (vl.plugin_instance));
88         sstrncpy (vl.type, type, sizeof (vl.type));
89         sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
90
91         plugin_dispatch_values (&vl);
92 } /* int aquaero_submit */
93
94 /* aquaero_submit_array submits every value of a given array of values */
95 static void aquaero_submit_array (const char *type, 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                 if (value_array[i] != AQ5_FLOAT_UNDEF)
102                 {
103                         snprintf(type_instance, sizeof(type_instance), "%s%d", type_instance_prefix, i+1);
104                         aquaero_submit(type, type_instance, value_array[i]);
105                 }
106 }
107
108 static int aquaero_read (void)
109 {
110         aq5_data_t aq_data;
111         aq5_settings_t aq_sett;
112         char *err_msg = NULL;
113         char type_instance[DATA_MAX_NAME_LEN];
114         char errbuf[1024];
115         int i;
116
117         if (libaquaero5_poll(conf_device, &aq_data, &err_msg) < 0)
118         {
119                 sstrerror(errno, errbuf, sizeof (errbuf));
120                 ERROR ("Failed to poll device '%s': %s (%s)",
121                                 conf_device?conf_device:"default", err_msg, errbuf);
122                 return (-1);
123         }
124
125         if (libaquaero5_getsettings(conf_device, &aq_sett, &err_msg) < 0)
126         {
127                 sstrerror(errno, errbuf, sizeof (errbuf));
128                 ERROR ("Failed to get settings for device '%s': %s (%s)\n",
129                                 conf_device?conf_device:"default", err_msg, errbuf);
130                 return (-1);
131         }
132
133         /* CPU Temperature sensor */
134         aquaero_submit("temperature", "cpu", aq_data.cpu_temp[0]);
135
136         /* Temperature sensors */
137         aquaero_submit_array("temperature", "temp", aq_data.temp, AQ5_NUM_TEMP);
138
139         /* Virtual temperature sensors */
140         aquaero_submit_array("temperature", "virttemp", aq_data.vtemp, AQ5_NUM_VIRT_SENSORS);
141
142         /* Software temperature sensors */
143         aquaero_submit_array("temperature", "softtemp", aq_data.stemp, AQ5_NUM_SOFT_SENSORS);
144
145         /* Other temperature sensors */
146         aquaero_submit_array("temperature", "othertemp", aq_data.otemp, AQ5_NUM_OTHER_SENSORS);
147
148         /* Fans */
149         for (i = 0; i < AQ5_NUM_FAN; i++)
150         {
151                 if ((aq_sett.fan_data_source[i] != NONE) && (aq_data.fan_vrm_temp[i] != AQ5_FLOAT_UNDEF))
152                 {
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
168         /* Flow sensors */
169         aquaero_submit_array("flow", "flow", aq_data.flow, AQ5_NUM_FLOW);
170
171         /* Liquid level */
172         aquaero_submit_array("level", "level", aq_data.level, AQ5_NUM_LEVEL);
173
174         return (0);
175 }
176
177 void module_register (void)
178 {
179         plugin_register_config ("aquaero", aquaero_config, config_keys,
180                         config_keys_num);
181         plugin_register_read ("aquaero", aquaero_read);
182         plugin_register_shutdown ("aquaero", aquaero_shutdown);
183 } /* void module_register */