reports: new project-disk-use.sh report
[girocco.git] / toolbox / reports / project-disk-use.sh
blob2a761fa471a0f039823413d4aba94df042a9572f
1 #!/bin/sh
3 # Report on project disk use.
4 # Output can be sent as email to admin with -m
5 # Automatically runs with nice and ionice (if available)
7 # Usage: project-disk-use [-m] [top-n-only]
9 # With -m mail the report to $cfg_admin instead of sending it to stdout
11 # Shows total disk usage in K bytes for $cfg_reporoot
12 # The top-n-only (all if not given) repos are then listed by
13 # decreasing order of disk space.
15 # Note that any *.git directories that are found in $cfg_reporoot that are
16 # not listed in $cfg_chroot/etc/group ARE included and have a '!' suffix added.
18 set -e
20 datefmt='%Y-%m-%d %H:%M:%S %z'
21 startdate="$(date "+$datefmt")"
23 . @basedir@/shlib.sh
25 mailresult=
26 if [ "$1" = "-m" ]; then
27 shift
28 mailresult=1
31 hasnice=
32 command -v nice > /dev/null && hasnice=1
33 hasionice=
34 command -v ionice > /dev/null && hasionice=1
36 get_use_k() {
37 _cmd="du -k -s \"$1\" | cut -f 1"
38 [ -n "$hasionice" ] && _cmd="ionice -c 3 $_cmd"
39 [ -n "$hasnice" ] && _cmd="nice -n 19 $_cmd"
40 eval "$_cmd"
43 projlist="$(cut -d : -f 1 < "$cfg_chroot/etc/group")"
45 is_listed_proj() {
46 echo "$projlist" | grep -q -e "^$1$"
49 cd "$cfg_reporoot"
50 total=0
51 results=
52 while IFS='' read -r proj; do
53 proj="${proj#./}"
54 proj="${proj%.git}"
56 is_listed_proj "$proj" || x='!'
57 usek="$(get_use_k "$proj.git")"
58 total="$(( $total + $usek ))"
59 line="$(printf '%8d\t%s%s\n*' "$usek" "$proj" "$x")"
60 line="${line%?}"
61 results="$results$line"
62 done <<EOT
63 $(find . -type d -name '*.git' -print -prune)
64 EOT
66 enddate="$(date "+$datefmt")"
67 domail=cat
68 [ -n "$mailresult" ] && domail='mail -s "[$cfg_name] Project Disk Use Report" "$cfg_admin"'
70 cat <<EOT
71 Project Disk Use Report
72 =======================
74 Repository Root: $cfg_reporoot
75 Start Time: $startdate
76 End Time: $enddate
77 Total Disk Use: $total (1024-byte blocks)
79 $(df -h "$cfg_reporoot")
80 EOT
82 if [ $# -lt 1 ] || [ $1 != "0" ]; then
83 topn=cat
84 message="Individual Repository Use"
85 case "${1%%[!0-9]*}" in ?*)
86 message="Individual Repository Use (top $1)"
87 topn="head -n ${1%%[!0-9]*}"
88 esac
89 echo ""
90 echo ""
91 echo "$message"
92 echo "$message" | tr -c '\n' -
93 echo ""
94 printf '%s' "$results" | sort -k1,1nr -k2,2 | $topn
96 } | eval "$domail"