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
25 changes: 21 additions & 4 deletions hawkular/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
from .metrics import HawkularMetricsClient, MetricType, Availability
from .alerts import HawkularAlertsClient, Trigger, FullTrigger, Condition, Dampening, FullTrigger, GroupMemberInfo
from .alerts import GroupConditionsInfo, TriggerType, TriggerMode, DampeningType, ConditionType, Operator, Severity
"""
Copyright 2015-2017 Red Hat, Inc. and/or its affiliates
and other contributors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
from hawkular.metrics import HawkularMetricsClient, MetricType, Availability
from hawkular.alerts import HawkularAlertsClient, Trigger, FullTrigger, Condition, Dampening, FullTrigger, GroupMemberInfo
from hawkular.alerts import GroupConditionsInfo, TriggerType, TriggerMode, DampeningType, ConditionType, Operator, Severity, Status

__all__ = ['HawkularMetricsClient',
'MetricType',
Expand All @@ -17,4 +33,5 @@
'DampeningType',
'ConditionType',
'Operator',
'Severity']
'Severity',
'Status']
36 changes: 36 additions & 0 deletions hawkular/alerts/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""
Copyright 2015-2017 Red Hat, Inc. and/or its affiliates
and other contributors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
from hawkular.client import ApiObject, HawkularBaseClient
from hawkular.alerts.common import *
from hawkular.alerts.triggers import *

__all__ = ['HawkularAlertsClient',
'Trigger',
'Condition',
'Dampening',
'FullTrigger',
'GroupMemberInfo',
'GroupConditionsInfo',
'TriggerType',
'TriggerMode',
'DampeningType',
'ConditionType',
'Operator',
'Severity',
'Status',
]
# 'AlertsTriggerClient']
65 changes: 65 additions & 0 deletions hawkular/alerts/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from hawkular.client import ApiObject, HawkularBaseClient
from hawkular.alerts.triggers import AlertsTriggerClient

class Status(ApiObject):
__slots__ = [
'status', 'implementation_version', 'built_from_git_sha1', 'distributed', 'members'
]

def isup(self):
return self.status == 'STARTED'

def isdistributed(self):
return self.distributed == 'true'

class HawkularAlertsClient(HawkularBaseClient):

def __init__(self, **opts):
"""
Available parameters:

tenant_id,
host='localhost',
port=8080,
path=None,
scheme='http',
cafile=None,
context=None,
token=None,
username=None,
password=None,
auto_set_legacy_api=True,
authtoken=None
"""
prop_defaults = {
"tenant_id": 'hawkular',
"host": 'localhost',
"port": 8080,
"scheme": 'http',
"path": None,
"cafile": None,
"context": None,
"token": None,
"username": None,
"password": None,
"authtoken": None,
}

for (prop, default) in prop_defaults.items():
setattr(self, prop, opts.get(prop, default))

super(HawkularAlertsClient, self)._setup_path()

self.triggers = AlertsTriggerClient(self)

# def triggers(self):
# """
# Returns triggers methods
# """
# return AlertsTriggerClient(self)

