有没有办法在Azure App Service的低磁盘空间上设置警报? 在“警报”选项卡下的可用选项中找不到与磁盘空间相关的任何内容。
据我所知,更改选项卡不包含Web应用程序的配额选择。因此,我建议您可以编写自己的逻辑在Azure App Service的低磁盘空间上设置警报。
你可以使用天蓝色的web应用程序的webjobs来运行后台任务来检查你的web应用程序的使用情况。
我建议您可以使用网络作品timertrigger(您需要安装来自nuget的webjobs扩展)来运行预定作业。然后,您可以向Azure管理API发送休息请求以获取您的Web应用程序的当前使用情况。您可以根据您的网络应用当前使用情况发送电子邮件或其他内容。
更多细节,你可以参考下面的代码示例:
注意:如果你想使用REST API来获取当前Web应用程序的使用情况,您需要首先创建一个Azure的Active Directory应用程序和服务主体。在生成服务主体后,您可以获取applicationid,访问密钥和传说。更多细节,你可以参考这个article。
代码:
// 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;
}
结果:
任何更新?如果您觉得我的回答很有用/有帮助,请将其标记为答案,以便其他人可以从中受益。 –
@ BrandoZhang-MSFT即使我不把它标记为答案,每个人都可以从您的帖子中受益。这很有帮助,但解决方案并不能真正满足我目前的需求,因为实施/维护的复杂性超过了获得的收益。 – Ondska