sensu-plugins-azure-acr-quota-metrics 0.0.1

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: b49ab9898831886478e7926e98ba3cff9670c501
4
+ data.tar.gz: e75d3eb336f904d744e8d18294e2f8a4d2bf41bc
5
+ SHA512:
6
+ metadata.gz: 63436eae04f92fdd110695d76bf02a4213812642d14beba38c45ac4cb12618ae1c28843c9bbf88aff0f52ffd584e58f77fb9b23b1a6a4a9881ec59397212b148
7
+ data.tar.gz: 4877afed2d10cda46d9f0df149b2ac342f2b5947b53f6d6688a472d8df4f6c7de51acb8eca76dc17729c520ad763223049d100d5436455d00144276d11334da6
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ Sensu Plugins Azure ACR Quota Metrics
2
+ =====================================
3
+
4
+ [![Build Status](https://travis-ci.org/ElvenSpellmaker/sensu-plugins-azure-acr-quota-metrics.svg?branch=master)](https://travis-ci.org/ElvenSpellmaker/sensu-plugins-azure-acr-quota-metrics) [![Gem Version](https://badge.fury.io/rb/sensu-plugins-azure-acr-quota-metrics.svg)](https://badge.fury.io/rb/sensu-plugins-azure-acr-quota-metrics)
5
+
6
+ This is a hacky Sensu plug-in to call out to the `az` to list all ACRs (Azure
7
+ Container Registries) in a subscription and get quota usage metrics.
8
+
9
+ Note: Quota metrics are only available for 'managed' ACRs, and so the `Classic`
10
+ sku is ignored.
11
+
12
+ The gem assumes `virualenv`, `python` and `pip` are installed, and will run
13
+ `install.bash` as a pre-gem hook to install a virualenv and the required `az`
14
+ command. Ick.
15
+
16
+ Tested on Ruby 2.3.
17
+
18
+ The unit test is a bit dubious, and it's almost impossible to run unit tests on
19
+ Sensu plug-ins as they run as soon as scoped...
20
+ The test must be run from the root directory also.
21
+
22
+ `bundle exec rspec` should run the test, after bundle installing of course.
23
+
24
+ Why call out to `az` instead of using the Ruby SDK?
25
+ ---------------------------------------------------
26
+ There's very little documentation and this was the path of least resistance.
27
+
28
+ I'm open to re-writes which use the Ruby Gems instead. You'll need the
29
+ `azure_mgmt_compute` and `azure_mgmt_monitor` gems.
@@ -0,0 +1,87 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ require 'json'
4
+ require 'parallel'
5
+ require 'sensu-plugin/metric/cli'
6
+ require 'time'
7
+
8
+ class AzureAcrQuotaMetrics < Sensu::Plugin::Metric::CLI::Graphite
9
+ option :schema,
10
+ description: 'Metric naming scheme, text to prepend to metric',
11
+ short: '-s SCHEME',
12
+ long: '--scheme SCHEME',
13
+ default: 'sensu'
14
+
15
+ option :spn_id,
16
+ description: 'The SPN ID with read access to the subscription',
17
+ short: '-i',
18
+ long: '--spn-id <SPN ID>',
19
+ default: nil
20
+
21
+ option :spn_secret,
22
+ description: 'The SPN secret for the SPN ID',
23
+ short: '-p',
24
+ long: '--spn-secret <SPN Secret>',
25
+ default: nil
26
+
27
+ option :tenant_id,
28
+ description: 'The Tenant ID for the SPN ID',
29
+ short: '-t',
30
+ long: '--tenant-id <Tenant ID>',
31
+ default: nil
32
+
33
+ @@time_format = '%Y-%m-%dT%H:%M:%SZ'
34
+ @@classic_sku = 'Classic'
35
+
36
+ def initialize(argv = ARGV, metrics_client = 'az')
37
+ super argv
38
+
39
+ dir = __FILE__ === '(eval)' ? '.' : "#{__dir__}/../"
40
+ require "#{dir}/lib/provider/#{metrics_client}.rb"
41
+ metrics_upper = metrics_client.capitalize
42
+ @metrics_client = Object.const_get("#{metrics_upper}Client").new
43
+ end
44
+
45
+ def run
46
+ @metrics_client.set_credentials(
47
+ config[:spn_id],
48
+ config[:spn_secret],
49
+ config[:tenant_id],
50
+ )
51
+
52
+ time = Time.now.utc
53
+
54
+ container_registries = @metrics_client.acr_list
55
+
56
+ Parallel.map(container_registries, in_threads: 5) { |acr|
57
+ # Classic ACR doesn't have a quota as it's 'unmanaged' (it's as large as
58
+ # the storage account is).
59
+ next if acr['sku']['tier'] == @@classic_sku
60
+
61
+ process_acr(acr, time)
62
+ }
63
+
64
+ ok
65
+ end
66
+
67
+ private
68
+
69
+ def process_acr(acr, time)
70
+ acr_name = acr['name']
71
+ acr_resource_group = acr['resourceGroup']
72
+
73
+ usage_metrics = @metrics_client.acr_show_usage(acr_name, acr_resource_group)
74
+
75
+ usage_metrics['value'].each { |metric_dimension|
76
+ next if metric_dimension['name'] != @metrics_client.quota_metrics_type
77
+
78
+ current_quota = metric_dimension['currentValue']
79
+ remaining_quota = metric_dimension['limit'] - current_quota
80
+
81
+ output [config[:schema], acr_name, 'used'].join('.'), current_quota, time.to_i
82
+ output [config[:schema], acr_name, 'remaining'].join('.'), remaining_quota, time.to_i
83
+
84
+ break
85
+ }
86
+ end
87
+ end
data/install.bash ADDED
@@ -0,0 +1,43 @@
1
+ #!/bin/bash
2
+
3
+ if [ "$TRAVIS" = true ]; then
4
+ echo "This is Travis, don't pre-install \`az\`... Exiting 0..."
5
+ exit 0
6
+ fi
7
+
8
+ ENV_DIR="/az-python"
9
+
10
+ check_error()
11
+ {
12
+ if [ $1 -ne 0 ]; then
13
+ echo 'Failed to install!' >&2
14
+ exit $1
15
+ fi
16
+ }
17
+
18
+ AZ_PACKAGE='azure-cli'
19
+ AZ_VERSION='2.0.31'
20
+
21
+ if [ -f "$ENV_DIR/bin/activate" ]; then
22
+ . "$ENV_DIR/bin/activate"
23
+ if [ "$(pip freeze | grep -oP "$AZ_PACKAGE"'==\K.*')" == "$AZ_VERSION" ]; then
24
+ echo '`az` is already installed in the virtualenv at the correct version!'
25
+ exit 0
26
+ fi
27
+ fi
28
+
29
+ virtualenv $ENV_DIR
30
+ check_error $?
31
+
32
+ . $ENV_DIR/bin/activate
33
+ check_error $?
34
+
35
+ pip install --upgrade 'pip==10.0.1'
36
+ check_error $?
37
+ # `azure-cli` will actually replace wheel with this version...
38
+ pip install --upgrade 'wheel==0.30.0'
39
+ check_error $?
40
+ pip install --upgrade 'setuptools==39.0.1'
41
+ check_error $?
42
+ pip install --upgrade "$AZ_PACKAGE==$AZ_VERSION"
43
+ 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-principal -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 acr_list
6
+ az('acr list')
7
+ end
8
+
9
+ def acr_show_usage(acr_name, acr_resource_group)
10
+ az("acr show-usage -g #{acr_resource_group} -n #{acr_name}")
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,18 @@
1
+ class BaseClient
2
+ @@quota_metrics_type = 'Size'.freeze
3
+
4
+ def set_credentials(spn_id = nil, spn_secret = nil, tenant_id = nil)
5
+ @spn_id = spn_id
6
+ @spn_secret = spn_secret
7
+ @tenant_id = tenant_id
8
+ end
9
+
10
+ def quota_metrics_type
11
+ @@quota_metrics_type
12
+ end
13
+
14
+ def quota_metrics_felds
15
+ @@quota_metrics_fields
16
+ end
17
+
18
+ end
@@ -0,0 +1,18 @@
1
+ require_relative 'base'
2
+ require 'json'
3
+
4
+ class FileClient < BaseClient
5
+ def acr_list
6
+ file_get_contents('spec/acr_list.json')
7
+ end
8
+
9
+ def acr_show_usage(acr_name, acr_resource_group)
10
+ file_get_contents('spec/acr_show_usage.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 SensuPluginsAzureAcrQuotaMetrics
2
+ class Version
3
+ MAJOR = 0
4
+ MINOR = 0
5
+ PATCH = 1
6
+
7
+ VER_STRING = [MAJOR, MINOR, PATCH].compact.join('.')
8
+ end
9
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sensu-plugins-azure-acr-quota-metrics
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jack
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-11-15 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: parallel
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 1.12.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 1.12.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: sensu-plugin
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.2'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: A hacky Sensu plug-in to provide ACR Quota metrics by calling out to
70
+ `az`
71
+ email:
72
+ executables:
73
+ - sensu-plugins-azure-acr-quota-metrics.rb
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - README.md
78
+ - bin/sensu-plugins-azure-acr-quota-metrics.rb
79
+ - install.bash
80
+ - lib/provider/az.bash
81
+ - lib/provider/az.rb
82
+ - lib/provider/base.rb
83
+ - lib/provider/file.rb
84
+ - lib/rubygems_plugin.rb
85
+ - lib/sensu-plugins-azure-acr-quota-metrics/version.rb
86
+ homepage: https://github.com/ElvenSpellmaker/sensu-plugins-azure-acr-quota-metrics
87
+ licenses:
88
+ - MIT
89
+ metadata:
90
+ maintainer: "@ElvenSpellmaker"
91
+ development_status: active
92
+ production_status: unstable - testing recommended
93
+ release_draft: 'false'
94
+ release_prerelease: 'false'
95
+ post_install_message: You can use the embedded Ruby by setting EMBEDDED_RUBY=true
96
+ in /etc/default/sensu
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '2.3'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.6.8
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: Sensu metrics plugin for Azure Container Registry Quota Metrics
116
+ test_files: []