Skip to content

Commit

Permalink
Add acknowledge and downtime functions for backend
Browse files Browse the repository at this point in the history
Ref #279
  • Loading branch information
algorys committed Apr 11, 2018
1 parent 82be6c3 commit a8d0442
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 72 deletions.
132 changes: 131 additions & 1 deletion alignak_app/backend/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from alignak_backend_client.client import Backend, BackendException

from alignak_app.backend.datamanager import data_manager
from alignak_app.backend.alignak_ws_client import WSClient
from alignak_app.backend.ws_client import WSClient

from alignak_app.items.daemon import Daemon
from alignak_app.items.event import Event
Expand Down Expand Up @@ -238,6 +238,136 @@ def patch(self, endpoint, data, headers):

return request

def acknowledge(self, item, sticky, notify, comment):
"""
Prepare data for acknowledge and POST on backend API or WS if available
:param item: item to acknowledge: host | service
:type item: alignak_app.items.host.Host | alignak_app.items.service.Service
:param sticky: define if ack is sticky or not
:type sticky: bool
:param notify: define if ack should notify user or not
:type notify: bool
:param comment: comment of ack
:type comment: str
:return: request response
:rtype: dict
"""

user = data_manager.database['user']

if self.ws_client.auth:
if item.item_type == 'service':
command = 'ACKNOWLEDGE_SVC_PROBLEM'
host = data_manager.get_item('host', '_id', item.data['host'])
element = host.name
item_name = item.name
else:
command = 'ACKNOWLEDGE_HOST_PROBLEM'
element = item.name
item_name = item.name
if sticky:
sticky = '2'
else:
sticky = '1'
if notify:
notify = '1'
else:
notify = '0'
persistent = '0'

parameters = ';'.join([item_name, sticky, notify, persistent, user.name, comment])
data = {
'command': command,
'element': element,
'parameters': parameters
}
request = self.ws_client.post('command', params=data)
else:
data = {
'action': 'add',
'user': user.item_id,
'comment': comment,
'notify': notify,
'sticky': sticky
}
if item.item_type == 'service':
data['host'] = item.data['host']
data['service'] = item.item_id
else:
data['host'] = item.item_id
data['service'] = None

request = self.post('actionacknowledge', data)

return request

# pylint: disable=too-many-arguments
def downtime(self, item, fixed, duration, start_stamp, end_stamp, comment):
"""
Prepare data for downtime and POST on backend API or WS if available
:param item: item to downtime: host | service
:type item: alignak_app.items.host.Host | alignak_app.items.service.Service
:param fixed: define if donwtime is fixed or not
:type fixed: bool
:param duration: duration timestamp of downtime
:type duration: int
:param start_stamp: start timestamp of downtime
:type start_stamp: int
:param end_stamp: end timestamp of downtime
:type end_stamp: int
:param comment: comment of downtime
:type comment: str
:return: request response
:rtype: dict
"""

if self.ws_client.auth:
if item.item_type == 'service':
host = data_manager.get_item('host', '_id', item.data['host'])
element = host.name
else:
element = item.name
if fixed:
fixed = '1'
else:
fixed = '0'
item_name = item.name
trigger_id = '0'
parameters = ';'.join(
[item_name, str(start_stamp), str(end_stamp), fixed, trigger_id, str(duration),
data_manager.database['user'].name, comment]
)
data = {
'command': 'SCHEDULE_SVC_DOWNTIME' if item.item_type == 'service' else
'SCHEDULE_HOST_DOWNTIME',
'element': element,
'parameters': parameters
}
request = self.ws_client.post('command', params=data)
else:
data = {
'action': 'add',
'user': data_manager.database['user'].item_id,
'fixed': fixed,
'duration': duration,
'start_time': start_stamp,
'end_time': end_stamp,
'comment': comment,
}

if item.item_type == 'service':
data['host'] = item.data['host']
data['service'] = item.item_id
else:
data['host'] = item.item_id
data['service'] = None

request = app_backend.post('actiondowntime', data)

return request

def query_realms(self):
"""
Launch a request on ``realm`` endpoint
Expand Down
40 changes: 3 additions & 37 deletions alignak_app/backend/ws_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@
"""

