/// <summary>
        /// Gets the directory quota limit.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <returns>Quota limit in bytes.</returns>
        public long GetDirectoryQuota(string path)
        {
            IFsrmQuota dirQutoa = this.GetDirectoryFsrmQuota(path);

            dirQutoa.RefreshUsageProperties();
            return((long)(decimal)dirQutoa.QuotaLimit);
        }
        public long GetDirectorySize(string path)
        {
            path = CanonicalPath(path);
            IFsrmQuota dirQutoa = this.GetDirectoryFsrmQuota(path);

            dirQutoa.RefreshUsageProperties();

            int retires = 400;

            // FsrmQuotaFlags_StatusRebuilding   = 0x00020000
            while ((dirQutoa.QuotaFlags & 0x00020000) != 0)
            {
                if (--retires < 0)
                {
                    throw new TimeoutException(string.Format(CultureInfo.InvariantCulture, "Quota for dir '{0}' is still in StatusRebuilding state.", path));
                }

                dirQutoa.RefreshUsageProperties();
                Thread.Sleep(10);
            }

            // FsrmQuotaFlags_StatusIncomplete  = 0x00010000
            if ((dirQutoa.QuotaFlags & 0x00010000) != 0)
            {
                this.quotaManager.Scan(path);
                dirQutoa.RefreshUsageProperties();
            }

            dirQutoa.RefreshUsageProperties();
            return((long)(decimal)dirQutoa.QuotaUsed);
        }
        /// <summary>
        /// Enforces the quota.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <param name="sizeBytes">The size bytes.</param>
        public void SetDirectoryQuota(string path, long sizeBytes)
        {
            path = CanonicalPath(path);
            IFsrmQuota dirQuota = this.GetDirectoryFsrmQuota(path);

            dirQuota.QuotaLimit = sizeBytes;
            dirQuota.QuotaFlags = 0x00000100; // Hard quota enforcement
            dirQuota.Commit();
        }
        /// <summary>
        /// Gets the directory quota.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <returns>The fsrm quota instance.</returns>
        private IFsrmQuota GetDirectoryFsrmQuota(string path)
        {
            path = CanonicalPath(path);
            IFsrmQuota dirQuota = null;

            lock (this.quotasCacheLock)
            {
                this.quotasCache.TryGetValue(path, out dirQuota);
            }

            if (dirQuota == null)
            {
                try
                {
                    dirQuota = this.quotaManager.GetQuota(path);
                }
                catch (COMException ex)
                {
                    if (ex.ErrorCode == -2147200255)
                    {
                        // Error code: 0x80045301
                        // The specified quota could not be found.
                        dirQuota = null;
                    }
                    else if (ex.ErrorCode == -2147200252)
                    {
                        // Error code: 0x80045301
                        // The quota for the specified path could not be found.
                        throw new DirectoryNotFoundException(path);
                    }
                    else
                    {
                        throw;
                    }
                }

                if (dirQuota == null)
                {
                    dirQuota            = this.quotaManager.CreateQuota(path);
                    dirQuota.QuotaLimit = (long)1024 * 1024 * 1024 * 10; // 10 GiB

                    // FsrmQuotaFlags_Enforce            = 0x00000100,
                    // FsrmQuotaFlags_Disable            = 0x00000200,
                    // FsrmQuotaFlags_StatusIncomplete   = 0x00010000,
                    // FsrmQuotaFlags_StatusRebuilding   = 0x00020000
                    dirQuota.QuotaFlags = 0x00000000; // Soft quota enforcement
                    dirQuota.Commit();
                }

                lock (this.quotasCacheLock)
                {
                    this.quotasCache[path] = dirQuota;
                }
            }

            return(dirQuota);
        }
