NTP Analyzer  0.8.2
Analyze the operation of time servers
NtpctlImporter.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 {
28  public abstract class NtpctlImporter<T> : Importer<T>
29  {
30  protected NtpctlImporter(int hostId, LogBase log)
31  : base(log)
32  {
33  this.hostId = hostId;
34  }
35 
36  private readonly int hostId;
37 
38  protected override string Command => "ntpctl";
39 
40  protected override string Arguments => "-sp";
41 
42  protected AssociationEntry ParseLines(string peer, string stats)
43  {
44  double result;
45 
46  char state = stats[1];
47  char weight = stats[4];
48  int trustlevel = Convert.ToInt32(stats.Substring(6, 2).Trim());
49  int stratus = Convert.ToInt32(stats.Substring(9, 2).Trim().Replace("-", "15"));
50  int lastPoll = CalcPoll(stats.Substring(11, 6));
51  int pollFrequency = CalcPoll(stats.Substring(17, 6));
52 
53  string value = stats.Substring(31, 10).Trim().Replace("ms", string.Empty);
54  double offset = double.TryParse(value, out result)
55  ? result
56  : 0.0;
57 
58  value = stats.Substring(41, 10).Trim().Replace("ms", string.Empty);
59  double delay = double.TryParse(value, out result)
60  ? result
61  : 0.0;
62 
63  value = stats.Substring(51, stats.Length - 51).Trim().Replace("ms", string.Empty);
64  double jitter = double.TryParse(value, out result)
65  ? result
66  : 0.0;
67 
68  string remote = peer.Substring(0, peer.IndexOf(" ", StringComparison.Ordinal)).Trim();
69 
70  var entry = new AssociationEntry(
71  hostId, state, remote, null, stratus, weight,
72  lastPoll, pollFrequency, trustlevel,
73  delay, offset, jitter);
74 
75  return entry;
76  }
77 
78  private static int CalcPoll(string pollString)
79  {
80  char pollUnit = pollString.Substring(pollString.Length - 1, 1)[0];
81  string pollValue = pollString.Substring(0, pollString.Length - 1).Trim();
82 
83  switch (pollUnit)
84  {
85  case 's':
86  return Convert.ToInt32(pollValue);
87  case 'm':
88  return Convert.ToInt32(pollValue)*60;
89  case 'h':
90  return Convert.ToInt32(pollValue)*60*60;
91  case 'd':
92  return Convert.ToInt32(pollValue)*60*60*24;
93  case '-':
94  return -1;
95  default:
96  return Convert.ToInt32(pollString);
97  }
98  }
99  }
100 }
static int CalcPoll(string pollString)
NtpctlImporter(int hostId, LogBase log)
AssociationEntry ParseLines(string peer, string stats)