From 3568fb9f9384fdffe24a6807d51ccca04fa12fcf Mon Sep 17 00:00:00 2001
From: Cameron Sparr <cameronsparr@gmail.com>
Date: Mon, 29 Feb 2016 16:13:00 +0000
Subject: [PATCH] Support specifying influxdb retention policy

closes #692
---
 CHANGELOG.md                         |  1 +
 etc/telegraf.conf                    | 25 +++++++++++++++--------
 plugins/outputs/influxdb/influxdb.go | 30 ++++++++++++++++------------
 3 files changed, 35 insertions(+), 21 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 1b98e8bc..72300f6d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -3,6 +3,7 @@
 ### Release Notes
 
 ### Features
+- [#692](https://github.com/influxdata/telegraf/pull/770): Support InfluxDB retention policies
 
 ### Bugfixes
 - [#748](https://github.com/influxdata/telegraf/issues/748): Fix sensor plugin split on ":"
diff --git a/etc/telegraf.conf b/etc/telegraf.conf
index d8a29544..f5a2b34d 100644
--- a/etc/telegraf.conf
+++ b/etc/telegraf.conf
@@ -56,15 +56,17 @@
 
 # Configuration for influxdb server to send metrics to
 [[outputs.influxdb]]
-  # The full HTTP or UDP endpoint URL for your InfluxDB instance.
-  # Multiple urls can be specified but it is assumed that they are part of the same
-  # cluster, this means that only ONE of the urls will be written to each interval.
+  ## The full HTTP or UDP endpoint URL for your InfluxDB instance.
+  ## Multiple urls can be specified as part of the same cluster,
+  ## this means that only ONE of the urls will be written to each interval.
   # urls = ["udp://localhost:8089"] # UDP endpoint example
   urls = ["http://localhost:8086"] # required
-  # The target database for metrics (telegraf will create it if not exists)
+  ## The target database for metrics (telegraf will create it if not exists).
   database = "telegraf" # required
-  # Precision of writes, valid values are "ns", "us" (or "µs"), "ms", "s", "m", "h".
-  # note: using second precision greatly helps InfluxDB compression
+  ## Retention policy to write to.
+  retention_policy = "default"
+  ## Precision of writes, valid values are "ns", "us" (or "µs"), "ms", "s", "m", "h".
+  ## note: using "s" precision greatly improves InfluxDB compression.
   precision = "s"
 
   ## Write timeout (for the InfluxDB client), formatted as a string.
@@ -72,11 +74,18 @@
   timeout = "5s"
   # username = "telegraf"
   # password = "metricsmetricsmetricsmetrics"
-  # Set the user agent for HTTP POSTs (can be useful for log differentiation)
+  ## Set the user agent for HTTP POSTs (can be useful for log differentiation)
   # user_agent = "telegraf"
-  # Set UDP payload size, defaults to InfluxDB UDP Client default (512 bytes)
+  ## Set UDP payload size, defaults to InfluxDB UDP Client default (512 bytes)
   # udp_payload = 512
 
+  ## Optional SSL Config
+  # ssl_ca = "/etc/telegraf/ca.pem"
+  # ssl_cert = "/etc/telegraf/cert.pem"
+  # ssl_key = "/etc/telegraf/key.pem"
+  ## Use SSL but skip chain & host verification
+  # insecure_skip_verify = false
+
 
 ###############################################################################
 #                                  INPUTS                                     #
diff --git a/plugins/outputs/influxdb/influxdb.go b/plugins/outputs/influxdb/influxdb.go
index db9926bb..5eef553a 100644
--- a/plugins/outputs/influxdb/influxdb.go
+++ b/plugins/outputs/influxdb/influxdb.go
@@ -18,15 +18,16 @@ import (
 
 type InfluxDB struct {
 	// URL is only for backwards compatability
-	URL        string
-	URLs       []string `toml:"urls"`
-	Username   string
-	Password   string
-	Database   string
-	UserAgent  string
-	Precision  string
-	Timeout    internal.Duration
-	UDPPayload int `toml:"udp_payload"`
+	URL             string
+	URLs            []string `toml:"urls"`
+	Username        string
+	Password        string
+	Database        string
+	UserAgent       string
+	Precision       string
+	RetentionPolicy string
+	Timeout         internal.Duration
+	UDPPayload      int `toml:"udp_payload"`
 
 	// Path to CA file
 	SSLCA string `toml:"ssl_ca"`
@@ -46,10 +47,12 @@ var sampleConfig = `
   ## this means that only ONE of the urls will be written to each interval.
   # urls = ["udp://localhost:8089"] # UDP endpoint example
   urls = ["http://localhost:8086"] # required
-  ## The target database for metrics (telegraf will create it if not exists)
+  ## The target database for metrics (telegraf will create it if not exists).
   database = "telegraf" # required
+  ## Retention policy to write to.
+  retention_policy = "default"
   ## Precision of writes, valid values are "ns", "us" (or "µs"), "ms", "s", "m", "h".
-  ## note: using "s" precision greatly improves InfluxDB compression
+  ## note: using "s" precision greatly improves InfluxDB compression.
   precision = "s"
 
   ## Write timeout (for the InfluxDB client), formatted as a string.
@@ -164,8 +167,9 @@ func (i *InfluxDB) Write(metrics []telegraf.Metric) error {
 		}
 	}
 	bp, err := client.NewBatchPoints(client.BatchPointsConfig{
-		Database:  i.Database,
-		Precision: i.Precision,
+		Database:        i.Database,
+		Precision:       i.Precision,
+		RetentionPolicy: i.RetentionPolicy,
 	})
 	if err != nil {
 		return err
-- 
GitLab