project-disk-use.sh: show type of mirror rather than always 'm'
[girocco.git] / toolbox / reports / project-disk-use.sh
blob5b9391b74b35249445ba2730913c046391cb75ed
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 nl="$(printf '\n*')"
37 nl="${nl%?}"
39 fmtcomma() {
40 # remove leading 0s
41 _y="${1%%[1-9]*}"; _x="${1#$_y}"; _x="${_x:-0}"
42 # add commas after each group of 3
43 _y=
44 while [ $_x -gt 999 ]; do
45 _y="$(printf '%03d' $(($_x - $_x / 1000 * 1000)))${_y:+,$_y}"
46 _x="$(($_x/1000))"
47 done
48 [ $_x -eq 0 ] || _y="$_x${_y:+,$_y}"
49 echo "${_y:-0}"
52 get_use_k() {
53 _cmd="du -k -s \"$1\" 2>/dev/null | cut -f 1"
54 [ -n "$hasionice" ] && _cmd="ionice -c 3 $_cmd"
55 [ -n "$hasnice" ] && _cmd="nice -n 19 $_cmd"
56 eval "$_cmd"
59 projlist="$(cut -d : -f 1 < "$cfg_chroot/etc/group")"
61 is_listed_proj() {
62 echo "$projlist" | grep -q -e "^$1$"
65 inuse="$(get_use_k "$cfg_reporoot")"
66 binned="$(get_use_k "$cfg_reporoot-recyclebin")"
68 cd "$cfg_reporoot"
69 total=0
70 ktotal=0
71 howmany=0
72 orphans=0
73 results=
74 while IFS='' read -r proj; do
75 proj="${proj#./}"
76 proj="${proj%.git}"
78 is_listed_proj "$proj" || { x='!'; orphans="$(( $orphans + 1 ))"; }
79 mirror="$(get_mirror_type "$proj.git" 2>/dev/null || :)"
80 : ${mirror:=M}
81 usek="$(get_use_k "$proj.git")"
82 repok="$(git --git-dir="$cfg_reporoot/$proj.git" config --get girocco.reposizek 2>/dev/null || :)"
83 repokpct=
84 case "$repok" in
85 [0-9]*)
86 repok="${repok%%[!0-9]*}"
87 repokpct=$(( ( $repok * 100 + $usek / 2 ) / $usek ))
88 if [ $repokpct -le 100 ]; then
89 repokpct="$repokpct%"
90 else
91 repokpct="100+"
92 fi;;
94 repok=0
95 repokpct=-;;
96 esac
97 ktotal=$(( $ktotal + $repok ))
98 total="$(( $total + $usek ))"
99 howmany="$(( $howmany + 1 ))"
100 line="$usek $repokpct $mirror $proj$x$nl"
101 results="$results$line"
102 done <<EOT
103 $(find . -type d -name '*.git' -print -prune 2>/dev/null)
106 kpct=$(( ( $ktotal * 100 + $inuse / 2 ) / $inuse ))
107 enddate="$(date "+$datefmt")"
108 domail=cat
109 [ -n "$mailresult" ] && domail='mail -s "[$cfg_name] Project Disk Use Report" "$cfg_admin"'
111 cat <<EOT
112 Project Disk Use Report
113 =======================
115 Start Time: $startdate
116 End Time: $enddate
118 Repository Root: $cfg_reporoot
119 Disk Use: $(fmtcomma "$inuse") (1024-byte blocks)
120 reposizek Total: $(fmtcomma "$ktotal") ($kpct%)
122 Recycle Bin Root: $cfg_reporoot-recyclebin
123 Disk Use: $(fmtcomma "$binned") (1024-byte blocks)
125 Total Disk Use: $(fmtcomma "$(( $inuse + $binned ))") (1024-byte blocks)
127 Repository Count: $(fmtcomma "$howmany")
128 Orphaned: $(fmtcomma "$orphans")
129 Repository Total: $(fmtcomma "$total") (1024-byte blocks)
131 $(df -h "$cfg_reporoot")
134 if [ $# -lt 1 ] || [ $1 != "0" ]; then
135 topn=cat
136 message="Individual Repository Use"
137 case "${1%%[!0-9]*}" in ?*)
138 message="Individual Repository Use (Top $1)"
139 topn="head -n ${1%%[!0-9]*}"
140 esac
141 echo ""
142 echo ""
143 echo "$message"
144 echo "$message" | tr -c '\n' -
145 echo ""
146 printf '%s' "$results" | sort -k1,1nr -k4,4 | \
147 while read -r a b c d; do
148 printf "%10s %4s %s %s\n" "$(fmtcomma "$a")" "$b" "$c" "$d"
149 done | \
150 sed -e 's/ [M-] / /g' | $topn
152 } | eval "$domail"