Changeset View
Changeset View
Standalone View
Standalone View
swh/icinga_plugins/vault.py
# Copyright (C) 2019 The Software Heritage developers | # Copyright (C) 2019 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import time | import time | ||||
import requests | import requests | ||||
from swh.storage import get_storage | from swh.storage import get_storage | ||||
from .base_check import BaseCheck | |||||
class NoDirectory(Exception): | class NoDirectory(Exception): | ||||
pass | pass | ||||
class VaultCheck: | class VaultCheck(BaseCheck): | ||||
DEFAULT_WARNING_THRESHOLD = 0 | |||||
DEFAULT_CRITICAL_THRESHOLD = 3600 | |||||
def __init__(self, obj): | def __init__(self, obj): | ||||
super().__init__(obj) | |||||
self._swh_storage = get_storage('remote', url=obj['swh_storage_url']) | self._swh_storage = get_storage('remote', url=obj['swh_storage_url']) | ||||
self._swh_web_url = obj['swh_web_url'] | self._swh_web_url = obj['swh_web_url'] | ||||
self._poll_interval = obj['poll_interval'] | self._poll_interval = obj['poll_interval'] | ||||
def _url_for_dir(self, dir_id): | def _url_for_dir(self, dir_id): | ||||
return self._swh_web_url + f'/api/1/vault/directory/{dir_id.hex()}/' | return self._swh_web_url + f'/api/1/vault/directory/{dir_id.hex()}/' | ||||
def _pick_directory(self): | def _pick_directory(self): | ||||
Show All 12 Lines | class VaultCheck(BaseCheck): | ||||
def main(self): | def main(self): | ||||
try: | try: | ||||
dir_id = self._pick_uncached_directory() | dir_id = self._pick_uncached_directory() | ||||
except NoDirectory: | except NoDirectory: | ||||
print('VAULT CRITICAL - No directory exists in the archive') | print('VAULT CRITICAL - No directory exists in the archive') | ||||
return 2 | return 2 | ||||
start_time = time.time() | start_time = time.time() | ||||
total_time = 0 | |||||
response = requests.post(self._url_for_dir(dir_id)) | response = requests.post(self._url_for_dir(dir_id)) | ||||
assert response.status_code == 200, (response, response.text) | assert response.status_code == 200, (response, response.text) | ||||
result = response.json() | result = response.json() | ||||
while result['status'] in ('new', 'pending'): | while result['status'] in ('new', 'pending'): | ||||
time.sleep(self._poll_interval) | time.sleep(self._poll_interval) | ||||
response = requests.get(self._url_for_dir(dir_id)) | response = requests.get(self._url_for_dir(dir_id)) | ||||
assert response.status_code == 200, (response, response.text) | assert response.status_code == 200, (response, response.text) | ||||
result = response.json() | result = response.json() | ||||
end_time = time.time() | total_time = time.time() - start_time | ||||
total_time = end_time - start_time | |||||
if total_time > self.critical_threshold: | |||||
print(f'VAULT CRITICAL - cooking directory {dir_id.hex()} ' | |||||
f'took more than {total_time:.2f}s and has status: ' | |||||
f'{result["progress_message"]}') | |||||
print(f"| 'total time' = {total_time:.2f}s") | |||||
return 2 | |||||
if result['status'] == 'done': | if result['status'] == 'done': | ||||
print(f'VAULT OK - cooking directory {dir_id.hex()} ' | (status_code, status) = self.get_status(total_time) | ||||
print(f'VAULT {status} - cooking directory {dir_id.hex()} ' | |||||
f'took {total_time:.2f}s and succeeded.') | f'took {total_time:.2f}s and succeeded.') | ||||
print(f"| 'total time' = {total_time:.2f}s") | print(f"| 'total time' = {total_time:.2f}s") | ||||
return 0 | return status_code | ||||
elif result['status'] == 'failed': | elif result['status'] == 'failed': | ||||
print(f'VAULT CRITICAL - cooking directory {dir_id.hex()} ' | print(f'VAULT CRITICAL - cooking directory {dir_id.hex()} ' | ||||
f'took {total_time:.2f}s and failed with: ' | f'took {total_time:.2f}s and failed with: ' | ||||
f'{result["progress_message"]}') | f'{result["progress_message"]}') | ||||
print(f"| 'total time' = {total_time:.2f}s") | print(f"| 'total time' = {total_time:.2f}s") | ||||
return 3 | return 2 | ||||
else: | else: | ||||
print(f'VAULT CRITICAL - cooking directory {dir_id.hex()} ' | print(f'VAULT CRITICAL - cooking directory {dir_id.hex()} ' | ||||
f'took {total_time:.2f}s and resulted in unknown: ' | f'took {total_time:.2f}s and resulted in unknown: ' | ||||
f'status: {result["status"]}') | f'status: {result["status"]}') | ||||
print(f"| 'total time' = {total_time:.2f}s") | print(f"| 'total time' = {total_time:.2f}s") | ||||
return 3 | return 2 |