NTP Analyzer  0.8.2
Analyze the operation of time servers
Ntp.Analyzer.Process.Initializer Class Reference
Collaboration diagram for Ntp.Analyzer.Process.Initializer:

Public Member Functions

 Initializer (string configFile, int pid, string pidFile, string name, LogGroup initlog)
 Initializes a new instance of the Initializer class. More...
 
void Run ()
 Run the NTP Analyzer. More...
 

Properties

SignalHandler Controller [get, private set]
 
List< ListenerListeners [get]
 
LogBase Log [get, private set]
 
List< IRequestNodes [get]
 
bool Ready [get, private set]
 
Scheduler Scheduler [get, private set]
 

Private Member Functions

void InitializeApplication ()
 Initializes the application and database state. More...
 
void InitializeCluster ()
 Initializes the cluster nodes. More...
 
bool InitializeConfiguration ()
 Initializes the configuration. More...
 
void InitializeData ()
 
bool InitializeDatabase ()
 
void InitializeListeners ()
 Initializes the listeners. More...
 
bool InitializeLog ()
 Initializes the log. More...
 
void InitializeScheduler ()
 Initializes the scheduler. More...
 
bool InitializeSecurity ()
 Initializes the security settings. More...
 

Private Attributes

Configuration config
 
readonly string configFile
 
readonly LogGroup initlog
 
readonly string name
 
readonly int pid
 
readonly string pidFile
 
readonly string version
 

Static Private Attributes

static bool firstrun = true
 

Detailed Description

Definition at line 50 of file Initializer.cs.

Constructor & Destructor Documentation

Ntp.Analyzer.Process.Initializer.Initializer ( string  configFile,
int  pid,
string  pidFile,
string  name,
LogGroup  initlog 
)
inline

Initializes a new instance of the Initializer class.

Parameters
configFileConfig file to use.
pidProcess ID.
pidFileWhere to write Process ID file.
nameName of process ID. Used when shutting down.
initlogLog used during initialization.

Definition at line 60 of file Initializer.cs.

References version.

61  {
62  this.configFile = configFile;
63  this.pid = pid;
64  this.pidFile = pidFile;
65  this.name = name;
66  this.initlog = initlog;
67  Nodes = new List<IRequest>();
68  Listeners = new List<Listener>();
69  version = "0.8.2";
70  }

Member Function Documentation

void Ntp.Analyzer.Process.Initializer.InitializeApplication ( )
inlineprivate

Initializes the application and database state.

Definition at line 128 of file Initializer.cs.

References Ntp.Analyzer.Monitor.Server.ApplicationState.Config, Ntp.Analyzer.Monitor.Server.ApplicationState.ConfigFile, Ntp.Analyzer.Config.Node.Configuration.Heartbeat, Ntp.Analyzer.Monitor.Server.ApplicationState.Log, Ntp.Analyzer.Monitor.Server.ApplicationState.Name, Ntp.Analyzer.Monitor.Server.ApplicationState.Pid, Ntp.Analyzer.Config.Node.HeartbeatConfiguration.Rate, version, and Ntp.Analyzer.Monitor.Server.ApplicationState.Version.

129  {
130  // Start signal controller
132  Controller.Start();
133 
134  // Initialize application state
141 
142  if (config.Heartbeat == null)
143  return;
144 
145  var beat = new Heartbeat(Log, config.Heartbeat.Rate);
146  beat.Start();
147  }
void Ntp.Analyzer.Process.Initializer.InitializeCluster ( )
inlineprivate

Initializes the cluster nodes.

Definition at line 152 of file Initializer.cs.

References Ntp.Analyzer.Config.Node.Configuration.Cluster, e, and Ntp.Analyzer.Config.Node.ClusterConfiguration.Nodes.

153  {
154  if (config.Cluster == null)
155  return;
156 
157  try
158  {
159  foreach (var node in config.Cluster.Nodes)
160  {
161  var ip = IPAddress.Parse(node.Ip);
162  var req = new TextRequest(ip, node.Port);
163  Nodes.Add(req);
164  Log.ClusterReady(node);
165  }
166  }
167  catch (Exception e)
168  {
169  initlog.ClusterError(e);
170  }
171  }
var e
Definition: bootstrap.min.js:6
bool Ntp.Analyzer.Process.Initializer.InitializeConfiguration ( )
inlineprivate

Initializes the configuration.

Definition at line 176 of file Initializer.cs.

References Ntp.Common.Log.File.

