//initial
        public bool SetPerformanceMonitorRecord(PerformanceMonitorRecord performanceMonitorRecord)
        {
            if (performanceMonitorRecord == null)
            {
                return(false);
            }

            string userName = _workContext.CurrentUser.UserName;

            performanceMonitorRecord.CreatedBy = userName;
            performanceMonitorRecord.Created   = DateTime.Now;
            performanceMonitorRecord.StartTime = DateTime.Now;
            performanceMonitorRecord.NodeName  = Environment.MachineName.ToUpperInvariant();

            IPAddress localIpAdress = IPAddress.Parse("127.0.0.1");

            performanceMonitorRecord.IpAddress = localIpAdress.ToString();
            performanceMonitorRecord.IsEnabled = true;

            performanceMonitorRecord.EndTime = DateTime.Now.AddHours(Convert.ToDouble(performanceMonitorRecord.Duration));

            //performanceMonitorRecord from string "1-24" to  double value to timespan ticks (nanoseconds)
            TimeSpan durationCount = TimeSpan.FromHours(Convert.ToDouble(performanceMonitorRecord.Duration));

            performanceMonitorRecord.Duration = durationCount.Ticks.ToString();

            _performanceMonitorRecordRepository.Update(performanceMonitorRecord);

            return(true);
        }
