markdown: update to latest with table support
[girocco.git] / toolbox / reports / project-disk-use.sh
blobbd44aa1f5ab9796978160eeac930c37d374d3889
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='
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 fmtpct() {
53 if [ -z "$1" ] || [ "$1" = "0" ]; then return; fi
54 if [ -z "$2" ] || [ "$2" = "0" ]; then return; fi
55 _fmtpct=$(( ( $1 * 100 + $2 / 2 ) / $2 ))
56 if [ $_fmtpct -le 100 ]; then
57 _of=
58 [ -z "$4" ] || _of=" of $(fmtcomma $2)"
59 echo "${3:- }($_fmtpct%$_of)"
63 get_use_k() {
64 _targ="$1"
65 shift
66 _cmd="du $var_du_follow -k -s"
67 if [ -n "$var_du_exclude" ]; then
68 while [ -n "$1" ]; do
69 _cmd="$_cmd $var_du_exclude \"$1\""
70 shift
71 done
72 elif [ $# -ne 0 ]; then
73 echo "get_use_k: error: no du exclude option available" >&2
74 exit 1
76 _cmd="$_cmd \"$_targ\" 2>/dev/null | cut -f 1"
77 [ -z "$hasionice" ] || _cmd="ionice -c 3 $_cmd"
78 [ -z "$hasnice" ] || _cmd="nice -n 19 $_cmd"
79 eval "$_cmd"
82 is_active() (
83 cd "$cfg_reporoot/$1.git" || return 1
84 check_interval lastchange 2592000 # 30 days
87 projlist="$(cut -d : -f 1 <"$cfg_chroot/etc/group")"
89 is_listed_proj() {
90 echo "$projlist" | grep -q -e "^$1$"
93 totaluse="$(get_use_k "$cfg_reporoot")"
94 if [ -n "$var_du_exclude" ]; then
95 # Attribute all hard-linked stuff in _recyclebin to non-_recyclebin
96 inuse="$(get_use_k "$cfg_reporoot" "_recyclebin")"
97 binned="$(( $totaluse - $inuse ))"
98 else
99 # No du exclude option, if binned contains hard links into other repos
100 # its size may appear larger than it actually is and the kpct will be off
101 binned="$(get_use_k "$cfg_reporoot/_recyclebin")"
102 inuse="$(( $totaluse - $binned ))"
105 cd "$cfg_reporoot"
106 total=0
107 ktotal=0
108 howmany=0
109 orphans=0
110 banged=0
111 bangsent=0
112 mirrors=0
113 forks=0
114 mactive=0
115 pactive=0
116 results=
117 while IFS='' read -r proj; do
118 proj="${proj#./}"
119 proj="${proj%.git}"
122 is_listed_proj "$proj" || { x='!'; orphans="$(( $orphans + 1 ))"; }
123 case "$proj" in */*) forks="$(( $forks + 1 ))"; esac
124 mirror="$(get_mirror_type "$proj.git" 2>/dev/null)" || :
125 [ -z "$mirror" ] || mirrors="$(( $mirrors + 1 ))"
126 if is_active "$proj"; then
127 a='*'
128 if [ -n "$mirror" ]; then
129 mactive="$(( $mactive + 1 ))"
130 else
131 pactive="$(( $pactive + 1 ))"
134 : ${mirror:=M}
135 usek="$(get_use_k "$proj.git")"
136 repok=
137 girocco_reposizek=
138 girocco_bang_count=
139 girocco_bang_firstfail=
140 girocco_bang_messagesent=
141 [ -L "$proj.git/objects" ] || ! [ -d "$proj.git/objects" ] ||
142 eval "$(git --git-dir="$cfg_reporoot/$proj.git" config --get-regexp \
143 '^girocco\.((bang\.(count|firstfail|messagesent))|reposizek)$' |
144 awk '{gsub(/[.]/,"_",$1); $0 ~ / / || sub(/$/," "); sub(/ /,"=\042"); print $0 "\042"}')" || :
146 if [ -n "$girocco_bang_count" ] && [ "${girocco_bang_count#*[!0-9]}" = "$girocco_bang_count" ] && [ "$girocco_bang_count" -gt 0 ]; then
147 banged=$(( $banged + 1 ))
148 girocco_bang_count=$(( 0 + $girocco_bang_count ))
149 b="~(${girocco_bang_firstfail:+$girocco_bang_firstfail/}$girocco_bang_count"
150 if [ "$girocco_bang_messagesent" = "true" ] || [ "$girocco_bang_messagesent" = "1" ]; then
151 b="$b/sent"
152 bangsent=$(( $bangsent + 1 ))
154 b="$b)"
156 repok="$girocco_reposizek"
157 repokpct=
158 case "$repok" in
159 [0-9]*)
160 repok="${repok%%[!0-9]*}"
161 repokpct=$(( ( $repok * 100 + $usek / 2 ) / $usek ))
162 if [ $repokpct -le 100 ]; then
163 repokpct="$repokpct%"
164 else
165 repokpct="100+"
166 fi;;
168 repok=0
169 repokpct=-;;
170 esac
171 ktotal=$(( $ktotal + $repok ))
172 total="$(( $total + $usek ))"
173 howmany="$(( $howmany + 1 ))"
174 line="$usek $repokpct $mirror $proj$a$x$b$nl"
175 results="$results$line"
176 done <<EOT
177 $(find . -type d \( -path ./_recyclebin -o -name '*.git' -print \) -prune 2>/dev/null)
180 kpct=$(( ( $ktotal * 100 + $inuse / 2 ) / $inuse ))
181 enddate="$(date "+$datefmt")"
182 domail=cat
183 [ -z "$mailresult" ] || domail='mailref "diskuse@$cfg_gitweburl" -s "[$cfg_name] Project Disk Use Report" "$cfg_admin"'
185 cat <<EOT
186 Project Disk Use Report
187 =======================
189 Start Time: $startdate
190 End Time: $enddate
192 Repository Root: $cfg_reporoot
193 Unbinned Disk Use: $(fmtcomma "$inuse") (1024-byte blocks)
194 reposizek Total: $(fmtcomma "$ktotal") ($kpct%)
196 Recycle Bin Root: $cfg_reporoot/_recyclebin
197 Binned Disk Use: $(fmtcomma "$binned") (1024-byte blocks)
199 Total Disk Use: $(fmtcomma "$totaluse") (1024-byte blocks)
201 Repository Count: $(fmtcomma "$howmany")
202 Forks: $(fmtcomma "$forks")$(fmtpct "$forks" "$howmany")
203 Mirrors: $(fmtcomma "$mirrors")$(fmtpct "$mirrors" "$howmany")
204 ~Banged: $(fmtcomma "$banged")$(fmtpct "$banged" "$howmany")/($(( $banged - $bangsent )) unsent)
205 Orphaned: $(fmtcomma "$orphans")
206 Repository Total: $(fmtcomma "$total") (1024-byte blocks)
208 *30-Day Active: $(fmtcomma "$(( $pactive + $mactive ))")$(fmtpct "$(( $pactive + $mactive ))" "$howmany")
209 Push: $(fmtcomma "$pactive")$(fmtpct "$pactive" "$howmany")$(fmtpct "$pactive" "$(( $pactive + $mactive ))" "/" 1)$(fmtpct "$pactive" "$(( $howmany - $mirrors ))" "/" 1)
210 Mirror: $(fmtcomma "$mactive")$(fmtpct "$mactive" "$howmany")$(fmtpct "$mactive" "$(( $pactive + $mactive ))" "/" 1)$(fmtpct "$mactive" "$mirrors" "/" 1)
212 $(df -h "$cfg_reporoot")
215 if [ $# -lt 1 ] || [ $1 != "0" ]; then
216 topn=cat
217 message="Individual Repository Use"
218 case "${1%%[!0-9]*}" in ?*)
219 message="Individual Repository Use (Top $1)"
220 topn="head -n ${1%%[!0-9]*}"
221 esac
222 echo ""
223 echo ""
224 echo "$message"
225 echo "$message" | tr -c '\n' -
226 echo ""
227 printf '%s' "$results" | sort -k1,1nr -k4,4 |
228 while read -r a b c d; do
229 printf "%10s %4s %s %s\n" "$(fmtcomma "$a")" "$b" "$c" "$d"
230 done |
231 sed -e 's/ [M-] / /g' | $topn
233 } | eval "$domail"