def status(self):
orig_dict = self._get(self._service_url('status'))
orig_dict['implementation_version'] = orig_dict.pop('Implementation-Version')
orig_dict['built_from_git_sha1'] = orig_dict.pop('Built-From-Git-SHA1')
return Status(orig_dict)
25 changes: 14 additions & 11 deletions hawkular/alerts.py → hawkular/alerts/triggers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
See the License for the specific language governing permissions and
limitations under the License.
"""
from hawkular.client import ApiObject, HawkularBaseClient
from hawkular.client import ApiObject

class Trigger(ApiObject):
__slots__ = [
Expand All @@ -34,7 +34,6 @@ class Condition(ApiObject):
'in_range', 'alerter_id', 'expression', 'direction', 'period', 'interval'
]


class Dampening(ApiObject):
__slots__ = [
'trigger_id', 'trigger_mode', 'type', 'eval_true_setting',
Expand All @@ -56,14 +55,12 @@ def __init__(self, dictionary=dict()):
self.dampenings = Dampening.list_to_object_list(udict.get('dampenings'))
self.conditions = Condition.list_to_object_list(udict.get('conditions'))


class GroupMemberInfo(ApiObject):
__slots__ = [
'group_id', 'member_id', 'member_name', 'member_description', 'member_context',
'member_tags', 'data_id_map'
]


class GroupConditionsInfo(ApiObject):
__slots__ = [
'conditions', 'data_id_member_map'
Expand All @@ -77,28 +74,24 @@ def __init__(self, dictionary=dict()):
def addCondition(self, c):
self.conditions.append(c)


class TriggerType:
STANDARD = 'STANDARD'
GROUP = 'GROUP'
DATA_DRIVEN_GROUP = 'DATA_DRIVEN_GROUP'
MEMBER = 'MEMBER'
ORPHAN = 'ORPHAN'


class TriggerMode:
FIRING = 'FIRING'
AUTORESOLVE = 'AUTORESOLVE'


class DampeningType:
STRICT = 'STRICT'
RELAXED_COUNT = 'RELAXED_COUNT'
RELAXED_TIME = 'RELAXED_TIME'
STRICT_TIME = 'STRICT_TIME'
STRICT_TIMEOUT = 'STRICT_TIMEOUT'


class ConditionType:
AVAILABILITY = 'AVAILABILITY'
COMPARE = 'COMPARE'
Expand All @@ -110,22 +103,27 @@ class ConditionType:
RATE = 'RATE'
MISSING = 'MISSING'


class Operator:
LT = 'LT'
GT = 'GT'
LTE = 'LTE'
GTE = 'GTE'


class Severity:
LOW = 'LOW'
MEDIUM = 'MEDIUM'
HIGH = 'HIGH'
CRITICAL = 'CRITICAL'

class AlertsTriggerClient(object):

def __init__(self, alerts_client):
self.__client = alerts_client
pass

def __getattr__(self, name):
return getattr(self.__client, name)

class HawkularAlertsClient(HawkularBaseClient):
def list_triggers(self, tags=[]):
tags = ','.join(tags)
url = self._service_url('triggers', {'tags': tags})
Expand Down Expand Up @@ -189,6 +187,7 @@ def create_group_member(self, member):
data = self._serialize_object(member)
return Trigger(self._post(self._service_url(['triggers', 'groups', 'members']), data))

# TODO The API defines two, PUT which updates conditions and PUT which updates trigger mode also
def put_trigger_conditions(self, trigger_id, trigger_mode, conditions):
data = self._serialize_object(conditions)
url = self._service_url(['triggers', trigger_id, 'conditions', trigger_mode])
Expand Down Expand Up @@ -243,3 +242,7 @@ def delete_group_dampening(self, group_id, dampening_id):
:param dampening_id: id of the Dampening to be deleted
"""
self._delete(self._service_url(['triggers', 'groups', group_id, 'dampenings', dampening_id]))

# def enable/disable_trigger (or should it be in the update?)
# def orphan / deorphan trigger
#
23 changes: 12 additions & 11 deletions hawkular/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def __init__(self,
auto_set_legacy_api=True,
authtoken=None):
"""
A new instance of HawkularMetricsClient is created with the following defaults:
A new instance of HawkularClient is created with the following defaults:

host = localhost
port = 8080
Expand All @@ -157,24 +157,25 @@ def __init__(self,
self.legacy_api = False
self.authtoken = authtoken

self._setup_path()

# Call the server status endpoint to get the version number,
# Use the return sematic version to set the value of legacy_api
if auto_set_legacy_api:
major, minor = self.query_semantic_version()
self.legacy_api = (major == 0 and minor < 16)

def _setup_path(self):
opener = build_opener(HawkularHTTPErrorProcessor())
install_opener(opener)

if path is None:
if self.path is None:
class_name = self.__class__.__name__
path_components = ''.join(["_" + c.lower() if c.isupper() else c for c in class_name]).split('_')
path_components.pop()
self.path = '/'.join(path_components);
else:
self.path = path
self.path = '/'.join(path_components)
self.path = self.path.strip('/')

# Call the server status endpoint to get the version number,
# Use the return sematic version to set the value of legacy_api
if auto_set_legacy_api:
major, minor = self.query_semantic_version()
self.legacy_api = (major == 0 and minor < 16)

def _get_base_url(self):
return "{0}://{1}:{2}/{3}/".format(self.scheme, self.host, str(self.port), self.path)

Expand Down
6 changes: 1 addition & 5 deletions hawkular/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,16 @@
"""
from __future__ import unicode_literals

import codecs
import time
import collections
import base64
import ssl
from datetime import datetime, timedelta

try:
import simplejson as json
except ImportError:
import json

from hawkular.client import ApiObject, HawkularBaseClient, HawkularError
from hawkular.client import HawkularConnectionError, HawkularStatusError
from hawkular.client import HawkularBaseClient, HawkularError

class MetricType:
Gauge = 'gauges'
Expand Down
Loading