from .cfg_app import PpCfgAppError, PpConfigApplication
-__version__ = '0.4.2'
+__version__ = '0.4.3'
LOG = logging.getLogger(__name__)
UTC = datetime.timezone.utc
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)
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
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:
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:
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):