NTP Analyzer  0.8.2
Analyze the operation of time servers
NtpqPeerImporter.cs
Go to the documentation of this file.
1 //
2 // Copyright (c) 2013-2017 Carsten Sonne Larsen <cs@innolan.net>
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21 
22 using System;
23 using Ntp.Analyzer.Objects;
24 using Ntp.Common.Log;
25 
26 namespace Ntp.Analyzer.Import
27 {
31  public sealed class NtpqPeerImporter : Importer<AssociationEntry>
32  {
33  internal NtpqPeerImporter(string address, int hostId, LogBase log)
34  : base(log)
35  {
36  this.address = address;
37  this.hostId = hostId;
38  }
39 
40  private readonly string address;
41  private readonly int hostId;
42 
43  protected override string Command => "ntpq";
44 
45  protected override string Arguments => "-np " + address;
46 
47  protected override string ErrorMessage => LogMessage.ImportPeerError;
48 
49  protected override void ReadFromStream()
50  {
51  // Skip header
52  Reader.ReadLine();
53  Reader.ReadLine();
54 
55  while (Reader.Peek() != -1)
56  {
57  string line = Reader.ReadLine();
58 
59  if (string.IsNullOrWhiteSpace(line))
60  continue;
61 
62  var entry = ParseLine(line);
63  Entries.Add(entry);
64  }
65  }
66 
67  private static int CalcLastPoll(string line)
68  {
69  string lastPollString = line.Substring(38, 4);
70 
71  char lastPollUnit = lastPollString.Substring(lastPollString.Length - 1, 1)[0];
72  string lastPollValue = lastPollString.Substring(0, lastPollString.Length - 1).Trim();
73 
74  switch (lastPollUnit)
75  {
76  case 'm':
77  return Convert.ToInt32(lastPollValue)*60;
78  case 'h':
79  return Convert.ToInt32(lastPollValue)*60*60;
80  case 'd':
81  return Convert.ToInt32(lastPollValue)*60*60*24;
82  case '-':
83  return -1;
84  default:
85  return Convert.ToInt32(lastPollString);
86  }
87  }
88 
89  private AssociationEntry ParseLine(string line)
90  {
91  char state = line[0];
92  string remote = line.Substring(1, 16).Trim();
93  string refid = line.Substring(17, 16).Trim();
94  int stratus = Convert.ToInt32(line.Substring(33, 2).Trim());
95  char t = line[36];
96  int lastPoll = CalcLastPoll(line);
97  int pollFrequency = Convert.ToInt32(line.Substring(43, 4).Trim());
98  int reach = Convert.ToInt32(line.Substring(49, 3).Trim());
99  double delay = Convert.ToDouble(line.Substring(53, 8).Trim());
100  double offset = Convert.ToDouble(line.Substring(61, 9).Trim());
101  double jitter = Convert.ToDouble(line.Substring(70, 8).Trim());
102 
103  var entry = new AssociationEntry(
104  hostId, state, remote, refid, stratus, t,
105  lastPoll, pollFrequency, reach, delay, offset, jitter);
106 
107  return entry;
108  }
109  }
110 }
NtpqPeerImporter(string address, int hostId, LogBase log)
Ntpq mapper parses status importLines from ntpq into objects.
static int CalcLastPoll(string line)
AssociationEntry ParseLine(string line)