Skip to content

Commit

Permalink
alerts
Browse files Browse the repository at this point in the history
  • Loading branch information
joente committed Nov 6, 2024
1 parent 81f025a commit 0e15a23
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 11 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,13 @@ Create a yaml file, for example _(test.yaml)_:
```yaml
asset:
name: "foo.local"
check: "oraclezfs"
check: "zfs"
config:
address: "192.168.1.2"
secure: True
port: 215
version: 'v2'
hours: 24
```

Run the probe with the `DRY_RUN` environment variable set the the yaml file above.
Expand Down
11 changes: 5 additions & 6 deletions lib/check/zfs.py → lib/check/alerts.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import logging
from libprobe.asset import Asset
from ..utils import get_token
from ..utils import get_token, get_logs_alert


async def check_zfs(
async def check_alerts(
asset: Asset,
asset_config: dict,
check_config: dict) -> dict:
token = await get_token(asset, asset_config, check_config)

...

return {}
state = await get_logs_alert(asset, check_config, token)
return state
61 changes: 61 additions & 0 deletions lib/utils.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import aiohttp
import time
import logging
from dateutil import parser
from datetime import datetime, timedelta
from libprobe.asset import Asset
from libprobe.exceptions import IncompleteResultException


DEFAULT_API_VERSION = 'v2' # v1 or v2
DEFAULT_SECURE = True
DEFAULT_PORT = 215
DEFAULT_ALERT_HOURS = 24 # return alers from the past 24 hours

# MAX_TOKEN_AGE is usually 15 minutes, we choose 12 minutes to be safe
MAX_TOKEN_AGE = 720
Expand Down Expand Up @@ -57,3 +62,59 @@ async def get_token(
_tokens[asset.id] = time.time(), token

return token


async def get_logs_alert(asset: Asset, check_config: dict, token: str):
address = check_config.get('address')
if not address:
address = asset.name
headers = {'X-Auth-Session': token}
api_version = check_config.get('version', DEFAULT_API_VERSION)
secure = check_config.get('secure', DEFAULT_SECURE)
port = check_config.get('port', DEFAULT_PORT)
alert_hours = check_config.get('hours', DEFAULT_ALERT_HOURS)

start = (datetime.utcnow() - timedelta(hours=alert_hours))
index = start.isoformat(sep='T', timespec='seconds') + 'Z'

protocol = 'https' if secure else 'http'
url = (
f'{protocol}://{address}:{port}'
f'/api/log/{api_version}/logs/alert?start={index}'
)

logging.info(f'GET {url}')

async with aiohttp.ClientSession() as session:
async with session.get(url, headers=headers, ssl=False) as resp:
assert resp.status // 100 == 2, \
f'response status code: {resp.status}. reason: {resp.reason}.'
data = await resp.json()

uuids = set() # TODO: remove if 100% sure uuids are unique
logs = []
state = {}

if data:
for log in data['logs']:
log['timestamp'] = int(parser.parse(log['timestamp']).timestamp())
uuid = log.pop('uuid')
log['name'] = uuid
if uuid in uuids:
logging.error('UUID not unique')
else:
logs.append(log)
uuids.add(uuid)

state['logs'] = logs
state['records'] = [{
'name': 'count',
'count': len(logs),
'since': int(start.timestamp()),
'hours': alert_hours,
}]

if len(logs) != len(uuids):
raise IncompleteResultException('UUIDs are not unique', result=state)

return state
2 changes: 1 addition & 1 deletion lib/version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Version string. Examples:
# '3.0.0'
# '3.0.0-alpha0'
__version__ = '3.0.0-alpha0'
__version__ = '3.0.0-alpha1'
5 changes: 2 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
from libprobe.probe import Probe
from lib.check.zfs import check_zfs
from lib.check.alerts import check_alerts
from lib.version import __version__ as version


if __name__ == '__main__':
checks = {
'zfs': check_zfs
'alerts': check_alerts
}

probe = Probe("oraclezfs", version, checks)

probe.start()

0 comments on commit 0e15a23

Please sign in to comment.