有没有办法为 Azure 应用服务的磁盘空间不足设置警报?我在“警报”选项卡下的可用选项中找不到与磁盘空间相关的任何内容。
据我所知,alter 选项卡不包含网络应用程序的配额选择。因此,我建议您可以编写自己的逻辑来为 Azure 应用服务的磁盘空间不足设置警报。
您可以使用 azure web 应用程序的 webjobs 运行后台任务来检查您的 web 应用程序的使用情况。
我建议您可以使用 webjob timertrigger(您需要从 nuget 安装 webjobs 扩展)来运行计划的作业。然后,您可以向 azure management api 发送一个休息请求,以获取您的 Web 应用程序的当前使用情况。您可以根据您的网络应用当前使用情况发送电子邮件或其他内容。
更多细节,你可以参考下面的代码示例:
注意:如果要使用 rest api 获取当前 web 应用程序的使用情况,首先需要创建一个 Azure Active Directory 应用程序和服务主体。生成服务主体后,您可以获得应用程序ID、访问密钥和人才ID。更多细节,你可以参考这篇文章。
代码:
// 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;
}
结果: