工作台使用率统计
为了对工作环境里50台console在工作时间段内8:30~17:30的使用情况做个统计,写了下面的代码来自动化统计。代码是修改了系统的屏保配置文件,通过屏保运行的时间sleep time来反向计算这些工作台的人为使用情况active time.
#!/bin/bash
#-----------GLOBAL variable define-----------
# array for IP Address and PASSword for 52 computers
declare -a HOST_IP
declare -a HOST_PASS
declare -a HOST_NAME
declare -a HOST_LAST_TIMES
declare -a HOST_ACTIVE_TIME
# declare host access variables
HOST_USER="spongebob"<details>
HOST_PASS_1="secretbox"
HOST_PASS_2="magicshell"
No=50
INTERVAL_S=300 # work hour interval: 5 min
INTERVAL_L=1800 # home hour interval: 30 min
ACTIVE_TIME=0
YESTERDAY=`date "+%Y-%m-%d"`
#CSV_NAME=$HOST_NAME"_"`date "+%Y%b%d_%H:%M"`
SS_PID_NO=0
#############################################################################
#-------------FUNCTION for initial each computer's IP address--------------- #
# return >> HOST_IP for ip address for each computer #
# return >> HOST_PASS as NONE for each host password initialization #
# return >> HOST_LAST_TIMES as 0 for the last timestamp of each host #
# return >> HOST_ACTIVE_TIME for record host active during time accumulation #
#############################################################################
host_ip_init() {
#assgin ip value for each element of IP Address
for ((i=1;i<$No;i++))
do
if [ $i -lt 10 ]; then
HOST_IP[i]="192.168.1.5"$i
HOST_NAME[i]="bob0"$i
elif [ $i -lt 48 ]; then
HOST_IP[i]="192.168.1."$i
HOST_NAME[i]="bob"$i
else [ $i -lt $No ]
HOST_IP[i]="192.168.1."$i
HOST_NAME[i]="bob"$i
fi
HOST_ACTIVE_TIME[i]=0
####debug message####
#echo "${HOST_NAME[i]}'s IP is ${HOST_IP[i]}"
done
return 0
}
#############################################################################
#------------FUNCTION for check the accessable for each computer----------- #
# return >> the HOST_PASS=NONE || if host not accessable #
# return >> the HOST_PASS=HOST_PASS1 or HOST_PASS2 || if host alive #
# return >> initial HOST_LAST_TIMES of host last timestamp #
# return >> Modify and active screensaver service || if host accessable #
# return >> Inactive_Host.csv to record the dead host #
#############################################################################
check_access() {
local HOST_ALIVE
local HOST_PASS_CURR
for ((i=1;i<$No;i++))
do
#global value initial
HOST_PASS[i]="NONE"
HOST_LAST_TIMES[i]=`date +%s`
#local value initial
HOST_ALIVE="NO"
HOST_PASS_CURR="NONE"
ping -c 1 -W 1 ${HOST_IP[i]} &> /dev/null
if [ $? -eq 0 ]; then
HOST_ALIVE="YES"
#echo "${HOST_IP[i]} is aliving"
else
HOST_ALIVE="NO"
#echo "${HOST_IP[i]} HOST Alive $HOST_ALIVE"
fi
if [ $HOST_ALIVE == "YES" ]; then
# remove the offending key if remote host identification changed
ssh-keygen -R ${HOST_IP[i]} -f /usr/g/ctuser/.ssh/known_hosts >&2 -q
# get xscreensaver-gfx process number to identify whether screensaver is running
sshpass -p $HOST_PASS_1 ssh -o StrictHostKeyChecking=no -o ConnectTimeout=3 $HOST_USER@${HOST_IP[i]} "exit"
if [ $? -eq 0 ]; then
HOST_PASS_CURR=$HOST_PASS_1
else
#Check whether PASSWORD_2 is available for current host.
sshpass -p $HOST_PASS_2 ssh -o StrictHostKeyChecking=no -o ConnectTimeout=3 $HOST_USER@${HOST_IP[i]} "exit"
if [ $? -eq 0 ]; then
HOST_PASS_CURR=$HOST_PASS_2
fi
fi
HOST_PASS[$i]=$HOST_PASS_CURR
#echo "${HOST_NAME[i]}'s password is: \"$HOST_PASS_CURR\""
fi
if [ ${HOST_PASS[i]} != "NONE" ]; then # active screensaver each day
active_screensaver $i >&2
#echo "Now activing screensaver for alive ${HOST_NAME[i]}"
fi
if [ $HOST_ALIVE == "NO" ] || [ $HOST_PASS_CURR == "NONE" ]; then # record dead host each day
echo `date "+%Y%b%d"`,`date "+%H:%M"`,${HOST_NAME[i]},"The host is not alive or unaccessable" >> Inactive_Host.csv
fi
done
return 0
}
#############################################################################
#--------------------FUNCTION for active screensaver as 10min delay--------- #
# return >> modify screensaver settings and restart the service #
#############################################################################
active_screensaver() {
local SCREENSAVER_FILE="/usr/g/ctuser/.xscreensaver"
#modify "mode", "timeout", "overlayStderr" value in xscreensaver config file and then restart xscreensaver service
sshpass -p ${HOST_PASS[$1]} ssh -o StrictHostKeyChecking=no \
-o ConnectTimeout=5 \
$HOST_USER@${HOST_IP[$1]} "\
sed -i -e '/mode:/c\mode: one' \
-e '/timeout:/c\timeout: 0:10:00' \
-e '/overlayStderr:/c\overlayStderr: False' \
$SCREENSAVER_FILE && \
xscreensaver-command -restart"
if [ $? -eq 0 ]; then
echo "${HOST_NAME[$1]} screensaver actived successfully"
fi
}
############################################################################
#------------------- This is the main function --------------------- #
# return >> computerxx.csv || record host active time #
# return >> Inactive_Host.csv from check_access || if host unavailable #
############################################################################
## here initial the profiles and modify screensaver settings
host_ip_init # Initial HOST_IP/HOST_NAME ... profile
check_access # Check every HOST available or not, get HOST_PASS
#TIME_STOP="10:00"
########### here continue to log the computer active time #########
while true; do
TODAY=`date "+%Y-%m-%d"`
NOW=`date "+%H:%M"`
echo "Monitor computers status in a new cycle @ $TODAY $NOW..."
#if [ `date "+%H:%M"` \> $TIME_STOP ]; then
if [ "$TODAY" != "$YESTERDAY" ] || ([ "$NOW" \> "12:30" ] && [ "$NOW" \< "12:39" ]); then # refresh HOSTs access and active screensaver
check_access # when each new day coming or
fi # when each 12:30PM at lunch time
for (( i=1;i<$No;i++ )) do
#if [ `date "+%H:%M"` \> $TIME_STOP ]; then
if [ "$TODAY" != "$YESTERDAY" ] && [ ${HOST_PASS[i]} != "NONE" ]; then # when new day coming, the timer roll back to zero
echo $YESTERDAY,"24:00",${HOST_ACTIVE_TIME[i]} >> ${HOST_NAME[i]}.csv # write the last record for old day
HOST_ACTIVE_TIME[i]=0 # set the ACTIVE_TIME to 0 for a new day
#echo $TODAY,$NOW, ${HOST_ACTIVE_TIME[i]} >> ${HOST_NAME[i]}.csv # write the first record for new day
if [ $i -eq 52 ]; then
#TIME_STOP="18:00"
YESTERDAY=$TODAY # refresh YESTERDAY at the end of 1st loop in new day
fi
fi
if [ ${HOST_PASS[i]} != "NONE" ]; then # Only if host avialible then contine next process
CURR_TIMES=`date +%s`
if [ "$NOW" \> "08:30" ] && [ "$NOW" \< "17:30" ]; then # logon and get the process number of "xscreensaver-gfx"
SS_PID_NO=`sshpass -p ${HOST_PASS[$i]} ssh -o StrictHostKeyChecking=no \
-o ConnectTimeout=5 \
$HOST_USER@${HOST_IP[$i]} \
"ps -ef | grep -c "xscreensaver-gfx""`
if [ $? -eq 0 ]; then
if [ $SS_PID_NO -eq 2 ]; then
DURING_MINUTS=$(($((CURR_TIMES - HOST_LAST_TIMES[i])) / 60)) # calculate the time range (min) from last record
HOST_ACTIVE_TIME[i]=$((HOST_ACTIVE_TIME[i] + DURING_MINUTS)) # accumulate computer active minitues when someone using it
#echo "${HOST_NAME[i]} during time is $DURING_MINUTS, HOST active time is: ${HOST_ACTIVE_TIME[i]}"
fi
echo $TODAY,$NOW, ${HOST_ACTIVE_TIME[i]} >> ${HOST_NAME[$i]}.csv # write record for each INTERVAL
else
echo $TODAY,$NOW, "unavilable" >> ${HOST_NAME[$i]}.csv # write Unavailable if host NOT alive
fi
fi
HOST_LAST_TIMES[$i]=$CURR_TIMES
fi
done
if [ "$NOW" \> "08:30" ] && [ "$NOW" \< "17:30" ]; then
sleep $INTERVAL_S
else
sleep $INTERVAL_L
fi
done

浙公网安备 33010602011771号