// // Runner.cs // // Author: // Carsten Sonne Larsen // // Copyright (c) 2013 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.IO; using System.Threading; using Ntp.Analyzer.Config; using Ntp.Analyzer.Config.Graph; using Ntp.Analyzer.Config.Page; 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 System.Net; namespace Ntp.Analyzer.Process { public sealed class Initializer { /// /// Initializes a new instance of the class. /// /// Config file to use. /// Process ID. Only used for logging. public Initializer (string version, string configFile, int pid) { this.version = version; this.configFile = configFile; this.pid = pid; nodes = new List (); listeners = new List (); } private readonly string version; private readonly string configFile; private readonly int pid; private readonly List nodes; private readonly List listeners; 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 Nodes { get { return nodes; } } public bool Ready { get { return ready; } } /// /// Run the NTP Analyzer. /// public void Run () { // Neutralize. Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; bool proceed = InitializeConfiguration (); if (proceed) proceed = InitializeLog (); if (proceed) { InitializeState (); InitializeListerners (); InitializeCluster (); InitializeScheduler (); } ready = proceed; } /// /// Initializes the configuration. /// 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; } /// /// Initializes the log. /// 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; } /// /// Initializes the application and database state. /// 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; } /// /// Initializes the listerners. /// private void InitializeListerners () { try { foreach (ListenerConfiguration monitor in config.Monitors) { Listener listener = new Listener (monitor.Ip, monitor.Port, log); listener.Open (); listeners.Add (listener); log.WriteLine ("Listening " + listener, Severity.Notice); } } catch (Exception ex) { log.WriteLine ("Error during initialization of listerners.", Severity.Error); log.WriteLine (ex); } } /// /// Initializes the cluster nodes. /// 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); } } /// /// Initializes the scheduler. /// 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; } } }