import json
from logging import getLogger

import requests

from requests import HTTPError
from requests import ConnectionError as RequestsConnectionError

from logging import getLogger

from alignak_app.utils.config import settings

logger = getLogger(__name__)
Expand Down Expand Up @@ -128,7 +128,7 @@ def post(self, endpoint, params=None):

request.raise_for_status()
logger.info('POST on [%s] backend > %s', endpoint, str(request))
logger.debug('\tdata: [%s]', str(data))
logger.debug('\tdata: [%s]', str(params))
logger.debug('\theaders: [%s]', str(headers))
except (RequestsConnectionError, HTTPError) as exp:
logger.warning("WS connection error, error: %s", str(exp))
Expand All @@ -137,37 +137,3 @@ def post(self, endpoint, params=None):
logger.exception("WS exception, error: %s", exp)

return request.json()


if __name__ == '__main__':
import os
from alignak_app.backend.backend import app_backend
os.environ['ALIGNAKAPP_APP_DIR'] = '/usr/local/alignak_app'
os.environ['ALIGNAKAPP_USR_DIR'] = '/home/algorys/.local/alignak_app'
settings.init_config()
app_backend.login(
settings.get_config('Alignak', 'username'),
settings.get_config('Alignak', 'password')
)

ws_client = WSClient()
ws_client.login(app_backend.user['token'])

# ACKNOWLEDGE_HOST_PROBLEM;<host_name>;<sticky>;<notify>;<persistent>;<author>;<comment>
# data = {
# 'command': 'ACKNOWLEDGE_HOST_PROBLEM',
# "element": "pc_matt",
# "parameters": "pc_matt;True;True;%s;Test ACK" % user_id
# }

# CHANGE_HOST_CHECK_COMMAND;<host_name>;<check_command>
# ACKNOWLEDGE_SVC_PROBLEM;<service_name>;<sticky>;<notify>;<persistent>;<author>;<comment>
data = {
'command': 'ACKNOWLEDGE_SVC_PROBLEM',
'element': 'pc_matt',
'parameters': 'Cpu;2;1;0;%s;Test Ack Service' % 'admin'
}

test = ws_client.post('command', data)

print(test)
38 changes: 4 additions & 34 deletions alignak_app/qobjects/common/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,13 @@ def add_acknowledge(self): # pragma: no cover
"""

# Initial comment
user = data_manager.database['user']

comment = _('%s %s acknowledged by %s, from Alignak-app') % (
self.item.item_type.capitalize(), self.item.get_display_name(), user.name
)

# Acknowledge dialog
ack_dialog = AckQDialog()
ack_dialog.initialize(self.item.item_type, self.item.get_display_name(), comment)

Expand All @@ -99,21 +100,7 @@ def add_acknowledge(self): # pragma: no cover
notify = ack_dialog.notify_toggle_btn.is_checked()
comment = str(ack_dialog.ack_comment_edit.toPlainText())

data = {
'action': 'add',
'user': user.item_id,
'comment': comment,
'notify': notify,
'sticky': sticky
}
if self.item.item_type == 'service':
data['host'] = self.item.data['host']
data['service'] = self.item.item_id
else:
data['host'] = self.item.item_id
data['service'] = None

post = app_backend.post('actionacknowledge', data)
post = app_backend.acknowledge(self.item, sticky, notify, comment)

send_event(
'ACK',
Expand Down Expand Up @@ -160,24 +147,7 @@ def add_downtime(self): # pragma: no cover
end_stamp = downtime_dialog.end_time.dateTime().toTime_t()
comment = downtime_dialog.comment_edit.toPlainText()

data = {
'action': 'add',
'user': user.item_id,
'fixed': fixed,
'duration': duration,
'start_time': start_stamp,
'end_time': end_stamp,
'comment': comment,
}

if self.item.item_type == 'service':
data['host'] = self.item.data['host']
data['service'] = self.item.item_id
else:
data['host'] = self.item.item_id
data['service'] = None

post = app_backend.post('actiondowntime', data)
post = app_backend.downtime(self.item, fixed, duration, start_stamp, end_stamp, comment)

send_event(
'DOWNTIME',
Expand Down

0 comments on commit a8d0442

Please sign in to comment.