#include "common.h"
#include "plugin.h"
#include "configfile.h"
-#include "utils_debug.h"
-#define MODULE_NAME "mbmon"
-
-#include <netdb.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <netinet/tcp.h>
+#if HAVE_NETDB_H && HAVE_SYS_SOCKET_H && HAVE_NETINET_IN_H && HAVE_NETINET_TCP_H
+# include <netdb.h>
+# include <sys/socket.h>
+# include <netinet/in.h>
+# include <netinet/tcp.h>
+# define MBMON_HAVE_READ 1
+#else
+# define MBMON_HAVE_READ 0
+#endif
#define MBMON_DEF_HOST "127.0.0.1"
#define MBMON_DEF_PORT "411" /* the default for Debian */
-/* BUFFER_SIZE
- Size of the buffer we use to receive from the mbmon daemon. */
-#define BUFFER_SIZE 1024
-
-static char *filename_temperature = "mbmon/temperature-%s.rrd";
-static char *filename_fanspeed = "mbmon/fanspeed-%s.rrd";
-static char *filename_voltage = "mbmon/voltage-%s.rrd";
-
-/* temperature and fan sensors */
-static char *ds_def[] =
-{
- "DS:value:GAUGE:"COLLECTD_HEARTBEAT":U:U",
- NULL
-};
-static int ds_num = 1;
-
-/* voltage sensors */
-static char *voltage_ds_def[] =
-{
- "DS:voltage:GAUGE:"COLLECTD_HEARTBEAT":U:U",
- NULL
-};
-static int voltage_ds_num = 1;
-
-static char *config_keys[] =
+static const char *config_keys[] =
{
"Host",
"Port",
};
static int config_keys_num = 2;
+#if MBMON_HAVE_READ
static char *mbmon_host = NULL;
static char *mbmon_port = NULL;
if ((ai_return = getaddrinfo (host, port, &ai_hints, &ai_list)) != 0)
{
- syslog (LOG_ERR, "mbmon: getaddrinfo (%s, %s): %s",
+ char errbuf[1024];
+ ERROR ("mbmon: getaddrinfo (%s, %s): %s",
host, port,
- ai_return == EAI_SYSTEM ? strerror (errno) : gai_strerror (ai_return));
+ (ai_return == EAI_SYSTEM)
+ ? sstrerror (errno, errbuf, sizeof (errbuf))
+ : gai_strerror (ai_return));
return (-1);
}
/* create our socket descriptor */
if ((fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol)) < 0)
{
- syslog (LOG_ERR, "mbmon: socket: %s",
- strerror (errno));
+ char errbuf[1024];
+ ERROR ("mbmon: socket: %s",
+ sstrerror (errno, errbuf,
+ sizeof (errbuf)));
continue;
}
/* connect to the mbmon daemon */
if (connect (fd, (struct sockaddr *) ai_ptr->ai_addr, ai_ptr->ai_addrlen))
{
- DBG ("mbmon: connect (%s, %s): %s", host, port,
- strerror (errno));
+ char errbuf[1024];
+ DEBUG ("mbmon: connect (%s, %s): %s", host, port,
+ sstrerror (errno, errbuf,
+ sizeof (errbuf)));
close (fd);
fd = -1;
continue;
if (fd < 0)
{
- syslog (LOG_ERR, "mbmon: Could not connect to daemon.");
+ ERROR ("mbmon: Could not connect to daemon.");
return (-1);
}
{
if (status == -1)
{
+ char errbuf[1024];
+
if ((errno == EAGAIN) || (errno == EINTR))
continue;
- syslog (LOG_ERR, "mbmon: Error reading from socket: %s",
- strerror (errno));
+ ERROR ("mbmon: Error reading from socket: %s",
+ sstrerror (errno, errbuf,
+ sizeof (errbuf)));
close (fd);
return (-1);
}
if (buffer_fill >= buffer_size)
{
buffer[buffer_size - 1] = '\0';
- syslog (LOG_WARNING, "mbmon: Message from mbmon has been truncated.");
+ WARNING ("mbmon: Message from mbmon has been truncated.");
}
else if (buffer_fill == 0)
{
- syslog (LOG_WARNING, "mbmon: Peer has unexpectedly shut down the socket. "
+ WARNING ("mbmon: Peer has unexpectedly shut down the socket. "
"Buffer: `%s'", buffer);
close (fd);
return (-1);
return (0);
}
-static int mbmon_config (char *key, char *value)
+static int mbmon_config (const char *key, const char *value)
{
if (strcasecmp (key, "host") == 0)
{
return (0);
}
-static void mbmon_init (void)
-{
- return;
-}
-
-static void mbmon_write_temperature (char *host, char *inst, char *val)
-{
- char filename[BUFFER_SIZE];
- int status;
-
- /* construct filename */
- status = snprintf (filename, BUFFER_SIZE, filename_temperature, inst);
- if ((status < 1) || (status >= BUFFER_SIZE))
- return;
-
- rrd_update_file (host, filename, val, ds_def, ds_num);
-}
-
-static void mbmon_write_fanspeed (char *host, char *inst, char *val)
-{
- char filename[BUFFER_SIZE];
- int status;
-
- /* construct filename */
- status = snprintf (filename, BUFFER_SIZE, filename_fanspeed, inst);
- if ((status < 1) || (status >= BUFFER_SIZE))
- return;
-
- rrd_update_file (host, filename, val, ds_def, ds_num);
-}
-
-static void mbmon_write_voltage (char *host, char *inst, char *val)
+static void mbmon_submit (const char *type, const char *type_instance,
+ double value)
{
- char filename[BUFFER_SIZE];
- int status;
-
- /* construct filename */
- status = snprintf (filename, BUFFER_SIZE, filename_voltage, inst);
- if ((status < 1) || (status >= BUFFER_SIZE))
- return;
+ value_t values[1];
+ value_list_t vl = VALUE_LIST_INIT;
- rrd_update_file (host, filename, val, voltage_ds_def, voltage_ds_num);
-}
-
-static void mbmon_submit (char *type, char *inst, double value)
-{
- char buf[BUFFER_SIZE];
+ values[0].gauge = value;
- if (snprintf (buf, BUFFER_SIZE, "%u:%.3f", (unsigned int) curtime, value)
- >= BUFFER_SIZE)
- return;
+ vl.values = values;
+ vl.values_len = 1;
+ vl.time = time (NULL);
+ strcpy (vl.host, hostname_g);
+ strcpy (vl.plugin, "mbmon");
+ strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
- plugin_submit (type, inst, buf);
-}
+ plugin_dispatch_values (type, &vl);
+} /* void mbmon_submit */
/* Trim trailing whitespace from a string. */
static void trim_spaces (char *s)
s[l] = '\0';
}
-static void mbmon_read (void)
+static int mbmon_read (void)
{
- char buf[BUFFER_SIZE];
+ char buf[1024];
char *s, *t;
- static int wait_time = 1;
- static int wait_left = 0;
-
- if (wait_left >= 10)
- {
- wait_left -= 10;
- return;
- }
-
/* get data from daemon */
- if (mbmon_query_daemon (buf, BUFFER_SIZE) < 0)
- {
- /* This limit is reached in log2(86400) =~ 17 steps. Since
- * there is a 2^n seconds wait between each step it will need
- * roughly one day to reach this limit. -octo */
-
- wait_time *= 2;
- if (wait_time > 86400)
- wait_time = 86400;
-
- wait_left = wait_time;
-
- return;
- }
- else
- {
- wait_time = 1;
- wait_left = 0;
- }
+ if (mbmon_query_daemon (buf, sizeof (buf)) < 0)
+ return (-1);
s = buf;
while ((t = strchr (s, ':')) != NULL)
value = strtod (t, &nextc);
if ((*nextc != '\n') && (*nextc != '\0'))
{
- syslog (LOG_ERR, "mbmon: value for `%s' contains invalid characters: `%s'", s, t);
+ ERROR ("mbmon: value for `%s' contains invalid characters: `%s'", s, t);
break;
}
if (strncmp (s, "TEMP", 4) == 0)
{
inst = s + 4;
- type = "mbmon_temperature";
+ type = "temperature";
}
else if (strncmp (s, "FAN", 3) == 0)
{
inst = s + 3;
- type = "mbmon_fanspeed";
+ type = "fanspeed";
}
else if (strncmp (s, "V", 1) == 0)
{
inst = s + 1;
- type = "mbmon_voltage";
+ type = "voltage";
}
else
{
s = nextc + 1;
}
+
+ return (0);
} /* void mbmon_read */
+#endif /* MBMON_HAVE_READ */
/* module_register
Register collectd plugin. */
void module_register (void)
{
- plugin_register (MODULE_NAME, mbmon_init, mbmon_read, NULL);
- plugin_register ("mbmon_temperature", NULL, NULL, mbmon_write_temperature);
- plugin_register ("mbmon_fanspeed", NULL, NULL, mbmon_write_fanspeed);
- plugin_register ("mbmon_voltage", NULL, NULL, mbmon_write_voltage);
- cf_register (MODULE_NAME, mbmon_config, config_keys, config_keys_num);
-}
-
-#undef MODULE_NAME
+#if MBMON_HAVE_READ
+ plugin_register_config ("mbmon", mbmon_config, config_keys, config_keys_num);
+ plugin_register_read ("mbmon", mbmon_read);
+#endif /* MBMON_HAVE_READ */
+} /* void module_register */