Skip to content

Commit a9a8256

Browse files
committed
Arbiter WS interface for WS passive checks
1 parent 68912a1 commit a9a8256

File tree

2 files changed

+86
-97
lines changed

2 files changed

+86
-97
lines changed

alignak/http/arbiter_interface.py

+11-71
Original file line numberDiff line numberDiff line change
@@ -1369,7 +1369,7 @@ def query(self):
13691369
# |___/
13701370
#####
13711371
def _build_host_livestate(self, host_name, livestate):
1372-
# pylint: disable=too-many-locals
1372+
# pylint: disable=no-self-use, too-many-locals
13731373
"""Build and notify the external command for an host livestate
13741374
13751375
PROCESS_HOST_CHECK_RESULT;<host_name>;<status_code>;<plugin_output>
@@ -1409,7 +1409,7 @@ def _build_host_livestate(self, host_name, livestate):
14091409
return command_line
14101410

14111411
def _build_service_livestate(self, host_name, service_name, livestate):
1412-
# pylint: disable=too-many-locals
1412+
# pylint: disable=no-self-use, too-many-locals
14131413
"""Build and notify the external command for a service livestate
14141414
14151415
PROCESS_SERVICE_CHECK_RESULT;<host_name>;<service_description>;<return_code>;<plugin_output>
@@ -1437,7 +1437,7 @@ def _build_service_livestate(self, host_name, service_name, livestate):
14371437
"UNKNOWN": 3,
14381438
"UNREACHABLE": 4
14391439
}
1440-
parameters = '%s;%s' % (service_state_to_id.get('state', 3), output)
1440+
parameters = '%s;%s' % (service_state_to_id.get(state, 3), output)
14411441
if long_output and perf_data:
14421442
parameters = '%s|%s\n%s' % (parameters, perf_data, long_output)
14431443
elif long_output:
@@ -1458,72 +1458,15 @@ def _build_service_livestate(self, host_name, service_name, livestate):
14581458
@cherrypy.tools.json_in()
14591459
@cherrypy.tools.json_out()
14601460
def host(self):
1461-
"""
1462-
Request object passed to datasource.query function:
1463-
1464-
{
1465-
'timezone': 'browser',
1466-
'panelId': 38,
1467-
'range': {
1468-
'from': '2018-08-29T02:38:09.633Z',
1469-
'to': '2018-08-29T03:38:09.633Z',
1470-
'raw': {'from': 'now-1h', 'to': 'now'}
1471-
},
1472-
'rangeRaw': {'from': 'now-1h', 'to': 'now'},
1473-
'interval': '10s',
1474-
'intervalMs': 10000,
1475-
'targets': [
1476-
{
1477-
'target': 'problems', 'refId': 'A', 'type': 'table'}
1478-
],
1479-
'format': 'json',
1480-
'maxDataPoints': 314,
1481-
'scopedVars': {
1482-
'__interval': {'text': '10s', 'value': '10s'},
1483-
'__interval_ms': {'text': 10000, 'value': 10000}
1484-
}
1485-
}
1486-
1487-
Only the first target is considered. If several targets are required, an error is raised.
1488-
1489-
The target is a string that is searched in the target_queries dictionary. If found
1490-
the corresponding query is executed and the result is returned.
1461+
# pylint: disable=too-many-branches
1462+
"""Get a passive checks for an host and its services
14911463
1492-
Table response from datasource.query. An array of:
1464+
This function builds the external commands corresponding to the host and services
1465+
provided information
14931466
1494-
[
1495-
{
1496-
"type": "table",
1497-
"columns": [
1498-
{
1499-
"text": "Time",
1500-
"type": "time",
1501-
"sort": true,
1502-
"desc": true,
1503-
},
1504-
{
1505-
"text": "mean",
1506-
},
1507-
{
1508-
"text": "sum",
1509-
}
1510-
],
1511-
"rows": [
1512-
[
1513-
1457425380000,
1514-
null,
1515-
null
1516-
],
1517-
[
1518-
1457425370000,
1519-
1002.76215352,
1520-
1002.76215352
1521-
],
1522-
]
1523-
}
1524-
]
1525-
:return: See upper comment
1526-
:rtype: list
1467+
:param host_name: host name
1468+
:param data: dictionary of the host properties to be modified
1469+
:return: command line
15271470
"""
15281471
logger.debug("Host status...")
15291472
if cherrypy.request.method not in ["PATCH", "POST"]:
@@ -1548,9 +1491,6 @@ def host(self):
15481491
# Get provided data
15491492
# ---
15501493
logger.debug("Posted data: %s", cherrypy.request.json)
1551-
data = {
1552-
'host_name': host_name
1553-
}
15541494

15551495
# Check if the host exist in Alignak
15561496
# ---
@@ -1627,7 +1567,7 @@ def host(self):
16271567
ws_result['_issues'].append("Missing state for the service %s/%s livestate, "
16281568
"assuming service is OK!"
16291569
% (host_name, service_name))
1630-
ls['state'] = 'UP'
1570+
ls['state'] = 'OK'
16311571

16321572
# Tag our own timestamp
16331573
ls['_ws_timestamp'] = now

tests_integ/test_daemons_api.py

+75-26
Original file line numberDiff line numberDiff line change
@@ -2144,20 +2144,80 @@ def test_host_passive_ws(self):
21442144
# -----
21452145

21462146
# -----
2147-
# 2/ Upload an host information
2147+
# 2/ Upload an host (only) information
21482148
now = int(time.time())
2149+
# Only the host name, no live state data
21492150
data = {
2150-
"name": "test_host_0",
2151+
'name': 'test_host'
2152+
}
2153+
raw_data = req.post("http://localhost:7770/host",
2154+
data=json.dumps(data),
2155+
headers={'Content-Type': 'application/json'}, verify=False)
2156+
print("command, got (raw): %s" % (raw_data.content))
2157+
assert raw_data.status_code == 200
2158+
data = raw_data.json()
2159+
print("Got: %s" % data)
2160+
# Status: OK
2161+
# Host is alive :)
2162+
# Created and raised an host passive check command
2163+
# No issues
2164+
assert data == {
2165+
u'_status': u'OK',
2166+
u'_result': [
2167+
u'test_host is alive :)',
2168+
u'Raised: [%s] PROCESS_HOST_CHECK_RESULT;test_host;0;' % now
2169+
],
2170+
u'_issues': []
2171+
}
2172+
2173+
# Now, with live state data
2174+
now = int(time.time())
2175+
data = {
2176+
"name": "test_host",
21512177
"livestate": {
21522178
"state": "UP",
21532179
"output": "Output...",
21542180
"long_output": "Long output...",
21552181
"perf_data": "'counter'=1",
21562182
}
21572183
}
2184+
raw_data = req.post("http://localhost:7770/host",
2185+
data=json.dumps(data),
2186+
headers={'Content-Type': 'application/json'}, verify=False)
2187+
print("command, got (raw): %s" % (raw_data.content))
2188+
assert raw_data.status_code == 200
2189+
data = raw_data.json()
2190+
print("Got: %s" % data)
2191+
# Status: OK
2192+
# Host is alive :)
2193+
# Created and raised an host passive check command
2194+
# No issues
2195+
assert data == {
2196+
u'_status': u'OK',
2197+
u'_result': [
2198+
u'test_host is alive :)',
2199+
u"Raised: [%s] PROCESS_HOST_CHECK_RESULT;test_host;0;Output...|'counter'=1\nLong output..." % now
2200+
],
2201+
u'_issues': []
2202+
}
21582203

2204+
# -----
2205+
# 3/ Upload an host and its services information
2206+
now = int(time.time())
2207+
# Only the host and its services names, no live state data
21592208
data = {
2160-
'name': 'test_host'
2209+
'name': 'test_host',
2210+
"services": [
2211+
{
2212+
"name": "test_ok_0"
2213+
},
2214+
{
2215+
"name": "test_ok_1"
2216+
},
2217+
{
2218+
"name": "test_ok_2"
2219+
}
2220+
]
21612221
}
21622222
raw_data = req.post("http://localhost:7770/host",
21632223
data=json.dumps(data),
@@ -2168,19 +2228,22 @@ def test_host_passive_ws(self):
21682228
print("Got: %s" % data)
21692229
# Status: OK
21702230
# Host is alive :)
2171-
# Created and raised an host passive check command
2231+
# Services are in OK state
2232+
# Created and raised some host and services passive check command
21722233
# No issues
21732234
assert data == {
21742235
u'_status': u'OK',
21752236
u'_result': [
21762237
u'test_host is alive :)',
2177-
u'Raised: [%s] PROCESS_HOST_CHECK_RESULT;test_host;0;' % now
2238+
u'Raised: [%s] PROCESS_HOST_CHECK_RESULT;test_host;0;' % now,
2239+
u'Raised: [%s] PROCESS_SERVICE_CHECK_RESULT;test_host;test_ok_0;0;' % now,
2240+
u'Raised: [%s] PROCESS_SERVICE_CHECK_RESULT;test_host;test_ok_1;0;' % now,
2241+
u'Raised: [%s] PROCESS_SERVICE_CHECK_RESULT;test_host;test_ok_2;0;' % now
21782242
],
21792243
u'_issues': []
21802244
}
21812245

2182-
# -----
2183-
# 2/ Upload an host information
2246+
# Now, with live state data
21842247
now = int(time.time())
21852248
data = {
21862249
'name': 'test_host',
@@ -2214,20 +2277,6 @@ def test_host_passive_ws(self):
22142277
},
22152278
]
22162279
}
2217-
data = {
2218-
'name': 'test_host',
2219-
"services": [
2220-
{
2221-
"name": "test_ok_0"
2222-
},
2223-
{
2224-
"name": "test_ok_1"
2225-
},
2226-
{
2227-
"name": "test_ok_2"
2228-
}
2229-
]
2230-
}
22312280
raw_data = req.post("http://localhost:7770/host",
22322281
data=json.dumps(data),
22332282
headers={'Content-Type': 'application/json'}, verify=False)
@@ -2237,16 +2286,16 @@ def test_host_passive_ws(self):
22372286
print("Got: %s" % data)
22382287
# Status: OK
22392288
# Host is alive :)
2240-
# Created and raised an host passive check command
2289+
# Created and raised some host and services passive check command
22412290
# No issues
22422291
assert data == {
22432292
u'_status': u'OK',
22442293
u'_result': [
22452294
u'test_host is alive :)',
2246-
u'Raised: [%s] PROCESS_HOST_CHECK_RESULT;test_host;0;' % now,
2247-
u'Raised: [%s] PROCESS_SERVICE_CHECK_RESULT;test_host;test_ok_0;3;' % now,
2248-
u'Raised: [%s] PROCESS_SERVICE_CHECK_RESULT;test_host;test_ok_1;3;' % now,
2249-
u'Raised: [%s] PROCESS_SERVICE_CHECK_RESULT;test_host;test_ok_2;3;' % now
2295+
u"Raised: [%s] PROCESS_HOST_CHECK_RESULT;test_host;0;" % now,
2296+
u"Raised: [%s] PROCESS_SERVICE_CHECK_RESULT;test_host;test_ok_0;0;Output 0|'counter'=0\nLong output 0" % now,
2297+
u"Raised: [%s] PROCESS_SERVICE_CHECK_RESULT;test_host;test_ok_1;1;Output 1|'counter'=1\nLong output 1" % now,
2298+
u"Raised: [%s] PROCESS_SERVICE_CHECK_RESULT;test_host;test_ok_2;2;Output 2|'counter'=2\nLong output 2" % now
22502299
],
22512300
u'_issues': []
22522301
}

0 commit comments

Comments
 (0)