Skip to main content

mackerel.clienthde

https://travis-ci.org/HDE/py-mackerel-client.svg?branch=hde-dev https://coveralls.io/repos/github/HDE/py-mackerel-client/badge.svg?branch=hde-dev

mackerel.clienthde is a python library to access Mackerel (https://mackerel.io/).

This project is forked from heavenshell/py-mackerel-client, which is initially ported from mackerel-client-ruby.

Install

$ pip install mackerel.clienthde

Dependency

mackerel.clienthde use requests, simplejson and click.

Usage

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')
hosts = client.get_hosts()

Get host

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>')

Update host status

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')

Retire host

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>')

Get latest 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')
# 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'])

Post 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 = [
    {
        '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)

Post service 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)

Get monitors [NEW in this forked version]

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'])

Create monitor [NEW in this forked version]

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)

Update monitor [NEW in this forked version]

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()
)

Delete monitor [NEW in this forked version]

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')

CLI

Get host(s) information from hostname or service, role.

$ mkr.py info [--name foo] [--service service] [--role role]

Set status of a host.

$ mkr.py status --host-id foo --status working

Retire a host.

$ mkr.py retire --host-id foo

Get status of a host.

$ mkr.py status --host-id foo

Authentication

$ export MACKEREL_APIKEY=foobar

Release files for mackerel.clienthde 0.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mackerel.clienthde 0.0.4
File Size Uploaded
mackerel.clienthde-0.0.4.tar.gz 9.2 kB Details

Release files / mackerel.clienthde-0.0.4.tar.gz

Download URL mackerel.clienthde-0.0.4.tar.gz
Size 9.2 kB
Tags Source
SHA-256 checksum
How to use checksums
463b0c25a2c85ff4d231156e14f9588fde77a0f939ce3bb0bd21554708989ff8
BLAKE2b-256 checksum
How to use checksums
0775a5a9280c65113b9208171dd6408eb8f4f344fc5228d84995b4b60d963dd3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

0.0.6

1 release file

0.0.5

1 release file

This release

0.0.4 This release

1 release file

0.0.3

1 release file

0.0.2

1 release file

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page