def test_recon_fail(self): mock_json = Mock() mock_json.side_effect = ValueError('error') mock_server_type = Mock(spec=ServerType) mock_server_type.name = 'servertype' mock_server_type.is_instance = True with patch(BUILTIN_OPEN): with patch('json.load', mock_json): actual = replication._recon_check(mock_server_type) self.assertIsInstance(actual, list) self.assertEqual(len(actual), 2) actual = [m.metric() for m in actual] expected1 = replication.BASE_RESULT.child( 'servertype.last_replication') expected1.value = 0 self.assertIn(expected1.metric(), actual) actual.remove(expected1.metric()) expected2 = CheckFailure.child(dimensions={ 'check': expected1.name, 'error': 'error', }) expected2.value = Severity.fail pprint.pprint(expected2.metric()) pprint.pprint(actual) self.assertIn(expected2.metric(), actual) actual.remove(expected2.metric())
def check_details(): """ Parses ntp data in the form: remote refid st t when poll reach delay offset jitter =========================================================================== bindcat.fhsu.ed .INIT. 16 u - 1024 0 0.000 0.000 0.000 origin.towfowi. .INIT. 16 u - 1024 0 0.000 0.000 0.000 time-b.nist.gov .INIT. 16 u - 1024 0 0.000 0.000 0.000 services.quadra .INIT. 16 u - 1024 0 0.000 0.000 0.000 associd=0 status=c011 leap_alarm, sync_unspec, 1 event, freq_not_set, version="ntpd [email protected] Fri Apr 10 19:04:04 UTC 2015 (1)", processor="x86_64", system="Linux/3.14.44-1-amd64-hlinux", leap=11, stratum=16, precision=-23, rootdelay=0.000, rootdisp=26.340, refid=INIT, reftime=00000000.00000000 Mon, Jan 1 1900 0:00:00.000, clock=d94f932a.13f33874 Tue, Jul 14 2015 13:54:50.077, peer=0, tc=3, mintc=3, offset=0.000, frequency=0.000, sys_jitter=0.000, clk_jitter=0.000, clk_wander=0.000 """ results = [] cmd_result = run_cmd('ntpq -pcrv') if cmd_result.exitcode != 0: failed = CheckFailure.child(dimensions={ 'check': BASE_RESULT.name, 'error': cmd_result.output, }) failed.value = Severity.fail return [failed] results.append(check_ntpq_fact(cmd_result, 'stratum')) results.append(check_ntpq_fact(cmd_result, 'offset')) return results
def check_ntpq_fact(cmd_result, fact_name): fact_result = BASE_RESULT.child(fact_name) # This regex will pick out the value after a fact. e.g # stratum=16, # will match and the value '16' will be stored in 'match.groups()[0]'. # If output does not match the regex 'match' will be None. fact_regex = re.compile(fact_name + '=(.*?),') match = fact_regex.search(cmd_result.output) if match is None: failed = CheckFailure.child( dimensions={ 'check': fact_result.name, 'error': 'Output does not contain "%s"' % fact_name, }) failed.value = Severity.fail return failed else: fact_level = match.groups()[0] fact_result.value = fact_level return fact_result
def check_ntpq_fact(cmd_result, fact_name): fact_result = BASE_RESULT.child(fact_name) # This regex will pick out the value after a fact. e.g # stratum=16, # will match and the value '16' will be stored in 'match.groups()[0]'. # If output does not match the regex 'match' will be None. fact_regex = re.compile(fact_name+'=(.*?),') match = fact_regex.search(cmd_result.output) if match is None: failed = CheckFailure.child( dimensions={ 'check': fact_result.name, 'error': 'Output does not contain "%s"' % fact_name, } ) failed.value = Severity.fail return failed else: fact_level = match.groups()[0] fact_result.value = fact_level return fact_result
def check_details(): """ Parses ntp data in the form: remote refid st t when poll reach delay offset jitter =========================================================================== bindcat.fhsu.ed .INIT. 16 u - 1024 0 0.000 0.000 0.000 origin.towfowi. .INIT. 16 u - 1024 0 0.000 0.000 0.000 time-b.nist.gov .INIT. 16 u - 1024 0 0.000 0.000 0.000 services.quadra .INIT. 16 u - 1024 0 0.000 0.000 0.000 associd=0 status=c011 leap_alarm, sync_unspec, 1 event, freq_not_set, version="ntpd [email protected] Fri Apr 10 19:04:04 UTC 2015 (1)", processor="x86_64", system="Linux/3.14.44-1-amd64-hlinux", leap=11, stratum=16, precision=-23, rootdelay=0.000, rootdisp=26.340, refid=INIT, reftime=00000000.00000000 Mon, Jan 1 1900 0:00:00.000, clock=d94f932a.13f33874 Tue, Jul 14 2015 13:54:50.077, peer=0, tc=3, mintc=3, offset=0.000, frequency=0.000, sys_jitter=0.000, clk_jitter=0.000, clk_wander=0.000 """ results = [] cmd_result = run_cmd('ntpq -pcrv') if cmd_result.exitcode != 0: failed = CheckFailure.child( dimensions={ 'check': BASE_RESULT.name, 'error': cmd_result.output, } ) failed.value = Severity.fail return [failed] results.append(check_ntpq_fact(cmd_result, 'stratum')) results.append(check_ntpq_fact(cmd_result, 'offset')) return results
def _recon_check(st): """ Parses the blah.recon file and returns the last replication. :param st: ServerType, Used to determine the metric names and recon file name. :param replication_field_name: string, name of the field in the json file that hold the last replication data. """ results = [] if not st.is_instance: return results r = BASE_RESULT.child(name=st.name + '.last_replication') recon_file = st.name + '.recon' try: with open(RECON_PATH + recon_file) as f: j = json.load(f) last_replication = j.get('replication_last') if last_replication is None: last_replication = j.get('object_replication_last') last_replication = int(last_replication) last_replication = timestamp() - last_replication except (ValueError, IOError) as e: c = CheckFailure.child(dimensions={ 'check': r.name, 'error': str(e) }) c.value = Severity.fail results.append(c) last_replication = 0 r.value = last_replication results.append(r) return results
def test_details_fail(self): mock_command = Mock() mock_command.return_value = CommandResult(0, 'stratum=1,') with patch('swiftlm.systems.ntp.run_cmd', mock_command): with patch('swiftlm.systems.ntp.check_status', lambda: []): actual = ntp.main() self.assertIsInstance(actual, list) self.assertEqual(len(actual), 2) actual = [a.metric() for a in actual] failed = CheckFailure.child() failed.value = Severity.fail failed['check'] = ntp.__name__ + '.offset' failed['error'] = 'Output does not contain "offset"' expected = [ failed, MetricData.single(ntp.__name__+'.stratum', '1', ''), ] for e in expected: self.assertIn(e.metric(), actual)