I'm setting up some Azure Files, and want to monitor the files' capacity. Where do I need to set the alert of when the specific files' capacity near the quota?
Now I am using Log activity and Azure Metric to monitor azure resources and set some alerts.
I want to monitor different files capacity and set some alerts when the capacity of the files nearly quota.
Update 0806:
We can use application insights and azure monitor to do that.
1.Create an application insights from azure portal. After it completes creation, copy the instrumentation key.
2.In visual studio, create a .net framework console project(I'm using .net framework 4.5)
3.Install nuget package for azure storage and application insights:
Microsoft.ApplicationInsights, version 2.10.0
WindowsAzure.Storage, version 9.3.3
4.Then write the code in 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.Nav to azure portal -> azure monitor -> alerts -> New Alert Rule: for Resource, select the application insights you used.
for condition, select "Custom log search" -> then in the Search query, fill in the following query(Note: the name is the metric name you defined in step 4):
customMetrics
| where name == "file_length"
| top 1 by timestamp desc
| project value
then in Alert logic: Based on = Number of results, Operator = Greater than, Threshold value=0.
then for "Evaluated based on", select the proper Period and Frequency, For testing purpose, you can select Period as 10 minutes, Frequency as 5 minutes. Click "Done" button to complete condition.
For Actions, properly configure it like fill in your email address and other field.
6.Run the console project from visual studio, and if the total file size in the file share is greater than the value you specified, you will receive one or more(depends on the Period and Frenquecy set in step 5) alert email.
This feature is not supported currently, please keep an eye on this user feedback.