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); }
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); }
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); } }
public override MonitorState GetState() { MonitorState returnState = new MonitorState(); StringBuilder plainTextDetails = new StringBuilder(); StringBuilder htmlTextTextDetails = new StringBuilder(); int errorCount = 0; int warningCount = 0; int okCount = 0; int totalFileCount = 0; int totalEntryCount = 0; try { htmlTextTextDetails.AppendLine("<ul>"); FileSystemCollectorConfig currentConfig = (FileSystemCollectorConfig)AgentConfig; foreach (FileSystemDirectoryFilterEntry directoryFilter in currentConfig.Entries) { DirectoryFileInfo directoryFileInfo = directoryFilter.GetDirFileInfo(); CollectorState currentState = directoryFilter.GetState(directoryFileInfo); totalEntryCount++; if (directoryFilter.DirectoryExistOnly && currentState != CollectorState.Good) { errorCount++; plainTextDetails.AppendLine(directoryFilter.LastErrorMsg); htmlTextTextDetails.AppendLine("<li>" + directoryFilter.LastErrorMsg + "</li>"); } else if (!directoryFilter.DirectoryExistOnly) { if (directoryFileInfo.FileCount == -1) { errorCount++; plainTextDetails.AppendLine(string.Format("An error occured while accessing '{0}'\r\n\t{1}", directoryFilter.FilterFullPath, directoryFilter.LastErrorMsg)); htmlTextTextDetails.AppendLine(string.Format("<li>'{0}' - Error accessing files<blockquote>{1}</blockquote></li>", directoryFilter.FilterFullPath, directoryFilter.LastErrorMsg)); } else { totalFileCount += directoryFileInfo.FileCount; if (directoryFileInfo.FileCount > 0) { htmlTextTextDetails.AppendLine("<li>"); if (directoryFilter.LastErrorMsg.Length > 0) { plainTextDetails.AppendLine(directoryFilter.LastErrorMsg); htmlTextTextDetails.AppendLine(directoryFilter.LastErrorMsg); } plainTextDetails.AppendLine(GetTop10FileInfos(directoryFileInfo.FileInfos)); htmlTextTextDetails.AppendLine("<blockquote>"); htmlTextTextDetails.AppendLine(GetTop10FileInfos(directoryFileInfo.FileInfos).Replace("\r\n", "<br/>")); htmlTextTextDetails.AppendLine("</blockquote></li>"); } else { plainTextDetails.AppendLine(string.Format("No files found '{0}'", directoryFilter.FilterFullPath)); htmlTextTextDetails.AppendLine(string.Format("<li>'{0}' - No files found</li>", directoryFilter.FilterFullPath)); } if (currentState == CollectorState.Warning) { warningCount++; } else if (currentState == CollectorState.Error) { errorCount++; } else { okCount++; } } } else { okCount++; } } htmlTextTextDetails.AppendLine("</ul>"); 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.RawDetails = plainTextDetails.ToString().TrimEnd('\r', '\n'); returnState.HtmlDetails = htmlTextTextDetails.ToString(); returnState.CurrentValue = totalFileCount; } catch (Exception ex) { //LastError = 1; //LastErrorMsg = ex.Message; returnState.RawDetails = ex.Message; returnState.HtmlDetails = ex.Message; returnState.State = CollectorState.Error; } return(returnState); }
private void LoadDirInfo(ListViewItem itm) { //int count = 0; string countString = "-"; string oldValue = "-"; try { FileSystemDirectoryFilterEntry filterEntry = (FileSystemDirectoryFilterEntry)itm.Tag; DirectoryFileInfo fileInfo = filterEntry.GetDirFileInfo(); CollectorState currentState = filterEntry.GetState(fileInfo); oldValue = itm.SubItems[1].Text; if (!fileInfo.Exists) { if (filterEntry.DirectoryExistOnly) { countString = "No"; } else { countString = "N/A"; } } else { if (filterEntry.DirectoryExistOnly) { countString = "Yes"; } else { countString = fileInfo.FileCount.ToString(); } } if (!filterEntry.DirectoryExistOnly && fileInfo.FileSize >= 0) { itm.SubItems[2].Text = (fileInfo.FileSize / 1024).ToString(); } else { itm.SubItems[2].Text = "N/A"; } if (currentState == CollectorState.Good) { itm.BackColor = SystemColors.Window; } else if (currentState == CollectorState.Warning) { itm.BackColor = Color.SandyBrown; } else { itm.BackColor = Color.Salmon; } } catch (Exception anyex) { itm.SubItems[1].Text = "err:" + anyex.Message; } //string oldValue = itm.SubItems[1].Text; itm.SubItems[1].Text = countString; if ((oldValue != "-") && (oldValue != countString)) { UpdateFont(itm, new System.Drawing.Font("Microsoft Sans Serif", 8.25f, System.Drawing.FontStyle.Bold)); } else { UpdateFont(itm, new System.Drawing.Font("Microsoft Sans Serif", 8.25f, System.Drawing.FontStyle.Regular)); } }
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 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 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); }