forked from fizwit/filesystem-reporting-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreport.py
executable file
·52 lines (42 loc) · 1.42 KB
/
report.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python
import sys
import csv
import operator
import os
import pwd
"""
Very simple Python filter to collect stats from pwalk output.
use as a template to create other filters
Read csv file generated by pwalk and output the biggest users
"""
_version_ = '1.0.0'
_author_ = '[email protected]'
# pwalk fields
fields = ['inode', 'parent-inode', 'directory-depth', 'filename', 'extension',
'UID', 'GID', 'st_size', 'st_dev', 'st_blocks"', 'st_nlink', '"st_mode"',
'atime', 'mtime', 'ctime', 'fcount', 'sum']
users = {}
with open(sys.argv[1]) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',', quotechar='"')
for row in csv_reader:
uid = row[fields.index('UID')]
if uid in users:
users[uid]['sum'] += int(row[fields.index('st_size')])
users[uid]['fcount'] += 1
else:
users[uid] = {}
users[uid]['sum'] = int(row[fields.index('st_size')])
users[uid]['fcount'] = 1
# create new hash to be sorted by size
summary = {}
for user in users:
summary[user] = users[user]['sum']
# sorted_size is list of tuples (uid, size)
sorted_size = sorted(summary.items(), key=operator.itemgetter(1))
print(type(sorted_size))
for user in sorted_size:
try:
uname = pwd.getpwuid(int(user[0])).pw_name
except KeyError:
uname = user[0]
print('{} UID: {} FileCount: {}'.format(user[1], uname, users[user[0]]['fcount']))