Esempio n. 1
0
 private static async Task<InfluxDBClient> GetClientAsync (InfluxerConfigSection settings)
 {
     var client = new InfluxDBClient (settings.InfluxDB.InfluxUri, settings.InfluxDB.UserName, settings.InfluxDB.Password);
     var dbNames = await client.GetInfluxDBNamesAsync ();
     if (dbNames.Contains (settings.InfluxDB.DatabaseName))
         return client;
     else
     {
         await client.CreateDatabaseAsync (settings.InfluxDB.DatabaseName);
         return client;
     }
 }
Esempio n. 2
0
 public GenericFile ()
 {
     settings = InfluxerConfigSection.GetCurrentOrDefault ();
     pattern = new Regex (settings.GenericFile.ColumnSplitter, RegexOptions.Compiled);
     defaultTags = new Dictionary<string, string> ();
     if (settings.GenericFile.DefaultTags.Tags != null && settings.GenericFile.DefaultTags.Tags.Count > 0)
     {
         foreach (var tag in settings.GenericFile.DefaultTags.Tags)
         {
             var tags = tag.Split ('=');
             defaultTags.Add (tags[0], tags[1]);
         }
     }
 }
Esempio n. 3
0
 /// <summary>
 /// Loads the configuration into application from the file passed.
 /// </summary>
 /// <param name="path">Path to the file which contains valid configuration entries. Without InfluxerConfiguration section will raise an exception</param>
 /// <returns>InfluxerConfigSection created based on entries in config file</returns>
 public static InfluxerConfigSection Load(string path, bool force = false)
 {
     if (force || _instance == null)
     {
         if (!File.Exists(path))
         {
             throw new InvalidOperationException("Configuration file was not found!!, Please check the path and retry.");
         }
         try
         {
             _instance = JsonConvert.DeserializeObject <InfluxerConfigSection>(File.ReadAllText(path));
         }
         catch (Exception e)
         {
             throw new InvalidOperationException("Config file couldn't be loaded, Use Config switch to get a config file with default values");
         }
     }
     return(_instance);
 }
