mirror of
https://bitbucket.org/anguist/ntpa
synced 2026-10-02 08:19:12 +00:00
Version 0.2 changes
This commit is contained in:
@@ -0,0 +1,333 @@
|
||||
//
|
||||
// Initializer.cs
|
||||
//
|
||||
// Author:
|
||||
// Carsten Sonne Larsen <cs@innolan.net>
|
||||
//
|
||||
// Copyright (c) 2014 Carsten Sonne Larsen
|
||||
//
|
||||
// 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 System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Net;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using Ntp.Analyzer.Config;
|
||||
using Ntp.Analyzer.Config.Graph;
|
||||
using Ntp.Analyzer.Config.Page;
|
||||
using Ntp.Analyzer.Config.Net;
|
||||
using Ntp.Analyzer.Data;
|
||||
using Ntp.Analyzer.Log;
|
||||
using Ntp.Config;
|
||||
using Ntp.Monitor.Server;
|
||||
using Ntp.Process;
|
||||
using Ntp.Analyzer.Process.Description;
|
||||
using Ntp.Analyzer.Config.Root;
|
||||
using Ntp.Monitor.Client;
|
||||
using Ntp.Web.Server;
|
||||
using Ntp.Web;
|
||||
using Ntp.Analyzer.Export;
|
||||
|
||||
namespace Ntp.Analyzer.Process
|
||||
{
|
||||
public sealed class Initializer
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="Initializer"/> class.
|
||||
/// </summary>
|
||||
/// <param name="version">The version number to show.</param>
|
||||
/// <param name="configFile">The configuration file to use.</param>
|
||||
/// <param name="pid">The pid.</param>
|
||||
public Initializer (string version, string configFile, int pid)
|
||||
{
|
||||
this.version = version;
|
||||
this.configFile = configFile;
|
||||
this.pid = pid;
|
||||
|
||||
nodes = new List<IRequest> ();
|
||||
listeners = new List<Listener> ();
|
||||
webListeners = new List<WebServer>();
|
||||
}
|
||||
|
||||
private readonly string version;
|
||||
private readonly string configFile;
|
||||
private readonly int pid;
|
||||
private readonly List<IRequest> nodes;
|
||||
private readonly List<Listener> listeners;
|
||||
private readonly List<WebServer> webListeners;
|
||||
private Configuration config;
|
||||
private LogBase log;
|
||||
private Scheduler scheduler;
|
||||
private bool ready;
|
||||
|
||||
public Scheduler Scheduler {
|
||||
get { return scheduler; }
|
||||
}
|
||||
|
||||
public LogBase Log {
|
||||
get { return log; }
|
||||
}
|
||||
|
||||
public List<IRequest> Nodes {
|
||||
get { return nodes; }
|
||||
}
|
||||
|
||||
public bool Ready {
|
||||
get { return ready; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Run the NTP Analyzer.
|
||||
/// </summary>
|
||||
public void Run ()
|
||||
{
|
||||
// Neutralize.
|
||||
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
|
||||
|
||||
bool proceed = InitializeConfiguration ();
|
||||
|
||||
if (proceed)
|
||||
proceed = InitializeLog ();
|
||||
|
||||
if (proceed) {
|
||||
InitializeState ();
|
||||
InitializeListerners ();
|
||||
InitializeWebListener ();
|
||||
InitializeCluster ();
|
||||
InitializeScheduler ();
|
||||
}
|
||||
|
||||
ready = proceed;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the configuration.
|
||||
/// </summary>
|
||||
private bool InitializeConfiguration ()
|
||||
{
|
||||
if (!File.Exists (configFile)) {
|
||||
Console.WriteLine ("Cannot find configuration file: " + configFile);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Read config.
|
||||
try {
|
||||
string[] configLines = File.ReadAllLines (configFile);
|
||||
ConfigurationBlock block = new ConfigurationBlock (configLines);
|
||||
config = Configuration.Create (block);
|
||||
} catch (Exception ex) {
|
||||
Console.WriteLine ("Cannot read configuration file: " + configFile);
|
||||
Console.WriteLine (ex.Message);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (ConfigurationBase.Errors != String.Empty) {
|
||||
Console.WriteLine ("Errors in configuration file");
|
||||
Console.WriteLine (ConfigurationBase.Errors);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the log.
|
||||
/// </summary>
|
||||
private bool InitializeLog ()
|
||||
{
|
||||
// Create log.
|
||||
try {
|
||||
log = LogFactory.CreateLog (config.Log);
|
||||
} catch (Exception ex) {
|
||||
Console.WriteLine ("Cannot create log files.");
|
||||
Console.WriteLine (ex.Message);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Initialize log.
|
||||
log.WriteLine ("NTP Analyzer " + version + " started.", Severity.Notice);
|
||||
log.WriteLine ("Using configuration: " + configFile, Severity.Notice);
|
||||
log.WriteLine ("Running with pid: " + pid, Severity.Notice);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the application and database state.
|
||||
/// </summary>
|
||||
private void InitializeState ()
|
||||
{
|
||||
// Initialize database connection
|
||||
DataConnection.String = config.Database.ConnectionString;
|
||||
|
||||
// Initialize application state
|
||||
ApplicationState.Version = version;
|
||||
ApplicationState.Pid = pid;
|
||||
ApplicationState.Config = config;
|
||||
ApplicationState.ConfigFile = configFile;
|
||||
ApplicationState.Log = log;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the listerners.
|
||||
/// </summary>
|
||||
private void InitializeListerners ()
|
||||
{
|
||||
try {
|
||||
foreach (MonitorListenerConfiguration monitor in config.Monitors) {
|
||||
Listener listener = new Listener (monitor.Ip, monitor.Port, log);
|
||||
listener.Open ();
|
||||
listeners.Add (listener);
|
||||
|
||||
log.WriteLine ("Listening on " + listener, Severity.Notice);
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
log.WriteLine ("Error during initialization of listerners.", Severity.Error);
|
||||
log.WriteLine (ex);
|
||||
}
|
||||
}
|
||||
|
||||
private void InitializeWebListener ()
|
||||
{
|
||||
config.WebListeners
|
||||
|
||||
|
||||
List<IWebPage> pages = new List<IWebPage> ();
|
||||
|
||||
foreach (ServerConfiguration server in config.Servers) {
|
||||
pages.Add (new AboutPageJob (server.Menu, server.AboutPage, log));
|
||||
|
||||
foreach (PeerPageConfiguration peerPage in server.PeerPages)
|
||||
pages.Add (new PeerPageJob (server.Menu, peerPage, log));
|
||||
|
||||
foreach (HostPageConfiguration hostPage in server.HostPages)
|
||||
pages.Add (new HostPageJob (server.Menu, hostPage, log));
|
||||
|
||||
foreach (PeerSummeryPageConfiguration peerSummeryPage in server.PeerSummeryPages)
|
||||
pages.Add (new PeerSummeryJob (server.Menu, peerSummeryPage, log));
|
||||
|
||||
pages.Add (new HostGraphPageJob (server.HostGraphPage, log));
|
||||
pages.Add (new PeerGraphPageJob (server.PeerGraphPage, log));
|
||||
}
|
||||
|
||||
try {
|
||||
foreach (WebListenerConfiguration webListener in config.WebListeners) {
|
||||
WebServer listener = new WebServer (
|
||||
webListener.Ip,
|
||||
webListener.Port,
|
||||
webListener.Timeout,
|
||||
log);
|
||||
|
||||
listener.AddPages(pages);
|
||||
listener.AddServables(webListener.Resource.Sources.Files);
|
||||
listener.Open ();
|
||||
|
||||
webListeners.Add (listener);
|
||||
|
||||
log.WriteLine ("Listening on " + listener, Severity.Notice);
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
log.WriteLine ("Error during initialization of listerners.", Severity.Error);
|
||||
log.WriteLine (ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the cluster nodes.
|
||||
/// </summary>
|
||||
private void InitializeCluster ()
|
||||
{
|
||||
if (config.Cluster == null)
|
||||
return;
|
||||
|
||||
try {
|
||||
foreach (NodeConfiguration node in config.Cluster.Nodes) {
|
||||
IPAddress ip = IPAddress.Parse (node.Ip);
|
||||
|
||||
IRequest req = new TextRequest (ip, node.Port);
|
||||
nodes.Add (req);
|
||||
|
||||
log.WriteLine ("Reqesting to cluster " + node.Address, Severity.Notice);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.WriteLine ("Error during initialization of cluster node.", Severity.Error);
|
||||
log.WriteLine (e.ToString (), Severity.Error);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the scheduler.
|
||||
/// </summary>
|
||||
private void InitializeScheduler ()
|
||||
{
|
||||
try {
|
||||
scheduler = new Scheduler (log);
|
||||
log.WriteLine ("Initializing jobs.", Severity.Debug);
|
||||
scheduler.Add (new BulkStatJob (config.Bulk, scheduler.Log));
|
||||
|
||||
// Add jobs to schedule.
|
||||
foreach (ServerConfiguration server in config.Servers) {
|
||||
scheduler.Add (new HostStatJob (server.HostStats, scheduler.Log));
|
||||
scheduler.Add (new HostIOStatJob (server.HostIoStats, scheduler.Log));
|
||||
scheduler.Add (new PeerStatJob (server.PeerStats, scheduler.Log));
|
||||
scheduler.Add (new AboutPageJob (server.Menu, server.AboutPage, scheduler.Log));
|
||||
|
||||
foreach (PeerPageConfiguration peerPage in server.PeerPages)
|
||||
scheduler.Add (new PeerPageJob (server.Menu, peerPage, scheduler.Log));
|
||||
|
||||
foreach (HostPageConfiguration hostPage in server.HostPages)
|
||||
scheduler.Add (new HostPageJob (server.Menu, hostPage, scheduler.Log));
|
||||
|
||||
foreach (HostGraphConfiguration hostGraph in server.HostGraphs)
|
||||
scheduler.Add (new HostGraphJob (hostGraph, scheduler.Log));
|
||||
|
||||
foreach (TrafficGraphConfiguration trafficGraph in server.TrafficGraphs)
|
||||
scheduler.Add (new TrafficGraphJob (trafficGraph, scheduler.Log));
|
||||
|
||||
foreach (PeerGraphConfiguration peerGraph in server.PeerGraphs)
|
||||
scheduler.Add (new PeerGraphJob (peerGraph, scheduler.Log));
|
||||
|
||||
foreach (PeerSummeryPageConfiguration peerSummeryPage in server.PeerSummeryPages)
|
||||
scheduler.Add (new PeerSummeryJob (server.Menu, peerSummeryPage, scheduler.Log));
|
||||
|
||||
scheduler.Add (new HostGraphPageJob (server.HostGraphPage, scheduler.Log));
|
||||
scheduler.Add (new PeerGraphPageJob (server.PeerGraphPage, scheduler.Log));
|
||||
}
|
||||
|
||||
// Add the survaliance/notify job.
|
||||
if (config.Notify != null) {
|
||||
NotifyJob adminJob = new NotifyJob (config.Notify, log, pid, configFile);
|
||||
scheduler.Add (adminJob);
|
||||
adminJob.SetJobList (scheduler);
|
||||
|
||||
// Done
|
||||
log.WriteLine ("All jobs initialized.", Severity.Debug);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.WriteLine ("Error during initialization of scheduler.", Severity.Error);
|
||||
log.WriteLine (e.ToString (), Severity.Error);
|
||||
}
|
||||
|
||||
ApplicationState.Scheduler = scheduler;
|
||||
ApplicationState.StartupTime = scheduler.StartTime;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user