Browse Source

Force the reschedule of the icinga checks

icinga2 documentation wrongly mentioned a 'force_check' parameter
instead of 'force' for 'reschedule-check' action. icinga2api uses the
wrong parameter. It looks like icinga2api isn't helpful and there isn't
a better alternative: don't use icinga2api anymore.

Closes #305
keep-around/31c20b9afe2d739ddbe2179546dbad127bc3cc1e
Pierre-Louis Bonicoli 1 year ago
parent
commit
31c20b9afe
No known key found for this signature in database GPG Key ID: 6914C4A5EDAA6DD
  1. 9
      Pipfile
  2. 134
      Pipfile.lock
  3. 10
      playbooks/bind/tests/test_zone.py
  4. 3
      playbooks/chat/tests/test_icinga.py
  5. 3
      playbooks/cloud/tests/test_icinga.py
  6. 3
      playbooks/enough/tests/test_icinga.py
  7. 3
      playbooks/forum/tests/test_icinga.py
  8. 3
      playbooks/gitlab/tests/test_icinga.py
  9. 20
      playbooks/icinga/tests/test_icinga_checks.py
  10. 3
      playbooks/jitsi/tests/test_icinga.py
  11. 3
      playbooks/openedx/tests/test_icinga.py
  12. 3
      playbooks/openvpn/tests/test_icinga.py
  13. 3
      playbooks/packages/tests/test_icinga.py
  14. 3
      playbooks/pad/tests/test_icinga.py
  15. 3
      playbooks/postfix/tests/test_icinga.py
  16. 3
      playbooks/psono/tests/test_icinga.py
  17. 3
      playbooks/wazuh/tests/test_icinga.py
  18. 3
      playbooks/weblate/tests/test_icinga.py
  19. 3
      playbooks/website/tests/test_icinga.py
  20. 3
      playbooks/wekan/tests/test_icinga.py
  21. 3
      playbooks/wordpress/tests/test_icinga.py
  22. 4
      requirements-dev.txt
  23. 11
      requirements.txt
  24. 99
      tests/icinga_helper.py

9
Pipfile

@ -12,7 +12,7 @@ pytest-cov = "==2.6.1"
pytest-mock = "==1.10.4"
pytest-django = "==3.4.8"
requests-mock = "==1.6.0"
sphinx = "==1.8.2"
Sphinx = "==1.8.2"
tox = "==3.15.0"
twine = "==1.13.0"
dnspython = "==1.16.0"
@ -22,15 +22,14 @@ pipenv-to-requirements = "*"
ansible = "~=2.9.0"
beautifulsoup4 = "==4.7.1"
cliff = "*"
django = "==2.2"
Django = "==2.2"
djangorestframework = "==3.9.2"
django-allauth = "==0.39.1"
django-bootstrap3 = "==11.0.0"
docker = "*"
docker-compose = "*"
future = "==0.18.2"
icinga2api = {git = "https://github.com/fmnisme/python-icinga2api.git", ref = "9a1a3cc7968d6c72bf49e97ef387b2824e6835e9"}
jinja2 = ">=1.1"
Jinja2 = ">=1.1"
testinfra = "==5.0.0"
netaddr = "==0.7.19"
openstacksdk = "*"
@ -39,7 +38,7 @@ python-heatclient = "*"
pytest = "==4.4.0"
sh = "==1.12.14"
shade = "==1.30.0"
pyopenssl = "*"
pyOpenSSL = "*"
[requires]
python_version = "3.8"

134
Pipfile.lock

