This repository has been archived by the owner on Dec 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Program.cs
216 lines (187 loc) · 10.2 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using Microsoft.Azure.Management.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent.Core;
using Microsoft.Azure.Management.Samples.Common;
using Microsoft.Azure.Management.Storage.Fluent.Models;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure.Storage.Shared.Protocol;
using System;
using System.Linq;
using System.IO;
namespace QueryMetricsAndActivityLogs
{
public class Program
{
/**
* This sample shows examples of retrieving metrics and activity logs for Storage Account.
* - List all metric definitions available for a storage account
* - Retrieve and show metrics for the past 7 days for Transactions where
* - Api name was 'PutBlob' and
* - response type was 'Success' and
* - Geo type was 'Primary'
* - Retrieve and show all activity logs for the past 7 days for the same Storage account.
*/
public static void RunSample(IAzure azure)
{
string storageAccountName = SdkContext.RandomResourceName("saMonitor", 15);
string rgName = SdkContext.RandomResourceName("rgMonitor", 15);
try
{
// ============================================================
// Create a storage account
Utilities.Log("Creating a Storage Account");
var storageAccount = azure.StorageAccounts.Define(storageAccountName)
.WithRegion(Region.USEast)
.WithNewResourceGroup(rgName)
.WithBlobStorageAccountKind()
.WithAccessTier(AccessTier.Cool)
.Create();
Utilities.Log("Created a Storage Account:");
Utilities.PrintStorageAccount(storageAccount);
var storageAccountKeys = storageAccount.GetKeys();
var storageConnectionString = $"DefaultEndpointsProtocol=http;AccountName={storageAccount.Name};AccountKey={storageAccountKeys.First().Value}";
// Add some blob transaction events
AddBlobTransactions(storageConnectionString);
DateTime recordDateTime = DateTime.Now;
// get metric definitions for storage account.
foreach (var metricDefinition in azure.MetricDefinitions.ListByResource(storageAccount.Id))
{
// find metric definition for Transactions
if (metricDefinition.Name.LocalizedValue.Equals("transactions", StringComparison.OrdinalIgnoreCase))
{
// get metric records
var metricCollection = metricDefinition.DefineQuery()
.StartingFrom(recordDateTime.AddDays(-7))
.EndsBefore(recordDateTime)
.WithAggregation("Average")
.WithInterval(TimeSpan.FromMinutes(5))
.WithOdataFilter("apiName eq 'PutBlob' and responseType eq 'Success' and geoType eq 'Primary'")
.Execute();
Utilities.Log("Metrics for '" + storageAccount.Id + "':");
Utilities.Log("Namespacse: " + metricCollection.Namespace);
Utilities.Log("Query time: " + metricCollection.Timespan);
Utilities.Log("Time Grain: " + metricCollection.Interval);
Utilities.Log("Cost: " + metricCollection.Cost);
foreach (var metric in metricCollection.Metrics)
{
Utilities.Log("\tMetric: " + metric.Name.LocalizedValue);
Utilities.Log("\tType: " + metric.Type);
Utilities.Log("\tUnit: " + metric.Unit);
Utilities.Log("\tTime Series: ");
foreach (var timeElement in metric.Timeseries)
{
Utilities.Log("\t\tMetadata: ");
foreach (var metadata in timeElement.Metadatavalues)
{
Utilities.Log("\t\t\t" + metadata.Name.LocalizedValue + ": " + metadata.Value);
}
Utilities.Log("\t\tData: ");
foreach (var data in timeElement.Data)
{
Utilities.Log("\t\t\t" + data.TimeStamp
+ " : (Min) " + data.Minimum
+ " : (Max) " + data.Maximum
+ " : (Avg) " + data.Average
+ " : (Total) " + data.Total
+ " : (Count) " + data.Count);
}
}
}
break;
}
}
// get activity logs for the same period.
var logs = azure.ActivityLogs.DefineQuery()
.StartingFrom(recordDateTime.AddDays(-7))
.EndsBefore(recordDateTime)
.WithAllPropertiesInResponse()
.FilterByResource(storageAccount.Id)
.Execute();
Utilities.Log("Activity logs for the Storage Account:");
foreach (var eventData in logs)
{
Utilities.Log("\tEvent: " + eventData.EventName?.LocalizedValue);
Utilities.Log("\tOperation: " + eventData.OperationName?.LocalizedValue);
Utilities.Log("\tCaller: " + eventData.Caller);
Utilities.Log("\tCorrelationId: " + eventData.CorrelationId);
Utilities.Log("\tSubscriptionId: " + eventData.SubscriptionId);
}
}
finally
{
if (azure.ResourceGroups.GetByName(rgName) != null)
{
Utilities.Log("Deleting Resource Group: " + rgName);
azure.ResourceGroups.BeginDeleteByName(rgName);
Utilities.Log("Deleted Resource Group: " + rgName);
}
else
{
Utilities.Log("Did not create any resources in Azure. No clean up is necessary");
}
}
}
public static void Main(string[] args)
{
try
{
//=================================================================
// Authenticate
var credentials = SdkContext.AzureCredentialsFactory.FromFile(Environment.GetEnvironmentVariable("AZURE_AUTH_LOCATION"));
var azure = Azure
.Configure()
.WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
.Authenticate(credentials)
.WithDefaultSubscription();
// Print selected subscription
Utilities.Log("Selected subscription: " + azure.SubscriptionId);
RunSample(azure);
}
catch (Exception ex)
{
Utilities.Log(ex);
}
}
private static void AddBlobTransactions(string storageConnectionString)
{
// Upload the script file as block blob
//
var account = CloudStorageAccount.Parse(storageConnectionString);
var cloudBlobClient = account.CreateCloudBlobClient();
var container = cloudBlobClient.GetContainerReference("scripts");
container.CreateIfNotExistsAsync().GetAwaiter().GetResult();
var serviceProps = cloudBlobClient.GetServicePropertiesAsync().GetAwaiter().GetResult();
// configure Storage logging and metrics
serviceProps.Logging = new LoggingProperties
{
LoggingOperations = LoggingOperations.All,
RetentionDays = 2,
Version = "1.0"
};
var metricProps = new MetricsProperties
{
MetricsLevel = MetricsLevel.ServiceAndApi,
RetentionDays = 2,
Version = "1.0"
};
serviceProps.HourMetrics = metricProps;
serviceProps.MinuteMetrics = metricProps;
// Set the default service version to be used for anonymous requests.
serviceProps.DefaultServiceVersion = "2015-04-05";
// Set the service properties.
cloudBlobClient.SetServicePropertiesAsync(serviceProps).GetAwaiter().GetResult();
var containerPermissions = new BlobContainerPermissions();
// Include public access in the permissions object
containerPermissions.PublicAccess = BlobContainerPublicAccessType.Container;
// Set the permissions on the container
container.SetPermissionsAsync(containerPermissions).GetAwaiter().GetResult();
var blob = container.GetBlockBlobReference("install_apache.sh");
blob.UploadFromFileAsync(Path.Combine(Utilities.ProjectPath, "Asset", "install_apache.sh")).GetAwaiter().GetResult();
// give sometime for the infrastructure to process the records and fit into time grain.
SdkContext.DelayProvider.Delay(6 * 60000);
}
}
}