5 alert monitoring azure azure-web-app-service file-system-storage
我正在标准应用服务计划上运行 Azure 应用服务,该计划允许使用最大 50 GB 的文件存储。该应用程序使用相当多的磁盘空间来缓存图像。目前的消耗水平约为 15 GB,但如果缓存清理策略由于某种原因失败,它将很快增长到顶部。
垂直自动扩展(扩展)并不是一种常见的做法,因为根据这篇 Microsoft 文章,它通常需要一些服务停机时间:
https://learn.microsoft.com/en-us/azure/architecture/best-practices/auto-scaling
所以问题是:
有没有办法为 Azure 应用服务设置磁盘空间不足警报?
我在“警报”选项卡下的可用选项中找不到与磁盘空间相关的任何内容。
有没有办法为 Azure 应用服务设置磁盘空间不足警报?我在“警报”选项卡下的可用选项中找不到与磁盘空间相关的任何内容。
据我所知,更改选项卡不包含网络应用程序的配额选择。因此,我建议您可以编写自己的逻辑来为 Azure 应用服务设置磁盘空间不足的警报。
您可以使用 azure Web 应用程序的 webjobs 运行后台任务来检查 Web 应用程序的使用情况。
我建议您可以使用 webjob timetrigger(您需要从 nuget 安装 webjobs 扩展)来运行计划作业。然后,您可以向 azure 管理 api 发送休息请求,以获取 Web 应用程序的当前使用情况。您可以根据您的网络应用程序当前使用情况发送电子邮件或其他内容。
更多详细信息,您可以参考下面的代码示例:
注意:如果要使用rest api获取当前Web应用程序的使用情况,您需要首先创建Azure Active Directory应用程序和服务主体。生成服务主体后,您可以获得applicationid、access key和talentid。更多详细信息,您可以参考这篇文章。
代码:
// Runs once every 5 minutes
public static void CronJob([TimerTrigger("0 */5 * * * *" ,UseMonitor =true)] TimerInfo timer,TextWriter log)
{
if (GetCurrentUsage() > 25)
{
// Here you could write your own code to do something when the file exceed the 25GB
log.WriteLine("fired");
}
}
private static double GetCurrentUsage()
{
double currentusage = 0;
string tenantId = "yourtenantId";
string clientId = "yourapplicationid";
string clientSecret = "yourkey";
string subscription = "subscriptionid";
string resourcegroup = "resourcegroupbane";
string webapp = "webappname";
string apiversion = "2015-08-01";
string authContextURL = "https://login.windows.net/" + tenantId;
var authenticationContext = new AuthenticationContext(authContextURL);
var credential = new ClientCredential(clientId, clientSecret);
var result = authenticationContext.AcquireTokenAsync(resource: "https://management.azure.com/", clientCredential: credential).Result;
if (result == null)
{
throw new InvalidOperationException("Failed to obtain the JWT token");
}
string token = result.AccessToken;
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(string.Format("https://management.azure.com/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Web/sites/{2}/usages?api-version={3}", subscription, resourcegroup, webapp, apiversion));
request.Method = "GET";
request.Headers["Authorization"] = "Bearer " + token;
request.ContentType = "application/json";
//Get the response
var httpResponse = (HttpWebResponse)request.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
string jsonResponse = streamReader.ReadToEnd();
dynamic ob = JsonConvert.DeserializeObject(jsonResponse);
dynamic re = ob.value.Children();
foreach (var item in re)
{
if (item.name.value == "FileSystemStorage")
{
currentusage = (double)item.currentValue / 1024 / 1024 / 1024;
}
}
}
return currentusage;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2658 次 |
最近记录: |