Exemple #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')
Exemple #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)
Exemple #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))
Exemple #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)
Exemple #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))
Exemple #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())
Exemple #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
Exemple #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'))
Exemple #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')
Exemple #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')
Exemple #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())
Exemple #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
Exemple #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])
Exemple #14
0
 def test_parsepeer(self):
     """Ensure the parsed peer lines matches the expected values."""
     parsed = NTPPeers.parse(alllines)
     self.assertEqual(parsed, parsedpeers)
Exemple #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))
Exemple #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))
Exemple #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))
Exemple #18
0
 def test_parsepeer(self):
     parsed = NTPPeers.parse(alllines)
     self.assertEqual(parsed, parsedpeers)
Exemple #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))
Exemple #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)
Exemple #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])
Exemple #22
0
 def test_isntnoiseline(self):
     for s in peerlines.split('\n'):
         self.assertFalse(NTPPeers.isnoiseline(s))
Exemple #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))
Exemple #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)))
Exemple #25
0
 def test_isnoiseline(self):
     for s in noiselines.split('\n'):
         self.assertTrue(NTPPeers.isnoiseline(s))
Exemple #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)
Exemple #27
0
 def test_isntnoiseline(self):
     for s in peerlines.split('\n'):
         self.assertFalse(NTPPeers.isnoiseline(s))
Exemple #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)))
Exemple #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)))
Exemple #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))
Exemple #31
0
 def test_parsepeer(self):
     parsed = NTPPeers.parse(alllines)
     self.assertEqual(parsed, parsedpeers)
Exemple #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)
Exemple #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))
Exemple #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)
Exemple #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)
Exemple #36
0
 def test_isnoiseline(self):
     """Ensure noise lines are classified as such."""
     for s in noiselines.split('\n'):
         self.assertTrue(NTPPeers.isnoiseline(s))
Exemple #37
0
 def test_getmetrics(self):
     p = NTPPeers(alllines)
     metrics = p.getmetrics()
     self.assertEqual(metrics['sync'], 1)
Exemple #38
0
 def test_isnoiseline(self):
     for s in noiselines.split('\n'):
         self.assertTrue(NTPPeers.isnoiseline(s))
Exemple #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)
Exemple #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'))
Exemple #41
0
 def test_rootmeansquare(self):
     l = [3, 4, 5]
     self.assertEqual(NTPPeers.rms(l), math.sqrt(50 / 3))
Exemple #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))
Exemple #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')
Exemple #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)))
Exemple #45
0
 def test_isnoiseline(self):
     """Ensure noise lines are classified as such."""
     for s in noiselines.split('\n'):
         self.assertTrue(NTPPeers.isnoiseline(s))
Exemple #46
0
 def test_rootmeansquare(self):
     l = [3, 4, 5]
     self.assertEqual(NTPPeers.rms(l), math.sqrt(50 / 3))
Exemple #47
0
 def test_parsepeer(self):
     """Ensure the parsed peer lines matches the expected values."""
     parsed = NTPPeers.parse(alllines)
     self.assertEqual(parsed, parsedpeers)
Exemple #48
0
 def test_getmetrics(self):
     p = NTPPeers(alllines)
     metrics = p.getmetrics()
     self.assertEqual(metrics['syncpeer'], 1)
Exemple #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])
Exemple #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')
Exemple #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])