有什么方法可以在磁盘空间不足的情况下为Azure App Service设置警报 [英] Is there any way to set up an alert on low disk space for Azure App Service

查看:78
本文介绍了有什么方法可以在磁盘空间不足的情况下为Azure App Service设置警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在按标准应用程序服务计划运行Azure应用程序服务,该计划允许使用最多50 GB的文件存储.该应用程序使用大量磁盘空间进行图像缓存.当前的消耗量大约为15 GB,但是如果缓存清理策略由于某种原因失败,它将很快增长到最高.

根据此Microsoft文章,垂直自动缩放(放大)并不是一种常见的做法,因为它通常需要一些服务停机时间:

I'm running an Azure App Service on a Standard App Service Plan which allows usage of max 50 GB of file storage. The application uses quite a lot of disk space for image cache. Currently the consumption level lies at around 15 GB but if cache cleaning policy fails for some reason it will grow up to the top very fast.

Vertical autoscaling (scaling up) is not a common practice as it often requires some service downtime according to this Microsoft article:

https://docs.microsoft.com/en-us/azure/architecture/best-practices/auto-scaling

So the question is:

Is there any way to set up an alert on low disk space for Azure App Service?

I can't find anything related to disk space in the options available under Alerts tab.

解决方案

Is there any way to set up an alert on low disk space for Azure App Service? I can't find anything related to disk space in the options available under Alerts tab.

As far as I know, the alter tab doesn't contain the web app's quota selection. So I suggest you could write your own logic to set up an alert on low disk space for Azure App Service.

You could use azure web app's webjobs to run a background task to check your web app's usage.

I suggest you could use webjob timertrigger(you need install webjobs extension from nuget) to run a scheduled job. Then you could send a rest request to azure management api to get your web app current usage. You could send e-mail or something else according your web app current usage.

More details, you could refer to below code sample:

Notice: If you want to use rest api to get the current web app's usage, you need firstly create an Azure Active Directory application and service principal. After you generate the service principal, you could get the applicationid,access key and talentid. More details, you could refer to this article.

Code:

 // 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;
    }

Result:

这篇关于有什么方法可以在磁盘空间不足的情况下为Azure App Service设置警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