Fix compile time issues
[collectd.git] / src / redis.c
index dd4deb1..77ce5fb 100644 (file)
@@ -22,8 +22,8 @@
 
 #include "collectd.h"
 
-#include "common.h"
 #include "plugin.h"
+#include "utils/common/common.h"
 
 #include <hiredis/hiredis.h>
 #include <sys/time.h>
@@ -70,6 +70,7 @@ typedef struct redis_node_s redis_node_t;
 struct redis_node_s {
   char *name;
   char *host;
+  char *socket;
   char *passwd;
   int port;
   struct timeval timeout;
@@ -101,6 +102,7 @@ static void redis_node_free(void *arg) {
     redisFree(rn->redisContext);
   sfree(rn->name);
   sfree(rn->host);
+  sfree(rn->socket);
   sfree(rn->passwd);
   sfree(rn);
 } /* void redis_node_free */
@@ -113,7 +115,7 @@ static int redis_node_add(redis_node_t *rn) /* {{{ */
   redis_have_instances = true;
 
   char cb_name[sizeof("redis/") + DATA_MAX_NAME_LEN];
-  snprintf(cb_name, sizeof(cb_name), "redis/%s", rn->name);
+  ssnprintf(cb_name, sizeof(cb_name), "redis/%s", rn->name);
 
   return plugin_register_complex_read(
       /* group = */ "redis",
@@ -121,7 +123,8 @@ static int redis_node_add(redis_node_t *rn) /* {{{ */
       /* callback  = */ redis_read,
       /* interval  = */ 0,
       &(user_data_t){
-          .data = rn, .free_func = redis_node_free,
+          .data = rn,
+          .free_func = redis_node_free,
       });
 } /* }}} */
 
@@ -213,6 +216,8 @@ static int redis_config_node(oconfig_item_t *ci) /* {{{ */
         rn->port = status;
         status = 0;
       }
+    } else if (strcasecmp("Socket", option->key) == 0) {
+      status = cf_util_get_string(option, &rn->socket);
     } else if (strcasecmp("Query", option->key) == 0) {
       redis_query_t *rq = redis_config_query(option);
       if (rq == NULL) {
@@ -482,7 +487,7 @@ static int redis_db_stats(const char *node, char const *info_line) /* {{{ */
     char *str;
     int i;
 
-    snprintf(field_name, sizeof(field_name), "db%d:keys=", db);
+    ssnprintf(field_name, sizeof(field_name), "db%d:keys=", db);
 
     str = strstr(info_line, field_name);
     if (!str)
@@ -498,7 +503,7 @@ static int redis_db_stats(const char *node, char const *info_line) /* {{{ */
       return -1;
     }
 
-    snprintf(db_id, sizeof(db_id), "%d", db);
+    ssnprintf(db_id, sizeof(db_id), "%d", db);
     redis_submit(node, "records", db_id, val);
   }
   return 0;
@@ -591,15 +596,23 @@ static void redis_check_connection(redis_node_t *rn) {
   if (rn->redisContext)
     return;
 
-  redisContext *rh = redisConnectWithTimeout(rn->host, rn->port, rn->timeout);
+  redisContext *rh;
+  if (rn->socket != NULL)
+    rh = redisConnectUnixWithTimeout(rn->socket, rn->timeout);
+  else
+    rh = redisConnectWithTimeout(rn->host, rn->port, rn->timeout);
 
   if (rh == NULL) {
     ERROR("redis plugin: can't allocate redis context");
     return;
   }
   if (rh->err) {
-    ERROR("redis plugin: unable to connect to node `%s' (%s:%d): %s.", rn->name,
-          rn->host, rn->port, rh->errstr);
+    if (rn->socket)
+      ERROR("redis plugin: unable to connect to node `%s' (%s): %s.", rn->name,
+            rn->socket, rh->errstr);
+    else
+      ERROR("redis plugin: unable to connect to node `%s' (%s:%d): %s.",
+            rn->name, rn->host, rn->port, rh->errstr);
     redisFree(rh);
     return;
   }
@@ -760,8 +773,14 @@ static int redis_read(user_data_t *user_data) /* {{{ */
 {
   redis_node_t *rn = user_data->data;
 
-  DEBUG("redis plugin: querying info from node `%s' (%s:%d).", rn->name,
-        rn->host, rn->port);
+#if COLLECT_DEBUG
+  if (rn->socket)
+    DEBUG("redis plugin: querying info from node `%s' (%s).", rn->name,
+          rn->socket);
+  else
+    DEBUG("redis plugin: querying info from node `%s' (%s:%d).", rn->name,
+          rn->host, rn->port);
+#endif
 
   redis_check_connection(rn);