Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        public DirectoryFileInfo GetFileListByFilters()
        {
            DirectoryFileInfo fileInfo;

            fileInfo.DirectoryExists = false;
            fileInfo.FileCount       = 0;
            fileInfo.TotalFileSize   = 0;
            fileInfo.FileInfos       = new List <FileInfo>();
            string fullFilePath = Environment.ExpandEnvironmentVariables(DirectoryPath);

            try
            {
                if (Directory.Exists(fullFilePath))
                {
                    fileInfo.DirectoryExists = true;
                    if (!DirectoryExistOnly)
                    {
                        SearchOption so = IncludeSubDirectories ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
                        foreach (string filePath in System.IO.Directory.GetFiles(fullFilePath, FileFilter, so))
                        {
                            System.IO.FileInfo fi = new System.IO.FileInfo(filePath);

                            if ((FileMaxAge == 0 || TimeUnitsTools.AddTime(fi.LastWriteTime, FileMaxAge, FileAgeUnit) > DateTime.Now) &&
                                (FileMinAge == 0 || TimeUnitsTools.AddTime(fi.LastWriteTime, FileMinAge, FileAgeUnit) < DateTime.Now) &&
                                (FileMaxSize == 0 || fi.Length <= FileSizeUnitsTools.ToBytes(FileSizeUnit, FileMaxSize)) &&
                                (FileMinSize == 0 || fi.Length >= FileSizeUnitsTools.ToBytes(FileSizeUnit, FileMinSize))
                                )
                            {
                                bool match = true;
                                if (ContainsText != null && ContainsText.Trim().Length > 0)
                                {
                                    string content = File.ReadAllText(filePath);
                                    if (UseRegEx)
                                    {
                                        System.Text.RegularExpressions.Match regMatch = System.Text.RegularExpressions.Regex.Match(content, ContainsText, System.Text.RegularExpressions.RegexOptions.Multiline);
                                        match = regMatch.Success;
                                    }
                                    else
                                    {
                                        match = content.Contains(ContainsText);
                                    }
                                }
                                if (match)
                                {
                                    fileInfo.TotalFileSize += fi.Length;
                                    fileInfo.FileCount     += 1;
                                    fileInfo.FileInfos.Add(fi);
                                }
                            }
                        }
                    }
                }
                else
                {
                    fileInfo.DirectoryExists = false;
                }
            }
            catch (Exception ex)
            {
                LastErrorMsg           = ex.Message;
                fileInfo.FileCount     = -1;
                fileInfo.TotalFileSize = -1;
            }
            return(fileInfo);
        }