@ -1,7 +1,7 @@
{
"_meta": {
"hash": {
"sha256": "e0b0ccf86a47afcb6bfb795f9ff3ebcb19c4b6c4f2859c0fec74891c0d7bfd41"
"sha256": "1e2a8b036150cb8b2636ea7cb4ac31b210b45452690fd120047549a885deb87c"
},
"pipfile-spec": 6,
"requires": {
@ -92,44 +92,42 @@
},
"cffi": {
"hashes": [
"sha256:005f2bfe11b6745d726dbb07ace4d53f057de66e336ff92d61b8c7e9c8f4777d",
"sha256:09e96138280241bd355cd585148dec04dbbedb4f46128f340d696eaafc82dd7b",
"sha256:0b1ad452cc824665ddc682400b62c9e4f5b64736a2ba99110712fdee5f2505c4",
"sha256:0ef488305fdce2580c8b2708f22d7785ae222d9825d3094ab073e22e93dfe51f",
"sha256:15f351bed09897fbda218e4db5a3d5c06328862f6198d4fb385f3e14e19decb3",
"sha256:22399ff4870fb4c7ef19fff6eeb20a8bbf15571913c181c78cb361024d574579",
"sha256:23e5d2040367322824605bc29ae8ee9175200b92cb5483ac7d466927a9b3d537",
"sha256:2791f68edc5749024b4722500e86303a10d342527e1e3bcac47f35fbd25b764e",
"sha256:2f9674623ca39c9ebe38afa3da402e9326c245f0f5ceff0623dccdac15023e05",
"sha256:3363e77a6176afb8823b6e06db78c46dbc4c7813b00a41300a4873b6ba63b171",
"sha256:33c6cdc071ba5cd6d96769c8969a0531be2d08c2628a0143a10a7dcffa9719ca",
"sha256:3b8eaf915ddc0709779889c472e553f0d3e8b7bdf62dab764c8921b09bf94522",
"sha256:3cb3e1b9ec43256c4e0f8d2837267a70b0e1ca8c4f456685508ae6106b1f504c",
"sha256:3eeeb0405fd145e714f7633a5173318bd88d8bbfc3dd0a5751f8c4f70ae629bc",
"sha256:44f60519595eaca110f248e5017363d751b12782a6f2bd6a7041cba275215f5d",
"sha256:4d7c26bfc1ea9f92084a1d75e11999e97b62d63128bcc90c3624d07813c52808",
"sha256:529c4ed2e10437c205f38f3691a68be66c39197d01062618c55f74294a4a4828",
"sha256:6642f15ad963b5092d65aed022d033c77763515fdc07095208f15d3563003869",
"sha256:85ba797e1de5b48aa5a8427b6ba62cf69607c18c5d4eb747604b7302f1ec382d",
"sha256:8f0f1e499e4000c4c347a124fa6a27d37608ced4fe9f7d45070563b7c4c370c9",
"sha256:a624fae282e81ad2e4871bdb767e2c914d0539708c0f078b5b355258293c98b0",
"sha256:b0358e6fefc74a16f745afa366acc89f979040e0cbc4eec55ab26ad1f6a9bfbc",
"sha256:bbd2f4dfee1079f76943767fce837ade3087b578aeb9f69aec7857d5bf25db15",
"sha256:bf39a9e19ce7298f1bd6a9758fa99707e9e5b1ebe5e90f2c3913a47bc548747c",
"sha256:c11579638288e53fc94ad60022ff1b67865363e730ee41ad5e6f0a17188b327a",
"sha256:c150eaa3dadbb2b5339675b88d4573c1be3cb6f2c33a6c83387e10cc0bf05bd3",
"sha256:c53af463f4a40de78c58b8b2710ade243c81cbca641e34debf3396a9640d6ec1",
"sha256:cb763ceceae04803adcc4e2d80d611ef201c73da32d8f2722e9d0ab0c7f10768",
"sha256:cc75f58cdaf043fe6a7a6c04b3b5a0e694c6a9e24050967747251fb80d7bce0d",
"sha256:d80998ed59176e8cba74028762fbd9b9153b9afc71ea118e63bbf5d4d0f9552b",
"sha256:de31b5164d44ef4943db155b3e8e17929707cac1e5bd2f363e67a56e3af4af6e",
"sha256:e66399cf0fc07de4dce4f588fc25bfe84a6d1285cc544e67987d22663393926d",
"sha256:f0620511387790860b249b9241c2f13c3a80e21a73e0b861a2df24e9d6f56730",
"sha256:f4eae045e6ab2bb54ca279733fe4eb85f1effda392666308250714e01907f394",
"sha256:f92cdecb618e5fa4658aeb97d5eb3d2f47aa94ac6477c6daf0f306c5a3b9e6b1",
"sha256:f92f789e4f9241cd262ad7a555ca2c648a98178a953af117ef7fad46aa1d5591"
],
"version": "==1.14.3"
"sha256:00a1ba5e2e95684448de9b89888ccd02c98d512064b4cb987d48f4b40aa0421e",
"sha256:00e28066507bfc3fe865a31f325c8391a1ac2916219340f87dfad602c3e48e5d",
"sha256:045d792900a75e8b1e1b0ab6787dd733a8190ffcf80e8c8ceb2fb10a29ff238a",
"sha256:0638c3ae1a0edfb77c6765d487fee624d2b1ee1bdfeffc1f0b58c64d149e7eec",
"sha256:105abaf8a6075dc96c1fe5ae7aae073f4696f2905fde6aeada4c9d2926752362",
"sha256:155136b51fd733fa94e1c2ea5211dcd4c8879869008fc811648f16541bf99668",
"sha256:1a465cbe98a7fd391d47dce4b8f7e5b921e6cd805ef421d04f5f66ba8f06086c",
"sha256:1d2c4994f515e5b485fd6d3a73d05526aa0fcf248eb135996b088d25dfa1865b",
"sha256:2c24d61263f511551f740d1a065eb0212db1dbbbbd241db758f5244281590c06",
"sha256:51a8b381b16ddd370178a65360ebe15fbc1c71cf6f584613a7ea08bfad946698",
"sha256:594234691ac0e9b770aee9fcdb8fa02c22e43e5c619456efd0d6c2bf276f3eb2",
"sha256:5cf4be6c304ad0b6602f5c4e90e2f59b47653ac1ed9c662ed379fe48a8f26b0c",
"sha256:64081b3f8f6f3c3de6191ec89d7dc6c86a8a43911f7ecb422c60e90c70be41c7",
"sha256:6bc25fc545a6b3d57b5f8618e59fc13d3a3a68431e8ca5fd4c13241cd70d0009",
"sha256:798caa2a2384b1cbe8a2a139d80734c9db54f9cc155c99d7cc92441a23871c03",
"sha256:7c6b1dece89874d9541fc974917b631406233ea0440d0bdfbb8e03bf39a49b3b",
"sha256:840793c68105fe031f34d6a086eaea153a0cd5c491cde82a74b420edd0a2b909",
"sha256:8d6603078baf4e11edc4168a514c5ce5b3ba6e3e9c374298cb88437957960a53",
"sha256:9cc46bc107224ff5b6d04369e7c595acb700c3613ad7bcf2e2012f62ece80c35",
"sha256:9f7a31251289b2ab6d4012f6e83e58bc3b96bd151f5b5262467f4bb6b34a7c26",
"sha256:9ffb888f19d54a4d4dfd4b3f29bc2c16aa4972f1c2ab9c4ab09b8ab8685b9c2b",
"sha256:a7711edca4dcef1a75257b50a2fbfe92a65187c47dab5a0f1b9b332c5919a3fb",
"sha256:af5c59122a011049aad5dd87424b8e65a80e4a6477419c0c1015f73fb5ea0293",
"sha256:b18e0a9ef57d2b41f5c68beefa32317d286c3d6ac0484efd10d6e07491bb95dd",
"sha256:b4e248d1087abf9f4c10f3c398896c87ce82a9856494a7155823eb45a892395d",
"sha256:ba4e9e0ae13fc41c6b23299545e5ef73055213e466bd107953e4a013a5ddd7e3",
"sha256:c6332685306b6417a91b1ff9fae889b3ba65c2292d64bd9245c093b1b284809d",
"sha256:d9efd8b7a3ef378dd61a1e77367f1924375befc2eba06168b6ebfa903a5e59ca",
"sha256:df5169c4396adc04f9b0a05f13c074df878b6052430e03f50e68adf3a57aa28d",
"sha256:ebb253464a5d0482b191274f1c8bf00e33f7e0b9c66405fbffc61ed2c839c775",
"sha256:ec80dc47f54e6e9a78181ce05feb71a0353854cc26999db963695f950b5fb375",
"sha256:f032b34669220030f905152045dfa27741ce1a6db3324a5bc0b96b6c7420c87b",
"sha256:f60567825f791c6f8a592f3c6e3bd93dd2934e3f9dac189308426bd76b00ef3b",
"sha256:f803eaa94c2fcda012c047e62bc7a51b0bdabda1cad7a92a522694ea2d76e49f"
],
"version": "==1.14.4"
},
"chardet": {
"hashes": [
@ -255,11 +253,11 @@
"ssh"
],
"hashes": [
"sha256:13966471e8bc23b36bfb3a6fb4ab75043a5ef1dac86516274777576bed3b9828",
"sha256:bad94b8dd001a8a4af19ce4becc17f41b09f228173ffe6a4e0355389eef142f2"
"sha256:317e95a48c32de8c1aac92a48066a5b73e218ed096e03758bcdd799a7130a1a1",
"sha256:cffc771d4ea1389fc66bc95cb72d304aa41d1a1563482a9a000fba3a84ed5071"
],
"index": "pypi",
"version": "==4.3.1"
"version": "==4.4.0"
},
"docker-compose": {
"hashes": [
@ -283,10 +281,10 @@
},
"dogpile.cache": {
"hashes": [
"sha256:eba3eb532be75a930f7a70c40c9a66829a3f7281650ad3cd3a786b2e4ba68e83"
"sha256:40147b19696f387415a7efaaa4cf8ea0b5d31bdd1b53e5187e75d48ddfee9f0e"
],
"markers": "python_version >= '3.6'",
"version": "==1.1.0"
"version": "==1.1.1"
},
"future": {
"hashes": [
@ -295,10 +293,6 @@
"index": "pypi",
"version": "==0.18.2"
},
"icinga2api": {
"git": "https://github.com/fmnisme/python-icinga2api.git",
"ref": "9a1a3cc7968d6c72bf49e97ef387b2824e6835e9"
},
"idna": {
"hashes": [
"sha256:b307872f855b18632ce0c21c5e45be78c0ea7ae4c15c828c20788b26921eb3f6",
@ -307,6 +301,14 @@
"markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'",
"version": "==2.10"
},
"importlib-metadata": {
"hashes": [
"sha256:590690d61efdd716ff82c39ca9a9d4209252adfe288a4b5721181050acbd4175",
"sha256:d9b8a46a0885337627a6430db287176970fff18ad421becec1d64cfc763c2099"
],
"markers": "python_version < '3.8'",
"version": "==3.1.0"
},
"iso8601": {
"hashes": [
"sha256:694be0743e9f1535ea873bfc7bd6fb62380c62b75822761859428073a17fd39c",
@ -333,11 +335,11 @@
},
"jsonpatch": {
"hashes": [
"sha256:83ff23119b336ea2feffa682307eb7269b58097b4e88c089a4950d946442db16",
"sha256:e45df18b0ab7df1925f20671bbc3f6bd0b4b556fb4b9c5d97684b0a7eac01744"
"sha256:4d08af10d71723b5b2924da6ba90f273a4d1a5c6accfb605eb970cb2f9b29cf9",
"sha256:86eaaccfac8891c6a09ab48aec13fb5921ff32c838e7a19981f0d2b94915f668"
],
"markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4'",
"version": "==1.26"
"version": "==1.27"
},
"jsonpointer": {
"hashes": [
@ -917,6 +919,14 @@
"sha256:b62ffa81fb85f4332a4f609cab4ac40709470da05643a082ec1eb88e6d9b97d7"
],
"version": "==1.12.1"
},
"zipp": {
"hashes": [
"sha256:102c24ef8f171fd729d46599845e95c7ab894a4cf45f5de11a44cc7444fb1108",
"sha256:ed5eee1974372595f9e416cc7bbeeb12335201d8081ca8a0743c954d4446e5cb"
],
"markers": "python_version >= '3.6'",
"version": "==3.4.0"
}
},
"develop": {
@ -1076,6 +1086,14 @@
"markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'",
"version": "==1.2.0"
},
"importlib-metadata": {
"hashes": [
"sha256:590690d61efdd716ff82c39ca9a9d4209252adfe288a4b5721181050acbd4175",
"sha256:d9b8a46a0885337627a6430db287176970fff18ad421becec1d64cfc763c2099"
],
"markers": "python_version < '3.8'",
"version": "==3.1.0"
},
"iniconfig": {
"hashes": [
"sha256:011e24c64b7f47f6ebd835bb12a743f2fbe9a26d4cecaa7f53bc4f35ee9da8b3",
@ -1388,11 +1406,11 @@
},
"virtualenv": {
"hashes": [
"sha256:6af42359fbb33a6c7eab4d3246524b96fd9d8e07e7141b7a65998f96e28b2c57",
"sha256:fd4147c5ba3f694e2e4fc3c767407dc2226899623bb9b49c2f15637c2ee335b3"
"sha256:07cff122e9d343140366055f31be4dcd61fd598c69d11cd33a9d9c8df4546dd7",
"sha256:e0aac7525e880a429764cefd3aaaff54afb5d9f25c82627563603f5d7de5a6e5"
],
"markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'",
"version": "==20.2.0"
"version": "==20.2.1"
},
"virtualenv-clone": {
"hashes": [
@ -1408,6 +1426,14 @@
"sha256:b36a1c245f2d304965eb4e0a82848379241dc04b865afcc4aab16748587e1923"
],
"version": "==0.5.1"
},
"zipp": {
"hashes": [
"sha256:102c24ef8f171fd729d46599845e95c7ab894a4cf45f5de11a44cc7444fb1108",
"sha256:ed5eee1974372595f9e416cc7bbeeb12335201d8081ca8a0743c954d4446e5cb"
],
"markers": "python_version >= '3.6'",
"version": "==3.4.0"
}
}
}

10
playbooks/bind/tests/test_zone.py

@ -1,6 +1,4 @@
from icinga2api.exceptions import Icinga2ApiException
from tests.icinga_helper import IcingaHelper
import pytest
testinfra_hosts = ['ansible://icinga-host']
@ -8,11 +6,9 @@ testinfra_hosts = ['ansible://icinga-host']
class TestChecks(IcingaHelper):
def test_icinga_host(self):
objects = self.get_client().objects
r = objects.get('Host', 'bind-host')
assert r['attrs']['name'] == 'bind-host'
with pytest.raises(Icinga2ApiException):
objects.get('Host', 'deleted-host')
hosts = self.get_hosts()
assert 'bind-host' in hosts
assert 'deleted-host' not in hosts
def test_icinga_service(self, host):
# r = self.get_client().objects.list('Service', joins=['host.name'])

3
playbooks/chat/tests/test_icinga.py

@ -8,8 +8,7 @@ IcingaHelper.icinga_host = 'bind-host'
class TestChecks(IcingaHelper):
def test_host(self):
r = self.get_client().objects.get('Host', 'chat-host')
assert r['attrs']['name'] == 'chat-host'
assert 'chat-host' in self.get_hosts(host='chat-host')
def test_service(self, host):
assert self.is_service_ok('chat-host!Chat')

3
playbooks/cloud/tests/test_icinga.py

@ -8,8 +8,7 @@ IcingaHelper.icinga_host = 'bind-host'
class TestChecks(IcingaHelper):
def test_host(self):
r = self.get_client().objects.get('Host', 'cloud-host')
assert r['attrs']['name'] == 'cloud-host'
assert 'cloud-host' in self.get_hosts(host='cloud-host')
def test_service(self, host):
assert self.is_service_ok('cloud-host!cloud-host')

3
playbooks/enough/tests/test_icinga.py

@ -8,8 +8,7 @@ IcingaHelper.icinga_host = 'bind-host'
class TestChecks(IcingaHelper):
def test_host(self):
r = self.get_client().objects.get('Host', 'cloud-host')
assert r['attrs']['name'] == 'cloud-host'
assert 'cloud-host' in self.get_hosts(host='cloud-host')
def test_service(self, host):
assert self.is_service_ok('cloud-host!cloud-host')

3
playbooks/forum/tests/test_icinga.py

@ -8,8 +8,7 @@ IcingaHelper.icinga_host = 'bind-host'
class TestChecks(IcingaHelper):
def test_host(self):
r = self.get_client().objects.get('Host', 'forum-host')
assert r['attrs']['name'] == 'forum-host'
assert 'forum-host' in self.get_hosts(host='forum-host')
def test_service(self, host):
assert self.is_service_ok('forum-host!Forum')

3
playbooks/gitlab/tests/test_icinga.py

@ -8,8 +8,7 @@ IcingaHelper.icinga_host = 'bind-host'
class TestChecks(IcingaHelper):
def test_host(self):
r = self.get_client().objects.get('Host', 'gitlab-host')
assert r['attrs']['name'] == 'gitlab-host'
assert 'gitlab-host' in self.get_hosts(host='gitlab-host')
def test_service(self, host):
assert self.is_service_ok('gitlab-host!Gitlab')

20
playbooks/icinga/tests/test_icinga_checks.py

@ -1,10 +1,9 @@
import re
from bs4 import BeautifulSoup
from icinga2api.exceptions import Icinga2ApiException
from tests.icinga_helper import IcingaHelper
import pytest
from enough.common import retry
import requests
import testinfra
import yaml
@ -14,16 +13,17 @@ testinfra_hosts = ['ansible://icinga-host']
class TestChecks(IcingaHelper):
def test_icinga_host(self):
objects = self.get_client().objects
r = objects.get('Host', 'bind-host')
assert r['attrs']['name'] == 'bind-host'
with pytest.raises(Icinga2ApiException):
objects.get('Host', 'deleted-host')
hosts = self.get_hosts()
assert 'bind-host' in hosts
assert 'deleted-host' not in hosts
def test_icinga_version(self, pytestconfig):
client = self.get_client()
ret = client.status.list('IcingaApplication')
version = ret['results'][0]['status']['icingaapplication']['app']['version']
url = f'{self.get_api_url()}/status/IcingaApplication'
answer = requests.get(url, **self.connection_params())
answer.raise_for_status()
results = answer.json().get('results', [])
assert len(results) == 1
version = results[0]['status']['icingaapplication']['app']['version']
host = testinfra.get_host(f'ansible://{TestChecks.icinga_host}',
ansible_inventory=self.inventory)

3
playbooks/jitsi/tests/test_icinga.py

@ -8,8 +8,7 @@ IcingaHelper.icinga_host = 'bind-host'
class TestChecks(IcingaHelper):
def test_host(self):
r = self.get_client().objects.get('Host', 'jitsi-host')
assert r['attrs']['name'] == 'jitsi-host'
assert 'jisti-host' in self.get_hosts(host='jisti-host')
def test_service(self, host):
assert self.is_service_ok('jitsi-host!Jitsi')

3
playbooks/openedx/tests/test_icinga.py

@ -8,8 +8,7 @@ IcingaHelper.icinga_host = 'bind-host'
class TestChecks(IcingaHelper):
def test_host(self):
r = self.get_client().objects.get('Host', 'openedx-host')
assert r['attrs']['name'] == 'openedx-host'
assert 'openedx-host' in self.get_hosts(host='openedx-host')
def test_service(self, host):
assert self.is_service_ok('openedx-host!openedX')

3
playbooks/openvpn/tests/test_icinga.py

@ -6,8 +6,7 @@ testinfra_hosts = ['ansible://icinga-host']
class TestChecks(IcingaHelper):
def test_host(self):
r = self.get_client().objects.get('Host', 'website-host')
assert r['attrs']['name'] == 'website-host'
assert 'website-host' in self.get_hosts(host='website-host')
def test_service_disk(self):
assert self.is_service_ok('website-host!disk')

3
playbooks/packages/tests/test_icinga.py

@ -8,8 +8,7 @@ IcingaHelper.icinga_host = 'bind-host'
class TestChecks(IcingaHelper):
def test_host(self):
r = self.get_client().objects.get('Host', 'packages-host')
assert r['attrs']['name'] == 'packages-host'
assert 'packages-host' in self.get_hosts(host='packages-host')
def test_service(self, host):
assert self.is_service_ok('packages-host!Packages')

3
playbooks/pad/tests/test_icinga.py

@ -8,8 +8,7 @@ IcingaHelper.icinga_host = 'bind-host'
class TestChecks(IcingaHelper):
def test_host(self):
r = self.get_client().objects.get('Host', 'website-host')
assert r['attrs']['name'] == 'website-host'
assert 'website-host' in self.get_hosts(host='website-host')
def test_service(self):
assert self.is_service_ok('website-host!Pad')

3
playbooks/postfix/tests/test_icinga.py

@ -8,8 +8,7 @@ IcingaHelper.icinga_host = 'bind-host'
class TestChecks(IcingaHelper):
def test_host(self):
r = self.get_client().objects.get('Host', 'postfix-host')
assert r['attrs']['name'] == 'postfix-host'
assert 'postfix-host' in self.get_hosts(host='postfix-host')
def test_service(self, host):
# r = self.get_client().objects.list('Service', joins=['host.name'])

3
playbooks/psono/tests/test_icinga.py

@ -8,8 +8,7 @@ IcingaHelper.icinga_host = 'bind-host'
class TestChecks(IcingaHelper):
def test_host(self):
r = self.get_client().objects.get('Host', 'psono-host')
assert r['attrs']['name'] == 'psono-host'
assert 'psono-host' in self.get_hosts(host='psono-host')
def test_service(self, host):
assert self.is_service_ok('psono-host!psono')

3
playbooks/wazuh/tests/test_icinga.py

@ -8,8 +8,7 @@ IcingaHelper.icinga_host = 'bind-host'
class TestChecks(IcingaHelper):
def test_host(self):
r = self.get_client().objects.get('Host', 'wazuh-host')
assert r['attrs']['name'] == 'wazuh-host'
assert 'wazuh-host' in self.get_hosts(host='wazuh-host')
def test_service(self, host):
assert self.is_service_ok('wazuh-host!ping4')

3
playbooks/weblate/tests/test_icinga.py

@ -8,8 +8,7 @@ IcingaHelper.icinga_host = 'bind-host'
class TestChecks(IcingaHelper):
def test_host(self):
r = self.get_client().objects.get('Host', 'weblate-host')
assert r['attrs']['name'] == 'weblate-host'
assert 'weblate-host' in self.get_hosts(host='weblate-host')
def test_service(self, host):
assert self.is_service_ok('weblate-host!Weblate')

3
playbooks/website/tests/test_icinga.py

@ -10,8 +10,7 @@ IcingaHelper.icinga_host = 'bind-host'
class TestChecks(IcingaHelper):
def test_host(self):
r = self.get_client().objects.get('Host', 'website-host')
assert r['attrs']['name'] == 'website-host'
assert 'website-host' in self.get_hosts(host='website-host')
def test_service(self):
website.update(testinfra.get_host('ansible://website-host',

3
playbooks/wekan/tests/test_icinga.py

@ -8,8 +8,7 @@ IcingaHelper.icinga_host = 'bind-host'
class TestChecks(IcingaHelper):
def test_host(self):
r = self.get_client().objects.get('Host', 'wekan-host')
assert r['attrs']['name'] == 'wekan-host'
assert 'wekan-host' in self.get_hosts(host='wekan-host')
def test_service(self):
assert self.is_service_ok('wekan-host!Wekan')

3
playbooks/wordpress/tests/test_icinga.py

@ -8,8 +8,7 @@ IcingaHelper.icinga_host = 'bind-host'
class TestChecks(IcingaHelper):
def test_host(self):
r = self.get_client().objects.get('Host', 'wordpress-host')
assert r['attrs']['name'] == 'wordpress-host'
assert 'wordpress-host' in self.get_hosts(host='wordpress-host')
def test_service(self, host):
assert self.is_service_ok('wordpress-host!WordPress')

4
requirements-dev.txt

@ -26,6 +26,7 @@ filelock==3.0.12
flake8==3.5
idna==2.10 ; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'
imagesize==1.2.0 ; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'
importlib-metadata==3.1.0 ; python_version < '3.8'
iniconfig==1.1.1
jinja2==2.11.2
markupsafe==1.1.1 ; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'
@ -63,5 +64,6 @@ tqdm==4.53.0 ; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2,
twine==1.13.0
urllib3==1.26.2 ; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4' and python_version < '4'
virtualenv-clone==0.5.4 ; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'
virtualenv==20.2.0 ; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'
virtualenv==20.2.1 ; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'
webencodings==0.5.1
zipp==3.4.0 ; python_version >= '3.6'

11
requirements.txt

@ -19,7 +19,7 @@ bcrypt==3.2.0 ; python_version >= '3.6'
beautifulsoup4==4.7.1
cached-property==1.5.2
certifi==2020.11.8
cffi==1.14.3
cffi==1.14.4
chardet==3.0.4
cliff==3.5.0
cmd2==1.4.0 ; python_version >= '3.5'
@ -34,17 +34,17 @@ django-bootstrap3==11.0.0
django==2.2
djangorestframework==3.9.2
docker-compose==1.27.4
docker[ssh]==4.3.1
docker[ssh]==4.4.0
dockerpty==0.4.1
docopt==0.6.2
dogpile.cache==1.1.0 ; python_version >= '3.6'
dogpile.cache==1.1.1 ; python_version >= '3.6'
future==0.18.2
icinga2api
idna==2.10 ; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'
importlib-metadata==3.1.0 ; python_version < '3.8'
iso8601==0.1.13
jinja2==2.11.2
jmespath==0.10.0 ; python_version >= '2.6' and python_version not in '3.0, 3.1, 3.2, 3.3'
jsonpatch==1.26 ; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4'
jsonpatch==1.27 ; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4'
jsonpointer==2.0 ; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'
jsonschema==3.2.0
keystoneauth1==4.2.1 ; python_version >= '3.6'
@ -103,3 +103,4 @@ urllib3==1.26.2 ; python_version >= '2.7' and python_version not in '3.0, 3.1, 3
wcwidth==0.2.5
websocket-client==0.57.0
wrapt==1.12.1
zipp==3.4.0 ; python_version >= '3.6'

99
tests/icinga_helper.py

@ -1,6 +1,8 @@
from bs4 import BeautifulSoup
from icinga2api.client import Client
from functools import lru_cache
import json
import re
import urllib.parse
from enough.common import retry
from enough.common import ansible_utils
import requests
@ -21,6 +23,7 @@ class IcingaHelper(object):
def set_ansible_inventory(inventory):
IcingaHelper.inventory = inventory
@lru_cache(maxsize=1)
def get_auth(self):
host = testinfra.get_host(f'ansible://{IcingaHelper.icinga_host}',
ansible_inventory=self.inventory)
@ -51,46 +54,78 @@ class IcingaHelper(object):
r.raise_for_status()
return (session, address)
@lru_cache(maxsize=1)
def get_address(self):
if get_driver() == 'openstack':
vars_dir = f'{self.inventory}/group_vars/all'
return 'icinga.' + yaml.load(
host = 'icinga.' + yaml.load(
open(vars_dir + '/domain.yml'))['domain']
else:
host = testinfra.get_host('ansible://icinga-service-group',
ansible_inventory=self.inventory)
return host.ansible.get_variables()['ansible_host']
def get_client(self):
(user, password) = self.get_auth()
client = Client(
'https://{}:5665'.format(self.get_address()),
user, password,
ca_certificate=False,
timeout=5
host = host.ansible.get_variables()['ansible_host']
return host
def get_api_url(self):
host = self.get_address()
return f'https://{host}:5665/v1'
def connection_params(self):
return dict(
headers={'Accept': 'application/json'},
auth=self.get_auth(),
timeout=5,
verify=False,
)
return client
@retry.retry(AssertionError, tries=7)
def wait_for_service(self, client, name):
answer = client.objects.get('Service', name)
state = answer.get('attrs', {}).get('state')
assert int(state) == 0, f"{name}: unexpected state ({state})"
def get_hosts(self, host=None):
url = self.get_api_url()
if host:
url += f'/objects/hosts/f{host}?attrs=name&attrs=state'
else:
url += f'/objects/hosts?attrs=name&attrs=state'
answer = requests.get(url, **self.connection_params())
answer.raise_for_status()
results = answer.json().get('results', [])
if host:
assert len(results) == 1
return {x['attrs']['name']: x['attrs']['state'] for x in results}
@retry.retry((AssertionError, requests.exceptions.HTTPError), tries=7)
def wait_for_service(self, name):
self.schedule_check(name)
name = urllib.parse.quote(name)
url = f'{self.get_api_url()}/objects/services/{name}'
answer = requests.get(url, **self.connection_params())
answer.raise_for_status()
results = answer.json().get('results', [])
assert len(results) == 1
attrs = results[0]['attrs']
assert attrs['last_check'] > 0, 'f{name} has not been checked yet'
state = attrs['state']
assert int(state) == 0, f"{name}: unexpected state ({state}: " \
"{results[0]['attrs']})"
return True
def is_service_ok(self, name):
#
# force the check to reduce the waiting time
#
client = self.get_client()
# For debug purposes
# answer = client.objects.list('Service')
# print(answer)
answer = client.actions.reschedule_check(
'Service',
'service.__name=="{}"'.format(name),
)
assert len(answer['results']) == 1
assert int(answer['results'][0]['code']) == 200
def schedule_check(self, name):
"""force the check to reduce the waiting time"""
data = {
'type': 'Service',
'filter': f'service.__name=="{name}"',
'force': True,
}
url = f'{self.get_api_url()}/actions/reschedule-check'
answer = requests.post(url, data=json.dumps(data),
**self.connection_params())
answer.raise_for_status()
results = answer.json().get('results', [])
assert len(results) == 1
assert int(results[0]['code']) == 200
return self.wait_for_service(client, name)
def is_service_ok(self, name):
return self.wait_for_service(name)

Loading…
Cancel
Save