Esempio n. 4
0
        public static bool ProcessArguments(string[] args)
        {
            if (args.Length == 0)
            {
                var help = new StringBuilder();
                help.AppendLine($"Influxer Version: {FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).ProductVersion}");
                help.AppendLine($"Build: {FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).FileVersion}");
                Logger.Log(LogLevel.Info, help.ToString());
                throw new ArgumentException("Command line arguments not valid, try --help to see valid ones!");
            }

            #region Parse command line arguments

            Dictionary <string, string> cmdArgs = new Dictionary <string, string>();
            Regex commandSwitch = new Regex("^-[-a-zA-Z+]|^/[?a-zA-Z+]", RegexOptions.Compiled);
            for (int i = 0; i < args.Length; i++)
            {
                if (commandSwitch.IsMatch(args[i]))
                {
                    var key = args[i].ToLower();
                    if (i + 1 < args.Length && !commandSwitch.IsMatch(args[i + 1]))
                    {
                        cmdArgs.Add(key.ToLower(), args[i + 1]);
                        i++;
                    }
                    else
                    {
                        cmdArgs.Add(key.ToLower(), "true");
                    }
                }
            }

            var totalArguments = cmdArgs.Count;

            if (cmdArgs.ContainsKey("--help") || cmdArgs.ContainsKey("/help") || cmdArgs.ContainsKey("/?"))
            {
                var help = new StringBuilder();
                help.AppendLine($"Influxer Version: {FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).ProductVersion}");
                help.AppendLine($"Build: {FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).FileVersion}");
                help.AppendLine("Influxer is an application to parse log files, push data to Influx for later visualization.");
                help.AppendLine("It currently supports Windows Perfmon and any generic delimited file formats");
                help.AppendLine("It uses InfluxDB.Client.Net to interact with Influx.");
                help.AppendLine(new String('-', 180));
                help.AppendLine("Supported command line arguments");
                help.AppendLine("--help /? or /help  shows this help text\n");
                help.AppendLine();
                help.AppendLine("/export to print possible config section, pipe it to a file to edit and reuse the config");
                help.AppendLine();
                help.AppendLine("-config <configuration file path> to load the config file.");
                help.AppendLine();
                help.AppendLine("Any configuration entries can be overridden by command line switches shown below\n");
                help.AppendLine(new String('-', 180));
                help.Append(InfluxerConfigSection.LoadDefault().PrintHelpText());
                Logger.Log(LogLevel.Info, help.ToString());
                return(false);
            }

            if (cmdArgs.ContainsKey("-config"))
            {
                try
                {
                    var configFile = Path.GetFullPath(cmdArgs["-config"]);
                    _settings = InfluxerConfigSection.Load(configFile);
                    cmdArgs.Remove("-config");
                    totalArguments -= 1;
                }
                catch (Exception e)
                {
                    throw new FileLoadException($"Error Loading config file:{e.GetType().Name},{e.Message}", e);
                }
            }
            else
            {
                _settings = InfluxerConfigSection.LoadDefault();
            }

            #endregion Parse command line arguments

            if (totalArguments >= 1)
            {
                if (!(cmdArgs.Count == 1 && cmdArgs.ContainsKey("/export")))
                {
                    try
                    {
                        if (!_settings.ProcessCommandLineArguments(cmdArgs))
                        {
                            throw new ArgumentException("Invalid commandline arguments!! Use /help to see valid ones");
                        }
                    }
                    catch (Exception e)
                    {
                        throw new ArgumentException($"Error processing arguments :{e.GetType().Name}, {e.Message}", e);
                    }
                }
            }

            if (cmdArgs.ContainsKey("/export"))
            {
                if (cmdArgs.ContainsKey("/autolayout"))
                {
                    if (string.IsNullOrWhiteSpace(_settings.InputFileName))
                    {
                        throw new ArgumentException("No Input file name mentioned!!");
                    }

                    var g = new GenericFile();
                    g.GetFileLayout(_settings.InputFileName);
                    g.ValidateData(_settings.InputFileName);
                }
                //just to be able to write to console or test/debug window. Console.OpenStandardOutput () will do just fine
                using (MemoryStream stream = new MemoryStream())
                {
                    InfluxerConfigSection.Export(stream, totalArguments > 1 ? false : true);
                    StreamReader reader = new StreamReader(stream);
                    stream.Position = 0;
                    Logger.Log(LogLevel.Info, reader.ReadToEnd());
                }
                return(false);
            }

            if (cmdArgs.Count > 0)
            {
                throw new ArgumentException($"Unknown command line arguments: {String.Join(", ", cmdArgs.Select(c => c.Key))}");
            }
            return(true);
        }
Esempio n. 5
0
 /// <summary>
 /// Sets the configuration, mainly aids in testing
 /// </summary>
 /// <returns></returns>
 public static void SetCurrentSettings(InfluxerConfigSection settings)
 {
     _instance = settings;
 }
Esempio n. 6
0
 /// <summary>
 /// Returns default configuration settings for the application
 /// </summary>
 /// <returns>cref:InfluxerConfigSection</returns>
 public static InfluxerConfigSection LoadDefault()
 {
     _instance = new InfluxerConfigSection ();
     return _instance;
 }
Esempio n. 7
0
        /// <summary>
        /// Loads the configuration into application from the file passed.
        /// </summary>
        /// <param name="path">Path to the file which contains valid configuration entries. Without InfluxerConfiguration section will raise an exception</param>
        /// <returns>InfluxerConfigSection created based on entries in config file</returns>
        public static InfluxerConfigSection Load(string path)
        {
            if (_instance == null)
            {
                //if ( path.EndsWith (".config",
                //        StringComparison.InvariantCultureIgnoreCase) )
                //    path = path.Remove (path.Length - 7);
                //Configuration config =
                //        ConfigurationManager.OpenExeConfiguration (path);

                ExeConfigurationFileMap configMap = new ExeConfigurationFileMap ();
                configMap.ExeConfigFilename = path;
                Configuration config = ConfigurationManager.OpenMappedExeConfiguration (configMap, ConfigurationUserLevel.None);

                if (config.Sections["InfluxerConfiguration"] != null)
                {
                    _instance = (InfluxerConfigSection) config.Sections["InfluxerConfiguration"];
                }
                else
                {
                    if(config.HasFile)
                        throw new InvalidOperationException ("InfluxerConfiguration section was not found!!, Use Config switch to get a config file with default values");
                    else
                        throw new InvalidOperationException ("Configuration file was not found!!, Please check the path and retry.");
                }
            }
            return _instance;
        }
