你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
用于 Python 的 Azure 监视库
概述
监视可以为用户提供数据,确保应用程序始终处于健康运行状态。 监视还有助于避免潜在问题,或者解决过去的问题。 此外,还可以利用监视数据深入了解应用程序的情况。 了解这些情况有助于改进应用程序的性能或可维护性,或者实现本来需要手动干预的操作的自动化。
在此处详细了解 Azure Monitor。
安装
pip install azure-mgmt-monitor
示例 - 指标
此示例获取 Azure 上的资源(VM 等)的指标。 此示例至少需要 Python 程序包的 0.4.0 版。
此处提供了筛选器可用关键字的完整列表。
此处提供了每个资源类型支持的指标。
import datetime
from azure.mgmt.monitor import MonitorManagementClient
# Get the ARM id of your resource. You might chose to do a "get"
# using the according management or to build the URL directly
# Example for a ARM VM
resource_id = (
"subscriptions/{}/"
"resourceGroups/{}/"
"providers/Microsoft.Compute/virtualMachines/{}"
).format(subscription_id, resource_group_name, vm_name)
# create client
client = MonitorManagementClient(
credentials,
subscription_id
)
# You can get the available metrics of this specific resource
for metric in client.metric_definitions.list(resource_id):
# azure.monitor.models.MetricDefinition
print("{}: id={}, unit={}".format(
metric.name.localized_value,
metric.name.value,
metric.unit
))
# Example of result for a VM:
# Percentage CPU: id=Percentage CPU, unit=Unit.percent
# Network In: id=Network In, unit=Unit.bytes
# Network Out: id=Network Out, unit=Unit.bytes
# Disk Read Bytes: id=Disk Read Bytes, unit=Unit.bytes
# Disk Write Bytes: id=Disk Write Bytes, unit=Unit.bytes
# Disk Read Operations/Sec: id=Disk Read Operations/Sec, unit=Unit.count_per_second
# Disk Write Operations/Sec: id=Disk Write Operations/Sec, unit=Unit.count_per_second
# Get CPU total of yesterday for this VM, by hour
today = datetime.datetime.now().date()
yesterday = today - datetime.timedelta(days=1)
metrics_data = client.metrics.list(
resource_id,
timespan="{}/{}".format(yesterday, today),
interval='PT1H',
metricnames='Percentage CPU',
aggregation='Total'
)
for item in metrics_data.value:
# azure.mgmt.monitor.models.Metric
print("{} ({})".format(item.name.localized_value, item.unit.name))
for timeserie in item.timeseries:
for data in timeserie.data:
# azure.mgmt.monitor.models.MetricData
print("{}: {}".format(data.time_stamp, data.total))
# Example of result:
# Percentage CPU (percent)
# 2016-11-16 00:00:00+00:00: 72.0
# 2016-11-16 01:00:00+00:00: 90.59
# 2016-11-16 02:00:00+00:00: 60.58
# 2016-11-16 03:00:00+00:00: 65.78
# 2016-11-16 04:00:00+00:00: 43.96
# 2016-11-16 05:00:00+00:00: 43.96
# 2016-11-16 06:00:00+00:00: 114.9
# 2016-11-16 07:00:00+00:00: 45.4
示例 - 警报
此示例演示如何在创建资源时自动对资源设置警报,确保正确监视所有资源。
在 VM 上创建一个数据源用于针对 CPU 使用率发出警报:
from azure.mgmt.monitor import MonitorMgmtClient
from azure.mgmt.monitor.models import RuleMetricDataSource
resource_id = (
"subscriptions/{}/"
"resourceGroups/MonitorTestsDoNotDelete/"
"providers/Microsoft.Compute/virtualMachines/MonitorTest"
).format(self.settings.SUBSCRIPTION_ID)
# create client
client = MonitorMgmtClient(
credentials,
subscription_id
)
# I need a subclass of "RuleDataSource"
data_source = RuleMetricDataSource(
resource_uri = resource_id,
metric_name = 'Percentage CPU'
)
创建当 VM 的平均 CPU 使用率在过去 5 分钟高于 90% 时触发的阈值条件(使用前面的数据源):
from azure.mgmt.monitor.models import ThresholdRuleCondition
# I need a subclasses of "RuleCondition"
rule_condition = ThresholdRuleCondition(
data_source = data_source,
operator = 'GreaterThanOrEqual',
threshold = 90,
window_size = 'PT5M',
time_aggregation = 'Average'
)
创建电子邮件操作:
from azure.mgmt.monitor.models import RuleEmailAction
# I need a subclass of "RuleAction"
rule_action = RuleEmailAction(
send_to_service_owners = True,
custom_emails = [
'monitoringemail@microsoft.com'
]
)
创建警报:
rule_name = 'MyPyTestAlertRule'
my_alert = client.alert_rules.create_or_update(
group_name,
rule_name,
{
'location': 'westus',
'alert_rule_resource_name': rule_name,
'description': 'Testing Alert rule creation',
'is_enabled': True,
'condition': rule_condition,
'actions': [
rule_action
]
}
)
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