177  {
178  if (!File.Exists(configFile))
179  {
180  initlog.NoConfig(configFile);
181  return false;
182  }
183 
184  var builder = new ConfigBuilder(configFile);
185  config = builder.Execute();
186 
187  if (!builder.Errors.Any() && config != null)
188  return true;
189 
190  initlog.AddErrors(builder.Errors);
191  return false;
192  }
void Ntp.Analyzer.Process.Initializer.InitializeData ( )
inlineprivate

Definition at line 194 of file Initializer.cs.

References Ntp.Analyzer.Data.DataFace.AssociationEntries, Ntp.Analyzer.Import.ImportFactory.CreatePeerImporter(), Ntp.Analyzer.Config.Node.Configuration.Database, e, Ntp.Analyzer.Data.DataFace.Hosts, Ntp.Analyzer.Config.Node.DatabaseConfiguration.Import, Ntp.Analyzer.Data.Import.TimeServerWebAdapter.Initialize(), Ntp.Analyzer.Config.Node.DatabaseConfiguration.Initialize, Ntp.Analyzer.Data.DataFace.Instance, Ntp.Analyzer.Data.Import.TimeServers.List, Ntp.Analyzer.Data.DataFace.Peers, Ntp.Analyzer.Data.DataFace.Servers, and Ntp.Analyzer.Config.Node.Configuration.Servers.

195  {
197  return;
198 
200 
201  try
202  {
203  // Initialize hosts
204  foreach (var server in config.Servers)
205  {
206  var host = DataFace.Instance.Hosts.SingleOrDefault(h => h.Id == server.HostId);
207  if (host == null)
208  {
209  IPAddress ip;
210  try
211  {
212  ip = Dns.GetHostAddresses(server.ServerName)[0];
213  }
214  catch
215  {
216  ip = IPAddress.None;
217  }
218 
219  int? orgId = null;
220  if (TimeServers.List.ContainsKey(server.ServerName))
221  {
222  orgId = TimeServers.List[server.ServerName];
223  Log.KnownServer(orgId.Value, server.ServerName);
224  }
225  else if (!Equals(ip, IPAddress.None) && TimeServers.List.ContainsKey(ip.ToString()))
226  {
227  orgId = TimeServers.List[ip.ToString()];
228  Log.KnownServer(orgId.Value, ip.ToString());
229  }
230 
231  host = new Host(server.HostId, server.ServerName, ip.ToString(), orgId);
232  DataFace.Instance.Hosts.Save(host);
233  Log.NewHost(server, ip);
234  }
235  else if (host.OrgId == null && TimeServers.List.ContainsKey(host.Ip))
236  {
237  host.OrgId = TimeServers.List[host.Ip];
238  Log.KnownServer(host.OrgId.Value, host.Ip);
239  DataFace.Instance.Hosts.Save(host);
240  }
241  else if (host.OrgId == null && TimeServers.List.ContainsKey(host.Name))
242  {
243  host.OrgId = TimeServers.List[host.Name];
244  Log.KnownServer(host.OrgId.Value, host.Name);
245  DataFace.Instance.Hosts.Save(host);
246  }
247 
248  // Flush old association entries
249  DataFace.Instance.AssociationEntries.Delete(server.HostId);
250 
251  // Initialize peers
252  var peerImporter = ImportFactory.CreatePeerImporter(server.ServerName, server.ServerType, host, Log);
253 
254  foreach (var entry in peerImporter.ToList())
255  {
256  var currentEntry = entry;
257  var peerList = DataFace.Instance.Peers.Where(p => p.Ip == currentEntry.Remote).ToList();
258 
259  if (!peerList.Any())
260  {
261  // Create new peer in database
262  TimeServer timeServer = null;
263  if (TimeServers.List.ContainsKey(entry.Remote))
264  {
265  int orgId = TimeServers.List[entry.Remote];
266  timeServer = DataFace.Instance.Servers[orgId];
267  Log.KnownServer(orgId, entry.Remote);
268  }
269 
270  var hostName = entry.Remote;
271 
272  try
273  {
274  var hostEntry = Dns.GetHostEntry(entry.Remote);
275  hostName = hostEntry.HostName;
276  }
277  catch (Exception e)
278  {
279  Log.HostNameNotFound(entry.Remote, e);
280  }
281 
282  var peer = new Peer(hostName, entry.Remote, timeServer);
283  DataFace.Instance.Peers.Save(peer);
284  Log.NewPeer(entry);
285  }
286  else if (TimeServers.List.ContainsKey(entry.Remote))
287  {
288  // Try to update existing peer
289  var peer = peerList.First();
290  if (peer.Server != null && peer.Server.IsOrgServer)
291  continue;
292 
293  int orgId = TimeServers.List[entry.Remote];
294  var timeServer = DataFace.Instance.Servers[orgId];
295  Log.KnownServer(orgId, entry.Remote);
296  if (timeServer == null)
297  continue;
298 
299  peer.Server = timeServer;
300  DataFace.Instance.Peers.Save(peer);
301  }
302  }
303  }
304  }
305  catch (Exception e)
306  {
307  Log.TableError(e);
308  }
309  }
HostDatabaseMapper Hosts
Gets the host mapper.
Definition: DataFace.cs:57
static Importer< AssociationEntry > CreatePeerImporter(string address, ServerType type, Host host, LogBase log)
TimeServerDatabaseMapper Servers
Gets the time server mapper.
Definition: DataFace.cs:69
var e
Definition: bootstrap.min.js:6
IEnumerable< HostConfiguration > Servers
AssociationEntryMapper AssociationEntries
Gets the association entry mapper.
Definition: DataFace.cs:111
PeerDatabaseMapper Peers
Gets the peer mapper.
Definition: DataFace.cs:63
Singleton facade class used to access memory persistent data.
Definition: DataFace.cs:30
static readonly Dictionary< string, int > List
Definition: TimeServers.cs:28
static DataFace Instance
Gets the Singleton instance.
Definition: DataFace.cs:51

