Add -o option
Some checks reported errors
continuous-integration/drone/push Build encountered an error

This option control the output of the script. Possible output are human
(same as usual), html and csv.

This commit also does some minor syntax changes.
This commit is contained in:
Alexis Ben Miloud--Josselin 2019-07-29 16:47:42 +02:00
parent 7cdea6920e
commit e179979d23
2 changed files with 57 additions and 12 deletions

View file

@ -8,7 +8,7 @@ error () {
}
usage () {
echo 'usage: kvmstats [-a] [-u k|m|g]' >&2
echo 'usage: kvmstats [-a] [-u k|m|g] [-o h|human|html|csv]' >&2
exit 1
}
@ -18,9 +18,29 @@ do
done
POW=$(echo 1024 ^ 3 | bc)
FMT=human
while [ $# -ne 0 ] && echo "$1" | grep -q '^-[[:alnum:]]'
do
case $1 in
'-a')
SHOW_AVAIL=y
;;
'-o')
case $2 in
'h' | 'human')
FMT=human
;;
'html')
FMT=html
;;
'csv')
FMT=csv
;;
*)
usage
;;
esac
;;
'-u')
case $2 in
'k')
@ -36,9 +56,6 @@ do
usage
esac
;;
'-a')
SHOW_AVAIL=y
;;
*)
usage
esac
@ -50,7 +67,7 @@ do
echo "$VM"
virsh vcpucount --maximum --current "$VM"
# libvirt store memory in KiB, POW must be lowered by 1
virsh domstats --balloon "$VM" | awk 'BEGIN { FS="=" } /balloon.maximum/ { print $2 / '$((POW / 1024))'}'
virsh domstats --balloon "$VM" | awk -F= '/balloon.maximum/ { print $2 / '$((POW / 1024))'}'
for BLK in $(virsh domblklist "$VM" | sed '1,2d;/-$/d;/^$/d' | cut -d\ -f1)
do
virsh domblkinfo "$VM" "$BLK"
@ -59,10 +76,18 @@ do
done | xargs -n5 | {
echo vm vcpu ram disk running
awk '{ print } /yes$/ { vcpu += $2; ram += $3; disk += $4; running++ } END { print "TOTAL(running)", vcpu, ram, disk, running }'
if [ "$SHOW_AVAIL" ]
then
AV_CPU=$(nproc)
AV_MEM=$(awk '/^MemTotal:/ { print int($2 / '$((POW / 1024))' ) }' /proc/meminfo)
echo AVAILABLE "$AV_CPU" "$AV_MEM"
fi
} | column -t
test "$SHOW_AVAIL" && {
nproc
awk '/^MemTotal:/ { print int($2 / '$((POW / 1024))' ) }' /proc/meminfo
} | xargs printf 'AVAILABLE %s %s\n'
} | case $FMT in
'human')
column -t
;;
'html')
awk 'BEGIN{print "<html><body><table>"}{printf "<tr>";for(i=1;i<=NF;i++)printf "<td>%s</td>", $i;print "</tr>"}END{print "</table></body></html>"}'
;;
'csv')
tr ' ' ','
;;
esac

20
kvmstats.html Normal file
View file

@ -0,0 +1,20 @@
<HTML><BODY><TABLE>
<TR><TD>vm vcpu ram disk running</TD></TR>
<TR><TD>icinga2-web</TD><TD>4</TD><TD>4</TD><TD>30</TD><TD>yes</TD></TR>
<TR><TD>icinga2-node</TD><TD>4</TD><TD>4</TD><TD>30</TD><TD>yes</TD></TR>
<TR><TD>bruno</TD><TD>4</TD><TD>2</TD><TD>12</TD><TD>yes</TD></TR>
<TR><TD>evoadmin-web_packweb</TD><TD>2</TD><TD>1</TD><TD>3</TD><TD>no</TD></TR>
<TR><TD>icinga-test</TD><TD>8</TD><TD>4</TD><TD>15</TD><TD>no</TD></TR>
<TR><TD>debian10</TD><TD>2</TD><TD>2</TD><TD>22</TD><TD>yes</TD></TR>
<TR><TD>ansible-test1</TD><TD>2</TD><TD>1</TD><TD>17</TD><TD>no</TD></TR>
<TR><TD>debian7</TD><TD>4</TD><TD>4</TD><TD>0</TD><TD>no</TD></TR>
<TR><TD>debian8</TD><TD>4</TD><TD>4</TD><TD>18</TD><TD>no</TD></TR>
<TR><TD>debian9</TD><TD>4</TD><TD>4</TD><TD>30</TD><TD>no</TD></TR>
<TR><TD>EvoBSD</TD><TD>2</TD><TD>1</TD><TD>42</TD><TD>no</TD></TR>
<TR><TD>EvoMaquette</TD><TD>4</TD><TD>8</TD><TD>15</TD><TD>no</TD></TR>
<TR><TD>for-build-rescue-cd</TD><TD>8</TD><TD>4</TD><TD>4</TD><TD>no</TD></TR>
<TR><TD>grafana</TD><TD>4</TD><TD>4</TD><TD>5</TD><TD>no</TD></TR>
<TR><TD>installbuster</TD><TD>1</TD><TD>1</TD><TD>20</TD><TD>no</TD></TR>
<TR><TD>TOTAL(running)</TD><TD>0</TD><TD>0</TD><TD>0</TD><TD>4</TD></TR>
<TR><TD>AVAILABLE</TD><TD>8</TD><TD>39</TD></TR>
</TABLE></BODY></HTML>