sensu-plugins-azure-burstable-metrics 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d5cdf2ac9d672268a82bbb734e5f397ceb03078f
4
+ data.tar.gz: 7a34963590928735f6c95cc11d27674e9ea35eb5
5
+ SHA512:
6
+ metadata.gz: d74553d99db3544b7a273fa4cdf8d3811e7faef5d1996d9d629cc9f8965354d4c0f2ae717fb6cf3f3c1318401eac8c5a869c502566776cc1e9d00f7177d230e7
7
+ data.tar.gz: 4d570ab6fc5ca5e5b9547ba58a23dbe940d31384e59bb09a59d01aa17a808d7edb655b7a10e666d20ddb092f60fc4582bb372fb280e1faf5bd8c07913723af6e
data/README.md ADDED
@@ -0,0 +1,25 @@
1
+ Sensu Plugins Azure Burstable Metrics
2
+ =====================================
3
+
4
+ This is a hacky Sensu plug-in to call out to the `az` to list all vms in a
5
+ subscription and then gather metrics about burstable credits and output them in
6
+ a Graphite way.
7
+
8
+ The gem assumes `virualenv`, `python` and `pip` are installed, and will run
9
+ `install.bash` as a pre-gem hook to install a virualenv and the required `az`
10
+ command. Ick.
11
+
12
+ Tested on Ruby 2.3.
13
+
14
+ The unit test is a bit dubious, and it's almost impossible to run unit tests on
15
+ Sensu plug-ins as they run as soon as scoped...
16
+ The test must be run from the root directory also.
17
+
18
+ `bundle exec rspec` should run the test, after bundle installing of course.
19
+
20
+ Why call out to `az` instead of using the Ruby SDK?
21
+ ---------------------------------------------------
22
+ There's very little documentation and this was the path of least resistance.
23
+
24
+ I'm open to re-writes which use the Ruby Gems instead. You'll need the
25
+ `azure_mgmt_compute` and `azure_mgmt_monitor` gems.
@@ -0,0 +1,79 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ require 'json'
4
+ require 'sensu-plugin/metric/cli'
5
+ require 'time'
6
+
7
+ class BurstableMetrics < Sensu::Plugin::Metric::CLI::Graphite
8
+ option :schema,
9
+ description: 'Metric naming scheme, text to prepend to metric',
10
+ short: '-s SCHEME',
11
+ long: '--scheme SCHEME',
12
+ default: 'sensu'
13
+
14
+ option :spn_id,
15
+ description: 'The SPN ID with read access to the subscription',
16
+ short: '-i',
17
+ long: '--spn-id <SPN ID>',
18
+ default: nil
19
+
20
+ option :spn_secret,
21
+ description: 'The SPN secret for the SPN ID',
22
+ short: '-p',
23
+ long: '--spn-secret <SPN Secret>',
24
+ default: nil
25
+
26
+ option :tenant_id,
27
+ description: 'The Tenant ID for the SPN ID',
28
+ short: '-t',
29
+ long: '--tenant-id <Tenant ID>',
30
+ default: nil
31
+
32
+ @@time_format = '%Y-%m-%dT%H:%M:%SZ'
33
+
34
+ def initialize(argv = ARGV, metrics_client = 'az')
35
+ super argv
36
+
37
+ dir = __FILE__ === '(eval)' ? '.' : "#{__dir__}/../"
38
+ require "#{dir}/lib/provider/#{metrics_client}.rb"
39
+ metrics_upper = metrics_client.capitalize
40
+ @metrics_client = Object.const_get("#{metrics_upper}Client").new
41
+ end
42
+
43
+ def run
44
+ @metrics_client.set_credentials(
45
+ config[:spn_id],
46
+ config[:spn_secret],
47
+ config[:tenant_id],
48
+ )
49
+
50
+ machines = @metrics_client.vm_list.select! { |machine| machine['hardwareProfile']['vmSize'] =~ /.*B\d{1,2}s$/ }
51
+
52
+ time = Time.now.utc
53
+ start_time = (time - 120).strftime(@@time_format)
54
+ end_time = (time - 60).strftime(@@time_format)
55
+
56
+ machines.each do |machine|
57
+ process_machine(machine, time, start_time, end_time)
58
+ end
59
+
60
+ ok
61
+ end
62
+
63
+ private
64
+
65
+ def process_machine(machine, time, start_time, end_time)
66
+ machine_id = machine['id']
67
+ machine_name = machine['name']
68
+
69
+ metrics = @metrics_client.machine_burstable_metrics(machine_id, start_time, end_time)
70
+
71
+ metrics['value'].each_with_index do |metric, index|
72
+ process_metric(machine_name, metric, index, time)
73
+ end
74
+ end
75
+
76
+ def process_metric(machine_name, metric, index, time)
77
+ output [config[:schema], machine_name, @metrics_client.graphite_burstable_metrics_list[index]].join('.'), metric['timeseries'][0]['data'][0]['average'], time.to_i
78
+ end
79
+ end
data/install.bash ADDED
@@ -0,0 +1,24 @@
1
+ #!/bin/bash
2
+
3
+ ENV_DIR="/az-python"
4
+
5
+ check_error()
6
+ {
7
+ if [ $1 -ne 0 ]; then
8
+ echo 'Failed to install!' >&2
9
+ exit $1
10
+ fi
11
+ }
12
+
13
+ virtualenv $ENV_DIR
14
+ check_error $?
15
+
16
+ . $ENV_DIR/bin/activate
17
+ check_error $?
18
+
19
+ pip install --upgrade 'wheel==0.31.0'
20
+ check_error $?
21
+ pip install --upgrade 'setuptools==39.0.1'
22
+ check_error $?
23
+ pip install --upgrade 'azure-cli==2.0.31'
24
+ check_error $?
@@ -0,0 +1,20 @@
1
+ #!/bin/bash
2
+
3
+ ENV_DIR="/az-python"
4
+
5
+ while getopts c:i:p:t: option
6
+ do
7
+ case "${option}"
8
+ in
9
+ c) AZ_COMMAND=${OPTARG};;
10
+ i) SPN_ID=${OPTARG};;
11
+ p) SPN_SECRET=${OPTARG};;
12
+ t) TENANT_ID=${OPTARG};;
13
+ esac
14
+ done
15
+
16
+ . $ENV_DIR/bin/activate &> /dev/null
17
+
18
+ az login --service-principle -u "$SPN_ID" -p "$SPN_SECRET" --tenant "$TENANT_ID" &> /dev/null
19
+
20
+ eval az $AZ_COMMAND
@@ -0,0 +1,19 @@
1
+ require_relative 'base'
2
+ require 'json'
3
+
4
+ class AzClient < BaseClient
5
+ def vm_list
6
+ az('vm list')
7
+ end
8
+
9
+ def machine_burstable_metrics(machine_id, start_time, end_time)
10
+ az("monitor metrics list --resource #{machine_id} --metric '#{@@burstable_metrics_list}' --interval PT1M --start-time #{start_time} --end-time #{end_time}")
11
+ end
12
+
13
+ private
14
+
15
+ def az(command)
16
+ az_output = %x(#{__dir__}/az.bash -c "#{command}" -i "#{@spn_id}" -p "#{@spn_secret}" -t "#{@tenant_id}")
17
+ JSON.parse(az_output)
18
+ end
19
+ end
@@ -0,0 +1,16 @@
1
+ class BaseClient
2
+ @@burstable_metrics = ['CPU Credits Consumed', 'CPU Credits Remaining'].freeze
3
+ @@burstable_metrics_list = @@burstable_metrics.join(',').freeze
4
+ @@output_burstable_metrics = @@burstable_metrics.map { |metric| metric.downcase.gsub!(' ','_') }.freeze
5
+
6
+ def set_credentials(spn_id = nil, spn_secret = nil, tenant_id = nil)
7
+ @spn_id = spn_id
8
+ @spn_secret = spn_secret
9
+ @tenant_id = tenant_id
10
+ end
11
+
12
+ def graphite_burstable_metrics_list
13
+ @@output_burstable_metrics
14
+ end
15
+
16
+ end
@@ -0,0 +1,18 @@
1
+ require_relative 'base'
2
+ require 'json'
3
+
4
+ class FileClient < BaseClient
5
+ def vm_list
6
+ file_get_contents('spec/vm_list.json')
7
+ end
8
+
9
+ def machine_burstable_metrics(machine_id, start_time, end_time)
10
+ file_get_contents('spec/machine_burstable_metrics.json')
11
+ end
12
+
13
+ private
14
+
15
+ def file_get_contents(file_name)
16
+ JSON.parse(File.open(file_name, 'r') { |file| file.read })
17
+ end
18
+ end
@@ -0,0 +1,5 @@
1
+ Gem.pre_install do
2
+ %x(#{File.dirname(__FILE__) + '/../install.bash'})
3
+
4
+ $?.success?
5
+ end
@@ -0,0 +1,9 @@
1
+ module SensuPluginsAzureBurstableMetrics
2
+ class Version
3
+ MAJOR = 0
4
+ MINOR = 0
5
+ PATCH = 3
6
+
7
+ VER_STRING = [MAJOR, MINOR, PATCH].compact.join('.')
8
+ end
9
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sensu-plugins-azure-burstable-metrics
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Jack
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-05-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 2.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 2.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: sensu-plugin
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: A hacky Sensu plug-in to provide burstable metrics by calling out to
56
+ `az`
57
+ email:
58
+ executables:
59
+ - sensu-plugins-azure-burstable-metrics.rb
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - README.md
64
+ - bin/sensu-plugins-azure-burstable-metrics.rb
65
+ - install.bash
66
+ - lib/provider/az.bash
67
+ - lib/provider/az.rb
68
+ - lib/provider/base.rb
69
+ - lib/provider/file.rb
70
+ - lib/rubygems_plugin.rb
71
+ - lib/sensu-plugins-azure-burstable-metrics/version.rb
72
+ homepage: https://github.com/ElvenSpellmaker/sensu-plugins-azure-burstable-metrics
73
+ licenses:
74
+ - MIT
75
+ metadata:
76
+ maintainer: "@ElvenSpellmaker"
77
+ development_status: active
78
+ production_status: unstable - testing recommended
79
+ release_draft: 'false'
80
+ release_prerelease: 'false'
81
+ post_install_message: You can use the embedded Ruby by setting EMBEDDED_RUBY=true
82
+ in /etc/default/sensu
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '2.3'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.6.8
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: Sensu metrics plugin for Azure Burstable Machines
102
+ test_files: []