由Python实现的Mackerel客户端。
项目描述
mackerel.clienthde 是一个用于访问Mackerel(https://mackerel.io/)的Python库。
该项目是从heavenshell/py-mackerel-client分叉而来,该库最初是从mackerel-client-ruby移植的。
安装
$ pip install mackerel.clienthde
依赖项
mackerel.clienthde 使用 requests,simplejson 和 click。
用法
获取主机
from mackerel.clienthde import Client
# Alternatively, you can set MACKEREL_APIKEY as environment variable
# And simply call "client = Client()"
client = Client(mackerel_api_key='<Put your API key')
hosts = client.get_hosts()
获取主机
from mackerel.clienthde import Client
# Alternatively, you can set MACKEREL_APIKEY as environment variable
# And simply call "client = Client()"
client = Client(mackerel_api_key='<Put your API key')
host = client.get_host('<hostId>')
更新主机状态
from mackerel.clienthde import Client
# Alternatively, you can set MACKEREL_APIKEY as environment variable
# And simply call "client = Client()"
client = Client(mackerel_api_key='<Put your API key')
# Poweroff.
self.client.update_host_status('<hostId>', 'poweroff')
# Standby.
self.client.update_host_status('<hostId>', 'standby')
# Working.
self.client.update_host_status('<hostId>', 'working')
# Maintenance.
self.client.update_host_status('<hostId>', 'maintenance')
退役主机
from mackerel.clienthde import Client
# Alternatively, you can set MACKEREL_APIKEY as environment variable
# And simply call "client = Client()"
client = Client(mackerel_api_key='<Put your API key')
self.client.retire_host('<hostId>')
获取最新指标
from mackerel.clienthde import Client
# Alternatively, you can set MACKEREL_APIKEY as environment variable
# And simply call "client = Client()"
client = Client(mackerel_api_key='<Put your API key')
# Get hostId A's and hostId B's loadavg5, memory.free value.
metrics = self.client.get_latest_metrics(['<hostId A>', '<hostId B>'],
['loadavg5', 'memory.free'])
发布指标
from mackerel.clienthde import Client
# Alternatively, you can set MACKEREL_APIKEY as environment variable
# And simply call "client = Client()"
client = Client(mackerel_api_key='<Put your API key>')
metrics = [
{
'hostId': '<hostId>', 'name': 'custom.metrics.loadavg',
'time': 1401537844, 'value': 1.4
},
{
'hostId': '<hostId>', 'name': 'custom.metrics.uptime',
'time': 1401537844, 'value': 500
}
]
# Post `custom.metrics.loadavg` and `custom.metrics.uptime` to `hostId`.
client.post_metrics(metrics)
发布服务指标
from mackerel.clienthde import Client
# Alternatively, you can set MACKEREL_APIKEY as environment variable
# And simply call "client = Client()"
client = Client(mackerel_api_key='<Put your API key>')
metrics = [
{
'name': 'custom.metrics.latency',
'time': 1401537844, 'value': 0.5
},
{
'name': 'custom.metrics.uptime',
'time': 1401537844, 'value': 500
}
]
# Post 'custom.metrics.latency' and 'custom.metrics.uptime' to `service_name`.
self.client.post_service_metrics('service_name', metrics)
获取监控器 [在本分支版本中新增]
from mackerel.clienthde import Client
# Alternatively, you can set MACKEREL_APIKEY as environment variable
# And simply call "client = Client()"
client = Client(mackerel_api_key='<Put your API key')
monitors = client.get_monitors() # list all Monitors
# Specify list of ids to search, return a dict with id as key
monitor_targets = client.get_monitors(ids=['1ABCDabcde1'])
创建监控器 [此分支版本新增]
from mackerel.clienthde import Client
# Alternatively, you can set MACKEREL_APIKEY as environment variable
# And simply call "client = Client()"
client = Client(mackerel_api_key='<Put your API key')
# Create monitor as specified in https://mackerel.io/api-docs/entry/monitors#create
params = {
'type': 'service',
'name': 'ConsumedReadCapacityUnits.table-name',
'service': 'HDE',
'duration': 1,
'metric': 'ConsumedReadCapacityUnits.table-name',
'operator': '>',
'warning': 700,
'critical': 900
}
# Post params to Mackerel
# result['id'] will give Monitor id if create operation succeeded
result = client.create_monitor(params)
更新监控器 [此分支版本新增]
from mackerel.clienthde import Client
# Alternatively, you can set MACKEREL_APIKEY as environment variable
# And simply call "client = Client()"
client = Client(mackerel_api_key='<Put your API key')
# Since update Monitor requires all fields to be specified,
# it is suggested to retrieve the latest value first
monitor_id = '1ABCDabcde1'
monitors = client.get_monitors(ids=[monitor_id])
monitor = monitors[monitor_id]
# In this example, we assume Monitor is class of MonitorService
monitor.warning = 800
monitor.critical = 1000
# Update params to Mackerel
result = client.update_monitor(
monitor_id=monitor_id,
monitor_params=monitor._to_post_params_dict()
)
删除监控器 [此分支版本新增]
from mackerel.clienthde import Client
# Alternatively, you can set MACKEREL_APIKEY as environment variable
# And simply call "client = Client()"
client = Client(mackerel_api_key='<Put your API key')
# Delete Monitor
result = client.delete_monitor(monitor_id='1ABCDabcde1')
命令行界面
从主机名或服务,角色获取主机信息。
$ mkr.py info [--name foo] [--service service] [--role role]
设置主机状态。
$ mkr.py status --host-id foo --status working
退役主机。
$ mkr.py retire --host-id foo
获取主机状态。
$ mkr.py status --host-id foo
身份验证
$ export MACKEREL_APIKEY=foobar
项目详情
关闭
mackerel.clienthde-0.0.6.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | fb28076a207853d0dd0e4eef5cf91cd9fcb46a6ce8496db866243994f2d045f0 |
|
MD5 | 0de8ce91326973d1c28436310b6f151d |
|
BLAKE2b-256 | 89bf3f494550cced51a10065735f5eb6e0c0018d28a897a1cce12be4238be650 |