예제 #5
0
        static void Main(string[] args)
        {
            String  path     = null;
            decimal critical = 0;
            decimal warning  = 0;
            decimal limit    = 0;
            decimal used     = 0;

            if (args.Length != 3)
            {
                Program.Usage();
                Environment.Exit(3);
            }
            try {
                path     = args[0];
                critical = decimal.Parse(args[2]);
                warning  = decimal.Parse(args[1]);
            } catch (Exception e) {
                Console.WriteLine();
                throw new ApplicationException("Error converting arguments!", e);
            }
            try {
                IFsrmQuotaManager quotaManager = new FsrmQuotaManager();
                IFsrmQuota        quota        = quotaManager.GetQuota(path);
                limit = quota.QuotaLimit / 1024 / 1024;
                used  = quota.QuotaUsed / 1024 / 1024;
            }
            catch (Exception e)
            {
                throw new ApplicationException("Error getting Quota '" + path + "'", e);
            }
            CultureInfo enUS         = CultureInfo.CreateSpecificCulture("en-US");
            String      usageToLimit = (used / 1024).ToString("N") + "GB/" + (limit / 1024).ToString("N") + "GB";
            String      perfdata     = "'" + path + "'=" +
                                       used.ToString("F0", enUS) + "MB;" +
                                       (limit * (warning / 100)).ToString("F0", enUS) + ";" +
                                       (limit * (critical / 100)).ToString("F0", enUS);

            if (used >= (limit * (critical / 100)))
            {
                PrintOutput(2, path, usageToLimit, perfdata);
                Console.WriteLine();
                Environment.Exit(2);
            }
            if (used >= (limit * (warning / 100)))
            {
                PrintOutput(1, path, usageToLimit, perfdata);
                Environment.Exit(1);
            }
            PrintOutput(0, path, usageToLimit, perfdata);
            Environment.Exit(0);
        }
예제 #6
0
        public QuotaInfo GetQuotaFromPath(string path)
        {
            IFsrmQuotaManager FSRMQuotaManager = new FsrmQuotaManagerClass();
            IFsrmQuota        Quota            = null;
            QuotaInfo         q      = new QuotaInfo();
            Decimal           qFree  = 0;
            Decimal           qTotal = 0;
            Decimal           qUsed  = 0;

            try
            {
                Quota   = FSRMQuotaManager.GetQuota(path);
                qFree   = Math.Round((Decimal)Quota.QuotaLimit - (Decimal)Quota.QuotaUsed, 0);
                qUsed   = (Decimal)Quota.QuotaUsed;
                qTotal  = (Decimal)Quota.QuotaLimit;
                q.Free  = Convert.ToDouble(qFree.ToString());
                q.Total = Convert.ToDouble(qTotal.ToString());
                q.Used  = Convert.ToDouble(qUsed.ToString());
            }
            catch
            {
                path = GetPath(path);
                try
                {
                    Quota   = FSRMQuotaManager.GetQuota(path);
                    qFree   = (Decimal)Quota.QuotaLimit - (Decimal)Quota.QuotaUsed;
                    qUsed   = (Decimal)Quota.QuotaUsed;
                    qTotal  = (Decimal)Quota.QuotaLimit;
                    q.Free  = Convert.ToDouble(qFree.ToString());
                    q.Total = Convert.ToDouble(qTotal.ToString());
                    q.Used  = Convert.ToDouble(qUsed.ToString());
                }
                catch
                {
                }
            }
            return(q);
        }
        /// <summary>
        /// Removes the directory quota.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <returns>True if quota was found and deleted. False if quota was not found.</returns>
        public bool RemoveDirectoryQuota(string path)
        {
            path = CanonicalPath(path);
            bool ret = false;

            IFsrmQuota dirQuota = null;

            lock (this.quotasCacheLock)
            {
                this.quotasCache.TryGetValue(path, out dirQuota);
            }

            if (dirQuota == null)
            {
                try
                {
                    dirQuota = this.quotaManager.GetQuota(path);
                }
                catch (COMException)
                {
                }
            }

            if (dirQuota != null)
            {
                dirQuota.Delete();
                dirQuota.Commit();
                ret = true;
            }

            lock (this.quotasCacheLock)
            {
                this.quotasCache.Remove(path);
            }

            return(ret);
        }