def test_tallytotype(self): for t in TestNTPPeers.types: self.assertEqual(NTPPeers.tallytotype(TestNTPPeers.types[t]), t) for i in ' .x-#+*o': self.assertNotEqual(NTPPeers.tallytotype(i), 'unknown') for i in '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnpqrstuvwyz~!@$%^&()_': self.assertEqual(NTPPeers.tallytotype(i), 'unknown')
def test_noparsepeer(self): """ Ensure the result of parsed noise lines is empty """ parsed = NTPPeers.parse(noiselines) empty = NTPPeers.newpeerdict() self.assertEqual(parsed, empty)
def test_rootmeansquare(self): """Test root mean square function.""" self.assertTrue(math.isnan(NTPPeers.rms([]))) self.assertEqual(NTPPeers.rms([3]), 3) self.assertEqual(NTPPeers.rms([3, 4]), math.sqrt((9 + 16) / 2)) self.assertEqual(NTPPeers.rms([3, 4, 5]), math.sqrt((9 + 16 + 25) / 3)) self.assertEqual(NTPPeers.rms([3, 4, 5, 6]), math.sqrt((9 + 16 + 25 + 36) / 4))
def main(): validchecks = ['proc', 'offset', 'peers', 'reach', 'reachability', 'sync', 'trace', 'vars'] defaultchecks = ['proc', 'offset', 'peers', 'reach', 'sync', 'vars'] args = get_args(validchecks) if args.check is None or len(args.check) < 1: args.check = defaultchecks else: # turn 'reachability' into 'reach' for backwards compatibility for i in range(0, len(args.check)): if args.check[i] == 'reachability': args.check[i] = 'reach' if args.test: # read from standard input in test mode checkobjs = { 'peers': NTPPeers([x.rstrip() for x in sys.stdin.readlines()]), } else: # run the checks checkobjs = ntpchecks(args.check, debug=args.debug) # alert on what we've collected alerter = NTPAlerter(args.check) alerter.alert_nagios(checkobjs=checkobjs, debug=args.debug) sys.exit(alerter.return_code())
def ntpchecks(checks, debug): """ Run all of the checks required by the argument list and return the resulting objects in a hash. """ objs = {} for check in checks: if ((check in ['offset', 'peers', 'reach', 'sync']) and 'peers' not in objs): (output, runtime) = execute('peers', debug=debug) objs['peers'] = NTPPeers(output, runtime) break if 'proc' in checks: objs['proc'] = NTPProcess() if 'trace' in checks: (output, runtime) = execute('trace', debug=debug) objs['trace'] = NTPTrace(output, runtime) if 'vars' in checks: (output, runtime) = execute('vars', debug=debug) objs['vars'] = NTPVars(output, runtime) return objs
def test_filternoiselines(self): """Compare the output of alllines filtered by isnoiseline() with the known non-noise lines.""" nonNoiseLines = [ x for x in alllines.split('\n') if not NTPPeers.isnoiseline(x) ] self.assertEqual(nonNoiseLines, inactivepeerlines.split('\n') + peerlines.split('\n'))
def test_tallytotype(self): for i in '*o': self.assertEqual(NTPPeers.tallytotype(i), 'syncpeer') for i in '+': self.assertEqual(NTPPeers.tallytotype(i), 'survivor') for i in '#': self.assertEqual(NTPPeers.tallytotype(i), 'backup') for i in ' .-x': self.assertEqual(NTPPeers.tallytotype(i), 'discard') for i in ' .-+ox#*': self.assertNotEqual(NTPPeers.tallytotype(i), 'unknown') for i in '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnpqrstuvwyz~!@$%^&()_': self.assertEqual(NTPPeers.tallytotype(i), 'unknown')
def main(): validchecks = ['proc', 'offset', 'peers', 'reach', 'sync', 'trace', 'vars'] defaultchecks = ['proc', 'offset', 'peers', 'reach', 'sync', 'vars'] args = get_args(validchecks) if args.check is None or len(args.check) < 1: args.check = defaultchecks if args.test: # read in ntpq output in test mode checkobjs = { 'peers': NTPPeers([x.rstrip() for x in sys.stdin.readlines()]), } else: # run the checks checkobjs = ntpchecks(args.check, args.debug) # alert on what we've collected alerter = NTPAlerter(args.check, checkobjs) alerter.alert_nagios(args.debug) sys.exit(alerter.return_code())
def ntpchecks(checks, debug, implementation=None): """ Run all of the checks required by the argument list and return the resulting objects in a hash. """ objs = {} if implementation not in _progs: implementation = detect_implementation() if 'proc' in checks: objs['proc'] = NTPProcess() if implementation is None: return objs for check in checks: if ((check in ['offset', 'peers', 'reach', 'sync']) and 'peers' not in objs): (output, elapsed) = execute('peers', debug=debug, implementation=implementation) objs['peers'] = NTPPeers(output, elapsed) break if 'trace' in checks: (output, elapsed) = execute('trace', debug=debug, implementation=implementation) objs['trace'] = NTPTrace(output, elapsed) if 'vars' in checks: (output, elapsed) = execute('vars', debug=debug, implementation=implementation) objs['vars'] = NTPVars(output, elapsed) return objs
def test_parsetestdata(self): """Ensure the test data matches the expected number of valid peers.""" for t in testdata: parsed = NTPPeers.parse(t) self.assertEqual(len(parsed['all']['address']), testdata[t])
def test_parsepeer(self): """Ensure the parsed peer lines matches the expected values.""" parsed = NTPPeers.parse(alllines) self.assertEqual(parsed, parsedpeers)
def test_peerline(self): """Ensure the known peer lines are valid peer lines.""" for s in peerlines.split('\n'): self.assertIsNotNone(NTPPeers.peerline(s))
def test_isntvalidpeerline(self): """Ensure the known noise lines aren't valid peer lines.""" for s in noiselines.split('\n'): self.assertIsNone(NTPPeers.peerline(s))
def test_peerisntnoise(self): """Ensure valid peer lines aren't classified as noise.""" for s in peerlines.split('\n'): self.assertFalse(NTPPeers.isnoiseline(s))
def test_parsepeer(self): parsed = NTPPeers.parse(alllines) self.assertEqual(parsed, parsedpeers)
def test_isntvalidpeerline(self): """ Ensure the known noise lines aren't valid peer lines. """ for s in noiselines.split('\n'): self.assertFalse(NTPPeers.peerline(s))
def test_noparsestratum99(self): """Ensure the result of parsed incorrect peer line is empty.""" empty = NTPPeers.newpeerdict() parsed = NTPPeers.parse(' 1234 5678 99 u 8 128 377 31.430 -16.143 74.185') self.assertEqual(parsed, empty)
def test_isntnoiseline(self): for s in peerlines.split('\n'): self.assertFalse(NTPPeers.isnoiseline(s))
def test_inactivepeerline(self): """Ensure the inactive peer lines aren't valid peer lines.""" for s in inactivepeerlines.split('\n'): self.assertIsNone(NTPPeers.peerline(s))
def test_ignorepeer(self): """ The first 6 peer lines in the test data should be ignored """ for s in peerlines.split('\n')[0:5]: self.assertFalse(NTPPeers.validpeer(NTPPeers.peerline(s)))
def test_isnoiseline(self): for s in noiselines.split('\n'): self.assertTrue(NTPPeers.isnoiseline(s))
def test_getmetrics(self): """Ensure the sync metric for parsed peer lines matches the expected values.""" p = NTPPeers(alllines) metrics = p.getmetrics() self.assertEqual(metrics['sync'], 1)
def test_dontignorepeer(self): """ The remaining peer lines in the test data shouldn't be ignored """ for s in peerlines.split('\n')[6:]: self.assertTrue(NTPPeers.validpeer(NTPPeers.peerline(s)))
def test_noparsestratum99(self): """Ensure the result of parsed incorrect peer line is empty.""" empty = NTPPeers.newpeerdict() parsed = NTPPeers.parse( ' 1234 5678 99 u 8 128 377 31.430 -16.143 74.185') self.assertEqual(parsed, empty)
def test_noparsestratum99(self): empty = NTPPeers.newpeerdict() parsed = NTPPeers.parse( ' 1234 5678 99 u 8 128 377 31.430 -16.143 74.185') self.assertEqual(parsed, empty)
def test_isnoiseline(self): """Ensure noise lines are classified as such.""" for s in noiselines.split('\n'): self.assertTrue(NTPPeers.isnoiseline(s))
def test_getmetrics(self): p = NTPPeers(alllines) metrics = p.getmetrics() self.assertEqual(metrics['sync'], 1)
def test_noparsestratum99(self): empty = NTPPeers.newpeerdict() parsed = NTPPeers.parse(' 1234 5678 99 u 8 128 377 31.430 -16.143 74.185') self.assertEqual(parsed, empty)
def test_filternoiselines(self): """ Compare the output of alllines filtered by isnoiseline() with the known non-noise lines. """ nonNoiseLines = [x for x in alllines.split('\n') if not NTPPeers.isnoiseline(x)] self.assertEqual(nonNoiseLines, peerlines.split('\n'))
def test_rootmeansquare(self): l = [3, 4, 5] self.assertEqual(NTPPeers.rms(l), math.sqrt(50 / 3))
def test_peerline(self): """ Ensure the known peer lines are valid peer lines. """ for s in peerlines.split('\n'): self.assertTrue(NTPPeers.peerline(s))
def test_tallytotype_unknown(self): """Ensure most printables are not valid tally types.""" for t in '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnpqrstuvwyz!@$%^&()_=[]{}|:;"<>,/\\\'': self.assertEqual(NTPPeers.tallytotype(t), 'unknown')
def test_getmetrics(self): p = NTPPeers(alllines) metrics = p.getmetrics() self.assertEqual(metrics['syncpeer'], 1)
def test_tallytotype_known(self): """Ensure known codes are valid tally types and that they correctly match their type.""" for t in TestNTPPeers.codes: self.assertEqual(NTPPeers.tallytotype(t), TestNTPPeers.codes[t])