cloudstack_stats 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 79b0167d9ff79e2411a694d656ccd81de2a37477
4
+ data.tar.gz: ea10cbbd9fa853cf09cb1e71aa536033b6a38ba0
5
+ SHA512:
6
+ metadata.gz: d349cff75e1884868ef1774109086639434744927fa40421f4ac3ff7c8e54f9b79ffea51d364aac13ef11efee3e42b9c19ad4039a7da38143043fc4e9a7a9fcb
7
+ data.tar.gz: d8211dc532a5adaa6fe1a9e9666c07b589fefbf7ac9326b6e522e5049aa0dd3f81104a9834e0d165f2d96d18763844208a30db6bfdff4b095583e6ee3f402762
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.3
5
+ before_install: gem install bundler -v 1.13.7
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cloudstack_stats.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 niwo
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # CloudstackStats
2
+
3
+ CloudstackStats pulls project and account statistics from the CloudStack API and feeds them into a influxdb.
4
+ Whit the help of Grafana this let's you craft beautiful usage dashboards.
5
+
6
+ ## Installation
7
+
8
+ Install the gem as:
9
+
10
+ $ gem install cloudstack_stats
11
+
12
+ ## Usage
13
+
14
+ Make sure you have a working [cloudstack-cli](https://github.com/niwo/cloudstack-cli) configuration file in your home directory.
15
+ Usually this is found under `~/.cloudstack-cli.yml`
16
+
17
+ Install gem dependencies:
18
+
19
+ $ bundle install
20
+
21
+ See the help for more options:
22
+
23
+ $ bin/cloudstack_stats help
24
+
25
+
26
+ ## Development
27
+
28
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
29
+
30
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
31
+
32
+ ## Contributing
33
+
34
+ Bug reports and pull requests are welcome on GitHub at https://github.com/niwo/cloudstack_stats.
35
+
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: UTF-8
3
+
4
+ # resolve bin path, ignoring symlinks
5
+ # resolve path, ignoring symlinks
6
+ require "pathname"
7
+ $:.unshift File.expand_path(
8
+ "../../lib", Pathname.new(__FILE__).realpath
9
+ )
10
+ require "cloudstack_stats/cli"
11
+
12
+ CloudstackStats::Cli.start
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "cloudstack_stats"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cloudstack_stats/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "cloudstack_stats"
8
+ spec.version = CloudstackStats::VERSION
9
+ spec.authors = ["niwo"]
10
+ spec.email = ["nik.wolfgramm@gmail.com"]
11
+
12
+ spec.summary = %q{Collect CloudStack stats and feed them to influxdb.}
13
+ spec.description = %q{Collect project and account statistics from the CloudStack API and feeds them into a influxdb.}
14
+ spec.homepage = "https://github.com/niwo/cloudstack_stats"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.executables = %w(cloudstack_stats)
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.13"
24
+ spec.add_development_dependency "rake", "~> 12.0"
25
+ spec.add_development_dependency "minitest", "~> 5.0"
26
+
27
+ spec.add_runtime_dependency "thor", "~> 0.19"
28
+ spec.add_runtime_dependency "cloudstack_client", "~> 1.5.0"
29
+ end
@@ -0,0 +1,104 @@
1
+ require "thor"
2
+ require "cloudstack_stats/version"
3
+ require "cloudstack_stats/collect"
4
+ require "cloudstack_stats/feed"
5
+
6
+ module CloudstackStats
7
+ class Cli < Thor
8
+ include Thor::Actions
9
+
10
+ package_name "cloudstack_stats"
11
+
12
+ class_option :config_file,
13
+ default: File.join(Dir.home, '.cloudstack-cli.yml'),
14
+ aliases: '-C',
15
+ desc: 'Location of your cloudstack-cli configuration file'
16
+
17
+ class_option :env,
18
+ aliases: '-E',
19
+ desc: 'cloudstack-cli environment to use'
20
+
21
+ class_option :influx_url,
22
+ default: "http://localhost:8086/",
23
+ aliases: '-U',
24
+ desc: 'Influxdb URL'
25
+
26
+ class_option :influx_user,
27
+ default: "cloudstack-stats",
28
+ aliases: '-u',
29
+ desc: 'Influxdb user'
30
+
31
+ class_option :influx_password,
32
+ aliases: '-p',
33
+ desc: 'Influxdb password'
34
+
35
+ class_option :database,
36
+ default: "cloudstack-stats",
37
+ aliases: '-D',
38
+ desc: 'Influxdb database'
39
+
40
+ class_option :debug,
41
+ aliases: '-D',
42
+ desc: 'Enable debug output',
43
+ type: :boolean,
44
+ default: false
45
+
46
+ # catch control-c and exit
47
+ trap("SIGINT") do
48
+ puts
49
+ puts "exiting.."
50
+ exit!
51
+ end
52
+
53
+ # exit with return code 1 in case of a error
54
+ def self.exit_on_failure?
55
+ true
56
+ end
57
+
58
+ desc "version", "Print version number"
59
+ def version
60
+ say "cloudstack_stats v#{CloudstackStats::VERSION}"
61
+ end
62
+ map %w(-v --version) => :version
63
+
64
+ desc "projects", "Pull projects stats from CloudStack."
65
+ option :domain,
66
+ desc: "Name of Domain (for recursive search)",
67
+ default: "ROOT"
68
+ def projects
69
+ say "Collect stats...", :yellow
70
+ stats = Collect.new(options).project_stats
71
+ say "Write stats to influxdb...", :yellow
72
+ Feed.new(options).write(stats) {|stat, res| print_status(stat, res)}
73
+ rescue => e
74
+ say "ERROR: ", :red
75
+ puts e.message
76
+ end
77
+
78
+ desc "accounts", "Pull account stats from CloudStack."
79
+ option :domain,
80
+ desc: "Name of Domain (for recursive search)",
81
+ default: "ROOT"
82
+ def accounts
83
+ say "Collect stats...", :yellow
84
+ stats = Collect.new(options).account_stats
85
+ say "Write stats to influxdb...", :yellow
86
+ Feed.new(options).write(stats) {|stat, res| print_status(stat, res)}
87
+ rescue => e
88
+ say "ERROR: ", :red
89
+ puts e.message
90
+ end
91
+
92
+ no_commands do
93
+ def print_status(stat, res)
94
+ print_in_columns [
95
+ stat['name'],
96
+ res.code == '204' ?
97
+ "OK (HTTP #{res.code})" :
98
+ "FAIL (HTTP #{res.code})"
99
+ ]
100
+ end
101
+ end
102
+
103
+ end # class
104
+ end # module
@@ -0,0 +1,54 @@
1
+ require "cloudstack_client"
2
+ require "cloudstack_client/configuration"
3
+ require "yaml"
4
+
5
+ module CloudstackStats
6
+ class Collect
7
+
8
+ def initialize(settings)
9
+ @settings = settings.dup
10
+ @config ||= CloudstackClient::Configuration.load(@settings)
11
+ @cs ||= CloudstackClient::Client.new(
12
+ @config[:url],
13
+ @config[:api_key],
14
+ @config[:secret_key]
15
+ )
16
+ @cs.debug = true if @settings[:debug]
17
+ @cs
18
+ end
19
+
20
+ def account_stats
21
+ {
22
+ type: "account",
23
+ stats: @cs.list_accounts(client_options)
24
+ }
25
+ end
26
+
27
+ def project_stats
28
+ {
29
+ type: "project",
30
+ stats: @cs.list_projects(client_options)
31
+ }
32
+ end
33
+
34
+ private
35
+
36
+ def client_options
37
+ { listall: true, isrecursive: true }.merge(
38
+ resolve_domain(@settings)
39
+ )
40
+ end
41
+
42
+ def resolve_domain(opts)
43
+ if opts[:domain]
44
+ if domain = @cs.list_domains(name: opts[:domain]).first
45
+ opts[:domainid] = domain['id']
46
+ else
47
+ raise "Domain #{opts[:domain]} not found."
48
+ end
49
+ end
50
+ opts
51
+ end
52
+
53
+ end
54
+ end
@@ -0,0 +1,68 @@
1
+ module CloudstackStats
2
+ CS_TAGS = %w(domain)
3
+
4
+ CS_STATS = %w(
5
+ vmrunning vmstopped
6
+ memorytotal cputotal iptotal
7
+ primarystoragetotal
8
+ secondarystoragetotal
9
+ snapshottotal networktotal
10
+ volumetotal sentbytes
11
+ )
12
+
13
+ class Feed
14
+ require "net/http"
15
+ require "uri"
16
+
17
+ def initialize(opts = {})
18
+ @database = opts[:database]
19
+ @url = opts[:influx_url] ||
20
+ "http://localhost:8086/"
21
+ @user = opts[:influx_user]
22
+ @password = opts[:influx_password]
23
+ @debug = opts[:debug]
24
+ end
25
+
26
+ def write(stats)
27
+ uri = URI.parse(
28
+ URI.join(
29
+ @url,
30
+ "write?db=#{@database}&precision=m"
31
+ ).to_s
32
+ )
33
+ http = Net::HTTP.new(uri.host, uri.port)
34
+
35
+ if uri.scheme == "https"
36
+ http.use_ssl = true
37
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
38
+ end
39
+
40
+ request = Net::HTTP::Post.new(uri.request_uri)
41
+ request.content_type = "application/octet-stream"
42
+ request.basic_auth(@user, @password) if @user && @password
43
+
44
+
45
+ type = stats[:type]
46
+ stats[:stats].map do |stat|
47
+ request.body = line = stat_to_line(stat, type)
48
+ puts line if @debug
49
+ response = http.request(request)
50
+ yield(stat, response) if block_given?
51
+ end
52
+ end
53
+
54
+ private
55
+
56
+ # builds influxdb line protocol strings
57
+ def stat_to_line(obj, type)
58
+ fields = CloudstackStats::CS_STATS.map {|name| "#{name}=#{obj[name] || 0}i" }
59
+ tags = CloudstackStats::CS_TAGS.map {|name| "#{name}=#{obj[name]}" }
60
+ "#{type}." +
61
+ obj["name"].downcase.tr(" ", "-").tr("--", "-").gsub(/[^0-9A-Za-z\-\_]/, '') +
62
+ ",type=#{type}" +
63
+ "," + tags.join(",") +
64
+ " " + fields.join(",")
65
+ end
66
+
67
+ end # class
68
+ end # module
@@ -0,0 +1,3 @@
1
+ module CloudstackStats
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1 @@
1
+ require "cloudstack_stats/version"
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cloudstack_stats
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - niwo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-05-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '12.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '12.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: thor
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.19'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.19'
69
+ - !ruby/object:Gem::Dependency
70
+ name: cloudstack_client
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 1.5.0
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 1.5.0
83
+ description: Collect project and account statistics from the CloudStack API and feeds
84
+ them into a influxdb.
85
+ email:
86
+ - nik.wolfgramm@gmail.com
87
+ executables:
88
+ - cloudstack_stats
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - ".gitignore"
93
+ - ".travis.yml"
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - bin/cloudstack_stats
99
+ - bin/console
100
+ - bin/setup
101
+ - cloudstack_stats.gemspec
102
+ - lib/cloudstack_stats.rb
103
+ - lib/cloudstack_stats/cli.rb
104
+ - lib/cloudstack_stats/collect.rb
105
+ - lib/cloudstack_stats/feed.rb
106
+ - lib/cloudstack_stats/version.rb
107
+ homepage: https://github.com/niwo/cloudstack_stats
108
+ licenses:
109
+ - MIT
110
+ metadata: {}
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 2.5.2
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: Collect CloudStack stats and feed them to influxdb.
131
+ test_files: []