1
0
mirror of https://bitbucket.org/anguist/ntpa synced 2025-11-22 03:17:31 +00:00

Code cleanup & segregation of logging

This commit is contained in:
Carsten
2016-08-27 13:42:12 +02:00
parent 91599dd5e6
commit 5f78427d24
10 changed files with 123 additions and 78 deletions

View File

@ -22,7 +22,7 @@
using System.Reflection;
using System.Runtime.InteropServices;
[assembly: AssemblyVersion("0.7.1.0")]
[assembly: AssemblyVersion("0.7.2.0")]
[assembly: AssemblyCopyright("Copyright (c) 2013-2016 Carsten Sonne Larsen <cs@innolan.dk>")]
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]

View File

@ -42,23 +42,6 @@ namespace Ntp.Analyzer.Import
protected override string ErrorMessage => LogMessage.ImportIoError;
/// <summary>
/// Reads from stream.
/// </summary>
/// <remarks>
/// time since reset: 2676627
/// receive buffers: 10
/// free receive buffers: 9
/// used receive buffers: 0
/// low water refills: 1
/// dropped packets: 2091
/// ignored packets: 26
/// received packets: 36174080
/// packets sent: 34848739
/// packets not sent: 56457
/// interrupts handled: 35712727
/// received by int: 35712727
/// </remarks>
protected override void ReadFromStream()
{
int timeSinceReset = -1;
@ -136,8 +119,7 @@ namespace Ntp.Analyzer.Import
receivedByInt = parsedValue;
break;
default:
Log.WriteLine(string.Format(LogMessage.ImportNtpValueError, name),
Severity.Warn);
Log.NtpValueError(name);
break;
}
}

View File

@ -0,0 +1,77 @@
//
// Copyright (c) 2013-2016 Carsten Sonne Larsen <cs@innolan.dk>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using Ntp.Common.Log;
namespace Ntp.Analyzer.Import
{
internal static class LogMessage
{
internal const string ImportHostError = "Error while reading host statistics.";
internal const string ImportPeerError = "Error while reading peer statistics.";
internal const string ImportIoError = "Error while reading I/O statistics.";
}
internal static class LogExtensions
{
internal static void MultiplePeersFound(this LogBase log, string host, string peerIp)
{
log.WriteLine(
$"Could not import host stats from {host}. Found more than one peer with IP {peerIp} in database.",
Severity.Warn);
}
internal static void NoSyncing(this LogBase log, string host)
{
log.WriteLine(
$"{host} is not syncing. Adjust clock to start sync.",
Severity.Warn);
}
internal static void NtpValueError(this LogBase log, string name)
{
log.WriteLine(
$"Received an unknown value from NTP Daemon: {name}",
Severity.Warn);
}
internal static void OpenNtpUnsynced(this LogBase log)
{
log.WriteLine(
"OpenNTP clock is unsynced.",
Severity.Info);
}
internal static void PeerNotFound(this LogBase log, string host, string peerIp)
{
log.WriteLine(
$"Could not import host stats from {host}. Peer with IP {peerIp} was not found in database.",
Severity.Warn);
}
internal static void Syncing(this LogBase log, string host, string peer)
{
log.WriteLine(
$"{host} is syncing to {peer}.",
Severity.Info);
}
}
}

View File

@ -34,6 +34,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="AssemblyInfo.cs" />
<Compile Include="LogExtensions.cs" />
<Compile Include="NtpctlHostImporter.cs" />
<Compile Include="NtpqHostImporter.cs" />
<Compile Include="NtpdcImporter.cs" />

View File

@ -58,20 +58,21 @@ namespace Ntp.Analyzer.Import
Reader.ReadLine();
Reader.ReadLine();
// Find SysPeer
while (Reader.Peek() != -1)
{
string peer = Reader.ReadLine();
string stat = Reader.ReadLine();
AssociationEntry entry = ParseLines(peer, stat);
var entry = ParseLines(peer, stat);
if (entry.TallyCode == TallyCode.SysPeer)
{
CreateEntry(entry);
return;
}
if (entry.TallyCode != TallyCode.SysPeer)
continue;
CreateEntry(entry);
return;
}
Log.WriteLine(LogMessage.ImportOpenNtpUnsynced, Severity.Info);
Log.OpenNtpUnsynced();
}
private void CreateEntry(AssociationEntry entry)
@ -86,16 +87,14 @@ namespace Ntp.Analyzer.Import
peer = peerList.Single();
break;
case 0:
Log.WriteLine(string.Format(LogMessage.ImportPeerNotFound, host.Name, entry.Remote),
Severity.Error);
Log.PeerNotFound(host.Name, entry.Remote);
return;
default:
Log.WriteLine(string.Format(LogMessage.ImportMultiplePeersNot, host.Name, entry.Remote),
Severity.Error);
Log.MultiplePeersFound(host.Name, entry.Remote);
return;
}
Log.WriteLine(string.Format(LogMessage.ImportSyncing, host.Name, peer.Name), Severity.Info);
Log.Syncing(host.Name, peer.Name);
var reading = new HostReading(host, peer, bulk, entry.Offset, entry.Jitter);
Entries.Add(reading);

View File

