from hotsos.core.issues import IssuesManager, NetworkWarning from hotsos.core.ycheck.engine import CallbackHelper from hotsos.core.host_helpers import CLIHelper, HostNetworkingHelper from hotsos.core.plugins.kernel import KernelEventChecksBase from hotsos.core.searchtools import FileSearcher EVENTCALLBACKS = CallbackHelper() class KernelLogEventChecks(KernelEventChecksBase): def __init__(self): super().__init__(yaml_defs_group='kernlog', searchobj=FileSearcher(), callback_helper=EVENTCALLBACKS) self.cli_helper = CLIHelper() self.hostnet_helper = HostNetworkingHelper() @EVENTCALLBACKS.callback() def over_mtu_dropped_packets(self, event): interfaces = {} for r in event.results: if r.get(1) in interfaces: interfaces[r.get(1)] += 1 else: interfaces[r.get(1)] = 1 if interfaces: # only report on interfaces that currently exist host_interfaces = [ iface.name for iface in self.hostnet_helper.host_interfaces_all ]
def test_yaml_def_entry_seq(self): with tempfile.TemporaryDirectory() as dtmp: setup_config(DATA_ROOT=dtmp) data_file = os.path.join(dtmp, 'data.txt') _yaml = YAML_DEF_EXPR_TYPES.format( path=os.path.basename(data_file)) open(os.path.join(dtmp, 'events.yaml'), 'w').write(_yaml) open(data_file, 'w').write('hello\nbrave\nworld\n') plugin_checks = yaml.safe_load(_yaml).get('myplugin') for name, group in plugin_checks.items(): group = YDefsSection(name, group) for entry in group.leaf_sections: self.assertEqual(entry.input.path, '{}*'.format(data_file)) test_self = self match_count = {'count': 0} callbacks_called = {} setup_config(PLUGIN_YAML_DEFS=dtmp, PLUGIN_NAME='myplugin') EVENTCALLBACKS = CallbackHelper() class MyEventHandler(events.YEventCheckerBase): def __init__(self): super().__init__(yaml_defs_group='mygroup', searchobj=FileSearcher(), callback_helper=EVENTCALLBACKS) @EVENTCALLBACKS.callback() def my_sequence_search(self, event): callbacks_called[event.name] = True for section in event.results: for result in section: if result.tag.endswith('-start'): match_count['count'] += 1 test_self.assertEqual(result.get(0), 'hello') elif result.tag.endswith('-body'): match_count['count'] += 1 test_self.assertEqual(result.get(0), 'brave') elif result.tag.endswith('-end'): match_count['count'] += 1 test_self.assertEqual(result.get(0), 'world') @EVENTCALLBACKS.callback() def my_standard_search(self, event): # expected to be passthough results (i.e. raw) callbacks_called[event.name] = True tag = '{}.my-standard-search-start'.format(event.section) start_results = event.results.find_by_tag(tag) test_self.assertEqual(start_results[0].get(0), 'hello') @EVENTCALLBACKS.callback('my-standard-search2', 'my-standard-search3') def my_standard_search_common(self, event): callbacks_called[event.name] = True test_self.assertEqual(event.results[0].get(0), 'hello') def __call__(self): self.run_checks() MyEventHandler()() self.assertEqual(match_count['count'], 3) self.assertEqual(list(callbacks_called.keys()), [ 'my-sequence-search', 'my-standard-search', 'my-standard-search2' ])