Here is the call graph for this function:

bool Ntp.Analyzer.Process.Initializer.InitializeDatabase ( )
inlineprivate

Definition at line 311 of file Initializer.cs.

References Ntp.Analyzer.Config.Node.Configuration.Database, e, Ntp.Data.Provider.SqlDatabaseFactory.Initialize(), Ntp.Analyzer.Config.Node.DatabaseConfiguration.Initialize, Ntp.Analyzer.Data.DataFace.Initialize(), Ntp.Data.Provider.SqlDatabaseFactory.Instance, and Ntp.Analyzer.Config.Node.DatabaseConfiguration.Upgrade.

312  {
315 
317  checker.CheckConnection();
318 
319  if (Controller.Stopped)
320  return false;
321 
322  try
323  {
324  var initializer = new DatabaseInitializer(
327  Log);
328 
329  return initializer.Execute();
330  }
331  catch (Exception e)
332  {
333  initlog.DatabaseError(e);
334  }
335 
336  return false;
337  }
static void Initialize(IDatabaseConfiguration factoryConfiguration)
var e
Definition: bootstrap.min.js:6
static void Initialize(LogBase log)
Definition: DataFace.cs:113
static SqlDatabaseFactory Instance
Singleton facade class used to access memory persistent data.
Definition: DataFace.cs:30

Here is the call graph for this function:

void Ntp.Analyzer.Process.Initializer.InitializeListeners ( )
inlineprivate

Initializes the listeners.

Definition at line 342 of file Initializer.cs.

References e, and Ntp.Analyzer.Config.Node.Configuration.Monitors.

343  {
344  try
345  {
346  foreach (var monitor in config.Monitors)
347  {
348  var listener = new Listener(monitor.Ip, monitor.Port, Log);
349  listener.Open();
350  Listeners.Add(listener);
351  initlog.ListenerReady(listener);
352  }
353  }
354  catch (Exception e)
355  {
356  initlog.ListenerError(e);
357  }
358  }
var e
Definition: bootstrap.min.js:6
IEnumerable< ListenerConfiguration > Monitors
bool Ntp.Analyzer.Process.Initializer.InitializeLog ( )
inlineprivate

Initializes the log.

Definition at line 363 of file Initializer.cs.

References Ntp.Analyzer.Config.Node.PermissionConfiguration.AppUserId, Ntp.Common.Log.LogFactory.CreateLog(), e, Ntp.Common.Log.LogBase.Initialize(), Ntp.Analyzer.Config.Node.Configuration.Log, Ntp.Analyzer.Config.Node.Configuration.Permission, and Ntp.Common.IO.Permission.SetUserId().

364  {
365  try
366  {
368  Log.Initialize();
369  }
370  catch (Exception e)
371  {
372  initlog.InitializationError(e);
373  return false;
374  }
375 
376  initlog.Add(Log);
377 
378  if (firstrun)
379  initlog.Starting(version);
380 
381  initlog.ConfigFile(configFile);
382  initlog.ProcessId(pid);
383  initlog.InstanceName(name);
384 
385  if (config.Permission?.AppUserId == null)
386  return true;
387 
389  initlog.UserId(config.Permission.AppUserId.Value);
390 
391  return true;
392  }
static LogBase CreateLog(ILogConfiguration config)
Definition: LogFactory.cs:68
void Add(LogBase log)
Definition: LogGroup.cs:71
var e
Definition: bootstrap.min.js:6
static bool SetUserId(uint userId)
Definition: Permission.cs:68
abstract void Initialize()
PermissionConfiguration Permission
IEnumerable< LogConfiguration > Log