@ -67,17 +67,15 @@ namespace Ntp.Analyzer.Import
string remote = peer.Substring(0, peer.IndexOf(" ", StringComparison.Ordinal)).Trim();
var entry =
new AssociationEntry(
hostId, state, remote, null, stratus, weight,
lastPoll, pollFrequency, trustlevel,
delay, offset, jitter
);
var entry = new AssociationEntry(
hostId, state, remote, null, stratus, weight,
lastPoll, pollFrequency, trustlevel,
delay, offset, jitter);
return entry;
}
private int CalcPoll(string pollString)
private static int CalcPoll(string pollString)
{
char pollUnit = pollString.Substring(pollString.Length - 1, 1)[0];
string pollValue = pollString.Substring(0, pollString.Length - 1).Trim();

View File

@ -43,7 +43,7 @@ namespace Ntp.Analyzer.Import
{
string peer = Reader.ReadLine();
string stat = Reader.ReadLine();
AssociationEntry entry = ParseLines(peer, stat);
var entry = ParseLines(peer, stat);
Entries.Add(entry);
}
}

View File

@ -62,29 +62,6 @@ namespace Ntp.Analyzer.Import
peers = DataFace.Instance.Peers.ToList();
}
/// <summary>
/// Reads from stream.
/// </summary>
/// <remarks>
/// * offset: -0.000284 s
/// * frequency: -2.994 ppm
/// 2 poll adjust: 30
/// watchdog timer: 173 s
/// 2 system peer: ntp1.gbg.netnod.se
/// system peer mode: client
/// leap indicator: 00
/// stratum: 2
/// precision: -19
/// root distance: 0.02441 s
/// root dispersion: 0.03212 s
/// reference ID: [192.36.133.17]
/// reference time: d57eb121.eca883cb Wed, Jul 3 2013 16:18:09.924
/// system flags: auth monitor ntp kernel stats
/// * jitter: 0.000198 s
/// * stability: 0.000 ppm
/// broadcastdelay: 0.000000 s
/// authdelay: 0.000037 s
/// </remarks>
protected override void ReadFromStream()
{
string peerIp = null;
@ -126,6 +103,17 @@ namespace Ntp.Analyzer.Import
}
}
if (peerIp != null && peerIp.StartsWith("0.0.0.0"))
{
Log.NoSyncing(host.Name);
return;
}
CreateEntry(peerIp, offset, jitter, frequency, stability);
}
private void CreateEntry(string peerIp, double offset, double jitter, double frequency, double stability)
{
IEnumerable<Peer> peerList = peers.Where(p => p.Ip == peerIp).ToList();
Peer peer;
@ -136,16 +124,14 @@ namespace Ntp.Analyzer.Import
peer = peerList.Single();
break;
case 0:
Log.WriteLine(string.Format(LogMessage.ImportPeerNotFound, host.Name, peerIp),
Severity.Error);
Log.PeerNotFound(host.Name, peerIp);
return;
default:
Log.WriteLine(string.Format(LogMessage.ImportMultiplePeersNot, host.Name, peerIp),
Severity.Error);
Log.MultiplePeersFound(host.Name, peerIp);
return;
}
Log.WriteLine(string.Format(LogMessage.ImportSyncing, host.Name, peer.Name), Severity.Info);
Log.Syncing(host.Name, peer.Name);
var reading = new HostReading(host, peer, bulk, offset, jitter, frequency, stability);
Entries.Add(reading);

View File

@ -103,12 +103,17 @@ namespace Ntp.Analyzer.Import
}
}
if (peerIp == "0.0.0.0:0")
if (peerIp != null && peerIp.StartsWith("0.0.0.0"))
{
Log.WriteLine(string.Format(LogMessage.ImportNoSyncing, host.Name), Severity.Warn);
Log.NoSyncing(host.Name);
return;
}
CreateEntry(peerIp, offset, jitter, frequency, stability);
}
private void CreateEntry(string peerIp, double offset, double jitter, double frequency, double stability)
{
IEnumerable<Peer> peerList = peers.Where(p => p.Ip == peerIp).ToList();
Peer peer;
@ -119,16 +124,14 @@ namespace Ntp.Analyzer.Import
peer = peerList.Single();
break;
case 0:
Log.WriteLine(string.Format(LogMessage.ImportPeerNotFound, host.Name, peerIp),
Severity.Error);
Log.PeerNotFound(host.Name, peerIp);
return;
default:
Log.WriteLine(string.Format(LogMessage.ImportMultiplePeersNot, host.Name, peerIp),
Severity.Error);
Log.MultiplePeersFound(host.Name, peerIp);
return;
}
Log.WriteLine(string.Format(LogMessage.ImportSyncing, host.Name, peer.Name), Severity.Info);
Log.Syncing(host.Name, peer.Name);
var reading = new HostReading(host, peer, bulk, offset, jitter, frequency, stability);
Entries.Add(reading);

View File

@ -59,13 +59,12 @@ namespace Ntp.Analyzer.Import
if (line == null || line.Trim() == string.Empty)
continue;
AssociationEntry entry = ParseLine(line);
var entry = ParseLine(line);
Entries.Add(entry);
}
}
private int CalcLastPoll(string line)
private static int CalcLastPoll(string line)
{
string lastPollString = line.Substring(38, 4);