#!/bin/bash
#===============================================================================
#
#          FILE: status_watch
#
#         USAGE: status_watch
#
#   DESCRIPTION: Watch the status and current players on live servers
#
#  REQUIREMENTS: sendscript
#
#          BUGS:  ---
#        AUTHOR: rickz (Rick Zhang), xlrickz@gmail.com
#       COMPANY: X-LEGEND Entertainment Corp.
#       CREATED: Thu Jul  7 06:05:00 EDT 2011
#      REVISION: 1.0
#
#          TODO:
#
#===============================================================================

#set -o nounset                              # Treat unset variables as an error
#set -m                                      # Enable job control

source ~/.gamerc 

T="$(tty|awk -F/ '{print $NF}')"

watch -n 1 -d "
PGPASSWORD=\"$LIVE_SERVER_DB_PASSWORD\" psql -t -h accountdb $ACCOUNT_DB_NAME spiritking <<< 'select id,name,ip,online_user,maxnum_user,state,version,sum(online_user) OVER  (PARTITION  by substr(id::text,1,2) ) from worlds order by id;select sum(online_user) from worlds;'

sendscript allall nosave <<< 'cat /proc/loadavg' 2> /dev/null | sed 's/\$/                                                   /g;s/^\\(..............................\\).*/\1/g' > /tmp/.all_loading$T

sendscript allwz nohostname nosave <<< 'for i in server*/ZoneServer*;do du -sb \$(ls -tr \$i/Item[0-9]* 2> /dev/null|tail -1)|grep -v \"\.$\";done' | sort -k 2 > /tmp/.all_itemlog$T

paste /tmp/.all_loading$T /tmp/.all_itemlog$T"