Esempio n. 8
0
 /// <summary>
 /// Sets the configuration, mainly aids in testing
 /// </summary>
 /// <returns></returns>
 public static void SetCurrentSettings(InfluxerConfigSection settings)
 {
     _instance = settings;
 }
Esempio n. 9
0
 /// <summary>
 /// Returns default configuration settings for the application
 /// </summary>
 /// <returns>cref:InfluxerConfigSection</returns>
 public static InfluxerConfigSection LoadDefault()
 {
     _instance = new InfluxerConfigSection();
     return(_instance);
 }
Esempio n. 10
0
        static int Main (string[] args)
        {
            try
            {
                if (!CommandLineProcessor.ProcessArguments (args))
                    return (int) ExitCode.Success;
                settings = CommandLineProcessor.Settings;
            }
            catch (ArgumentException e)
            {
                Logger.LogLine (LogLevel.Error, e.Message);
                return (int) ExitCode.InvalidArgument;
            }
            catch (FileLoadException e)
            {
                Logger.LogLine (LogLevel.Error, e.Message);
                Logger.LogLine (LogLevel.Info, "Problem loading config file, regenerate it with /config option");
                return (int) ExitCode.InvalidFilename;
            }
            catch (Exception e)
            {
                Logger.LogLine (LogLevel.Error, "Error processing arguments {0}: {1}", e.GetType ().Name, e.Message);
                return (int) ExitCode.InvalidArgument;
            }

            #region Validate inputs
            
            if (String.IsNullOrWhiteSpace (settings.InputFileName))
            {
                Logger.LogLine (LogLevel.Error, "Input File Name is not specified!! Can't continue");
                return (int) ExitCode.InvalidArgument;
            }

            try
            {
                settings.InputFileName = Path.GetFullPath (settings.InputFileName);
            }
            catch (Exception e)
            {
                Logger.LogLine (LogLevel.Error, "Error with input file:{0},{1}", e.GetType ().Name, e.Message);
                Logger.LogLine (LogLevel.Info, "Problem with inputfile name, check path");
                return (int) ExitCode.InvalidFilename;
            }

            if (String.IsNullOrWhiteSpace (settings.InfluxDB.InfluxUri))
            {
                Logger.LogLine (LogLevel.Error, "Influx DB Uri is not configured!!");
                return (int) ExitCode.InvalidArgument;
            }

            if (String.IsNullOrWhiteSpace (settings.InfluxDB.DatabaseName))
            {
                Logger.LogLine (LogLevel.Error, "Influx DB name is not configured!!");
                return (int) ExitCode.InvalidArgument;
            }

            #endregion

            ProcessStatus result = new ProcessStatus () { ExitCode = ExitCode.UnknownError };
            try
            {
                Stopwatch stopwatch = new Stopwatch ();
                stopwatch.Start ();

                var client = new InfluxDBClient (settings.InfluxDB.InfluxUri, settings.InfluxDB.UserName, settings.InfluxDB.Password);

                if (!VerifyDatabaseAsync (client, settings.InfluxDB.DatabaseName).Result)
                {
                    Logger.LogLine (LogLevel.Info, "Unable to create DB {0}", settings.InfluxDB.DatabaseName);
                    return (int) ExitCode.UnableToProcess;
                }
                switch (settings.FileFormat)
                {
                    case FileFormats.Perfmon:
                        result = new PerfmonFile ().ProcessPerfMonLog (settings.InputFileName, client).Result;
                        break;
                    case FileFormats.Generic:
                        if (String.IsNullOrWhiteSpace (settings.InfluxDB.Measurement))
                            throw new ArgumentException ("Generic format needs TableName input");
                        result = new GenericFile ().ProcessGenericFile (settings.InputFileName, client).Result;
                        break;
                }

                stopwatch.Stop ();
                Logger.LogLine (LogLevel.Info, "\n Finished!! Processed {0} points (Success: {1}, Failed:{2}) in {3}", result.PointsFound, result.PointsProcessed, result.PointsFailed, stopwatch.Elapsed.ToString ());

            }

            catch (AggregateException e)
            {
                Logger.LogLine (LogLevel.Error, "Error!! {0}:{1} - {2}", e.InnerException.GetType ().Name, e.InnerException.Message, e.InnerException.StackTrace);
            }

            catch (Exception e)
            {
                Logger.LogLine (LogLevel.Error, "Error!! {0}:{1} - {2}", e.GetType ().Name, e.Message, e.StackTrace);
            }
            return (int) result.ExitCode;
        }