Can an Azure web site/app determine its instance's CPU/memory usage?

僤鯓⒐⒋嵵緔 提交于 2019-12-24 15:44:00

问题


I have a need for my instances to log their cpu/mem usage (% of total preferably), but everything I've tried in an Azure web site doesn't work. It breaks startup and results in 502 errors. I've tried ManagementObjectSearcher and PerformanceCounter so far. These work find locally but not when deployed to Azure. Is this functionality just disabled? Would a WebJob have better luck maybe?

The only alternative I can think of if there's no way to do this on the web site instance itself is make a PowerShell script or other app that can look it up via the Azure APIs and store it in a shared place for the instances to look up on the fly...


回答1:


You cannot access Performance counters data and similar stuff in Azure Webapps. You should be able to use the System.Diagnostics.Process class and retrieve a process list and find out the w3wp process and get its CPU and memory usage if you really want to do this within the website code itself.

<%@ Page language="cs" %>
<%@ Import Namespace="System.Diagnostics" %>
<%
foreach (Process p in  System.Diagnostics.Process.GetProcesses())
{
Response.Write ("<br/>" + p.ProcessName + "\t" + p.Id + "\t" +    p.PrivateMemorySize+ "\t" + "\t" );
}

%>

The easiest thing to do is to write a powershell script and runt it as a webjob. A powershell code like this in your webjob might give you the information that you are looking for

gps|where {$_.name -eq 'w3wp' }|select PrivateMemorySize, CPU, Id

hope this helps...



来源:https://stackoverflow.com/questions/29352297/can-an-azure-web-site-app-determine-its-instances-cpu-memory-usage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!