Example #2
0
        public void HeartBeat()
        {
            //perform and process the counterreading every 'heartbeat'
            PerformanceMonitorRecord activeMonitorRecord =
                _performanceMonitorDataService.PerformanceMonitorRecords.FirstOrDefault(r => r.IsEnabled == true);

            string performanceCounterReading =
                _performanceCounterService.GetPerformanceCounterReading(activeMonitorRecord.CategoryName, activeMonitorRecord.InstanceName, activeMonitorRecord.CounterName);

            //calculate the the duration (in ticks)
            DateTime startTime     = (DateTime)activeMonitorRecord.Created;
            TimeSpan durationCount = DateTime.Now.Subtract(startTime);

            //threshold check
            bool   passedThreshold;
            var    passedThresholdValue = 0;
            double countervalue         = double.Parse(performanceCounterReading);

            passedThreshold = CheckThreshold(activeMonitorRecord.Threshold, activeMonitorRecord.ThresholdWhen, countervalue, ref passedThresholdValue);

            PerformanceMonitorDataRecord dataRecord = new PerformanceMonitorDataRecord()
            {
                PerformanceMonitorRecord_Id = activeMonitorRecord.Id,
                Count                = performanceCounterReading,
                HeartBeat            = DateTime.Now,
                PassedThreshold      = passedThreshold,
                PassedThresholdValue = passedThresholdValue,
                Ticks                = Convert.ToString(durationCount.Ticks)
            };

            _performanceMonitorDataService.WriteDataRecord(dataRecord);
        }
        public PerformanceMonitorRecord DeletePermanceMonitorRecord(int id)
        {
            PerformanceMonitorRecord recordToDelete =
                _performanceMonitorDataService.PerformanceMonitorRecords.FirstOrDefault(r => r.Id == id);

            if (recordToDelete == null)
            {
                _notifier.Error(T("Could not find the performance counter record with id {0}", id));
                return(null);
            }

            //check and delete childrecords
            List <PerformanceMonitorDataRecord> dataRecordsToDelete =
                _performanceMonitorDataService.GetDataRecords(id);

            if (dataRecordsToDelete.Count > 0)
            {
                foreach (PerformanceMonitorDataRecord dataRecordToDelete in dataRecordsToDelete)
                {
                    _performanceMonitorDataService.DeletePerformanceMonitorDataRecord(dataRecordToDelete);
                }
            }

            if (_performanceMonitorDataService.DeletePerformanceMonitorRecord(recordToDelete))
            {
                _notifier.Information(T("performance counter is deleted"));
            }
            else
            {
                _notifier.Error(T("Failed to delete performance counter"));
            }

            return(recordToDelete);
        }
        //[HandleError, OrchardAuthorization(PermissionName = OrchardPermissionStrings.SiteOwner)]
        public ActionResult Show(ShowInputModel inputModel)
        {
            int performanceMonitorRecordId = inputModel.PerformanceMonitorRecord_Id;

            ShowViewModel viewModel = _performanceMonitorWorkerService.Show(performanceMonitorRecordId);

            viewModel.PerformanceMonitorRecord_Id = performanceMonitorRecordId;

            if (inputModel.AutoRefresh == true)
            {
                viewModel.AutoRefresh = true;
            }
            else
            {
                viewModel.AutoRefresh = false;
            }

            PerformanceMonitorRecord currentMonitorRecord = _performanceMonitorWorkerService.GetPerformanceMonitorRecord(performanceMonitorRecordId);

            int    maxLength         = 20;
            string labelCounter      = string.Empty;
            var    counterNameString = currentMonitorRecord.CounterName;

            if (counterNameString.Length <= maxLength)
            {
                labelCounter = counterNameString;
            }
            else
            {
                labelCounter = Truncate(counterNameString, maxLength);
                labelCounter = string.Concat(labelCounter, "...");
            }
            viewModel.LabelCounter   = labelCounter;
            viewModel.Threshold      = currentMonitorRecord.Threshold;
            viewModel.ThresholdWhen  = currentMonitorRecord.ThresholdWhen;
            viewModel.SampleInterval = currentMonitorRecord.SampleInterval;

            viewModel.MeanValue    = currentMonitorRecord.Mean;
            viewModel.MinimumValue = currentMonitorRecord.Minimum;
            viewModel.MaximumValue = currentMonitorRecord.Maximum;

            List <PerformanceMonitorDataRecord> dataRecordsToShow = _performanceMonitorWorkerService.Plot(performanceMonitorRecordId);

            viewModel.LineName   = "Countervalues";
            viewModel.LabelAxisY = currentMonitorRecord.CounterName.ToString();
            viewModel.id         = 1;
            viewModel.data       = new List <PlotDataViewModel>();

            foreach (PerformanceMonitorDataRecord dataRecord in dataRecordsToShow)
            {
                double valueCount = double.Parse(dataRecord.Count);
                viewModel.data.Add(new PlotDataViewModel()
                {
                    Ticks = dataRecord.Ticks,
                    Value = valueCount
                });
            }
            return(View(viewModel));
        }
        public bool DeletePerformanceMonitorRecord(PerformanceMonitorRecord recordToDelete)
        {
            if (recordToDelete == null)
            {
                return(false);
            }
            _performanceMonitorRecordRepository.Delete(recordToDelete);

            return(true);
        }
        //counter stopped : update records
        public bool UpdatePerformanceMonitorRecord(PerformanceMonitorRecord performanceMonitorRecord)
        {
            if (performanceMonitorRecord == null)
            {
                return(false);
            }

            performanceMonitorRecord.IsEnabled = false;
            _performanceMonitorRecordRepository.Update(performanceMonitorRecord);

            return(true);
        }
        public void Sweep()
        {
            PerformanceMonitorDataRecord lastCounterDataRecord = _performanceMonitorDataService.PerformanceMonitorDataRecords.LastOrDefault();
            PerformanceMonitorRecord     activeMonitorRecord   = _performanceMonitorDataService.ActiveRecord;

            if (activeMonitorRecord == null)
            {
                return;
            }

            //calculate the the duration in seconds between the last registration and the current time of sweep()
            DateTime lastHeartBeat        = lastCounterDataRecord.HeartBeat;
            TimeSpan totalDuration        = DateTime.Now.Subtract(lastHeartBeat);
            double   totalDurationSeconds = totalDuration.TotalSeconds;


            //stop the counter automatically if the endtime has been reached (or exceeded)
            if (activeMonitorRecord.EndTime <= DateTime.Now)
            {
                _performanceMonitorWorkerService.StopCounter(activeMonitorRecord.Id);
            }
            else
            {
                int    interval        = activeMonitorRecord.SampleInterval;
                double intervalSeconds = interval * 60;

                //write a new datarecord when the last heartbeat (datetime) was at least
                //the given interval (in minutes) in the past (interval negative)
                if (lastCounterDataRecord.HeartBeat < DateTime.Now.AddMinutes((interval * -1)))
                {
                    _performanceMonitorService.HeartBeat();
                }
                else
                {
                    //sometimes Sweep method runs outside of the minute interval
                    //to compensate, we use a little 'slack'
                    //see: http://orchard.codeplex.com/workitem/20383
                    var diff = Math.Abs(intervalSeconds - totalDurationSeconds);
                    if (diff <= 10)
                    {
                        _performanceMonitorService.HeartBeat();
                    }
                }
            }
        }
        public PerformanceMonitorRecord GetPerformanceMonitorRecord(int performanceMonitorRecordId)
        {
            PerformanceMonitorRecord record = _performanceMonitorDataService.PerformanceMonitorRecords.FirstOrDefault(r => r.Id == performanceMonitorRecordId);

            return(record);
        }
        public bool StopCounter(int id)
        {
            PerformanceMonitorRecord recordToUpdate =
                _performanceMonitorDataService.PerformanceMonitorRecords.FirstOrDefault(r => r.Id == id);

            string performanceCounterReading =
                _performanceCounterService.GetPerformanceCounterReading(recordToUpdate.CategoryName, recordToUpdate.InstanceName, recordToUpdate.CounterName);

            //threshold check
            bool   passedThreshold;
            var    passedThresholdValue = 0;
            double countervalue         = double.Parse(performanceCounterReading);

            passedThreshold = _performanceMonitorService.CheckThreshold(recordToUpdate.Threshold, recordToUpdate.ThresholdWhen, countervalue, ref passedThresholdValue);

            //calculate the the duration (in ticks)
            DateTime startTime     = (DateTime)recordToUpdate.Created;
            TimeSpan durationCount = DateTime.Now.Subtract(startTime);

            DateTime sharedTimeStampValue = DateTime.Now;

            //first write last values to datarecord (then to the monitorrecord for min/max/average)
            PerformanceMonitorDataRecord datarecord = new PerformanceMonitorDataRecord()
            {
                PerformanceMonitorRecord_Id = recordToUpdate.Id,
                //should be finally equal to latest reading in performancemonitorrecord
                Count                = performanceCounterReading,
                HeartBeat            = sharedTimeStampValue,
                PassedThreshold      = passedThreshold,
                PassedThresholdValue = passedThresholdValue,
                //duration count(ticks in nanoseconds from starttime (created)) should be equal to value in performancemonitorrecord
                Ticks = Convert.ToString(durationCount.Ticks)
            };

            var result = _performanceMonitorDataService.WriteDataRecord(datarecord);

            if (result)
            {
                recordToUpdate.LastValue     = performanceCounterReading;
                recordToUpdate.DurationCount = Convert.ToString(durationCount.Ticks);
                recordToUpdate.EndTime       = sharedTimeStampValue;

                //mean, min and max values
                List <PerformanceMonitorDataRecord> dataRecords = _performanceMonitorDataService.GetDataRecords(id);
                //first convert counted values from string to int
                List <double> countedValues    = new List <double>();
                bool          conversionResult = false;
                foreach (PerformanceMonitorDataRecord record in dataRecords)
                {
                    double value;
                    bool   conversionPassed = double.TryParse(record.Count, out value);
                    if (conversionPassed)
                    {
                        countedValues.Add(value);
                        conversionResult = true;
                    }
                    else
                    {
                        //conversion failed
                        conversionResult = false;
                    }
                }
                double meanValue = 0;
                double minValue  = 0;
                double maxValue  = 0;

                if (conversionResult)
                {
                    meanValue = Math.Round(countedValues.Average(), 2);
                    minValue  = Math.Round(countedValues.Min(), 2);
                    maxValue  = Math.Round(countedValues.Max(), 2);
                }
                recordToUpdate.Mean    = meanValue;
                recordToUpdate.Minimum = minValue;
                recordToUpdate.Maximum = maxValue;

                var monitorrecordresult = _performanceMonitorDataService.UpdatePerformanceMonitorRecord(recordToUpdate);
                if (monitorrecordresult)
                {
                    _notifier.Information(T("Counter stopped and all values updated succesfully"));
                }
                else
                {
                    _notifier.Error(T("Error: Failed to stop counter and update the monitor record"));
                }
            }
            else
            {
                _notifier.Error(T("Error: Failed to stop counter and update all records"));
            }

            return(result);
        }
        public bool EditPost(EditPostInputModel inputModel)
        {
            string performanceCounterReading =
                _performanceCounterService.GetPerformanceCounterReading(inputModel.CategoryName, inputModel.InstanceName, inputModel.CounterName);

            if (performanceCounterReading == String.Empty)
            {
                _notifier.Error(T("Error: Not able to perform a reading on this type of counter"));
                return(false);
            }

            PerformanceMonitorRecord record = new PerformanceMonitorRecord()
            {
                Id             = inputModel.Id,
                CategoryName   = inputModel.CategoryName,
                InstanceName   = inputModel.InstanceName,
                CounterName    = inputModel.CounterName,
                InitialValue   = performanceCounterReading,
                Duration       = inputModel.Duration,
                SampleInterval = inputModel.SampleInterval,
                Threshold      = inputModel.Threshold,
                ThresholdWhen  = inputModel.ThresholdWhen
            };

            var result = _performanceMonitorDataService.SetPerformanceMonitorRecord(record);

            if (result)
            {
                PerformanceMonitorRecord activeRecord =
                    _performanceMonitorDataService.PerformanceMonitorRecords.FirstOrDefault(r => r.IsEnabled == true);

                //threshold check
                bool   passedThreshold;
                var    passedThresholdValue = 0;
                double countervalue         = double.Parse(activeRecord.InitialValue);

                passedThreshold = _performanceMonitorService.CheckThreshold(activeRecord.Threshold, activeRecord.ThresholdWhen, countervalue, ref passedThresholdValue);

                PerformanceMonitorDataRecord datarecord = new PerformanceMonitorDataRecord()
                {
                    PerformanceMonitorRecord_Id = activeRecord.Id,
                    Count                = activeRecord.InitialValue,
                    HeartBeat            = activeRecord.Created,
                    PassedThreshold      = passedThreshold,
                    PassedThresholdValue = passedThresholdValue,

                    //duration count(ticks in nanoseconds from starttime (created)) should initially be set to startvalue (zero)
                    Ticks = "0"
                };

                var dataResult = _performanceMonitorDataService.WriteDataRecord(datarecord);
                if (dataResult)
                {
                    _notifier.Information(T("New counter records added succesfully"));
                }
                else
                {
                    _notifier.Warning(T("New counter record added succesfully, but failed to write initial datarecord"));
                }
            }
            else
            {
                _notifier.Error(T("Error: Failed to add new counter"));
            }
            return(result);
        }
        public EditViewModel Edit(EditInputModel inputModel)
        {
            PerformanceMonitorRecord recordToEdit = _performanceMonitorDataService.PerformanceMonitorRecords.FirstOrDefault(r => r.Id == inputModel.Id);

            if (recordToEdit != null)
            {
                //edit an existing record (no implementation in current version)
            }

            //No existing record in the database with the given id, create a new one
            if (recordToEdit == null)
            {
                recordToEdit = new PerformanceMonitorRecord();

                recordToEdit.Id           = 0;
                recordToEdit.CategoryName = inputModel.CategoryName;
                if (inputModel.InstanceName != null)
                {
                    recordToEdit.InstanceName = inputModel.InstanceName;
                }

                recordToEdit.CounterName = inputModel.CounterName;

                if (inputModel.SampleInterval <= 0)
                {
                    recordToEdit.SampleInterval = 1;
                }
                else
                {
                    recordToEdit.SampleInterval = inputModel.SampleInterval;
                }

                if (inputModel.Duration == null || Int32.Parse(inputModel.Duration) <= 0)
                {
                    recordToEdit.Duration = "1";
                }
                else
                {
                    recordToEdit.Duration = inputModel.Duration;
                }
                recordToEdit.Threshold = 0;
                //ThresholdWhen default value is 'Above'
                recordToEdit.ThresholdWhen = false;
            }

            string        myCatName = inputModel.CategoryName;
            List <string> performanceCounters;

            if (inputModel.InstanceName == "none")
            {
                performanceCounters = _performanceMonitorService.GetCounterNames(myCatName, null);
            }
            else
            {
                string myInstanceName = inputModel.InstanceName;
                performanceCounters = _performanceMonitorService.GetCounterNames(myCatName, myInstanceName);
            }

            var counterList = performanceCounters.Select(c => new SelectListItem
            {
                Text  = c.ToString(),
                Value = c.ToString()
            });


            Dictionary <string, string> thresholdWhenValues = new Dictionary <string, string>();

            thresholdWhenValues.Add("false", "Above");
            thresholdWhenValues.Add("true", "Below");

            var thresholdWhenList = thresholdWhenValues.Select(c => new SelectListItem
            {
                Value = c.Key,
                Text  = c.Value
            });

            EditViewModel viewModel = new EditViewModel()
            {
                Id                = recordToEdit.Id,
                CategoryName      = inputModel.CategoryName,
                CounterList       = new SelectList(counterList, "Value", "Text", recordToEdit.CounterName),
                ThresholdWhenList = new SelectList(thresholdWhenList, "Value", "Text", recordToEdit.ThresholdWhen),
                Duration          = recordToEdit.Duration,
                SampleInterval    = recordToEdit.SampleInterval
            };

            return(viewModel);
        }