project-disk-use: send transient errors to dev/null
[girocco.git] / toolbox / reports / project-disk-use.sh
blobcec7ab5b35450fef89bb53aa339bfd47a981e7bd
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\" 2>/dev/null | 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 inuse="$(get_use_k "$cfg_reporoot")"
50 binned="$(get_use_k "$cfg_reporoot-recyclebin")"
52 cd "$cfg_reporoot"
53 total=0
54 howmany=0
55 orphans=0
56 results=
57 while IFS='' read -r proj; do
58 proj="${proj#./}"
59 proj="${proj%.git}"
61 is_listed_proj "$proj" || { x='!'; orphans="$(( $orphans + 1 ))"; }
62 mirror=M
63 [ -e "$proj.git/.nofetch" ] || mirror=m
64 usek="$(get_use_k "$proj.git")"
65 total="$(( $total + $usek ))"
66 howmany="$(( $howmany + 1 ))"
67 line="$(printf '%8d %s %s%s\n*' "$usek" "$mirror" "$proj" "$x")"
68 line="${line%?}"
69 results="$results$line"
70 done <<EOT
71 $(find . -type d -name '*.git' -print -prune 2>/dev/null)
72 EOT
74 enddate="$(date "+$datefmt")"
75 domail=cat
76 [ -n "$mailresult" ] && domail='mail -s "[$cfg_name] Project Disk Use Report" "$cfg_admin"'
78 cat <<EOT
79 Project Disk Use Report
80 =======================
82 Start Time: $startdate
83 End Time: $enddate
85 Repository Root: $cfg_reporoot
86 Disk Use: $inuse (1024-byte blocks)
88 Recycle Bin Root: $cfg_reporoot-recyclebin
89 Disk Use: $binned (1024-byte blocks)
91 Total Disk Use: $(( $inuse + $binned )) (1024-byte blocks)
93 Repository Count: $howmany
94 Orphaned: $orphans
95 Repository Total: $total (1024-byte blocks)
97 $(df -h "$cfg_reporoot")
98 EOT
100 if [ $# -lt 1 ] || [ $1 != "0" ]; then
101 topn=cat
102 message="Individual Repository Use"
103 case "${1%%[!0-9]*}" in ?*)
104 message="Individual Repository Use (Top $1)"
105 topn="head -n ${1%%[!0-9]*}"
106 esac
107 echo ""
108 echo ""
109 echo "$message"
110 echo "$message" | tr -c '\n' -
111 echo ""
112 printf '%s' "$results" | sort -k1,1nr -k3,3 | \
113 sed -e 's/ M / /' | $topn
115 } | eval "$domail"