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

老子叫甜甜 提交于 2021-02-07 20:21:01

问题


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.


回答1:


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:



来源:https://stackoverflow.com/questions/44303809/is-there-any-way-to-set-up-an-alert-on-low-disk-space-for-azure-app-service

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