Ejemplo n.º 1
0
 /// <summary>
 /// Initializes this configuration control to default values.
 ///
 /// <seealso cref="PluginEnvironment"/>
 /// </summary>
 /// <param name="environment">Information about the plug-in environment.</param>
 public void Initialize(PluginEnvironment environment)
 {
     // Initialize the activity data.
     _data = new BashLoggerActivityData();
     assetSelectionControl_bashLog.Initialize(AssetAttributes.Printer);
     BashCollectorServiceHostCheck(environment);
 }
Ejemplo n.º 2
0
        /// <summary>
        /// </summary>
        /// <param name="configuration">The configuration data.</param>
        /// <param name="environment">Information about the plug-in environment.</param>
        public void Initialize(PluginConfigurationData configuration, PluginEnvironment environment)
        {
            // Initialize the activity data by deserializing it from an existing copy of the
            // configuration information.
            _data = configuration.GetMetadata <BashLoggerActivityData>();
            assetSelectionControl_bashLog.Initialize(configuration.Assets, AssetAttributes.Printer);
            BashCollectorServiceHostCheck(environment);
            if (comboBoxLoggerAction.Enabled)
            {
                comboBoxLoggerAction.SelectedIndex = comboBoxLoggerAction.Items.IndexOf(_data.LoggerAction);
            }

            if (Directory.Exists(_data.FolderPath))
            {
                textBoxLoggerDirectory.Text = _data.FolderPath;
            }
        }
        /// <summary>
        ///Execution point for the plugin
        /// <seealso cref="PluginExecutionData"/>
        /// <seealso cref="PluginExecutionResult"/>
        /// <seealso cref="PluginResult"/>
        /// <param name="executionData">The execution data.</param>
        /// <returns>A <see cref="PluginExecutionResult"/> indicating the outcome of the
        /// execution.</returns>
        /// </summary>

        public PluginExecutionResult Execute(PluginExecutionData executionData)
        {
            var serviceHost = executionData.Environment.PluginSettings["BashLogCollectorServiceHost"];

            if (string.IsNullOrEmpty(serviceHost))
            {
                return(new PluginExecutionResult(PluginResult.Error, "Bash Logger Service Host setting missing. Please enter the value in Plugin Settings"));
            }

            _client = new BashLogCollectorClient(serviceHost);
            _data   = executionData.GetMetadata <BashLoggerActivityData>();

            try
            {
                Parallel.ForEach(executionData.Assets.OfType <IDeviceInfo>(),
                                 asset =>
                {
                    var assetId = _client.CreateLogger(asset.AssetId);
                    if (string.IsNullOrEmpty(assetId))
                    {
                        ExecutionServices.SystemTrace.LogError(
                            $"Unable to Create Logger for {asset.AssetId}, Please verify the bash logger information for the device in Asset Inventory");
                        return;
                    }
                    switch (_data.LoggerAction)
                    {
                    case LoggerAction.Start:
                        _client.StartLogging(assetId);
                        UpdateStatus($"Logging Started for device: {asset.AssetId}");
                        break;

                    case LoggerAction.Stop:
                        _client.StopLogging(assetId);
                        UpdateStatus($"Logging Stopped for device: {asset.AssetId}");
                        break;

                    case LoggerAction.CollectLog:
                        {
                            UpdateStatus($"Collecting Logs for device: {asset.AssetId}");
                            var log = _client.CollectLog(assetId);
                            UpdateStatus($"Collection of Logs completed for device: {asset.AssetId}");
                            _client.Flush(assetId);
                            UpdateStatus($"Clearing the Logs for device: {asset.AssetId}");
                            _bashLogConcurrentDictionary.AddOrUpdate(assetId, log, (key, oldvalue) => oldvalue + log);
                        }
                        break;
                    }
                });
            }
            catch (Exception e)
            {
                return(new PluginExecutionResult(PluginResult.Error, e.InnerException?.Message ?? e.Message));
            }

            if (_data.LoggerAction == LoggerAction.CollectLog)
            {
                foreach (var bashLogPair in _bashLogConcurrentDictionary)
                {
                    var fileSplitSize = _data.FileSplitSize * 1024 * 1024;
                    var numOfFiles    = bashLogPair.Value.Length / fileSplitSize + 1;

                    if (!Directory.Exists(_data.FolderPath))
                    {
                        UpdateStatus("Given path doesn't exist, switching to temporary location");
                        _data.FolderPath = Path.GetTempPath();
                    }

                    for (int i = 0; i < numOfFiles; i++)
                    {
                        using (
                            var logFile =
                                File.Create(
                                    Path.Combine(_data.FolderPath,
                                                 $"{bashLogPair.Key}_{executionData.SessionId}_BashLog_{i}.txt"), fileSplitSize,
                                    FileOptions.None))
                        {
                            byte[] buffer = new byte[fileSplitSize];

                            //complicated math below
                            //checks if the file split size is less than total file length, check for total log size is smaller than split size, check for last chunk file to be written to avoid null character entry
                            int charCount = fileSplitSize < bashLogPair.Value.Length
                                ? fileSplitSize < bashLogPair.Value.Length - i * fileSplitSize
                                    ? fileSplitSize
                                    : bashLogPair.Value.Length - i * fileSplitSize
                                : bashLogPair.Value.Length;
                            Encoding.ASCII.GetBytes(bashLogPair.Value, i * fileSplitSize, charCount, buffer,
                                                    buffer.GetLowerBound(0));
                            //write the log file
                            UpdateStatus($"Writing the log file: {logFile.Name}");
                            logFile.Write(buffer, 0, charCount);
                            logFile.Flush(true);
                        }
                    }
                }
            }

            return(new PluginExecutionResult(PluginResult.Passed));
        }