Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ repos:
- repo: https://github.com/pre-commit/pre-commit-hooks.git
rev: v1.4.0
hooks:
- id: autopep8-wrapper
- id: check-added-large-files
- id: check-ast
- id: check-byte-order-marker
Expand All @@ -23,16 +22,20 @@ repos:
args: ['--django']
- id: requirements-txt-fixer
- id: trailing-whitespace
- repo: https://github.com/pre-commit/mirrors-autopep8
rev: v1.5
hooks:
- id: autopep8
- repo: https://github.com/asottile/pyupgrade
rev: v1.6.1
rev: v1.26.2
hooks:
- id: pyupgrade
args: ['--py3-plus']
- repo: https://github.com/asottile/seed-isort-config
rev: v1.2.0
rev: v1.9.4
hooks:
- id: seed-isort-config
- repo: https://github.com/pre-commit/mirrors-isort
rev: v4.3.4
rev: v4.3.21
hooks:
- id: isort
4 changes: 2 additions & 2 deletions alertaclient/api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import json
import logging
import os
Expand Down Expand Up @@ -200,10 +199,11 @@ def delete_customer(self, id):
return self.http.delete('/customer/%s' % id)

# Heartbeats
def heartbeat(self, origin, tags=None, timeout=None, customer=None):
def heartbeat(self, origin, tags=None, attributes=None, timeout=None, customer=None):
data = {
'origin': origin,
'tags': tags or list(),
'attributes': attributes or dict(),
'timeout': timeout,
'createTime': datetime.utcnow(),
'customer': customer
Expand Down
1 change: 0 additions & 1 deletion alertaclient/auth/azure.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import webbrowser
from uuid import uuid4

Expand Down
1 change: 0 additions & 1 deletion alertaclient/auth/github.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import webbrowser
from uuid import uuid4

Expand Down
1 change: 0 additions & 1 deletion alertaclient/auth/gitlab.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import webbrowser
from uuid import uuid4

Expand Down
1 change: 0 additions & 1 deletion alertaclient/auth/google.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import webbrowser
from uuid import uuid4

Expand Down
1 change: 0 additions & 1 deletion alertaclient/auth/oidc.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import webbrowser
from uuid import uuid4

Expand Down
1 change: 0 additions & 1 deletion alertaclient/auth/token.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import base64
import json
from http.server import BaseHTTPRequestHandler, HTTPServer
Expand Down
1 change: 0 additions & 1 deletion alertaclient/auth/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import os
from netrc import netrc
from urllib.parse import urlparse
Expand Down
1 change: 0 additions & 1 deletion alertaclient/commands/cmd_blackouts.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import json

import click
Expand Down
1 change: 0 additions & 1 deletion alertaclient/commands/cmd_config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import click


Expand Down
1 change: 0 additions & 1 deletion alertaclient/commands/cmd_customers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import json

import click
Expand Down
1 change: 0 additions & 1 deletion alertaclient/commands/cmd_groups.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import json

import click
Expand Down
34 changes: 27 additions & 7 deletions alertaclient/commands/cmd_heartbeat.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,47 @@
import os
import platform
import sys

import click

prog = os.path.basename(sys.argv[0])
from alertaclient.utils import origin


@click.command('heartbeat', short_help='Send a heartbeat')
@click.option('--origin', '-O', metavar='ORIGIN', default='{}/{}'.format(prog, platform.uname()[1]), help='Origin of heartbeat.')
@click.option('--origin', '-O', metavar='ORIGIN', default=origin, help='Origin of heartbeat.')
@click.option('--environment', '-E', metavar='ENVIRONMENT', help='Environment eg. Production, Development')
@click.option('--severity', '-s', metavar='SEVERITY', help='Severity eg. critical, major, minor, warning')
@click.option('--service', '-S', metavar='SERVICE', multiple=True, help='List of affected services eg. app name, Web, Network, Storage, Database, Security')
@click.option('--group', '-g', metavar='GROUP', help='Group event by type eg. OS, Performance')
@click.option('--tag', '-T', 'tags', multiple=True, metavar='TAG', help='List of tags eg. London, os:linux, AWS/EC2')
@click.option('--timeout', metavar='SECONDS', type=int, help='Seconds before heartbeat is stale')
@click.option('--customer', metavar='STRING', help='Customer')
@click.option('--delete', '-D', metavar='ID', help='Delete hearbeat using ID')
@click.pass_obj
def cli(obj, origin, tags, timeout, customer, delete):
"""Send or delete a heartbeat."""
def cli(obj, origin, environment, severity, service, group, tags, timeout, customer, delete):
"""
Send or delete a heartbeat.

Note: The "environment", "severity", "service" and "group" values are only
used when heartbeat alerts are generated from slow or stale heartbeats.
"""
client = obj['client']
if delete:
client.delete_heartbeat(delete)
else:
if any(t.startswith('environment') or t.startswith('group') for t in tags):
click.secho('ERROR: Do not use tags for "environment" or "group". See help.', bold=True)

attributes = dict()
if environment:
attributes['environment'] = environment
if severity:
attributes['severity'] = severity
if service:
attributes['service'] = service
if group:
attributes['group'] = group

try:
heartbeat = client.heartbeat(origin=origin, tags=tags, timeout=timeout, customer=customer)
heartbeat = client.heartbeat(origin=origin, tags=tags, attributes=attributes, timeout=timeout, customer=customer)
except Exception as e:
click.echo('ERROR: {}'.format(e))
sys.exit(1)
Expand Down
47 changes: 24 additions & 23 deletions alertaclient/commands/cmd_heartbeats.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

import json

import click
from tabulate import tabulate

from alertaclient.models.heartbeat import Heartbeat
from alertaclient.utils import origin


@click.command('heartbeats', short_help='List heartbeats')
Expand All @@ -29,7 +29,7 @@ def cli(obj, alert, severity, timeout, purge):
else:
timezone = obj['timezone']
headers = {
'id': 'ID', 'origin': 'ORIGIN', 'customer': 'CUSTOMER', 'tags': 'TAGS',
'id': 'ID', 'origin': 'ORIGIN', 'customer': 'CUSTOMER', 'tags': 'TAGS', 'attributes': 'ATTRIBUTES',
'createTime': 'CREATED', 'receiveTime': 'RECEIVED', 'since': 'SINCE', 'timeout': 'TIMEOUT',
'latency': 'LATENCY', 'maxLatency': 'MAX LATENCY', 'status': 'STATUS'
}
Expand All @@ -45,24 +45,20 @@ def cli(obj, alert, severity, timeout, purge):
if alert:
with click.progressbar(heartbeats, label='Alerting {} heartbeats'.format(len(heartbeats))) as bar:
for b in bar:
params = dict(filter(lambda a: len(a) == 2, map(lambda a: a.split(':'), b.tags)))
environment = params.get('environment', 'Production')
group = params.get('group', 'System')
tags = list(filter(lambda a: not a.startswith('environment:')
and not a.startswith('group:'), b.tags))

if b.status == 'expired': # aka. "stale"
client.send_alert(
resource=b.origin,
event='HeartbeatFail',
environment=b.attributes.get('environment', 'Production'),
severity=b.attributes.get('severity', severity),
correlate=['HeartbeatFail', 'HeartbeatSlow', 'HeartbeatOK'],
group=group,
environment=environment,
service=['Alerta'],
severity=severity,
service=b.attributes.get('service', ['Alerta']),
group=b.attributes.get('group', 'System'),
value='{}'.format(b.since),
text='Heartbeat not received in {} seconds'.format(b.timeout),
tags=tags,
tags=b.tags,
attributes=b.attributes,
origin=origin,
type='heartbeatAlert',
timeout=timeout,
customer=b.customer
Expand All @@ -71,14 +67,16 @@ def cli(obj, alert, severity, timeout, purge):
client.send_alert(
resource=b.origin,
event='HeartbeatSlow',
environment=b.attributes.get('environment', 'Production'),
severity=b.attributes.get('severity', severity),
correlate=['HeartbeatFail', 'HeartbeatSlow', 'HeartbeatOK'],
group=group,
environment=environment,
service=['Alerta'],
severity=severity,
service=b.attributes.get('service', ['Alerta']),
group=b.attributes.get('group', 'System'),
value='{}ms'.format(b.latency),
text='Heartbeat took more than {}ms to be processed'.format(b.max_latency),
tags=tags,
tags=b.tags,
attributes=b.attributes,
origin=origin,
type='heartbeatAlert',
timeout=timeout,
customer=b.customer
Expand All @@ -87,14 +85,17 @@ def cli(obj, alert, severity, timeout, purge):
client.send_alert(
resource=b.origin,
event='HeartbeatOK',
environment=b.attributes.get('environment', 'Production'),
severity=b.attributes.get('severity', default_normal_severity),
correlate=['HeartbeatFail', 'HeartbeatSlow', 'HeartbeatOK'],
group=group,
environment=environment,
service=['Alerta'],
severity=default_normal_severity,
service=b.attributes.get('service', ['Alerta']),
group=b.attributes.get('group', 'System'),
value='',
text='Heartbeat OK',
tags=tags,
tags=b.tags,
attributes=b.attributes,
origin=origin,
type='heartbeatAlert',
timeout=timeout,
customer=b.customer
)
1 change: 0 additions & 1 deletion alertaclient/commands/cmd_help.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import click


Expand Down
1 change: 0 additions & 1 deletion alertaclient/commands/cmd_history.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import json

import click
Expand Down
1 change: 0 additions & 1 deletion alertaclient/commands/cmd_keys.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import json

import click
Expand Down
1 change: 0 additions & 1 deletion alertaclient/commands/cmd_login.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import sys

import click
Expand Down
1 change: 0 additions & 1 deletion alertaclient/commands/cmd_logout.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import click

from alertaclient.auth.utils import clear_token
Expand Down
1 change: 0 additions & 1 deletion alertaclient/commands/cmd_perms.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import json

import click
Expand Down
3 changes: 1 addition & 2 deletions alertaclient/commands/cmd_query.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import json

import click
Expand Down Expand Up @@ -97,7 +96,7 @@ def cli(obj, ids, query, filters, display, from_date=None):
DateTime.localtime(alert.receive_time, timezone)), fg=color['fg'])
click.secho(' last received | {}'.format(
DateTime.localtime(alert.last_receive_time, timezone)), fg=color['fg'])
click.secho(' latency | {}ms'.format((latency.microseconds / 1000)), fg=color['fg'])
click.secho(' latency | {}ms'.format(latency.microseconds / 1000), fg=color['fg'])
click.secho(' timeout | {}s'.format(alert.timeout), fg=color['fg'])

click.secho(' alert id | {}'.format(alert.id), fg=color['fg'])
Expand Down
1 change: 0 additions & 1 deletion alertaclient/commands/cmd_raw.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import click
from tabulate import tabulate

Expand Down
2 changes: 1 addition & 1 deletion alertaclient/commands/cmd_send.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import json
import os
import sys
Expand Down Expand Up @@ -29,6 +28,7 @@ def cli(obj, resource, event, environment, severity, correlate, service, group,
client = obj['client']

def send_alert(resource, event, **kwargs):
click.echo('send')
try:
id, alert, message = client.send_alert(
resource=resource,
Expand Down
1 change: 0 additions & 1 deletion alertaclient/commands/cmd_status.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import click
from tabulate import tabulate

Expand Down
1 change: 0 additions & 1 deletion alertaclient/commands/cmd_token.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import click

from alertaclient.auth.utils import get_token
Expand Down
1 change: 0 additions & 1 deletion alertaclient/commands/cmd_top.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import click

from alertaclient.top import Screen
Expand Down
1 change: 0 additions & 1 deletion alertaclient/commands/cmd_uptime.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

from datetime import datetime, timedelta

import click
Expand Down
1 change: 0 additions & 1 deletion alertaclient/commands/cmd_users.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import json

import click
Expand Down
1 change: 0 additions & 1 deletion alertaclient/commands/cmd_version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import click
from requests import __version__ as requests_version

Expand Down
1 change: 0 additions & 1 deletion alertaclient/commands/cmd_watch.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import sys
import time

Expand Down
1 change: 0 additions & 1 deletion alertaclient/commands/cmd_whoami.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import click


Expand Down
1 change: 0 additions & 1 deletion alertaclient/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

try:
from click import ClickException as ClientException # type: ignore
except Exception:
Expand Down
1 change: 0 additions & 1 deletion alertaclient/models/alert.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

from datetime import datetime

from alertaclient.utils import DateTime
Expand Down
1 change: 0 additions & 1 deletion alertaclient/models/blackout.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

from datetime import datetime, timedelta

from alertaclient.utils import DateTime
Expand Down
1 change: 0 additions & 1 deletion alertaclient/models/customer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

class Customer:

def __init__(self, match, customer, **kwargs):
Expand Down
1 change: 0 additions & 1 deletion alertaclient/models/group.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

from typing import Any, Dict
from uuid import uuid4

Expand Down
Loading