]> Frank Brehm's Git Trees - pixelpark/admin-tools.git/commitdiff
Reformatted status data to save
authorFrank Brehm <frank.brehm@pixelpark.com>
Fri, 24 Mar 2017 09:46:26 +0000 (10:46 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Fri, 24 Mar 2017 09:46:26 +0000 (10:46 +0100)
pp_lib/quota_check.py

index b243d11be57e5f76e57c1e90a80862e3ab89c2bc..f7a9e7678112df22e70beab203a239bc50f362f8 100644 (file)
@@ -38,7 +38,7 @@ from .common import pp, terminal_can_colors, to_bytes, to_bool, to_str
 
 from .cfg_app import PpCfgAppError, PpConfigApplication
 
-__version__ = '0.4.2'
+__version__ = '0.4.3'
 LOG = logging.getLogger(__name__)
 UTC = datetime.timezone.utc
 
@@ -241,7 +241,7 @@ class PpQuotaCheckApp(PpConfigApplication):
 
         if not os.path.exists(self.statusfile):
             LOG.debug("Status file {!r} does not exists.".format(self.statusfile))
-            return
+            return {}
 
         if not os.path.isfile(self.statusfile):
             msg = "Status file {!r} is not a regular file.".format(self.statusfile)
@@ -276,17 +276,17 @@ class PpQuotaCheckApp(PpConfigApplication):
             LOG.debug("Status from {f!r}:\n{s}".format(
                 f=self.statusfile, s=pp(status)))
 
-        if 'checks' in status:
+        if 'checks' in status and 'data' in status['checks']:
             dates = []
-            for date in status['checks'].keys():
+            for date in status['checks']['data'].keys():
                 dates.append(date)
             if dates:
                 dates.sort()
                 first_date = dates[0].replace(tzinfo=UTC)
                 tdiff = self.now - first_date
-                if tdiff.days > 7 or len(dates) > 5:
-                    self.rotate_status_file(dates[-1])
-                    return {}
+#                if tdiff.days > 7 or len(dates) > 5:
+#                    self.rotate_status_file(dates[-1])
+#                    return {}
 
         return status
 
@@ -344,19 +344,8 @@ class PpQuotaCheckApp(PpConfigApplication):
         entries = pwd.getpwall()
 
         for entry in entries:
-            home = entry.pw_dir
             user_name = entry.pw_name
             uid = entry.pw_uid
-            if not home:
-                continue
-            home_relative = os.path.relpath(home, self.home_root_abs)
-            if home == os.sep or home_relative.startswith(upper_dir):
-                if self.verbose > 1:
-                    LOG.debug((
-                        "Home directory {d!r} of user {u!r} "
-                        "is outside home root {h!r}.").format(
-                        d=home, u=entry.pw_name, h=self.home_root_abs))
-                continue
             if user_name not in self.passwd_data:
                 self.passwd_data[user_name] = entry
             if uid not in self.map_uid:
@@ -419,12 +408,26 @@ class PpQuotaCheckApp(PpConfigApplication):
             self.status_data['checks'] = {}
         self.status_data['checks'][self.now] = {}
         check = self.status_data['checks'][self.now]
+        check['data'] = {}
+        check['stats'] = {
+            'begin': self.now,
+            'end': None,
+            'duration': None,
+            'total_kb': 0,
+            'total_mb': 0,
+            'total_gb': 0.0,
+            'number_dirs': 0,
+        }
+
+        total_kb = 0
+        number_dirs = 0
 
         i = 0
 
         for path in all_home_entries:
             if not os.path.isdir(path):
                 continue
+            number_dirs += 1
             i += 1
             home_rel = os.sep + os.path.relpath(path, self.chroot_homedir)
             if self.verbose > 2:
@@ -434,24 +437,57 @@ class PpQuotaCheckApp(PpConfigApplication):
             dir_uid = dir_stat.st_uid
             dir_owner = str(dir_uid)
             username = dir_owner
-            if dir_uid in self.map_uid:
+            if dir_uid == 0:
+                username = 'root'
+                dir_owner = 'root'
+            elif dir_uid in self.map_uid:
                 dir_owner = self.map_uid[dir_uid]
                 username = dir_owner
                 if dir_owner in self.passwd_data and self.passwd_data[dir_owner].pw_gecos:
                     dir_owner = self.passwd_data[dir_owner].pw_gecos
             util = self.get_util_dir_kb(path)
+            total_kb += util
             result = {
-                'home': home_rel,
+                'checked': datetime.datetime.now(UTC),
                 'util_kb': util,
                 'uid': dir_uid,
                 'gid': dir_stat.st_gid,
                 'user': username,
                 'gecos': dir_owner,
             }
-            check[home_rel] = result
+            check['data'][home_rel] = result
             if i > 10:
                 break
 
+        end_ts = datetime.datetime.now(UTC)
+        duration = end_ts - self.now
+        dur_days = duration.days
+        dur_secs = duration.seconds
+        if duration.microseconds >= 500000:
+            dur_secs += 1
+        dur_mins = 0
+        dur_hours = 0
+        if dur_secs >= 60:
+            dur_mins = int(dur_secs / 60)
+            dur_secs = dur_secs % 60
+            if dur_mins >= 60:
+                dur_hours = int(dur_mins / 60)
+                dur_mins = dur_mins % 60
+        dur_parts = []
+        if dur_days:
+            dur_parts.append("{} days".format(dur_days))
+        if dur_days or dur_hours:
+            dur_parts.append("{} hours".format(dur_hours))
+        if dur_days or dur_hours or dur_mins:
+            dur_parts.append("{} minutes".format(dur_mins))
+        dur_parts.append("{} seconds".format(dur_secs))
+        check['stats']['end'] = end_ts
+        check['stats']['duration'] = ', '.join(dur_parts)
+        check['stats']['number_dirs'] = number_dirs
+        check['stats']['total_kb'] = total_kb
+        check['stats']['total_mb'] = total_kb // 1024
+        check['stats']['total_gb'] = float(total_kb) / 1024.0 / 1024.0
+
     # -------------------------------------------------------------------------
     def send_results(self):