NTP Analyzer  0.8.2
Analyze the operation of time servers
IoStatsImporter.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 Ntp.Analyzer.Objects;
23 using Ntp.Common.Log;
24 
25 namespace Ntp.Analyzer.Import
26 {
27  public sealed class IoStatsImporter : Importer<IoStatsEntry>
28  {
29  internal IoStatsImporter(string address, bool ntpq, LogBase log)
30  : base(log)
31  {
32  this.address = address;
33  this.ntpq = ntpq;
34  }
35 
36  private readonly string address;
37  private readonly bool ntpq;
38 
39  protected override string Command => ntpq ? "ntpq" : "ntpdc";
40 
41  protected override string Arguments => "-c iostats " + address;
42 
43  protected override string ErrorMessage => LogMessage.ImportIoError;
44 
45  protected override void ReadFromStream()
46  {
47  int timeSinceReset = -1;
48  int receiveBuffers = -1;
49  int freeReceiveBuffers = -1;
50  int usedReceiveBuffers = -1;
51  int lowWaterRefills = -1;
52  long droppedPackets = -1;
53  long ignoredPackets = -1;
54  long receivedPackets = -1;
55  long packetsSent = -1;
56  long packetsNotSent = -1;
57  int interruptsHandled = -1;
58  int receivedByInt = -1;
59 
60  while (Reader.Peek() != -1)
61  {
62  string line = Reader.ReadLine();
63 
64  if (string.IsNullOrWhiteSpace(line) || line.Length < 24)
65  continue;
66 
67  string name = line.Substring(0, 22).Replace(":", string.Empty).TrimEnd();
68  string value = line.Substring(22).TrimEnd();
69 
70  long parsedValue;
71 
72  if (!long.TryParse(value, out parsedValue))
73  {
74  parsedValue = -2;
75  }
76 
77  switch (name)
78  {
79  case "time since reset":
80  timeSinceReset = (int) parsedValue;
81  break;
82  case "receive buffers":
83  receiveBuffers = (int) parsedValue;
84  break;
85  case "free receive buffers":
86  freeReceiveBuffers = (int) parsedValue;
87  break;
88  case "used receive buffers":
89  usedReceiveBuffers = (int) parsedValue;
90  break;
91  case "low water refills":
92  lowWaterRefills = (int) parsedValue;
93  break;
94  case "dropped packets":
95  droppedPackets = parsedValue;
96  break;
97  case "ignored packets":
98  ignoredPackets = parsedValue;
99  break;
100  case "received packets":
101  receivedPackets = parsedValue;
102  break;
103  case "packets sent":
104  packetsSent = parsedValue;
105  break;
106  case "packets not sent":
107  case "packet send failures":
108  packetsNotSent = parsedValue;
109  break;
110  case "input wakeups":
111  case "interrupts handled":
112  interruptsHandled = (int) parsedValue;
113  break;
114  case "useful input wakeups":
115  case "received by int":
116  receivedByInt = (int) parsedValue;
117  break;
118  default:
119  Log.NtpValueError(name);
120  break;
121  }
122  }
123 
124  Entries.Add(new IoStatsEntry(
125  timeSinceReset,
126  receiveBuffers,
127  freeReceiveBuffers,
128  usedReceiveBuffers,
129  lowWaterRefills,
130  droppedPackets,
131  ignoredPackets,
132  receivedPackets,
133  packetsSent,
134  packetsNotSent,
135  interruptsHandled,
136  receivedByInt));
137  }
138  }
139 }
IoStatsImporter(string address, bool ntpq, LogBase log)