private void GetFileInformation(string path) { try { this.FileName = FileUtils.Name(path); this.FileFolder = FileUtils.Folder(path); this.FilePath = path; this.FileSize = FormatUtils.FormatFileSize(FileUtils.SizeInBytes(path)); this.FileLastModified = FileUtils.DateModified(path).ToString("D", new CultureInfo(ResourceUtils.GetString("Language_ISO639-1"))); } catch (Exception ex) { LogClient.Error("Error while getting file Information. Exception: {0}", ex.Message); } }
private string GetTop10FileInfos(List <FileInfo> fileInfos) { StringBuilder sb = new StringBuilder(); int topCount = 10; for (int i = 0; i < topCount && i < fileInfos.Count; i++) { FileInfo fi = fileInfos[i]; sb.AppendLine(string.Format("\t{0} - {1}", fi.Name, FormatUtils.FormatFileSize(fi.Length))); } if (fileInfos.Count > 10) { sb.AppendLine("..."); } return(sb.ToString()); }
private void GetFileInformation(TrackInfo selectedTrackInfo) { try { var fi = new FileInformation(selectedTrackInfo.Path); this.FileName = fi.Name; this.FileFolder = fi.Folder; this.FilePath = selectedTrackInfo.Path; this.FileSize = FormatUtils.FormatFileSize(fi.SizeInBytes); this.FileLastModified = fi.DateModified.ToString("D"); } catch (Exception ex) { LogClient.Instance.Logger.Error("Error while getting file Information. Exception: {0}", ex.Message); } }
public CollectorState GetState(DirectoryFileInfo fileInfo) { CollectorState returnState = CollectorState.Good; LastErrorMsg = ""; if (!fileInfo.Exists) { returnState = CollectorState.Error; LastErrorMsg = string.Format("Directory '{0}' not found or not accessible!", DirectoryPath); } else if (DirectoryExistOnly) { returnState = CollectorState.Good; } else if (fileInfo.FileCount == -1) { returnState = CollectorState.Error; LastErrorMsg = string.Format("An error occured while accessing '{0}'\r\n\t{1}", FilterFullPath, LastErrorMsg); } else if (ErrorOnFilesExist) { returnState = fileInfo.FileCount > 0 ? CollectorState.Error : CollectorState.Good; } else if (FilesExistOnly) { returnState = fileInfo.FileCount > 0 ? CollectorState.Good : CollectorState.Error; } else { if ( (CountErrorIndicator > 0 && CountErrorIndicator <= fileInfo.FileCount) || (SizeKBErrorIndicator > 0 && SizeKBErrorIndicator * 1024 <= fileInfo.FileSize) ) { returnState = CollectorState.Error; LastErrorMsg = string.Format("Error state reached for '{0}': {1} file(s), {2}", FilterFullPath, fileInfo.FileCount, FormatUtils.FormatFileSize(fileInfo.FileSize)); } else if ( (CountWarningIndicator > 0 && CountWarningIndicator <= fileInfo.FileCount) || (SizeKBWarningIndicator > 0 && SizeKBWarningIndicator * 1024 <= fileInfo.FileSize) ) { returnState = CollectorState.Warning; LastErrorMsg = string.Format("Warning state reached for '{0}': {1} file(s), {2}", FilterFullPath, fileInfo.FileCount, FormatUtils.FormatFileSize(fileInfo.FileSize)); } else { returnState = CollectorState.Good; } } return(returnState); }
private void cmdTest_Click(object sender, EventArgs e) { try { FileSystemDirectoryFilterEntry testEntry = new FileSystemDirectoryFilterEntry(); testEntry.DirectoryPath = txtDirectory.Text; testEntry.DirectoryExistOnly = optDirectoryExistOnly.Checked; testEntry.FileFilter = txtFilter.Text; testEntry.IncludeSubDirectories = chkIncludeSubDirs.Checked; testEntry.FilesExistOnly = optCheckIfFilesExistOnly.Checked; testEntry.ErrorOnFilesExist = optErrorOnFilesExist.Checked; testEntry.ContainsText = txtContains.Text; testEntry.UseRegEx = chkUseRegEx.Checked; testEntry.CountWarningIndicator = Convert.ToInt32(numericUpDownCountWarningIndicator.Value); testEntry.CountErrorIndicator = Convert.ToInt32(numericUpDownCountErrorIndicator.Value); testEntry.FileSizeIndicatorUnit = (FileSizeUnits)cboFileSizeIndicatorUnit.SelectedIndex; testEntry.SizeWarningIndicator = (int)numericUpDownSizeWarningIndicator.Value; testEntry.SizeErrorIndicator = (int)numericUpDownSizeErrorIndicator.Value; testEntry.FileAgeUnit = (TimeUnits)cboFileAgeUnit.SelectedIndex; testEntry.FileMinAge = (int)numericUpDownFileAgeMin.Value; testEntry.FileMaxAge = (int)numericUpDownFileAgeMax.Value; testEntry.FileSizeUnit = (FileSizeUnits)cboFileSizeUnit.SelectedIndex; testEntry.FileMinSize = (int)numericUpDownFileSizeMin.Value; testEntry.FileMaxSize = (int)numericUpDownFileSizeMax.Value; testEntry.ShowFilenamesInDetails = chkShowFilenamesInDetails.Checked; DirectoryFileInfo directoryFileInfo = testEntry.GetFileListByFilters(); CollectorState currentState = testEntry.GetState(directoryFileInfo); MessageBox.Show(string.Format("State: {0}\r\nDetails: {1} file(s), {2}", currentState, directoryFileInfo.FileCount, FormatUtils.FormatFileSize(directoryFileInfo.TotalFileSize)), "Test", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show(ex.Message, "Test", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void nudMemErr_ValueChanged(object sender, EventArgs e) { lblMemErrorFormatted.Text = FormatUtils.FormatFileSize((long)nudMemErr.Value * 1024); }
private CollectorState GetState(DirectoryFileInfo fileInfo) { CollectorState returnState = CollectorState.NotAvailable; stateDescription = ""; if (!fileInfo.DirectoryExists) { returnState = CollectorState.Error; stateDescription = string.Format("Directory '{0}' not found or not accessible!", DirectoryPath); } else if (DirectoryExistOnly) { returnState = CollectorState.Good; } else if (fileInfo.FileCount == -1) { returnState = CollectorState.Error; stateDescription = string.Format("An error occured while accessing '{0}'\r\n\t{1}", FilterFullPath, LastErrorMsg); } else if (ErrorOnFilesExist) { returnState = fileInfo.FileCount > 0 ? CollectorState.Error : CollectorState.Good; } else if (FilesExistOnly) { returnState = fileInfo.FileCount > 0 ? CollectorState.Good : CollectorState.Error; } else { if ((CountErrorIndicator > 0 || CountWarningIndicator > 0) && (CountWarningIndicator != CountErrorIndicator)) { if (CountWarningIndicator < CountErrorIndicator) { if (fileInfo.FileCount < CountWarningIndicator) { returnState = CollectorState.Good; } else if (fileInfo.FileCount >= CountErrorIndicator) { returnState = CollectorState.Error; } else { returnState = CollectorState.Warning; } } else if (fileInfo.FileCount > CountWarningIndicator) { returnState = CollectorState.Good; } else if (fileInfo.FileCount <= CountErrorIndicator) { returnState = CollectorState.Error; } else { returnState = CollectorState.Warning; } } if (returnState == CollectorState.Good || returnState == CollectorState.NotAvailable) { if ((SizeWarningIndicator > 0 || SizeErrorIndicator > 0) && SizeWarningIndicator != SizeErrorIndicator) { if (SizeWarningIndicator < SizeErrorIndicator) { if (FileSizeUnitsTools.ToBytes(FileSizeIndicatorUnit, SizeWarningIndicator) > fileInfo.TotalFileSize) { returnState = CollectorState.Good; } else if (FileSizeUnitsTools.ToBytes(FileSizeIndicatorUnit, SizeErrorIndicator) < fileInfo.TotalFileSize) { returnState = CollectorState.Error; } else { returnState = CollectorState.Warning; } } else { if (FileSizeUnitsTools.ToBytes(FileSizeIndicatorUnit, SizeWarningIndicator) < fileInfo.TotalFileSize) { returnState = CollectorState.Good; } else if (FileSizeUnitsTools.ToBytes(FileSizeIndicatorUnit, SizeErrorIndicator) > fileInfo.TotalFileSize) { returnState = CollectorState.Error; } else { returnState = CollectorState.Warning; } } } } if (returnState == CollectorState.Warning) { stateDescription = string.Format("Warning state reached for '{0}': {1} file(s), {2}", FilterFullPath, fileInfo.FileCount, FormatUtils.FormatFileSize(fileInfo.TotalFileSize)); } else if (returnState == CollectorState.Error) { stateDescription = string.Format("Error state reached for '{0}': {1} file(s), {2}", FilterFullPath, fileInfo.FileCount, FormatUtils.FormatFileSize(fileInfo.TotalFileSize)); } } return(returnState); }
public MonitorState GetCurrentState() { DirectoryFileInfo directoryFileInfo = GetFileListByFilters(); //int totalFileCount = 0; MonitorState currentState = new MonitorState() { ForAgent = DirectoryPath, State = GetState(directoryFileInfo) }; if (DirectoryExistOnly && currentState.State != CollectorState.Good) { currentState.CurrentValue = stateDescription; } else if (DirectoryExistOnly) { currentState.CurrentValue = stateDescription; } else { currentState.RawDetails = stateDescription; if (directoryFileInfo.FileCount == -1) { currentState.CurrentValue = stateDescription; } else { //totalFileCount += directoryFileInfo.FileCount; if (directoryFileInfo.FileCount > 0) { //if (LastErrorMsg.Length > 0) //{ // currentState.CurrentValue = string.Format("{0} file(s), {1}", directoryFileInfo.FileCount, FormatUtils.FormatFileSize(directoryFileInfo.TotalFileSize)); //} //else //{ //currentState.CurrentValue = string.Format("{0} file(s) found", directoryFileInfo.FileInfos.Count); if (ShowFileCountInOutputValue && ShowFileSizeInOutputValue) { currentState.CurrentValue = string.Format("{0} file(s), {1}", directoryFileInfo.FileCount, FormatUtils.FormatFileSize(directoryFileInfo.TotalFileSize)); } else if (ShowFileCountInOutputValue) { currentState.CurrentValue = directoryFileInfo.FileInfos.Count; currentState.CurrentValueUnit = "file(s)"; } else if (ShowFileSizeInOutputValue) { currentState.CurrentValue = FormatUtils.FormatFileSize(directoryFileInfo.TotalFileSize); } else { currentState.CurrentValue = stateDescription; } if (ShowFilenamesInDetails) { int topCount = 10; for (int i = 0; i < topCount && i < directoryFileInfo.FileInfos.Count; i++) { FileInfo fi = directoryFileInfo.FileInfos[i]; currentState.ChildStates.Add( new MonitorState() { ForAgent = fi.Name, ForAgentType = "FileInfo", CurrentValue = string.Format("{0}", FormatUtils.FormatFileSize(fi.Length)) }); } } //} } else { //currentState.CurrentValue = "No files found"; currentState.CurrentValue = "No"; currentState.CurrentValueUnit = "file(s)"; } } } CurrentAgentValue = currentState.CurrentValue; return(currentState); }
public override List <System.Data.DataTable> GetDetailDataTables() { List <System.Data.DataTable> tables = new List <System.Data.DataTable>(); System.Data.DataTable dt = new System.Data.DataTable(); try { dt.Columns.Add(new System.Data.DataColumn("Path", typeof(string))); dt.Columns.Add(new System.Data.DataColumn("Details", typeof(string))); FileSystemCollectorConfig currentConfig = (FileSystemCollectorConfig)AgentConfig; foreach (FileSystemDirectoryFilterEntry directoryFilter in currentConfig.Entries) { DirectoryFileInfo directoryFileInfo = directoryFilter.GetFileListByFilters(); string details = ""; try { if (!directoryFileInfo.DirectoryExists) { details = "Directory does not exists"; } else if (directoryFilter.DirectoryExistOnly) { details = "Directory exists"; } else { details = directoryFileInfo.DirectoryExists ? directoryFileInfo.FileCount.ToString() + " file(s), " + FormatUtils.FormatFileSize(directoryFileInfo.TotalFileSize) : "Directory does not exists"; } } catch (Exception ex) { details = ex.Message; } dt.Rows.Add(directoryFilter.Description, details); } } catch (Exception ex) { dt = new System.Data.DataTable("Exception"); dt.Columns.Add(new System.Data.DataColumn("Text", typeof(string))); dt.Rows.Add(ex.ToString()); } tables.Add(dt); return(tables); }
public MonitorState GetCurrentState() { DirectoryFileInfo directoryFileInfo = GetFileListByFilters(); string fullFilePath = Environment.ExpandEnvironmentVariables(DirectoryPath); MonitorState currentState = new MonitorState() { ForAgent = fullFilePath, State = GetState(directoryFileInfo) }; if (!fullFilePath.EndsWith("\\")) { fullFilePath += "\\"; } if (DirectoryExistOnly && currentState.State != CollectorState.Good) { currentState.CurrentValue = stateDescription; } else if (DirectoryExistOnly) { currentState.CurrentValue = stateDescription; } else { currentState.RawDetails = stateDescription; if (directoryFileInfo.FileCount == -1) { currentState.CurrentValue = stateDescription; } else { if (directoryFileInfo.FileCount > 0) { if (ShowFileCountInOutputValue && ShowFileSizeInOutputValue) { currentState.CurrentValue = string.Format("{0} file(s), {1}", directoryFileInfo.FileCount, FormatUtils.FormatFileSize(directoryFileInfo.TotalFileSize)); } else if (ShowFileCountInOutputValue) { currentState.CurrentValue = directoryFileInfo.FileInfos.Count; currentState.CurrentValueUnit = "file(s)"; } else if (ShowFileSizeInOutputValue) { currentState.CurrentValue = FormatUtils.FormatFileSize(directoryFileInfo.TotalFileSize); } else { currentState.CurrentValue = stateDescription; } if (ShowFilenamesInDetails) { int topCount = TopFileNameCountInDetails; if (topCount <= 0) { topCount = directoryFileInfo.FileInfos.Count; } for (int i = 0; i < TopFileNameCountInDetails && i < directoryFileInfo.FileInfos.Count; i++) { FileInfo fi = directoryFileInfo.FileInfos[i]; currentState.ChildStates.Add( new MonitorState() { ForAgent = fi.FullName.ReplaceCaseInsensitive(fullFilePath, ""), //ForAgent = fi.Name, ForAgentType = "FileInfo", CurrentValue = string.Format("{0},{1}", FormatUtils.FormatFileSize(fi.Length), fi.LastWriteTime.ToString("yyyy-MM-dd HH:mm")) }); } } } else { currentState.CurrentValue = "No"; currentState.CurrentValueUnit = "file(s)"; } } } CurrentAgentValue = currentState.CurrentValue; return(currentState); }
public MonitorState GetCurrentState() { List <ProcessInfo> processes = null; //CollectorState agentState = CollectorState.NotAvailable; StringBuilder sbRawDetails = new StringBuilder(); object reportedValue = ""; MonitorState currentState = new MonitorState() { ForAgent = Description, State = CollectorState.NotAvailable }; try { processes = GetValue(); currentState.State = GetStateFromValue(processes); if (processes == null || processes.Count == 0) { currentState.CurrentValue = "No processes!"; currentState.CurrentValueUnit = ""; sbRawDetails.Append("No process found!"); } else if (ProcessCollectorTestType == ProcessCollectorTestType.ProcessInstanceCount || ProcessCollectorTestType == ProcessCollectorTestType.ProcessInstanceNotRunning || ProcessCollectorTestType == ProcessCollectorTestType.ProcessInstanceRunning) { currentState.CurrentValue = processes.Count; currentState.CurrentValueUnit = "process(es)"; sbRawDetails.Append($"{processes.Count} process(es)"); } else if (ProcessCollectorTestType == ProcessCollectorTestType.ThreadCount) { currentState.CurrentValue = (from ProcessInfo p in processes select p.ThreadCount).Max(); currentState.CurrentValueUnit = "Thread(s)"; sbRawDetails.Append($"{processes.Count} process(es), Threads(Max): {currentState.CurrentValue}"); } else if (ProcessCollectorTestType == ProcessCollectorTestType.ProcessorUsage) { var maxCPU = (from ProcessInfo p in processes select p.CPUPerc).Max(); currentState.CurrentValue = maxCPU.ToString("0.0"); currentState.CurrentValueUnit = "%"; } else if (ProcessCollectorTestType == ProcessCollectorTestType.MemoryUsage) { var maxMem = (from ProcessInfo p in processes select p.WorkingSetSize).Max(); currentState.CurrentValue = FormatUtils.FormatFileSize(maxMem, false); currentState.CurrentValueUnit = FormatUtils.FormatFileSizeUnitOnly(maxMem); } } catch (Exception ex) { currentState.State = CollectorState.Error; sbRawDetails.AppendLine(ex.Message); } foreach (ProcessInfo p in processes) { MonitorState processState = new MonitorState() { ForAgent = p.ToString(), State = CollectorState.NotAvailable, CurrentValue = $"Threads:{p.ThreadCount}, Id:{p.ProcessId}", CurrentValueUnit = "" }; if (ProcessCollectorTestType == ProcessCollectorTestType.ProcessInstanceCount || ProcessCollectorTestType == ProcessCollectorTestType.ProcessInstanceRunning || ProcessCollectorTestType == ProcessCollectorTestType.ProcessInstanceNotRunning) { processState.CurrentValue = $"Id:{p.ProcessId}"; } else if (ProcessCollectorTestType == ProcessCollectorTestType.ThreadCount) { processState.CurrentValue = p.ThreadCount; processState.CurrentValueUnit = "Thread(s)"; } else if (ProcessCollectorTestType == ProcessCollectorTestType.ProcessorUsage) { processState.CurrentValue = p.CPUPerc.ToString("0.0"); processState.CurrentValueUnit = "%"; } else if (ProcessCollectorTestType == ProcessCollectorTestType.MemoryUsage) { processState.CurrentValue = FormatUtils.FormatFileSize(p.WorkingSetSize, false); processState.CurrentValueUnit = FormatUtils.FormatFileSizeUnitOnly(p.WorkingSetSize); } processState.RawDetails = $"ProcessId:{p.ProcessId}\r\n" + $"Path:{p.Path}\r\n" + $"CMD:{p.CommandLine}\r\n" + $"Threads:{p.ThreadCount}\r\n" + $"CPU:{p.CPUPerc.ToString("0.00")}%\r\n" + $"Mem:{FormatUtils.FormatFileSize(p.WorkingSetSize)}"; currentState.ChildStates.Add(processState); } return(currentState); }
public override MonitorState RefreshState() { MonitorState returnState = new MonitorState(); int errorCount = 0; int warningCount = 0; int okCount = 0; int totalFileCount = 0; int totalEntryCount = 0; try { FileSystemCollectorConfig currentConfig = (FileSystemCollectorConfig)AgentConfig; foreach (FileSystemDirectoryFilterEntry directoryFilter in currentConfig.Entries) { DirectoryFileInfo directoryFileInfo = directoryFilter.GetFileListByFilters(); CollectorState currentState = directoryFilter.GetState(directoryFileInfo); totalEntryCount++; if (directoryFilter.DirectoryExistOnly && currentState != CollectorState.Good) { errorCount++; returnState.ChildStates.Add( new MonitorState() { ForAgent = directoryFilter.DirectoryPath, State = CollectorState.Error, CurrentValue = directoryFilter.LastErrorMsg }); } else if (directoryFilter.DirectoryExistOnly) { okCount++; returnState.ChildStates.Add( new MonitorState() { ForAgent = directoryFilter.DirectoryPath, State = currentState, CurrentValue = directoryFilter.LastErrorMsg }); } else { if (directoryFileInfo.FileCount == -1) { errorCount++; returnState.ChildStates.Add( new MonitorState() { ForAgent = directoryFilter.DirectoryPath, State = CollectorState.Error, CurrentValue = directoryFilter.LastErrorMsg }); } else { totalFileCount += directoryFileInfo.FileCount; if (directoryFileInfo.FileCount > 0) { if (directoryFilter.LastErrorMsg.Length > 0) { returnState.ChildStates.Add( new MonitorState() { ForAgent = directoryFilter.DirectoryPath, State = currentState, CurrentValue = string.Format("{0} file(s), {1}", directoryFileInfo.FileCount, FormatUtils.FormatFileSize(directoryFileInfo.TotalFileSize)) }); } else { returnState.ChildStates.Add( new MonitorState() { ForAgent = directoryFilter.DirectoryPath, State = currentState, CurrentValue = string.Format("{0} file(s) found", directoryFileInfo.FileInfos.Count) }); if (directoryFilter.ShowFilenamesInDetails) { int topCount = 10; for (int i = 0; i < topCount && i < directoryFileInfo.FileInfos.Count; i++) { FileInfo fi = directoryFileInfo.FileInfos[i]; returnState.ChildStates.Add( new MonitorState() { ForAgent = fi.Name, State = currentState, CurrentValue = string.Format("{0}", FormatUtils.FormatFileSize(fi.Length)) }); } } } } else { returnState.ChildStates.Add( new MonitorState() { ForAgent = directoryFilter.DirectoryPath, State = currentState, CurrentValue = "No files found" }); } if (currentState == CollectorState.Warning) { warningCount++; } else if (currentState == CollectorState.Error) { errorCount++; } else { okCount++; } } } } if (errorCount > 0 && totalEntryCount == errorCount) // any errors { returnState.State = CollectorState.Error; } else if (okCount != totalEntryCount) //any warnings { returnState.State = CollectorState.Warning; } else { returnState.State = CollectorState.Good; } returnState.CurrentValue = totalFileCount; } catch (Exception ex) { returnState.RawDetails = ex.Message; returnState.HtmlDetails = ex.Message; returnState.State = CollectorState.Error; } return(returnState); }