Esempio n. 1
0
 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')
Esempio n. 2
0
 def test_noparsepeer(self):
     """
     Ensure the result of parsed noise lines is empty
     """
     parsed = NTPPeers.parse(noiselines)
     empty = NTPPeers.newpeerdict()
     self.assertEqual(parsed, empty)
Esempio n. 3
0
 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))
Esempio n. 4
0
 def test_noparsepeer(self):
     """
     Ensure the result of parsed noise lines is empty
     """
     parsed = NTPPeers.parse(noiselines)
     empty = NTPPeers.newpeerdict()
     self.assertEqual(parsed, empty)
Esempio n. 5
0
 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))
Esempio n. 6
0
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())
Esempio n. 7
0
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
Esempio n. 8
0
 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'))
Esempio n. 9
0
 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')
Esempio n. 10
0
 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')
Esempio n. 11
0
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())
Esempio n. 12
0
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
Esempio n. 13
0
 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])
Esempio n. 14
0
 def test_parsepeer(self):
     """Ensure the parsed peer lines matches the expected values."""
     parsed = NTPPeers.parse(alllines)
     self.assertEqual(parsed, parsedpeers)
Esempio n. 15
0
 def test_peerline(self):
     """Ensure the known peer lines are valid peer lines."""
     for s in peerlines.split('\n'):
         self.assertIsNotNone(NTPPeers.peerline(s))
Esempio n. 16
0
 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))
Esempio n. 17
0
 def test_peerisntnoise(self):
     """Ensure valid peer lines aren't classified as noise."""
     for s in peerlines.split('\n'):
         self.assertFalse(NTPPeers.isnoiseline(s))
Esempio n. 18
0
 def test_parsepeer(self):
     parsed = NTPPeers.parse(alllines)
     self.assertEqual(parsed, parsedpeers)
Esempio n. 19
0
 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))
Esempio n. 20
0
 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)
Esempio n. 21
0
 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])
Esempio n. 22
0
 def test_isntnoiseline(self):
     for s in peerlines.split('\n'):
         self.assertFalse(NTPPeers.isnoiseline(s))
Esempio n. 23
0
 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))
Esempio n. 24
0
 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)))
Esempio n. 25
0
 def test_isnoiseline(self):
     for s in noiselines.split('\n'):
         self.assertTrue(NTPPeers.isnoiseline(s))
Esempio n. 26
0
 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)
Esempio n. 27
0
 def test_isntnoiseline(self):
     for s in peerlines.split('\n'):
         self.assertFalse(NTPPeers.isnoiseline(s))
Esempio n. 28
0
 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)))
Esempio n. 29
0
 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)))
Esempio n. 30
0
 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))
Esempio n. 31
0
 def test_parsepeer(self):
     parsed = NTPPeers.parse(alllines)
     self.assertEqual(parsed, parsedpeers)
Esempio n. 32
0
 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)
Esempio n. 33
0
 def test_peerisntnoise(self):
     """Ensure valid peer lines aren't classified as noise."""
     for s in peerlines.split('\n'):
         self.assertFalse(NTPPeers.isnoiseline(s))
Esempio n. 34
0
 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)
Esempio n. 35
0
 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)
Esempio n. 36
0
 def test_isnoiseline(self):
     """Ensure noise lines are classified as such."""
     for s in noiselines.split('\n'):
         self.assertTrue(NTPPeers.isnoiseline(s))
Esempio n. 37
0
 def test_getmetrics(self):
     p = NTPPeers(alllines)
     metrics = p.getmetrics()
     self.assertEqual(metrics['sync'], 1)
Esempio n. 38
0
 def test_isnoiseline(self):
     for s in noiselines.split('\n'):
         self.assertTrue(NTPPeers.isnoiseline(s))
Esempio n. 39
0
 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)
Esempio n. 40
0
 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'))
Esempio n. 41
0
 def test_rootmeansquare(self):
     l = [3, 4, 5]
     self.assertEqual(NTPPeers.rms(l), math.sqrt(50 / 3))
Esempio n. 42
0
 def test_peerline(self):
     """
     Ensure the known peer lines are valid peer lines.
     """
     for s in peerlines.split('\n'):
         self.assertTrue(NTPPeers.peerline(s))
Esempio n. 43
0
 def test_tallytotype_unknown(self):
     """Ensure most printables are not valid tally types."""
     for t in '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnpqrstuvwyz!@$%^&()_=[]{}|:;"<>,/\\\'':
         self.assertEqual(NTPPeers.tallytotype(t), 'unknown')
Esempio n. 44
0
 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)))
Esempio n. 45
0
 def test_isnoiseline(self):
     """Ensure noise lines are classified as such."""
     for s in noiselines.split('\n'):
         self.assertTrue(NTPPeers.isnoiseline(s))
Esempio n. 46
0
 def test_rootmeansquare(self):
     l = [3, 4, 5]
     self.assertEqual(NTPPeers.rms(l), math.sqrt(50 / 3))
Esempio n. 47
0
 def test_parsepeer(self):
     """Ensure the parsed peer lines matches the expected values."""
     parsed = NTPPeers.parse(alllines)
     self.assertEqual(parsed, parsedpeers)
Esempio n. 48
0
 def test_getmetrics(self):
     p = NTPPeers(alllines)
     metrics = p.getmetrics()
     self.assertEqual(metrics['syncpeer'], 1)
Esempio n. 49
0
 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])
Esempio n. 50
0
 def test_tallytotype_unknown(self):
     """Ensure most printables are not valid tally types."""
     for t in '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnpqrstuvwyz!@$%^&()_=[]{}|:;"<>,/\\\'':
         self.assertEqual(NTPPeers.tallytotype(t), 'unknown')
Esempio n. 51
0
 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])