我正在设置一些 Azure 文件,并且想要监视文件的容量。我需要在哪里设置特定文件容量接近配额时的警报?
现在我正在使用日志事件和 Azure Metric 来监视 Azure 资源并设置一些警报。
我想监控不同的文件容量,并在文件容量接近配额时设置一些警报。
最佳答案
更新0806:
我们可以使用应用程序洞察和 azure 监视器来做到这一点。
1. Create an application insights来自 azure 门户。创建完成后,copy the instrumentation key .
2.在Visual Studio中,创建一个.net Framework控制台项目(我使用的是.net Framework 4.5)
3.安装nuget包以获取azure存储和应用程序洞察:
Microsoft.ApplicationInsights, version 2.10.0
WindowsAzure.Storage, version 9.3.3
4.然后在Program.cs中编写代码:
class Program
{
private static List<CloudFile> files = new List<CloudFile>();
static void Main(string[] args)
{
string account_name = "xx";
string account_key = "xx";
//use while to keep running the code
while (true)
{
CloudStorageAccount storageAccount = new CloudStorageAccount(new StorageCredentials(account_name, account_key), true);
CloudFileClient fileClient = storageAccount.CreateCloudFileClient();
CloudFileShare fileShare = fileClient.GetShareReference("t88");
IEnumerable<IListFileItem> fileList = fileShare.GetRootDirectoryReference().ListFilesAndDirectories();
//clear the list
files.Clear();
//add all the files in the fileshare to the list
foreach (IListFileItem listItem in fileList)
{
if (listItem.GetType() == typeof(CloudFile))
{
files.Add((CloudFile)listItem);
}
else if (listItem.GetType() == typeof(CloudFileDirectory))
{
list_subdir(listItem);
}
}
Console.WriteLine(files.Count);
//the variable to store the total files' length in the fileshare
long file_length = 0;
//specify the threshold value, if the total size is more than the value, then send data to application insights.
long threshold_value = 99999; //here, I use bytes as unit. You can change it MB / GB properly as per your need.
//calculate the size(bytes here) of the total files in the fileshare
foreach (var f in files)
{
file_length += f.Properties.Length;
Console.WriteLine($"file name: {f.Name}, file size: {f.Properties.Length}");
}
TelemetryClient telemetryClient = new TelemetryClient { InstrumentationKey = "xxxx" };
//telemetryClient.GetMetric("file_length").TrackValue(file_length);
//telemetryClient.TrackTrace("aaaaa");
//add if statement here, means if the value is greater than threshold value, send the value to app insights
if (file_length > threshold_value)
{
//the metric name here is "file_length", you can change it to other values, but be sure that use the correct metric name in the query in azure monitor in next step.
telemetryClient.TrackMetric("file_length", file_length);
}
//wait for xx seconds, then calculate the size again.
System.Threading.Thread.Sleep(1000*30);
}
}
public static List<CloudFile> list_subdir(IListFileItem list)
{
CloudFileDirectory fileDirectory = (CloudFileDirectory)list;
IEnumerable<IListFileItem> fileList = fileDirectory.ListFilesAndDirectories();
foreach (IListFileItem listItem in fileList)
{
if (listItem.GetType() == typeof(CloudFileDirectory))
{
list_subdir(listItem);
}
else
{
if (listItem.GetType() == typeof(CloudFile))
{
files.Add((CloudFile)listItem);
}
}
}
return files;
}
}
5.导航到 azure 门户 -> azure 监视器 -> 警报 -> 新警报规则: 对于资源,选择您使用的应用程序见解。
对于条件,选择“自定义日志搜索” -> 然后在搜索查询中填写以下查询(注意:名称是您在步骤 4 中定义的指标名称):
customMetrics
| where name == "file_length"
| top 1 by timestamp desc
| project value
然后在警报逻辑中:基于=结果数,运算符=大于,阈值=0。
然后对于“评估依据”,选择适当的周期和频率,出于测试目的,您可以选择周期为 10 分钟,频率为 5 分钟。单击“完成”按钮完成条件。
对于操作,请正确配置它,例如填写您的电子邮件地址和其他字段。
6.从 Visual Studio 运行控制台项目,如果文件共享中的总文件大小大于您指定的值,您将收到一个或多个(取决于步骤 5 中设置的周期和频率)警报电子邮件。
<小时/>暂不支持该功能,请关注此user feedback .
关于azure - 有没有办法监控Azure文件容量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57303398/