首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有没有办法在Azure App服务的低磁盘空间上设置警报?

有没有办法在Azure App服务的低磁盘空间上设置警报?
EN

Stack Overflow用户
提问于 2017-06-01 09:32:52
回答 1查看 2.3K关注 0票数 5

我正在运行一个Azure应用程序服务的标准应用服务计划,它允许使用最多50 GB的文件存储。应用程序使用相当多的磁盘空间进行图像缓存。目前的消费水平在15 GB左右,但如果缓存清理策略由于某种原因而失败,它将迅速成长到顶端。

垂直自动标度(扩展)并不是一种常见的做法,因为根据Microsoft的这篇文章,它通常需要一些服务停机时间:

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

所以问题是:

有没有办法在Azure应用程序服务的低磁盘空间上设置警报?

在Alerts选项卡下的选项中,我找不到任何与磁盘空间有关的内容。

EN

回答 1

Stack Overflow用户

发布于 2017-06-02 05:01:41

有没有办法在Azure应用程序服务的低磁盘空间上设置警报?在Alerts选项卡下的选项中,我找不到任何与磁盘空间有关的内容。

据我所知,alter选项卡不包含web应用程序的配额选择。因此,我建议您可以编写自己的逻辑,以便在Azure的低磁盘空间上设置警报。

您可以使用蓝色web应用程序的网页作业运行后台任务,以检查您的web应用程序的使用情况。

我建议您可以使用webjobs 定时器(需要安装nuget的webjobs扩展)来运行预定作业。然后,您可以发送一个rest请求到蔚蓝管理api,以获得您的web应用程序当前的使用。您可以发送电子邮件或其他东西,根据您的web应用程序当前的使用。

更多详细信息,请参阅下面的代码示例:

注意:如果您想使用rest来获得当前web应用程序的使用,那么首先需要创建一个Azure Active应用程序和服务主体。生成服务主体后,可以获得应用程序and、访问密钥和talentid。更多细节,您可以参考这个文章

代码:

代码语言:javascript
复制
 // 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;
    }

结果:

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44303809

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档