咱們知道,雲計算的特色之一是:彈性計算。若是在某一時間點,用於對雲計算資源的使用量超過了實際部署的硬件水平,咱們就能夠經過修改:html
可是Windows Azure畢竟是在遠程數據中心的託管服務,咱們在本地是沒法監視和查看Windows Azure託管服務的資源使用狀況。若是咱們想查看Windows Azure的性能監視器,你可能會想到:web
可是這種方法的缺點顯而易見:windows
考慮到這些問題,微軟建立了Windows Azure Diagnostics(診斷)的機制。Windows Azure Diagnostics能夠診斷檢索許多內容,好比跟蹤日誌,奔潰事件日誌等,而且保存下來。在本章裏,我將側重於Peformance Counter(性能計數器),該功能能夠提供WIndows Azure應用程序的關鍵信息。實際上,性能計數器,能夠幫助您隔離性能問題。最重要的是,它能夠幫您省錢,讓Windows Azure的資源獲得最佳利用。緩存
Windows Azure Diagnostics網絡
每個Windows Azure計算節點都有一個內置的診斷進程(DiagnosticsAgent.exe),負責按期收集診斷數據,並緩存到本地文件,並最終存儲到一個預約義的Azure存儲。請注意,在診斷過程當中,也能夠手動觸發。ide
具體來講,Peformance Counter的數據被保存到Windows Azure Storage的Table中,表名爲WADPerformanceCountersTable。其餘Diagnostics,好比跟蹤日誌,事件日誌等數據也都存儲在指定的表,像WadLogsTable
,WADDiagnosticInfrastructureLogsTable等
。更多信息,能夠參考這裏。工具
每個Role Instance都有一個Configuration File (配置文件),位於Azure Storage Blob下,Container(概念上相似於文件夾)爲wad-control-container。配置文件主要收集性能計數器數據和相關的使用率。post
下面顯示的是Disnostics Configuration File的位置。你能夠下載Cloud Storage Manager訪問wad-control-container。性能
配置文件中使用了一個標準的XML格式,並能夠手動修改(不推薦)。雲計算
在這篇文章中,咱們會研究
1.若是配置Role.cs,而且從外部程序讀取Peformance Counter
2.如何訪問Azure Storage Table中的數據
3.性能計數器的快速分析
參考自MSDN
爲了Diagnostics 你的Role,你必須先在ServiceDefinition.csdef中修改配置文件。具體的您能夠參考個人博文Windows
Azure Platform (二十)使用Windows Azure診斷收集日誌記錄數據
public class WebRole : RoleEntryPoint { /// <summary> /// Entry point before starting the Role /// </summary> public override bool OnStart() { // Get the Role instance Diagnostics configuration. var config = DiagnosticMonitor.GetDefaultInitialConfiguration(); // Define interval for persisting the performance counter data to azure storage. config.PerformanceCounters.ScheduledTransferPeriod = TimeSpan.FromMinutes(5); // CPU Utilization Counter PerformanceCounterConfiguration cpuUtilizationCounter = new PerformanceCounterConfiguration() { CounterSpecifier = @"\Processor(_Total)\% Processor Time", //define the sample internal for the specific performance counter SampleRate = TimeSpan.FromSeconds(1) }; if (!config.PerformanceCounters.DataSources.Contains(cpuUtilizationCounter, new PerformanceCounterComparer())) { config.PerformanceCounters.DataSources.Add(cpuUtilizationCounter); } // Start diagnostic monitor with the new configuration. DiagnosticMonitor.Start ("Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString", config); return base.OnStart(); } /// <summary> /// Performance Counter Comparer /// </summary> private class PerformanceCounterComparer : IEqualityComparer<PerformanceCounterConfiguration> { public bool Equals(PerformanceCounterConfiguration a, PerformanceCounterConfiguration b) { //Check whether the compared objects reference the same data. if (Object.ReferenceEquals(a, b)) return true; //Check whether any of the compared objects is null. if (Object.ReferenceEquals(a, null) || Object.ReferenceEquals(b, null)) return false; // Check if the counter specifier and the sampling rate are the same. return (a.CounterSpecifier == b.CounterSpecifier && a.SampleRate == b.SampleRate); } public int GetHashCode(PerformanceCounterConfiguration counter) { //Check whether the object is null if (Object.ReferenceEquals(counter, null)) return 0; //Get hash code for the CounterSpecifier field if it is not null. int hashCounterSpecifier = counter.CounterSpecifier == null ? 0 : counter.CounterSpecifier.GetHashCode(); //Calculate the hash code for the counter. return hashCounterSpecifier ^ counter.SampleRate.GetHashCode(); } } }
使用 Microsoft.WindowsAzure.Diagnostics.Management
.
const string storageAccoutName = "Storage-Name-Here"; const string privateKey = "Storge-Private-Key-Here"; const string deploymentId = "Deployment-Id-Here"; var storageAccount = CloudStorageAccount.Parse(String.Format( "DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}", storageAccoutName, privateKey)); // Get the diagnostics manager for the entire storage account. var diagnosticManager = storageAccount.CreateDeploymentDiagnosticManager(deploymentId); // Get diagnostics manager for the specific role instance. RoleInstanceDiagnosticManager roleDiagManager = diagnosticManager.GetRoleInstanceDiagnosticManager("WebRole1", "WebRole1_IN_0"); //Modify current configuration var currentConfiguariton = roleDiagManager.GetCurrentConfiguration(); currentConfiguariton.PerformanceCounters.ScheduledTransferPeriod = TimeSpan.FromMinutes(5); currentConfiguariton.PerformanceCounters.DataSources.Add (new PerformanceCounterConfiguration() { CounterSpecifier = @"\Processor(_Total)\% Processor Time", SampleRate = TimeSpan.FromSeconds(1) }); //Commit the changes roleDiagManager.SetCurrentConfiguration(currentConfiguariton);
如今,咱們已經配置了咱們要監視的計數器。讓咱們訪問Azure Storage Table(表名WADPerformanceCountersTable),並顯示出來。
我建立了PerformanceDataContext,派生自TableServiceContext
。這是爲了鏈接到Azure表,Microsoft提供的ADO擴展的一部分。你可使用LINQ查詢以檢 索數據。
/// <summary> /// Query helper for retrieving data from the WADPerformanceCountersTable /// </summary> public class QueryExecuter { /// <summary> /// Cloud storage account client /// </summary> private CloudStorageAccount accountStorage; /// <summary> /// Default Constructor - Use development storage emulator. /// </summary> public QueryExecuter() { accountStorage = CloudStorageAccount.DevelopmentStorageAccount; } /// <summary> /// Constructor /// </summary> /// <param name="accountName">Azure storage name</param> /// <param name="privateKey">Azure storage private key</param> public QueryExecuter(string accountName, string privateKey) { accountStorage = CloudStorageAccount.Parse(String.Format( "DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}", account } /// <summary> /// Retrieve Performance counter data /// </summary> /// <param name="counterFullName">Counter specifier full name</param> /// <param name="deploymentid">Deployment id</param> /// <param name="roleName">Role name</param> /// <param name="roleInstanceName">Role instance name</param> /// <param name="startPeriod">Start sample date time</param> /// <param name="endPeriod">End sample date time</param> /// <returns></returns> public List<PerformanceData> QueryPerformanceCounter(string counterFullName, string deploymentid, string roleName, string roleInstanceName, DateTime startPeriod, DateTime endPeriod) { PerformanceDataContext context = new PerformanceDataContext( accountStorage.TableEndpoint.ToString(), accountStorage.Credentials); var data = context.PerfData; CloudTableQuery<PerformanceData> query = null; query = (from d in data where d.PartitionKey.CompareTo("0" + startPeriod.Ticks) >= 0 && d.PartitionKey.CompareTo ("0" + endPeriod.Ticks) <= 0 && d.CounterName == counterFullName && d.EventTickCount >= startPeriod.Ticks && d.EventTickCount <= endPeriod.Ticks && d.DeploymentId == deploymentid && d.Role == roleName && d.RoleInstance == roleInstanceName select d).AsTableServiceQuery<PerformanceData>(); List<PerformanceData> selectedData = new List<PerformanceData>(); try { selectedData = query.Execute().ToList<PerformanceData>(); } catch { } return selectedData; } }
此演示,我建立了一個WindowsForm,將Diagnostics Table中的數據填充到圖表中。
顯示的是過去2個小時內,某一個Role Instance的CPU使用率。
Quest 軟件公司開發了一個很是方便和容易使用的工具。稱爲Spotlight on Azure。爲咱們對整個Azure訂閱提供了深度監控的功能,包括Role Instance,數據聚合,歷史數據展現,提醒機制,用戶自定義深度分析計數器。