NTP Analyzer  0.8.2
Analyze the operation of time servers
NtpdcImporter.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 System.Collections.Generic;
24 using System.Linq;
25 using Ntp.Analyzer.Data;
26 using Ntp.Analyzer.Objects;
27 using Ntp.Common.Log;
28 
29 namespace Ntp.Analyzer.Import
30 {
34  public sealed class NtpdcImporter : Importer<HostReading>
35  {
36  internal NtpdcImporter(string address, Host host, ReadingBulk bulk, LogBase log)
37  : base(log)
38  {
39  this.address = address;
40  this.host = host;
41  this.bulk = bulk;
42  }
43 
44  private readonly string address;
45  private readonly ReadingBulk bulk;
46  private readonly Host host;
47  private List<Peer> peers;
48 
49  protected override string Command => "ntpdc";
50 
51  protected override string Arguments => "-nc loopinfo -c sysinfo " + address;
52 
53  protected override string ErrorMessage => LogMessage.ImportHostError;
54 
55  protected override void Initialize()
56  {
57  base.Initialize();
58 
59  if (host == null)
60  return;
61 
62  peers = DataFace.Instance.Peers.ToList();
63  }
64 
65  protected override void ReadFromStream()
66  {
67  string peerIp = null;
68  double offset = 0;
69  double frequency = 0;
70  double jitter = 0;
71  double stability = 0;
72 
73  while (Reader.Peek() != -1)
74  {
75  string line = Reader.ReadLine();
76 
77  if (string.IsNullOrWhiteSpace(line) || line.Length < 24)
78  continue;
79 
80  string name = line.Substring(0, 22).Replace(":", string.Empty).TrimEnd();
81  string value = line.Substring(22).TrimEnd();
82 
83  switch (name)
84  {
85  case "system peer":
86  peerIp = value.Trim();
87  break;
88  case "offset":
89  offset = Convert.ToDouble(value.Replace("s", string.Empty).Trim());
90  break;
91  case "frequency":
92  frequency = Convert.ToDouble(value.Replace("ppm", string.Empty).Trim());
93  break;
94  case "jitter":
95  jitter = Convert.ToDouble(value.Replace("s", string.Empty).Trim());
96  break;
97  case "stability":
98  stability = Convert.ToDouble(value.Replace("ppm", string.Empty).Trim());
99  break;
100  }
101  }
102 
103  if (peerIp != null && peerIp.StartsWith("0.0.0.0"))
104  {
105  Log.NoSyncing(host.Name);
106  return;
107  }
108 
109  CreateEntry(peerIp, offset, jitter, frequency, stability);
110  }
111 
112  private void CreateEntry(string peerIp, double offset, double jitter, double frequency, double stability)
113  {
114  IEnumerable<Peer> peerList = peers.Where(p => p.Ip == peerIp).ToList();
115 
116  Peer peer;
117 
118  switch (peerList.Count())
119  {
120  case 1:
121  peer = peerList.Single();
122  break;
123  case 0:
124  Log.PeerNotFound(host.Name, peerIp);
125  return;
126  default:
127  Log.MultiplePeersFound(host.Name, peerIp);
128  return;
129  }
130 
131  Log.Syncing(host.Name, peer.Name);
132 
133  var reading = new HostReading(host, peer, bulk, offset, jitter, frequency, stability);
134  Entries.Add(reading);
135  }
136  }
137 }
void CreateEntry(string peerIp, double offset, double jitter, double frequency, double stability)
PeerDatabaseMapper Peers
Gets the peer mapper.
Definition: DataFace.cs:63
Singleton facade class used to access memory persistent data.
Definition: DataFace.cs:30
NtpdcImporter(string address, Host host, ReadingBulk bulk, LogBase log)
static DataFace Instance
Gets the Singleton instance.
Definition: DataFace.cs:51