Here is the call graph for this function:

void Ntp.Analyzer.Process.Initializer.InitializeScheduler ( )
inlineprivate

Initializes the scheduler.

Definition at line 397 of file Initializer.cs.

References Ntp.Common.Process.Scheduler.Add(), Ntp.Analyzer.Config.Node.Configuration.Bulks, e, Ntp.Common.Process.Scheduler.Log, Ntp.Analyzer.Config.Node.Configuration.Notify, Ntp.Common.Process.Job.Reset(), Ntp.Analyzer.Monitor.Server.ApplicationState.Scheduler, Ntp.Analyzer.Config.Node.Configuration.Servers, Ntp.Common.Process.Scheduler.StartTime, and Ntp.Analyzer.Monitor.Server.ApplicationState.StartupTime.

398  {
399  Job.Reset();
400 
401  try
402  {
403  Scheduler = new Scheduler(Log);
404  Log.JobInitStart();
405 
406  foreach (var bulk in config.Bulks)
407  Scheduler.Add(new BulkStatJob(bulk, Scheduler.Log));
408 
409  // Add jobs to schedule.
410  foreach (var server in config.Servers)
411  {
412  Scheduler.Add(new HostStatJob(server.HostStats, Scheduler.Log));
413  Scheduler.Add(new HostIoStatJob(server.HostIoStats, Scheduler.Log));
414  Scheduler.Add(new PeerStatJob(server.PeerStats, Scheduler.Log));
415  Scheduler.Add(new DriftStatJob(server.DriftStats, Scheduler.Log));
416  Scheduler.Add(new AboutPageJob(server.Menu, server.AboutPage, Scheduler.Log));
417 
418  foreach (var peerPage in server.PeerPages)
419  Scheduler.Add(new PeerPageJob(server.Menu, peerPage, Scheduler.Log));
420 
421  foreach (var hostPage in server.HostPages)
422  Scheduler.Add(new HostPageJob(server.Menu, hostPage, Scheduler.Log));
423 
424  foreach (var hostGraph in server.HostGraphs)
425  Scheduler.Add(new HostGraphJob(hostGraph, Scheduler.Log));
426 
427  foreach (var trafficGraph in server.TrafficGraphs)
428  Scheduler.Add(new TrafficGraphJob(trafficGraph, Scheduler.Log));
429 
430  foreach (var peerGraph in server.PeerGraphs)
431  Scheduler.Add(new PeerGraphJob(peerGraph, Scheduler.Log));
432 
433  foreach (var peerSummaryPage in server.PeerSummaryPages)
434  Scheduler.Add(new PeerSummaryJob(server.Menu, peerSummaryPage, Scheduler.Log));
435 
436  Scheduler.Add(new HostGraphPageJob(server.HostGraphPage, Scheduler.Log));
437  Scheduler.Add(new PeerGraphPageJob(server.PeerGraphPage, Scheduler.Log));
438  }
439 
440  // Add the surveillance/notify job.
441  if (config.Notify != null)
442  {
443  var adminJob = new NotifyJob(config.Notify, Log, pid, configFile);
444  Scheduler.Add(adminJob);
445  adminJob.SetJobList(Scheduler);
446  }
447 
448  Log.JobInitEnd();
449  }
450  catch (Exception e)
451  {
452  initlog.SchedulerError(e);
453  }
454 
457  }
LogBase Log
Gets the log used by this Scheduler.
Definition: Scheduler.cs:82
A scheduler performs scheduling of jobs according to job schedule descriptions.
Definition: Scheduler.cs:35
void Add(JobDescription description)
Add the specified job to the scheduler queue.
Definition: Scheduler.cs:130
Job which read statistics from an ntp drift file and saves the result to a database.
Definition: DriftStatJob.cs:37
static void Reset()
Definition: Job.cs:167
Job which read statistics about an ntp host and saves the result to a database.
Definition: HostStatJob.cs:37
var e
Definition: bootstrap.min.js:6
IEnumerable< ReadingBulkConfiguration > Bulks
IEnumerable< HostConfiguration > Servers
Job which read statistics about peers and saves the result to a database.
Definition: PeerStatJob.cs:38
DateTime StartTime
Gets the start up time of this Scheduler.
Definition: Scheduler.cs:76

Here is the call graph for this function:

bool Ntp.Analyzer.Process.Initializer.InitializeSecurity ( )
inlineprivate

Initializes the security settings.

Definition at line 462 of file Initializer.cs.

