influxdb_retention_manager 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/bin/irm +4 -0
  3. data/lib/irm.rb +92 -0
  4. metadata +46 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 14ec891a246e9400611dfbed8df530b7c762ee8e
4
+ data.tar.gz: 4cd98d3b2c237018b95670948c6ed12f2e06f62c
5
+ SHA512:
6
+ metadata.gz: 9fe88465cc6ea60e001ad33d0e3e177c997da4a78bbde83c20195de86dea1c8fbabfa036ee73fed38e6348c978ae6b18883f4b4cf84cac5cff69d9fb876056b5
7
+ data.tar.gz: 2620a7a548d6845b0f868b059a90b1f662eb67fec1905a4708d638ff68ccba3d571ce35794c6b1c47d1a58ebb5ccf9a316d35d8e31fa01ef43afef3122a4410f
data/bin/irm ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'irm'
4
+ Irm.start(ARGV)
data/lib/irm.rb ADDED
@@ -0,0 +1,92 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'awesome_print'
4
+ require "thor"
5
+ require 'json'
6
+ require 'yaml'
7
+ require 'pp'
8
+
9
+ class Irm < Thor
10
+ method_option :host, default: "127.0.0.1", aliases: %w[-h]
11
+ method_option :database, required: true, aliases: %w[-d]
12
+ method_option :user, aliases: %w[-u]
13
+ method_option :password, aliases: %w[-p]
14
+ desc "recon", "Connect database and prepare config file"
15
+ def recon
16
+ auth = ""
17
+ if options[:user] and options[:password]
18
+ auth = "-u #{options[:user]}:#{options[:password]}"
19
+ end
20
+ json = `curl #{auth } -G 'http://#{options[:host]}:8086/query?pretty=true' --data-urlencode "db=#{options[:database] }" --data-urlencode "q=show tag keys"`
21
+ hash = JSON.parse(json)
22
+ ap hash
23
+ yaml = {}
24
+ yaml["measurements"] = {}
25
+ hash["results"][0]["series"].collect do |series|
26
+ yaml["measurements"][series["name"]] = {
27
+ "tags" => series["values"].collect { |tag| tag[0]}
28
+ }
29
+ end
30
+ json = `curl #{auth} -G 'http://#{options[:host]}:8086/query?pretty=true' --data-urlencode "db=#{options[:database]}" --data-urlencode "q=show field keys"`
31
+ hash = JSON.parse(json)
32
+ ap hash
33
+ hash["results"][0]["series"].collect do |series|
34
+ yaml["measurements"][series["name"]]["fields"] ||= {}
35
+ series["values"].each { |tag| yaml["measurements"][series["name"]]["fields"][tag[0]] = "mean"}
36
+ end
37
+ yaml["database"] = options[:database]
38
+ yaml["retention_policies"] = [
39
+ {"name" => "biweekly", "duration" => "15d"},
40
+ {"name" => "months3", "duration" => "92d", "precision" => "1m"},
41
+ {"name" => "yearly", "duration" => "366d", "precision" => "5m"}
42
+ ]
43
+
44
+ File.open("#{options[:database]}.yaml","w") do |io|
45
+ io << YAML.dump(yaml)
46
+ puts "Recon written to: #{options[:database]}.yaml"
47
+ end
48
+ end
49
+ method_option :database, required: true, aliases: %w[-d]
50
+ desc "create_cq", "Generate influx qcommands"
51
+ def create_cq
52
+ hash = YAML.load_file("#{options[:database]}.yaml")
53
+ hash["retention_policies"].each_index do |retention_policy_index|
54
+ retention_policy = hash["retention_policies"][retention_policy_index]
55
+ command = "CREATE RETENTION POLICY #{retention_policy["name"]} ON #{options[:database]} DURATION #{retention_policy["duration"]} REPLICATION 1"
56
+ command << " DEFAULT" if retention_policy_index == 0
57
+ puts command
58
+ end
59
+ hash["measurements"].each_pair do |measurement,measurement_hash|
60
+ # migrate from DEFAULT retention policy first policy
61
+ retention_policy = hash["retention_policies"][0]
62
+ retention_name = retention_policy["name"]
63
+ retention_time = retention_policy["duration"]
64
+ prefix = "SELECT "
65
+ fields = measurement_hash["fields"].collect do |field, function|
66
+ "#{function}(#{field}) AS #{field}"
67
+ end.join(", ")
68
+ suffix = %Q| INTO #{options[:database]}."#{retention_name}".#{measurement} FROM #{options[:database]}."default".#{measurement} WHERE time > '2016-03-05' AND time < now() |
69
+ tags = ""
70
+ tags = %Q|GROUP BY time(15s), "#{measurement_hash["tags"].join(%Q[", "])}" fill(none)| unless measurement_hash["tags"].to_a.empty?
71
+
72
+ puts "#{prefix}#{fields}#{suffix}#{tags}"
73
+ # create CQ for moving data to all other policies
74
+ hash["retention_policies"][1..-1].each_index do |retention_policy_index|
75
+ retention_policy = hash["retention_policies"][retention_policy_index+1]
76
+ retention_name = retention_policy["name"]
77
+ retention_time = retention_policy["precision"]
78
+ prefix = "CREATE CONTINUOUS QUERY #{measurement}_#{retention_name} ON #{options[:database]} BEGIN SELECT "
79
+ fields = measurement_hash["fields"].collect do |field, function|
80
+ "#{function}(#{field}) AS #{field}"
81
+ end.join(", ")
82
+ suffix = %Q| INTO #{options[:database]}."#{retention_name}".#{measurement} FROM #{options[:database]}."#{hash["retention_policies"][retention_policy_index]["name"]}".#{measurement} GROUP BY time(#{retention_time}) |
83
+ tags = ""
84
+ tags = %Q|, "#{measurement_hash["tags"].join(%Q[", "])}"| unless measurement_hash["tags"].to_a.empty?
85
+ puts "#{prefix}#{fields}#{suffix}#{tags} END"
86
+ end
87
+ puts "#################################### Done with: #{measurement}"
88
+ end
89
+ end
90
+ end
91
+
92
+ Irm.start(ARGV)
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: influxdb_retention_manager
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Rubycut
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-06-21 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A simple hello world gem
14
+ email: ruby.cutter@gmail.com
15
+ executables:
16
+ - irm
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - bin/irm
21
+ - lib/irm.rb
22
+ homepage: https://github.com/rubycut/influxdb_retention_manager
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 2.2.2
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: Hola!
46
+ test_files: []