Changeset View
Changeset View
Standalone View
Standalone View
swh/icinga_plugins/deposit.py
# Copyright (C) 2019-2020 The Software Heritage developers | # Copyright (C) 2019-2020 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 datetime | import datetime | ||||
import sys | import sys | ||||
import time | import time | ||||
from typing import Any, Dict, Optional | from typing import Any, Dict, Optional | ||||
import requests | |||||
from swh.deposit.client import PublicApiDepositClient | from swh.deposit.client import PublicApiDepositClient | ||||
from .base_check import BaseCheck | from .base_check import BaseCheck | ||||
class DepositCheck(BaseCheck): | class DepositCheck(BaseCheck): | ||||
TYPE = "DEPOSIT" | TYPE = "DEPOSIT" | ||||
DEFAULT_WARNING_THRESHOLD = 120 | DEFAULT_WARNING_THRESHOLD = 120 | ||||
DEFAULT_CRITICAL_THRESHOLD = 3600 | DEFAULT_CRITICAL_THRESHOLD = 3600 | ||||
def __init__(self, obj): | def __init__(self, obj): | ||||
super().__init__(obj) | super().__init__(obj) | ||||
self.api_url = obj["swh_web_url"].rstrip("/") | |||||
self._poll_interval = obj["poll_interval"] | self._poll_interval = obj["poll_interval"] | ||||
self._archive_path = obj["archive"] | self._archive_path = obj["archive"] | ||||
self._metadata_path = obj["metadata"] | self._metadata_path = obj["metadata"] | ||||
self._collection = obj["collection"] | self._collection = obj["collection"] | ||||
self._slug: Optional[str] = None | self._slug: Optional[str] = None | ||||
self._provider_url = obj["provider_url"] | |||||
self._client = PublicApiDepositClient( | self._client = PublicApiDepositClient( | ||||
{ | { | ||||
"url": obj["server"], | "url": obj["server"], | ||||
"auth": {"username": obj["username"], "password": obj["password"]}, | "auth": {"username": obj["username"], "password": obj["password"]}, | ||||
} | } | ||||
) | ) | ||||
def upload_deposit(self): | def upload_deposit(self): | ||||
slug = "check-deposit-%s" % datetime.datetime.now().isoformat() | slug = ( | ||||
"check-deposit-%s" | |||||
% datetime.datetime.fromtimestamp(time.time()).isoformat() | |||||
) | |||||
result = self._client.deposit_create( | result = self._client.deposit_create( | ||||
archive=self._archive_path, | archive=self._archive_path, | ||||
metadata=self._metadata_path, | metadata=self._metadata_path, | ||||
collection=self._collection, | collection=self._collection, | ||||
in_progress=False, | in_progress=False, | ||||
slug=slug, | slug=slug, | ||||
) | ) | ||||
self._slug = slug | self._slug = slug | ||||
▲ Show 20 Lines • Show All 87 Lines • ▼ Show 20 Lines | def main(self): | ||||
self.print_result( | self.print_result( | ||||
"CRITICAL", | "CRITICAL", | ||||
f'Deposit got unexpected status: {result["deposit_status"]} ' | f'Deposit got unexpected status: {result["deposit_status"]} ' | ||||
f'({result["deposit_status_detail"]})', | f'({result["deposit_status_detail"]})', | ||||
**metrics, | **metrics, | ||||
) | ) | ||||
return 2 | return 2 | ||||
# Get the SWHID | |||||
if "deposit_swh_id" not in result: | |||||
# if the deposit succeeded immediately (which is rare), it does not | |||||
# contain the SWHID, so we need to re-fetch its status. | |||||
result = self.get_deposit_status() | |||||
if result.get("deposit_swh_id") is None: | |||||
self.print_result( | |||||
"CRITICAL", | |||||
f"'deposit_swh_id' missing from result: {result!r}", | |||||
**metrics, | |||||
) | |||||
return 2 | |||||
swhid = result["deposit_swh_id"] | |||||
# Check for unexpected status | |||||
if result["deposit_status"] != "done": | |||||
self.print_result( | |||||
"CRITICAL", | |||||
f'Deposit status went from "done" to: {result["deposit_status"]} ' | |||||
f'({result["deposit_status_detail"]})', | |||||
**metrics, | |||||
) | |||||
return 2 | |||||
# Get metadata from swh-web | |||||
metadata_objects = requests.get( | |||||
f"{self.api_url}/api/1/raw-extrinsic-metadata/swhid/{swhid}/" | |||||
f"?authority=deposit_client%20{self._provider_url}" | |||||
).json() | |||||
expected_origin = f"{self._provider_url}/{self._slug}" | |||||
origins = [d.get("origin") for d in metadata_objects] | |||||
if expected_origin not in origins: | |||||
self.print_result( | |||||
"CRITICAL", | |||||
f"Deposited metadata on {swhid} with origin {expected_origin}, " | |||||
f"missing from the list of origins: {origins!r}", | |||||
**metrics, | |||||
) | |||||
return 2 | |||||
# Everything went fine, check total time wasn't too large and | # Everything went fine, check total time wasn't too large and | ||||
# print result | # print result | ||||
(status_code, status) = self.get_status(metrics["total_time"]) | (status_code, status) = self.get_status(metrics["total_time"]) | ||||
self.print_result( | self.print_result( | ||||
status, | status, | ||||
f'Deposit took {metrics["total_time"]:.2f}s and succeeded.', | f'Deposit took {metrics["total_time"]:.2f}s and succeeded.', | ||||
**metrics, | **metrics, | ||||
) | ) | ||||
Show All 33 Lines |