References Ntp.Analyzer.Config.Node.PermissionConfiguration.AppUserId, e, Ntp.Common.Log.File, Ntp.Analyzer.Export.FileSystemDestination.FileGroupId, Ntp.Analyzer.Export.FileSystemDestination.FileMask, Ntp.Analyzer.Config.Node.PermissionConfiguration.FileMode, Ntp.Analyzer.Export.FileSystemDestination.FileUserId, Ntp.Analyzer.Config.Node.PermissionConfiguration.GroupId, Ntp.Analyzer.Config.Node.Configuration.Permission, Ntp.Common.IO.Permission.SetUserId(), and Ntp.Analyzer.Config.Node.PermissionConfiguration.UserId.

463  {
464  // Drop privileges
465  if (config.Permission?.AppUserId != null)
466  {
468  {
469  initlog.UserIdError(config.Permission.AppUserId.Value);
470  }
471  }
472 
473  // Create pid file
474  if (pidFile != null)
475  {
476  try
477  {
478  File.WriteAllText(pidFile, pid.ToString(CultureInfo.InvariantCulture));
479  }
480  catch (Exception e)
481  {
482  initlog.PidFileError(e);
483  return false;
484  }
485  }
486 
487  // Set permissions
488  if (config.Permission == null)
489  return true;
490 
494 
495  return true;
496  }
uint FileMode
The file mode to apply for created files.
var e
Definition: bootstrap.min.js:6
static bool SetUserId(uint userId)
Definition: Permission.cs:68
PermissionConfiguration Permission

Here is the call graph for this function:

void Ntp.Analyzer.Process.Initializer.Run ( )
inline

Run the NTP Analyzer.

Definition at line 96 of file Initializer.cs.

97  {
98  // Neutralize.
99  Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
100 
101  bool proceed = InitializeConfiguration();
102 
103  if (proceed)
104  proceed = InitializeSecurity();
105 
106  if (proceed)
107  proceed = InitializeLog();
108 
109  if (proceed)
110  {
115  proceed = InitializeDatabase();
116  }
117 
118  if (proceed)
119  InitializeData();
120 
121  Ready = proceed;
122  firstrun = false;
123  }
bool InitializeConfiguration()
Initializes the configuration.
Definition: Initializer.cs:176
void InitializeCluster()
Initializes the cluster nodes.
Definition: Initializer.cs:152
bool InitializeSecurity()
Initializes the security settings.
Definition: Initializer.cs:462
void InitializeScheduler()
Initializes the scheduler.
Definition: Initializer.cs:397
bool InitializeLog()
Initializes the log.
Definition: Initializer.cs:363
void InitializeListeners()
Initializes the listeners.
Definition: Initializer.cs:342
void InitializeApplication()
Initializes the application and database state.
Definition: Initializer.cs:128

Member Data Documentation

Configuration Ntp.Analyzer.Process.Initializer.config
private

Definition at line 79 of file Initializer.cs.

readonly string Ntp.Analyzer.Process.Initializer.configFile
private

Definition at line 73 of file Initializer.cs.

bool Ntp.Analyzer.Process.Initializer.firstrun = true
staticprivate

Definition at line 72 of file Initializer.cs.

readonly LogGroup Ntp.Analyzer.Process.Initializer.initlog
private

Definition at line 74 of file Initializer.cs.

readonly string Ntp.Analyzer.Process.Initializer.name
private

Definition at line 75 of file Initializer.cs.

readonly int Ntp.Analyzer.Process.Initializer.pid
private

Definition at line 76 of file Initializer.cs.

readonly string Ntp.Analyzer.Process.Initializer.pidFile
private

Definition at line 77 of file Initializer.cs.

readonly string Ntp.Analyzer.Process.Initializer.version
private

Definition at line 78 of file Initializer.cs.

Property Documentation

SignalHandler Ntp.Analyzer.Process.Initializer.Controller
getprivate set

Definition at line 81 of file Initializer.cs.

List<Listener> Ntp.Analyzer.Process.Initializer.Listeners
get

Definition at line 89 of file Initializer.cs.

LogBase Ntp.Analyzer.Process.Initializer.Log
getprivate set

Definition at line 85 of file Initializer.cs.

List<IRequest> Ntp.Analyzer.Process.Initializer.Nodes
get

Definition at line 87 of file Initializer.cs.

bool Ntp.Analyzer.Process.Initializer.Ready
getprivate set

Definition at line 91 of file Initializer.cs.

Scheduler Ntp.Analyzer.Process.Initializer.Scheduler
getprivate set

Definition at line 83 of file Initializer.cs.


The documentation for this class was generated from the following file: