Explorar el Código

Default to not requiring elevated timescale permissions

Nick Babcock hace 6 años
padre
commit
0044930c59

+ 1 - 1
CHANGELOG.md

@@ -1,6 +1,6 @@
 ## 0.6.0 - 2018-10-07
 
-The big news for this release is [TimescaleDB](https://www.timescale.com/) support, so OhmGraphite now writes to a PostgreSQL database!
+The big news for this release is [TimescaleDB](https://www.timescale.com/) support, so OhmGraphite can now writes to a PostgreSQL database!
 
 One can configure OhmGraphite to send to Timescale with the following (configuration values will differ depending on your environment):
 

+ 4 - 4
OhmGraphite/MetricConfig.cs

@@ -4,7 +4,7 @@ namespace OhmGraphite
 {
     public class MetricConfig
     {
-        public MetricConfig(TimeSpan interval, GraphiteConfig graphite, InfluxConfig influx, PrometheusConfig prometheus, string timescale)
+        public MetricConfig(TimeSpan interval, GraphiteConfig graphite, InfluxConfig influx, PrometheusConfig prometheus, TimescaleConfig timescale)
         {
             Interval = interval;
             Graphite = graphite;
@@ -17,7 +17,7 @@ namespace OhmGraphite
         public GraphiteConfig Graphite { get; }
         public InfluxConfig Influx { get; }
         public PrometheusConfig Prometheus { get; }
-        public string Timescale { get; }
+        public TimescaleConfig Timescale { get; }
 
         public static MetricConfig ParseAppSettings(IAppConfig config)
         {
@@ -32,7 +32,7 @@ namespace OhmGraphite
             GraphiteConfig gconfig = null;
             InfluxConfig iconfig = null;
             PrometheusConfig pconfig = null;
-            string timescale = null;
+            TimescaleConfig timescale = null;
 
             switch (type.ToLowerInvariant())
             {
@@ -48,7 +48,7 @@ namespace OhmGraphite
                     break;
                 case "timescale":
                 case "timescaledb":
-                    timescale = config["timescale_connection"];
+                    timescale = TimescaleConfig.ParseAppSettings(config);
                     break;
             }
 

+ 1 - 1
OhmGraphite/Program.cs

@@ -71,7 +71,7 @@ namespace OhmGraphite
             }
             else if (config.Timescale != null)
             {
-                var writer = new TimescaleWriter(config.Timescale, Environment.MachineName);
+                var writer = new TimescaleWriter(config.Timescale.Connection, config.Timescale.SetupTable, Environment.MachineName);
                 return new MetricTimer(config.Interval, collector, writer);
             }
             else

+ 25 - 0
OhmGraphite/TimescaleConfig.cs

@@ -0,0 +1,25 @@
+namespace OhmGraphite
+{
+    public class TimescaleConfig
+    {
+        public bool SetupTable { get; }
+        public string Connection { get; }
+
+        public TimescaleConfig(string connection, bool setupTable)
+        {
+            SetupTable = setupTable;
+            Connection = connection;
+        }
+
+        internal static TimescaleConfig ParseAppSettings(IAppConfig config)
+        {
+            string connection = config["timescale_connection"];
+            if (!bool.TryParse(config["timescale_setup"], out bool setupTable))
+            {
+                setupTable = false;
+            }
+
+            return new TimescaleConfig(connection, setupTable);
+        }
+    }
+}

+ 24 - 18
OhmGraphite/TimescaleWriter.cs

@@ -15,13 +15,15 @@ namespace OhmGraphite
 
         private readonly string _connStr;
         private readonly string _localHost;
+        private readonly bool _setupTable;
         private NpgsqlConnection _conn;
         private bool _failure = true;
 
-        public TimescaleWriter(string connStr, string localHost)
+        public TimescaleWriter(string connStr, bool setupTable, string localHost)
         {
             _connStr = connStr;
             _localHost = localHost;
+            _setupTable = setupTable;
             _conn = new NpgsqlConnection(_connStr);
         }
 
@@ -36,24 +38,28 @@ namespace OhmGraphite
                     Logger.Debug("New timescale connection");
                     await _conn.OpenAsync();
 
-                    using (var cmd = new NpgsqlCommand(
-                        "CREATE TABLE IF NOT EXISTS ohm_stats (" +
-                        "   time TIMESTAMPTZ NOT NULL," +
-                        "   host TEXT,hardware TEXT," +
-                        "   hardware_type TEXT," +
-                        "   identifier TEXT," +
-                        "   sensor TEXT," +
-                        "   sensor_type TEXT," +
-                        "   sensor_index INT," +
-                        "   value REAL" +
-                        ");" +
-                        "" +
-                        @"SELECT create_hypertable('ohm_stats', 'time', if_not_exists => TRUE);" +
-                        "CREATE INDEX IF NOT EXISTS idx_ohm_host ON ohm_stats (host);" +
-                        "CREATE INDEX IF NOT EXISTS idx_ohm_identifier ON ohm_stats (identifier);",
-                        _conn))
+                    if (_setupTable)
                     {
-                        await cmd.ExecuteNonQueryAsync();
+                        using (var cmd = new NpgsqlCommand(
+                            "CREATE TABLE IF NOT EXISTS ohm_stats (" +
+                            "   time TIMESTAMPTZ NOT NULL," +
+                            "   host TEXT," +
+                            "   hardware TEXT," +
+                            "   hardware_type TEXT," +
+                            "   identifier TEXT," +
+                            "   sensor TEXT," +
+                            "   sensor_type TEXT," +
+                            "   sensor_index INT," +
+                            "   value REAL" +
+                            ");" +
+                            "" +
+                            @"SELECT create_hypertable('ohm_stats', 'time', if_not_exists => TRUE);" +
+                            "CREATE INDEX IF NOT EXISTS idx_ohm_host ON ohm_stats (host);" +
+                            "CREATE INDEX IF NOT EXISTS idx_ohm_identifier ON ohm_stats (identifier);",
+                            _conn))
+                        {
+                            await cmd.ExecuteNonQueryAsync();
+                        }
                     }
                 }