Let plugin_dispatch_values() set value_list.time in case of 'now'.
[collectd.git] / src / nginx.c
index a44e8a5..8cb7628 100644 (file)
 
 #include <curl/curl.h>
 
-static char *url    = NULL;
-static char *user   = NULL;
-static char *pass   = NULL;
-static char *cacert = NULL;
+static char *url         = NULL;
+static char *user        = NULL;
+static char *pass        = NULL;
+static char *verify_peer = NULL;
+static char *verify_host = NULL;
+static char *cacert      = NULL;
 
 static CURL *curl = NULL;
 
@@ -44,6 +46,8 @@ static const char *config_keys[] =
   "URL",
   "User",
   "Password",
+  "VerifyPeer",
+  "VerifyHost",
   "CACert"
 };
 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
@@ -89,6 +93,10 @@ static int config (const char *key, const char *value)
     return (config_set (&user, value));
   else if (strcasecmp (key, "password") == 0)
     return (config_set (&pass, value));
+  else if (strcasecmp (key, "verifypeer") == 0)
+    return (config_set (&verify_peer, value));
+  else if (strcasecmp (key, "verifyhost") == 0)
+    return (config_set (&verify_host, value));
   else if (strcasecmp (key, "cacert") == 0)
     return (config_set (&cacert, value));
   else
@@ -114,7 +122,8 @@ static int init (void)
 
   if (user != NULL)
   {
-    if (snprintf (credentials, 1024, "%s:%s", user, pass == NULL ? "" : pass) >= 1024)
+    if (ssnprintf (credentials, sizeof (credentials),
+         "%s:%s", user, pass == NULL ? "" : pass) >= sizeof (credentials))
     {
       ERROR ("nginx plugin: Credentials would have been truncated.");
       return (-1);
@@ -128,6 +137,24 @@ static int init (void)
     curl_easy_setopt (curl, CURLOPT_URL, url);
   }
 
+  if ((verify_peer == NULL) || (strcmp (verify_peer, "true") == 0))
+  {
+    curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 1);
+  }
+  else
+  {
+    curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 0);
+  }
+
+  if ((verify_host == NULL) || (strcmp (verify_host, "true") == 0))
+  {
+    curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 2);
+  }
+  else
+  {
+    curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 0);
+  }
+
   if (cacert != NULL)
   {
     curl_easy_setopt (curl, CURLOPT_CAINFO, cacert);
@@ -150,18 +177,15 @@ static void submit (char *type, char *inst, long long value)
 
   vl.values = values;
   vl.values_len = 1;
-  vl.time = time (NULL);
-  strcpy (vl.host, hostname_g);
-  strcpy (vl.plugin, "nginx");
-  strcpy (vl.plugin_instance, "");
+  sstrncpy (vl.host, hostname_g, sizeof (vl.host));
+  sstrncpy (vl.plugin, "nginx", sizeof (vl.plugin));
+  sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
+  sstrncpy (vl.type, type, sizeof (vl.type));
 
   if (inst != NULL)
-  {
-    strncpy (vl.type_instance, inst, sizeof (vl.type_instance));
-    vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
-  }
+    sstrncpy (vl.type_instance, inst, sizeof (vl.type_instance));
 
-  plugin_dispatch_values (type, &vl);
+  plugin_dispatch_values (&vl);
 } /* void submit */
 
 static int nginx_read (void)
@@ -171,6 +195,7 @@ static int nginx_read (void)
   char *ptr;
   char *lines[16];
   int   lines_num = 0;
+  char *saveptr;
 
   char *fields[16];
   int   fields_num;
@@ -188,7 +213,8 @@ static int nginx_read (void)
   }
 
   ptr = nginx_buffer;
-  while ((lines[lines_num] = strtok (ptr, "\n\r")) != NULL)
+  saveptr = NULL;
+  while ((lines[lines_num] = strtok_r (ptr, "\n\r", &saveptr)) != NULL)
   {
     ptr = NULL;
     lines_num++;