163 lines
7.5 KiB
Python
163 lines
7.5 KiB
Python
import pytest
|
|||
|
|
|
||
|
|
from thor.checks import NeedsSource, counter_rises, query_regex
|
||
|
|
from thor.checks.memory import MemoryCheck, kernel_query, parse_kills
|
||
|
|
from thor.checks.symptoms import CephAlertCheck, PvestatdCheck, ScrapeCheck
|
||
|
|
from thor.clients import SourceError
|
||
|
|
from thor.hosts import HostRegistry
|
||
|
|
from thor.model import CAUSE, NOTE, Incident
|
||
|
|
from thor.monitor import CycleContext
|
||
|
|
|
||
|
|
GLOBAL = ('oom-kill:constraint=CONSTRAINT_NONE,nodemask=(null),cpuset=ceph-osd@3.service,mems_allowed=0,'
|
||
|
|
'global_oom,task_memcg=/lxc/154/ns/system.slice/docker-a.scope,task=java,pid=711,uid=100999')
|
||
|
|
KILLED = ('Out of memory: Killed process 711 (java) total-vm:295326748kB, anon-rss:2351856kB, file-rss:8kB, '
|
||
|
|
'shmem-rss:16544832kB, UID:100999 pgtables:38172kB oom_score_adj:0')
|
||
|
|
MEMCG = ('oom-kill:constraint=CONSTRAINT_MEMCG,nodemask=(null),cpuset=/,mems_allowed=0,oom_memcg=/lxc/120,'
|
||
|
|
'task_memcg=/lxc/120/ns/x,task=node,pid=55,uid=0')
|
||
|
|
|
||
|
|
|
||
|
|
def test_counter_rises_and_resets():
|
||
|
|
vals = [(0, 2), (30, 2), (60, 3), (90, 3), (120, 5), (150, 1)]
|
||
|
|
assert counter_rises(vals, after=0) == [(60, 1), (120, 2), (150, 1)]
|
||
|
|
assert counter_rises(vals, after=100) == [(120, 2), (150, 1)]
|
||
|
|
|
||
|
|
|
||
|
|
def test_query_regex_escapes_for_promql_strings():
|
||
|
|
assert query_regex(['10.10.10.4', 'a-b']) == '10\\\\.10\\\\.10\\\\.4|a\\\\-b'
|
||
|
|
assert kernel_query(['compute-storage-01']).startswith('{job="journal", host=~"compute\\\\-storage\\\\-01"')
|
||
|
|
|
||
|
|
|
||
|
|
def test_parse_kills_pairs_lines_and_reads_everything():
|
||
|
|
kills = parse_kills([(10.0, GLOBAL, {}), (10.1, KILLED, {}), (20.0, MEMCG, {}),
|
||
|
|
(30.0, 'Out of memory: Killed process 99 (x) anon-rss:1kB', {})])
|
||
|
|
assert len(kills) == 3
|
||
|
|
g = kills[0]
|
||
|
|
assert g['global'] and g['guest'] == '154' and g['task'] == 'java' and g['pid'] == '711'
|
||
|
|
assert g['cpuset'] == 'ceph-osd@3.service'
|
||
|
|
assert g['shmem_rss_kb'] == 16544832 and g['anon_rss_kb'] == 2351856
|
||
|
|
assert not kills[1]['global'] and kills[1]['guest'] == '120'
|
||
|
|
assert kills[2]['pid'] == '99' and kills[2]['global'] # oom-kill line lost; Killed line kept
|
||
|
|
|
||
|
|
|
||
|
|
class FakeProm:
|
||
|
|
def __init__(self, series):
|
||
|
|
self.series = series
|
||
|
|
|
||
|
|
def query_range(self, expr, start, end, step):
|
||
|
|
if isinstance(self.series, Exception):
|
||
|
|
raise self.series
|
||
|
|
return [{'metric': s['metric'], 'values': [(t, v) for t, v in s['values'] if start <= t <= end]}
|
||
|
|
for s in self.series.get(expr.split('{')[0], [])]
|
||
|
|
|
||
|
|
|
||
|
|
class FakeLoki:
|
||
|
|
def __init__(self, lines):
|
||
|
|
self._lines = lines
|
||
|
|
|
||
|
|
def lines(self, logql, start, end, limit=100, direction='backward'):
|
||
|
|
if isinstance(self._lines, Exception):
|
||
|
|
raise self._lines
|
||
|
|
return [x for x in self._lines if start <= x[0] <= end]
|
||
|
|
|
||
|
|
|
||
|
|
class FakePx:
|
||
|
|
def resources(self, kind=None):
|
||
|
|
return [{'vmid': 154, 'name': 'pelican-wings-02'}]
|
||
|
|
|
||
|
|
|
||
|
|
def ctx(prom=None, loki=None, up=None, start=0.0, end=300.0):
|
||
|
|
up = up or {'prometheus': True, 'loki': True, 'proxmox': True}
|
||
|
|
return CycleContext(start, end, False, {'prometheus': prom, 'loki': loki, 'proxmox': FakePx()}, up,
|
||
|
|
HostRegistry())
|
||
|
|
|
||
|
|
|
||
|
|
OOM_SERIES = {'node_vmstat_oom_kill': [
|
||
|
|
{'metric': {'instance': '10.10.10.4:9100'}, 'values': [(0, 2), (30, 2), (60, 3), (90, 4)]},
|
||
|
|
{'metric': {'instance': '10.10.10.29:9100'}, 'values': [(0, 0), (60, 9)]}, # a guest: ignored
|
||
|
|
]}
|
||
|
|
|
||
|
|
|
||
|
|
def test_memory_check_global_and_cgroup():
|
||
|
|
lines = [(55.0, GLOBAL, {}), (55.1, KILLED, {}), (85.0, MEMCG, {})]
|
||
|
|
found = MemoryCheck().run(ctx(FakeProm(OOM_SERIES), FakeLoki(lines)))
|
||
|
|
roles = {f.role: f for f in found}
|
||
|
|
assert set(roles) == {CAUSE, NOTE}
|
||
|
|
c = roles[CAUSE]
|
||
|
|
assert c.key == 'host:compute-storage-01:oom' and len(c.events) == 1
|
||
|
|
assert c.events[0]['ceph'] and 'CT 154 pelican-wings-02' in c.events[0]['text']
|
||
|
|
assert roles[NOTE].key == 'host:compute-storage-01:oom_cgroup'
|
||
|
|
|
||
|
|
|
||
|
|
def test_memory_check_counts_kills_whose_lines_never_reached_loki():
|
||
|
|
found = MemoryCheck().run(ctx(FakeProm(OOM_SERIES), FakeLoki([])))
|
||
|
|
assert len(found) == 1 and len(found[0].events) == 2
|
||
|
|
assert 'never reached Loki' in found[0].events[0]['text']
|
||
|
|
|
||
|
|
|
||
|
|
def test_memory_check_needs_sources():
|
||
|
|
with pytest.raises(NeedsSource):
|
||
|
|
MemoryCheck().run(ctx(up={'prometheus': False}))
|
||
|
|
with pytest.raises(NeedsSource):
|
||
|
|
MemoryCheck().run(ctx(FakeProm(SourceError('prometheus', 'x')), None))
|
||
|
|
with pytest.raises(NeedsSource, match='loki'):
|
||
|
|
MemoryCheck().run(ctx(FakeProm(OOM_SERIES), None, up={'prometheus': True, 'loki': False}))
|
||
|
|
with pytest.raises(NeedsSource, match='loki'):
|
||
|
|
MemoryCheck().run(ctx(FakeProm(OOM_SERIES), FakeLoki(SourceError('loki', '500'))))
|
||
|
|
assert MemoryCheck().run(ctx(FakeProm({}), None)) == []
|
||
|
|
|
||
|
|
|
||
|
|
def test_memory_rules_summary_and_priority():
|
||
|
|
m = MemoryCheck()
|
||
|
|
inc = Incident(key='k', kind='oom', subject='compute-storage-01', layer=40, check='memory',
|
||
|
|
facts={'guest_names': {'154': 'pelican-wings-02'}},
|
||
|
|
events=[{'at': 0, 'guest': '154', 'task': 'java'}])
|
||
|
|
assert m.summarize(inc) == 'Out of memory: CT 154 pelican-wings-02 java OOM-killed 1 time'
|
||
|
|
assert m.priority(inc) == 3
|
||
|
|
inc.events += [{'at': 600, 'guest': '154'}, {'at': 1200, 'guest': '154'}]
|
||
|
|
assert m.priority(inc) == 2
|
||
|
|
inc.symptoms = {'s': {'subject': 'storage-01', 'cluster_effect': True}}
|
||
|
|
assert m.priority(inc) == 1
|
||
|
|
inc.symptoms = {}
|
||
|
|
inc.events.append({'at': 1300, 'guest': '155', 'ceph': True})
|
||
|
|
assert m.priority(inc) == 1
|
||
|
|
assert m.summarize(inc) == 'Out of memory: 4 OOM kills on compute-storage-01'
|
||
|
|
|
||
|
|
|
||
|
|
def test_scrape_check():
|
||
|
|
series = {'up': [{'metric': {'instance': '10.10.10.2:9100'}, 'values': [(30, 1), (60, 0)]}],
|
||
|
|
'scrape_duration_seconds': [{'metric': {'instance': '10.10.10.11:9100'}, 'values': [(30, 7.5)]},
|
||
|
|
{'metric': {'instance': '10.10.10.8:9100'}, 'values': [(30, 0.1)]}]}
|
||
|
|
found = {f.subject: f for f in ScrapeCheck().run(ctx(FakeProm(series)))}
|
||
|
|
assert set(found) == {'compute-storage-02', 'storage-01'}
|
||
|
|
assert 'timed out' in found['compute-storage-02'].summary
|
||
|
|
assert 'up to 7.5 s' in found['storage-01'].summary
|
||
|
|
assert found['storage-01'].cluster_effect
|
||
|
|
|
||
|
|
|
||
|
|
def test_pvestatd_check_maps_old_host_names():
|
||
|
|
lines = [(10.0, 'got timeout', {'host': 'micro1'}), (11.0, 'got timeout', {'host': 'micro1'}),
|
||
|
|
(12.0, 'got timeout', {'host': 'someone-else'})]
|
||
|
|
found = PvestatdCheck().run(ctx(loki=FakeLoki(lines)))
|
||
|
|
assert len(found) == 1 and found[0].subject == 'monitor-01' and '(2x)' in found[0].summary
|
||
|
|
|
||
|
|
|
||
|
|
def test_ceph_alert_check_only_alerts_that_switch_on():
|
||
|
|
series = {'ceph_health_detail': [
|
||
|
|
{'metric': {'name': 'BLUESTORE_SLOW_OP_ALERT'}, 'values': [(0, 1), (60, 1)]}, # standing
|
||
|
|
{'metric': {'name': 'OSD_SLOW_PING_TIME_BACK'}, 'values': [(0, 0), (60, 1), (90, 0)]},
|
||
|
|
]}
|
||
|
|
found = CephAlertCheck().run(ctx(FakeProm(series), start=30))
|
||
|
|
assert [f.key for f in found] == ['cluster:ceph_alert:OSD_SLOW_PING_TIME_BACK']
|
||
|
|
assert found[0].start == 60 and found[0].subject == 'cluster'
|
||
|
|
|
||
|
|
|
||
|
|
def test_symptom_checks_need_their_source():
|
||
|
|
for check, up in ((ScrapeCheck(), {'prometheus': False}), (PvestatdCheck(), {'loki': False}),
|
||
|
|
(CephAlertCheck(), {'prometheus': False})):
|
||
|
|
with pytest.raises(NeedsSource):
|
||
|
|
check.run(ctx(up=up))
|
||
|
|
for check in (ScrapeCheck(), CephAlertCheck()):
|
||
|
|
with pytest.raises(NeedsSource):
|
||
|
|
check.run(ctx(FakeProm(SourceError('prometheus', 'x'))))
|
||
|
|
with pytest.raises(NeedsSource):
|
||
|
|
PvestatdCheck().run(ctx(loki=FakeLoki(SourceError('loki', 'x'))))
|