blob: f63b2336b3b365b53fd1b977aca45f2298589930 [file] [log] [blame]
Oleksii Zhurba020fab42017-11-01 20:13:28 +00001import json
2import requests
Oleksii Zhurba84ce7fe2018-01-16 21:34:01 +00003import datetime
Oleksii Zhurba468e6c72018-01-16 17:43:15 +00004from cvp_checks import utils
Oleksii Zhurba020fab42017-11-01 20:13:28 +00005
6
7def test_elasticsearch_cluster(local_salt_client):
Oleksii Zhurbae37cdab2017-11-02 20:00:03 +00008 salt_output = local_salt_client.cmd(
Oleksii Zhurbabc512882018-01-29 16:47:20 -06009 'kibana:server',
Oleksii Zhurbae37cdab2017-11-02 20:00:03 +000010 'pillar.get',
11 ['_param:haproxy_elasticsearch_bind_host'],
12 expr_form='pillar')
Oleksii Zhurbae592ed12018-06-21 18:01:09 -050013 proxies = {"http": None, "https": None}
Oleksii Zhurba88bc0472017-11-09 21:04:09 +000014 for node in salt_output.keys():
15 IP = salt_output[node]
Oleksii Zhurbae592ed12018-06-21 18:01:09 -050016 assert requests.get('http://{}:9200/'.format(IP),
17 proxies=proxies).status_code == 200, \
Oleksii Zhurba88bc0472017-11-09 21:04:09 +000018 'Cannot check elasticsearch url on {}.'.format(IP)
Oleksii Zhurbae592ed12018-06-21 18:01:09 -050019 resp = requests.get('http://{}:9200/_cat/health'.format(IP),
20 proxies=proxies).content
Oleksii Zhurba88bc0472017-11-09 21:04:09 +000021 assert resp.split()[3] == 'green', \
22 'elasticsearch status is not good {}'.format(
Oleksii Zhurba0c039ee2018-01-16 19:44:53 +000023 json.dumps(resp, indent=4))
Oleksii Zhurba88bc0472017-11-09 21:04:09 +000024 assert resp.split()[4] == '3', \
25 'elasticsearch status is not good {}'.format(
Oleksii Zhurba0c039ee2018-01-16 19:44:53 +000026 json.dumps(resp, indent=4))
Oleksii Zhurba88bc0472017-11-09 21:04:09 +000027 assert resp.split()[5] == '3', \
28 'elasticsearch status is not good {}'.format(
Oleksii Zhurba0c039ee2018-01-16 19:44:53 +000029 json.dumps(resp, indent=4))
Oleksii Zhurba88bc0472017-11-09 21:04:09 +000030 assert resp.split()[10] == '0', \
31 'elasticsearch status is not good {}'.format(
Oleksii Zhurba0c039ee2018-01-16 19:44:53 +000032 json.dumps(resp, indent=4))
Oleksii Zhurbab31323f2017-11-20 15:35:19 -060033 assert resp.split()[13] == '100.0%', \
Oleksii Zhurba88bc0472017-11-09 21:04:09 +000034 'elasticsearch status is not good {}'.format(
Oleksii Zhurba0c039ee2018-01-16 19:44:53 +000035 json.dumps(resp, indent=4))
Oleksii Zhurba020fab42017-11-01 20:13:28 +000036
37
Oleksii Zhurba84ce7fe2018-01-16 21:34:01 +000038def test_elasticsearch_node_count(local_salt_client):
39 now = datetime.datetime.now()
40 today = now.strftime("%Y.%m.%d")
41 active_nodes = utils.get_active_nodes()
42 salt_output = local_salt_client.cmd(
Oleksii Zhurbabc512882018-01-29 16:47:20 -060043 'kibana:server',
Oleksii Zhurba84ce7fe2018-01-16 21:34:01 +000044 'pillar.get',
45 ['_param:haproxy_elasticsearch_bind_host'],
46 expr_form='pillar')
47 IP = salt_output.values()[0]
Oleksii Zhurbae592ed12018-06-21 18:01:09 -050048 proxies = {"http": None, "https": None}
Oleksii Zhurbad2847dc2018-02-16 15:13:09 -060049 resp = json.loads(requests.post('http://{0}:9200/log-{1}/_search?pretty'.
50 format(IP, today),
Oleksii Zhurbae592ed12018-06-21 18:01:09 -050051 proxies=proxies,
Oleksii Zhurba7f463412018-03-21 16:32:44 -050052 data='{"size": 0, "aggs": '
53 '{"uniq_hostname": '
Oleksii Zhurbad2847dc2018-02-16 15:13:09 -060054 '{"terms": {"size": 500, '
Oleksii Zhurba7f463412018-03-21 16:32:44 -050055 '"field": "Hostname.keyword"}}}}').text)
Oleksii Zhurbad2847dc2018-02-16 15:13:09 -060056 cluster_domain = local_salt_client.cmd('salt:control',
57 'pillar.get',
58 ['_param:cluster_domain'],
59 expr_form='pillar').values()[0]
60 monitored_nodes = []
Oleksii Zhurba7f463412018-03-21 16:32:44 -050061 for item_ in resp['aggregations']['uniq_hostname']['buckets']:
Oleksii Zhurbad2847dc2018-02-16 15:13:09 -060062 node_name = item_['key']
63 monitored_nodes.append(node_name + '.' + cluster_domain)
64 missing_nodes = []
65 for node in active_nodes.keys():
66 if node not in monitored_nodes:
67 missing_nodes.append(node)
68 assert len(missing_nodes) == 0, \
Oleksii Zhurba84ce7fe2018-01-16 21:34:01 +000069 'Not all nodes are in Elasticsearch. Found {0} keys, ' \
Oleksii Zhurbad2847dc2018-02-16 15:13:09 -060070 'expected {1}. Missing nodes: \n{2}'. \
71 format(len(monitored_nodes), len(active_nodes), missing_nodes)
Oleksii Zhurba84ce7fe2018-01-16 21:34:01 +000072
73
Oleksii Zhurba020fab42017-11-01 20:13:28 +000074def test_stacklight_services_replicas(local_salt_client):
75 salt_output = local_salt_client.cmd(
76 'docker:client:stack:monitoring',
77 'cmd.run',
78 ['docker service ls'],
79 expr_form='pillar')
80 wrong_items = []
81 for line in salt_output[salt_output.keys()[0]].split('\n'):
82 if line[line.find('/') - 1] != line[line.find('/') + 1] \
83 and 'replicated' in line:
84 wrong_items.append(line)
85 assert len(wrong_items) == 0, \
86 '''Some monitoring services doesn't have expected number of replicas:
87 {}'''.format(json.dumps(wrong_items, indent=4))
88
89
Oleksii Zhurba468e6c72018-01-16 17:43:15 +000090def test_prometheus_alert_count(local_salt_client):
91 IP = utils.get_monitoring_ip('cluster_public_host')
92 # keystone:server can return 3 nodes instead of 1
93 # this will be fixed later
94 # TODO
Oleksii Zhurba0c039ee2018-01-16 19:44:53 +000095 nodes_info = local_salt_client.cmd(
Oleksii Zhurba468e6c72018-01-16 17:43:15 +000096 'keystone:server',
97 'cmd.run',
Oleksii Zhurba0c039ee2018-01-16 19:44:53 +000098 ['curl -s http://{}:15010/alerts | grep icon-chevron-down | '
99 'grep -v "0 active"'.format(IP)],
Oleksii Zhurba468e6c72018-01-16 17:43:15 +0000100 expr_form='pillar')
Oleksii Zhurba0c039ee2018-01-16 19:44:53 +0000101 result = nodes_info[nodes_info.keys()[0]].replace('</td>', '').replace(
102 '<td><i class="icon-chevron-down"></i> <b>', '').replace('</b>', '')
103 assert result == '', 'AlertManager page has some alerts! {}'.format(
104 json.dumps(result), indent=4)
Oleksii Zhurba468e6c72018-01-16 17:43:15 +0000105
106
Oleksii Zhurba020fab42017-11-01 20:13:28 +0000107def test_stacklight_containers_status(local_salt_client):
108 salt_output = local_salt_client.cmd(
109 'docker:swarm:role:master',
110 'cmd.run',
111 ['docker service ps $(docker stack services -q monitoring)'],
112 expr_form='pillar')
113 result = {}
Oleksii Zhurba468e6c72018-01-16 17:43:15 +0000114 # for old reclass models, docker:swarm:role:master can return
115 # 2 nodes instead of one. Here is temporary fix.
116 # TODO
117 if len(salt_output.keys()) > 1:
118 if 'CURRENT STATE' not in salt_output[salt_output.keys()[0]]:
119 del salt_output[salt_output.keys()[0]]
Oleksii Zhurbaf2af6372017-11-01 22:53:03 +0000120 for line in salt_output[salt_output.keys()[0]].split('\n')[1:]:
Oleksii Zhurba020fab42017-11-01 20:13:28 +0000121 shift = 0
Oleksii Zhurba020fab42017-11-01 20:13:28 +0000122 if line.split()[1] == '\\_':
123 shift = 1
124 if line.split()[1 + shift] not in result.keys():
125 result[line.split()[1]] = 'NOT OK'
126 if line.split()[4 + shift] == 'Running' \
127 or line.split()[4 + shift] == 'Ready':
128 result[line.split()[1 + shift]] = 'OK'
129 assert 'NOT OK' not in result.values(), \
130 '''Some containers are in incorrect state:
131 {}'''.format(json.dumps(result, indent=4))
Oleksii Zhurbae592ed12018-06-21 18:01:09 -0500132
133
134def test_running_telegraf_services(local_salt_client):
135 salt_output = local_salt_client.cmd('telegraf:agent',
136 'service.status',
137 'telegraf',
138 expr_form='pillar')
139 result = [{node: status} for node, status
140 in salt_output.items()
141 if status is False]
142 assert result == [], 'Telegraf service is not running ' \
143 'on following nodes:'.format(result)