1. Как получить токен доступа к списку Azure метрик ресурса
a. создать участника службы и назначить роль Reader для sp. (Я использую Azure CLI для этого)
az login
az account set --subscription "<your subscription id>"
az ad sp create-for-rbac -n "readMetric" --role Reader
b. получить токен доступа
POST /<your sp tenant>/oauth2/token HTTP/1.1
Host: login.microsoftonline.com
Content-Type: application/x-www-form-urlencoded
grant_type=client_credentials
&client_id=<your sp appid>
&client_secret=<your sp password>
&resource=https://management.azure.com/
c. Список метрик для одного Azure ресурса
GET https://management.azure.com/{resource id}/providers/microsoft.insights/metrics?api-version=2018-01-01&metricnames=""
Authorization : Bearer <access_token>
2. Для Azure Management Fluent SDK, как использовать токен напрямую?
Что касается проблемы, обратитесь к следующим шагам
static async Task Main(string[] args)
{
/*
Please install sdk Microsoft.IdentityModel.Clients.ActiveDirectory and Microsoft.Azure.Management.Fluent
*/
var tenantId = "<your sp tenant>";
var clientId = "<your sp appId>";
var clientSecret = "<your sp password>";
var subscriptionId = "<your subscription id>";
var context = new AuthenticationContext("https://login.microsoftonline.com/" + tenantId, false);
var credential = new ClientCredential(clientId: clientId, clientSecret: clientSecret);
var result = await context.AcquireTokenAsync("https://management.azure.com/", credential);
var token = result.AccessToken;
var tokenCredentials = new TokenCredentials(token);
var azureCredentials = new AzureCredentials(
tokenCredentials,
tokenCredentials,
tenantId,
AzureEnvironment.AzureGlobalCloud);
var client = RestClient
.Configure()
.WithEnvironment(AzureEnvironment.AzureGlobalCloud)
.WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
.WithCredentials(azureCredentials)
.Build();
var azure = Microsoft.Azure.Management.Fluent.Azure
.Authenticate(client, tenantId)
.WithSubscription(subscriptionId);
var resourceId = "/subscriptions/e5b0fcfa-e859-43f3-8d84-5e5fe29f4c68/resourceGroups/jimtest/providers/Microsoft.Compute/virtualMachines/testvm";
foreach (var metricDefinition in await azure.MetricDefinitions.ListByResourceAsync(resourceId))
{
var metricCollection = await metricDefinition.DefineQuery()
.StartingFrom(DateTime.UtcNow.AddMinutes(-5))
.EndsBefore(DateTime.UtcNow)
.ExecuteAsync();
Console.WriteLine("Metrics for '" + resourceId + "':");
Console.WriteLine("Namespacse: " + metricCollection.Namespace);
Console.WriteLine("Query time: " + metricCollection.Timespan);
Console.WriteLine("Time Grain: " + metricCollection.Interval);
Console.WriteLine("Cost: " + metricCollection.Cost);
foreach (var metric in metricCollection.Metrics)
{
Console.WriteLine("\tMetric: " + metric.Name.LocalizedValue);
Console.WriteLine("\tType: " + metric.Type);
Console.WriteLine("\tUnit: " + metric.Unit);
Console.WriteLine("\tTime Series: ");
foreach (var timeElement in metric.Timeseries)
{
Console.WriteLine("\t\tMetadata: ");
foreach (var metadata in timeElement.Metadatavalues)
{
Console.WriteLine("\t\t\t" + metadata.Name.LocalizedValue + ": " + metadata.Value);
}
Console.WriteLine("\t\tData: ");
foreach (var data in timeElement.Data)
{
Console.WriteLine("\t\t\t" + data.TimeStamp
+ " : (Min) " + data.Minimum
+ " : (Max) " + data.Maximum
+ " : (Avg) " + data.Average
+ " : (Total) " + data.Total
+ " : (Count) " + data.Count);
}
}
}
}
}
Подробнее см. В документе