Skip to content

Instantly share code, notes, and snippets.

@djberg96
Created November 13, 2020 14:26
Show Gist options
  • Select an option

  • Save djberg96/0facce6abae88a5a9c67beb94c1e19c8 to your computer and use it in GitHub Desktop.

Select an option

Save djberg96/0facce6abae88a5a9c67beb94c1e19c8 to your computer and use it in GitHub Desktop.
# azure_metrics_memory.rb
# -- Get memory information from a diagnostics storage account
require 'azure-armrest'
require 'pp'
# Put your real credentials here
conf = Azure::Armrest::Configuration.new(
:subscription_id => 'xxx',
:tenant_id => 'xxx',
:client_id => 'xxx',
:client_key => 'xxx'
)
vms = Azure::Armrest::VirtualMachineService.new(conf)
ims = Azure::Armrest::Insights::MetricsService.new(conf)
sas = Azure::Armrest::StorageAccountService.new(conf)
# Replace with real values
vm_name = 'your_vm_name'
rgroup = 'your_resource_group_name'
sa_name = 'name_of_diagnostics_storage_account_attached_to_your_vm'
vm = vms.get(vm_name, rgroup)
sa = sas.get(sa_name, rgroup)
key = sas.list_account_keys(sa.name, sa.resource_group).fetch('key1')
# One day back. Update as desired.
start_time = Time.now.utc - 86400
end_time = Time.now.utc
metric_name = '/builtin/memory/percentusedmemory'
filter = <<~FILTER
CounterName eq '#{metric_name}' and \
Average ge 0 and \
Timestamp ge datetime'#{start_time.iso8601}' and \
Timestamp le datetime'#{end_time.iso8601}'
FILTER
# Update as needed. You can find your metrics table name in the storage explorer.
table_name = 'WADMetricsPT1MP10DV2S20201110'
table_data = sa.table_data(
table_name,
key,
:filter => filter,
:select => 'TIMESTAMP,Average',
:all => true
)
p table_data.size # Make sure it's more than 0
pp table_data # Might be alot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment