如果你的最终目标是
- 设置警报并在达到阈值时收到通知 - 然后您可以通过创建日志警报规则来完成。如需了解更多信息,请参阅this 文档。
- 使用 API 查询 Azure VM 的可用磁盘/内存空间指标 - 然后遵循this API 参考或this 和this 文档。
- 使用 PowerShell 查询 Azure VM 的可用磁盘/内存空间指标 - 然后遵循 this cmdlet。
对于上述所有工作方式,您必须首先
- 创建 Log Analytics 工作区并在 Azure VM 中安装 Log Analytics 代理(或者换句话说,启用 Log Analytics VM 扩展)。要使用 Azure 门户完成此操作,请遵循 this 文档。要使用 Azure PowerShell / CLI 完成相同操作,请根据您的 Azure VM 的操作系统遵循 this 或 this 文档。
- 一旦您的 Azure Log Analytics 工作区开始收集日志数据,然后转到 Azure 门户 -> Log Analytics 工作区 -> 您的 Log Analytics 工作区 -> 日志,然后键入您的 kusto 查询以查找 VM 的可用磁盘/内存空间详细信息。查询将如下所示。
如果您的 Azure VM 是 Windows 操作系统,则查询以查找磁盘总可用空间为:
Perf
| where ( ObjectName == "LogicalDisk" )
| where ( CounterName == "% Free Space" )
| where ( InstanceName == "_Total" )
| summarize AggregatedValue= avg(CounterValue) by Computer, bin(TimeGenerated, 30s)
如果您的 Azure VM 是 Windows 操作系统,则查找磁盘 C 驱动器可用空间的查询是:
Perf
| where ( ObjectName == "LogicalDisk" )
| where ( CounterName == "% Free Space" )
| where ( InstanceName == "C:" )
| summarize AggregatedValue= avg(CounterValue) by Computer, bin(TimeGenerated, 30s)
如果您的 Azure VM 是 Linux 操作系统,则查询以查找磁盘总可用空间为:
Perf
| where ( ObjectName == "Logical Disk" )
| where ( CounterName == "% Free Space" )
| where ( InstanceName == "_Total" )
| summarize AggregatedValue= avg(CounterValue) by Computer, bin(TimeGenerated, 30s)
如果您的 Azure VM 是 Linux 操作系统,则查询以查找安装在 Root 可用空间上的磁盘是:
Perf
| where ( ObjectName == "Logical Disk" )
| where ( CounterName == "% Free Space" )
| where ( InstanceName == "/" )
| summarize AggregatedValue= avg(CounterValue) by Computer, bin(TimeGenerated, 30s)
如果您的 Azure VM 是 Linux 操作系统,则查询可用 MBytes 内存为:
Perf
| where ( ObjectName == "Memory" )
| where ( CounterName == "Available MBytes Memory" )
| summarize AggregatedValue= avg(CounterValue) by Computer, bin(TimeGenerated, 30s)
如果您的 Azure VM 是 Windows 操作系统,那么对可用 MBytes 的查询是:
Perf
| where ( ObjectName == "Memory" )
| where ( CounterName == "Available MBytes" )
| summarize AggregatedValue= avg(CounterValue) by Computer, bin(TimeGenerated, 30s)
如果您的 Azure VM 是 Windows 操作系统,则查询使用中的已提交字节是:
Perf
| where ( ObjectName == "Memory" )
| where ( CounterName == "% Committed Bytes In Use" )
| summarize AggregatedValue= avg(CounterValue) by Computer, bin(TimeGenerated, 30s)
注意 1:要使上述所有查询正常工作,请确保在 Azure 门户 -> Log Analytics 工作区 -> 您的 Log Analytics 工作区 -> 高级设置 -> 数据 -> Windows 性能计数器/Linux 中添加相应的性能计数器性能计数器。
注意2:使用其他性能计数器,您还可以获取更多数据,例如磁盘读取时间、磁盘写入时间、空闲时间、当前磁盘队列长度、缓存字节、提交字节、页面错误、页面读取、页面写入、空闲索引节点等。
希望这会有所帮助!干杯!