diff --git a/README.md b/README.md index 3a21f068..8bdbe1b6 100755 --- a/README.md +++ b/README.md @@ -1591,7 +1591,6 @@ $ sudo netstat -apee | jc --netstat -p # or: sudo jc -p netstat -apee $ ntpq -p | jc --ntpq -p # or: jc -p ntpq -p [ { - "state": null, "remote": "44.190.6.254", "refid": "127.67.113.92", "st": 2, @@ -1601,10 +1600,10 @@ $ ntpq -p | jc --ntpq -p # or: jc -p ntpq -p "reach": 1, "delay": 23.399, "offset": -2.805, - "jitter": 2.131 + "jitter": 2.131, + "state": null }, { - "state": null, "remote": "mirror1.sjc02.s", "refid": "216.218.254.202", "st": 2, @@ -1614,7 +1613,8 @@ $ ntpq -p | jc --ntpq -p # or: jc -p ntpq -p "reach": 1, "delay": 29.325, "offset": 1.044, - "jitter": 4.069 + "jitter": 4.069, + "state": null } ] ``` diff --git a/docs/parsers/ntpq.md b/docs/parsers/ntpq.md index 343da1f5..fdd565ca 100644 --- a/docs/parsers/ntpq.md +++ b/docs/parsers/ntpq.md @@ -14,7 +14,6 @@ Examples: $ ntpq -p | jc --ntpq -p [ { - "state": null, "remote": "44.190.6.254", "refid": "127.67.113.92", "st": 2, @@ -24,10 +23,10 @@ Examples: "reach": 1, "delay": 23.399, "offset": -2.805, - "jitter": 2.131 + "jitter": 2.131, + "state": null }, { - "state": null, "remote": "ntp.wdc1.us.lea", "refid": "130.133.1.10", "st": 2, @@ -37,10 +36,10 @@ Examples: "reach": 1, "delay": 93.053, "offset": -0.807, - "jitter": 2.839 + "jitter": 2.839, + "state": null }, { - "state": null, "remote": "clock.team-cymr", "refid": "204.9.54.119", "st": 2, @@ -50,10 +49,10 @@ Examples: "reach": 1, "delay": 70.337, "offset": -2.909, - "jitter": 2.6 + "jitter": 2.6, + "state": null }, { - "state": null, "remote": "mirror1.sjc02.s", "refid": "216.218.254.202", "st": 2, @@ -63,14 +62,14 @@ Examples: "reach": 1, "delay": 29.325, "offset": 1.044, - "jitter": 4.069 + "jitter": 4.069, + "state": null, } ] $ ntpq -pn| jc --ntpq -p [ { - "state": "+", "remote": "44.190.6.254", "refid": "127.67.113.92", "st": 2, @@ -80,10 +79,10 @@ Examples: "reach": 377, "delay": 22.69, "offset": -0.392, - "jitter": 2.085 + "jitter": 2.085, + "state": "+" }, { - "state": "-", "remote": "108.59.2.24", "refid": "130.133.1.10", "st": 2, @@ -93,10 +92,10 @@ Examples: "reach": 377, "delay": 90.805, "offset": 2.84, - "jitter": 1.908 + "jitter": 1.908, + "state": "-" }, { - "state": "+", "remote": "38.229.71.1", "refid": "204.9.54.119", "st": 2, @@ -106,10 +105,10 @@ Examples: "reach": 377, "delay": 68.699, "offset": -0.61, - "jitter": 2.576 + "jitter": 2.576, + "state": "+" }, { - "state": "*", "remote": "72.5.72.15", "refid": "216.218.254.202", "st": 2, @@ -119,14 +118,15 @@ Examples: "reach": 377, "delay": 22.654, "offset": 0.231, - "jitter": 1.964 + "jitter": 1.964, + "state": "*" } ] $ ntpq -pn| jc --ntpq -p -r [ { - "state": "+", + "s": "+", "remote": "44.190.6.254", "refid": "127.67.113.92", "st": "2", @@ -139,7 +139,7 @@ Examples: "jitter": "2.085" }, { - "state": "-", + "s": "-", "remote": "108.59.2.24", "refid": "130.133.1.10", "st": "2", @@ -152,7 +152,7 @@ Examples: "jitter": "1.908" }, { - "state": "+", + "s": "+", "remote": "38.229.71.1", "refid": "204.9.54.119", "st": "2", @@ -165,7 +165,7 @@ Examples: "jitter": "2.576" }, { - "state": "*", + "s": "*", "remote": "72.5.72.15", "refid": "216.218.254.202", "st": "2", diff --git a/jc/parsers/ntpq.py b/jc/parsers/ntpq.py index cb41b626..2292f5e4 100644 --- a/jc/parsers/ntpq.py +++ b/jc/parsers/ntpq.py @@ -13,7 +13,6 @@ Examples: $ ntpq -p | jc --ntpq -p [ { - "state": null, "remote": "44.190.6.254", "refid": "127.67.113.92", "st": 2, @@ -23,10 +22,10 @@ Examples: "reach": 1, "delay": 23.399, "offset": -2.805, - "jitter": 2.131 + "jitter": 2.131, + "state": null }, { - "state": null, "remote": "ntp.wdc1.us.lea", "refid": "130.133.1.10", "st": 2, @@ -36,10 +35,10 @@ Examples: "reach": 1, "delay": 93.053, "offset": -0.807, - "jitter": 2.839 + "jitter": 2.839, + "state": null }, { - "state": null, "remote": "clock.team-cymr", "refid": "204.9.54.119", "st": 2, @@ -49,10 +48,10 @@ Examples: "reach": 1, "delay": 70.337, "offset": -2.909, - "jitter": 2.6 + "jitter": 2.6, + "state": null }, { - "state": null, "remote": "mirror1.sjc02.s", "refid": "216.218.254.202", "st": 2, @@ -62,14 +61,14 @@ Examples: "reach": 1, "delay": 29.325, "offset": 1.044, - "jitter": 4.069 + "jitter": 4.069, + "state": null, } ] $ ntpq -pn| jc --ntpq -p [ { - "state": "+", "remote": "44.190.6.254", "refid": "127.67.113.92", "st": 2, @@ -79,10 +78,10 @@ Examples: "reach": 377, "delay": 22.69, "offset": -0.392, - "jitter": 2.085 + "jitter": 2.085, + "state": "+" }, { - "state": "-", "remote": "108.59.2.24", "refid": "130.133.1.10", "st": 2, @@ -92,10 +91,10 @@ Examples: "reach": 377, "delay": 90.805, "offset": 2.84, - "jitter": 1.908 + "jitter": 1.908, + "state": "-" }, { - "state": "+", "remote": "38.229.71.1", "refid": "204.9.54.119", "st": 2, @@ -105,10 +104,10 @@ Examples: "reach": 377, "delay": 68.699, "offset": -0.61, - "jitter": 2.576 + "jitter": 2.576, + "state": "+" }, { - "state": "*", "remote": "72.5.72.15", "refid": "216.218.254.202", "st": 2, @@ -118,14 +117,15 @@ Examples: "reach": 377, "delay": 22.654, "offset": 0.231, - "jitter": 1.964 + "jitter": 1.964, + "state": "*" } ] $ ntpq -pn| jc --ntpq -p -r [ { - "state": "+", + "s": "+", "remote": "44.190.6.254", "refid": "127.67.113.92", "st": "2", @@ -138,7 +138,7 @@ Examples: "jitter": "2.085" }, { - "state": "-", + "s": "-", "remote": "108.59.2.24", "refid": "130.133.1.10", "st": "2", @@ -151,7 +151,7 @@ Examples: "jitter": "1.908" }, { - "state": "+", + "s": "+", "remote": "38.229.71.1", "refid": "204.9.54.119", "st": "2", @@ -164,7 +164,7 @@ Examples: "jitter": "2.576" }, { - "state": "*", + "s": "*", "remote": "72.5.72.15", "refid": "216.218.254.202", "st": "2", @@ -227,8 +227,10 @@ def process(proc_data): """ for entry in proc_data: - if entry['state'] == '~': - entry['state'] = None + if entry['s'] == '~': + entry['s'] = None + + entry['state'] = entry.pop('s') int_list = ['st', 'when', 'poll', 'reach'] for key in int_list: @@ -269,18 +271,23 @@ def parse(data, raw=False, quiet=False): raw_output = [] cleandata = data.splitlines() - cleandata[0] = 'state ' + cleandata[0] + cleandata[0] = 's ' + cleandata[0] cleandata[0] = cleandata[0].lower() # delete header delimiter del cleandata[1] # separate first character with a space for easier parsing - for i, line in enumerate(cleandata[1:]): + for i, line in list(enumerate(cleandata[1:])): if line[0] == ' ': - cleandata[i + 1] = '~ ' + line[1:] + # fixup for no-state + cleandata[i + 1] = '~ ' + line[1:] else: - cleandata[i + 1] = line[:1] + ' ' + line[1:] + # fixup - realign columns since we added the 's' column + cleandata[i + 1] = line[:1] + ' ' + line[1:] + + # fixup for occaisional ip/hostname fields with a space + cleandata[i + 1] = cleandata[i + 1].replace(' (', '_(') raw_output = jc.parsers.universal.simple_table_parse(cleandata) diff --git a/tests/fixtures/ubuntu-18.04/ntpq-p2.json b/tests/fixtures/ubuntu-18.04/ntpq-p2.json new file mode 100644 index 00000000..03ac3755 --- /dev/null +++ b/tests/fixtures/ubuntu-18.04/ntpq-p2.json @@ -0,0 +1 @@ +[{"remote": "0.ubuntu.pool.n", "refid": ".POOL.", "st": 16, "t": "p", "when": null, "poll": 64, "reach": 0, "delay": 0.0, "offset": 0.0, "jitter": 0.0, "state": null}, {"remote": "1.ubuntu.pool.n", "refid": ".POOL.", "st": 16, "t": "p", "when": null, "poll": 64, "reach": 0, "delay": 0.0, "offset": 0.0, "jitter": 0.0, "state": null}, {"remote": "2.ubuntu.pool.n", "refid": ".POOL.", "st": 16, "t": "p", "when": null, "poll": 64, "reach": 0, "delay": 0.0, "offset": 0.0, "jitter": 0.0, "state": null}, {"remote": "3.ubuntu.pool.n", "refid": ".POOL.", "st": 16, "t": "p", "when": null, "poll": 64, "reach": 0, "delay": 0.0, "offset": 0.0, "jitter": 0.0, "state": null}, {"remote": "ntp.ubuntu.com", "refid": ".POOL.", "st": 16, "t": "p", "when": null, "poll": 64, "reach": 0, "delay": 0.0, "offset": 0.0, "jitter": 0.0, "state": null}, {"remote": "45.79.36.123_(t", "refid": "216.218.254.202", "st": 2, "t": "u", "when": 11, "poll": 64, "reach": 7, "delay": 68.172, "offset": 4.404, "jitter": 4.878, "state": "+"}, {"remote": "199.188.64.12_(", "refid": "173.161.33.165", "st": 2, "t": "u", "when": 9, "poll": 64, "reach": 7, "delay": 78.823, "offset": 2.148, "jitter": 8.739, "state": "+"}, {"remote": "ntp2.wiktel.com", "refid": ".GPS.", "st": 1, "t": "u", "when": 25, "poll": 64, "reach": 7, "delay": 76.857, "offset": 0.473, "jitter": 7.89, "state": "*"}, {"remote": "atl0.jane.mattn", "refid": "35.73.197.144", "st": 2, "t": "u", "when": 32, "poll": 64, "reach": 7, "delay": 87.536, "offset": 8.641, "jitter": 4.544, "state": "#"}, {"remote": "x.ns.gin.ntt.ne", "refid": "249.224.99.213", "st": 2, "t": "u", "when": 28, "poll": 64, "reach": 7, "delay": 25.021, "offset": 3.361, "jitter": 7.793, "state": "+"}, {"remote": "srcf-ntp.stanfo", "refid": "171.64.7.105", "st": 2, "t": "u", "when": 26, "poll": 64, "reach": 7, "delay": 28.053, "offset": 7.253, "jitter": 4.524, "state": "-"}, {"remote": "linode.ibendit.", "refid": "64.250.105.227", "st": 2, "t": "u", "when": 28, "poll": 64, "reach": 7, "delay": 56.841, "offset": -0.305, "jitter": 8.279, "state": "#"}, {"remote": "71.66.197.233", "refid": ".GPS.", "st": 1, "t": "u", "when": 26, "poll": 64, "reach": 7, "delay": 127.766, "offset": -0.769, "jitter": 3.465, "state": "+"}, {"remote": "time.aja.com", "refid": "198.169.208.144", "st": 2, "t": "u", "when": 32, "poll": 64, "reach": 7, "delay": 39.673, "offset": 4.684, "jitter": 4.916, "state": "+"}, {"remote": "23.152.160.126", "refid": "84.168.88.243", "st": 2, "t": "u", "when": 28, "poll": 64, "reach": 7, "delay": 21.553, "offset": 3.245, "jitter": 8.612, "state": "+"}, {"remote": "ntp.backplanedn", "refid": "152.2.133.52", "st": 2, "t": "u", "when": 30, "poll": 64, "reach": 7, "delay": 93.009, "offset": 9.337, "jitter": 10.03, "state": "#"}, {"remote": "chilipepper.can", "refid": "193.79.237.14", "st": 2, "t": "u", "when": 32, "poll": 64, "reach": 7, "delay": 165.084, "offset": 5.872, "jitter": 3.797, "state": null}, {"remote": "198.255.68.106", "refid": "192.168.1.193", "st": 2, "t": "u", "when": 26, "poll": 64, "reach": 7, "delay": 30.911, "offset": 3.034, "jitter": 8.201, "state": "+"}, {"remote": "golem.canonical", "refid": "17.253.34.251", "st": 2, "t": "u", "when": 28, "poll": 64, "reach": 7, "delay": 156.657, "offset": 3.767, "jitter": 5.448, "state": null}, {"remote": "vps.jan-jaap.ne", "refid": "132.163.96.1", "st": 2, "t": "u", "when": 23, "poll": 64, "reach": 7, "delay": 184.394, "offset": -7.118, "jitter": 4.585, "state": "#"}, {"remote": "vf2.bbnx.net", "refid": "252.74.143.178", "st": 2, "t": "u", "when": 28, "poll": 64, "reach": 7, "delay": 97.275, "offset": 0.824, "jitter": 18.08, "state": "+"}, {"remote": "pugot.canonical", "refid": "17.253.34.253", "st": 2, "t": "u", "when": 29, "poll": 64, "reach": 7, "delay": 157.069, "offset": 3.709, "jitter": 8.753, "state": null}] diff --git a/tests/fixtures/ubuntu-18.04/ntpq-p2.out b/tests/fixtures/ubuntu-18.04/ntpq-p2.out new file mode 100644 index 00000000..a2bd4033 --- /dev/null +++ b/tests/fixtures/ubuntu-18.04/ntpq-p2.out @@ -0,0 +1,24 @@ + remote refid st t when poll reach delay offset jitter +============================================================================== + 0.ubuntu.pool.n .POOL. 16 p - 64 0 0.000 0.000 0.000 + 1.ubuntu.pool.n .POOL. 16 p - 64 0 0.000 0.000 0.000 + 2.ubuntu.pool.n .POOL. 16 p - 64 0 0.000 0.000 0.000 + 3.ubuntu.pool.n .POOL. 16 p - 64 0 0.000 0.000 0.000 + ntp.ubuntu.com .POOL. 16 p - 64 0 0.000 0.000 0.000 ++45.79.36.123 (t 216.218.254.202 2 u 11 64 7 68.172 4.404 4.878 ++199.188.64.12 ( 173.161.33.165 2 u 9 64 7 78.823 2.148 8.739 +*ntp2.wiktel.com .GPS. 1 u 25 64 7 76.857 0.473 7.890 +#atl0.jane.mattn 35.73.197.144 2 u 32 64 7 87.536 8.641 4.544 ++x.ns.gin.ntt.ne 249.224.99.213 2 u 28 64 7 25.021 3.361 7.793 +-srcf-ntp.stanfo 171.64.7.105 2 u 26 64 7 28.053 7.253 4.524 +#linode.ibendit. 64.250.105.227 2 u 28 64 7 56.841 -0.305 8.279 ++71.66.197.233 .GPS. 1 u 26 64 7 127.766 -0.769 3.465 ++time.aja.com 198.169.208.144 2 u 32 64 7 39.673 4.684 4.916 ++23.152.160.126 84.168.88.243 2 u 28 64 7 21.553 3.245 8.612 +#ntp.backplanedn 152.2.133.52 2 u 30 64 7 93.009 9.337 10.030 + chilipepper.can 193.79.237.14 2 u 32 64 7 165.084 5.872 3.797 ++198.255.68.106 192.168.1.193 2 u 26 64 7 30.911 3.034 8.201 + golem.canonical 17.253.34.251 2 u 28 64 7 156.657 3.767 5.448 +#vps.jan-jaap.ne 132.163.96.1 2 u 23 64 7 184.394 -7.118 4.585 ++vf2.bbnx.net 252.74.143.178 2 u 28 64 7 97.275 0.824 18.080 + pugot.canonical 17.253.34.253 2 u 29 64 7 157.069 3.709 8.753 diff --git a/tests/test_ntpq.py b/tests/test_ntpq.py index 59843474..b9c2f23a 100644 --- a/tests/test_ntpq.py +++ b/tests/test_ntpq.py @@ -22,6 +22,9 @@ class MyTests(unittest.TestCase): with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/ntpq-pn.out'), 'r', encoding='utf-8') as f: self.ubuntu_18_4_ntpq_pn = f.read() + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/ntpq-p2.out'), 'r', encoding='utf-8') as f: + self.ubuntu_18_4_ntpq_p2 = f.read() + # output with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/ntpq-p.json'), 'r', encoding='utf-8') as f: self.centos_7_7_ntpq_p_json = json.loads(f.read()) @@ -35,6 +38,9 @@ class MyTests(unittest.TestCase): with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/ntpq-pn.json'), 'r', encoding='utf-8') as f: self.ubuntu_18_4_ntpq_pn_json = json.loads(f.read()) + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/ntpq-p2.json'), 'r', encoding='utf-8') as f: + self.ubuntu_18_4_ntpq_p2_json = json.loads(f.read()) + def test_ntpq_p_centos_7_7(self): """ Test 'ntpq -p' on Centos 7.7 @@ -59,7 +65,11 @@ class MyTests(unittest.TestCase): """ self.assertEqual(jc.parsers.ntpq.parse(self.ubuntu_18_4_ntpq_pn, quiet=True), self.ubuntu_18_4_ntpq_pn_json) - + def test_ntpq_p2_ubuntu_18_4(self): + """ + Test 'ntpq -p' with ip data with spaces on Ubuntu 18.4 + """ + self.assertEqual(jc.parsers.ntpq.parse(self.ubuntu_18_4_ntpq_p2, quiet=True), self.ubuntu_18_4_ntpq_p2_json) if __name__ == '__main__':