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