We have multiple computers in our company that sometimes used as work stations and sometimes as server (running user defined jobs)
I would like to harnessed all the available computing power of the workstations to be part of the grid (to add them to the dedicated servers)
Each grid client can work in one of two modes low (30%) and high (100%) -(the max percentage of cpu and ram that is allocated to the grid client)
The user should not be effected by it, the moment the user starts using the computer (locally or remotely) the client switch to low mode (30%) after the user is idle for configured time, for example 5 minuets, and the cpu usage is low (no running tasks) the client should switch to high mode
Here is the solution i made based on few examples i found in stackoverflow:
- time since screen idle
- time since last console action based on "who"
- cpu usage of user based on top
to enter the idle state and setting the mode to high mode i wait for idle time in cmd and screen, and require a limited cpu usage, to exit from high mode any action via cmd or screen will set it back to low mode
#!/bin/bash
idle=false
idleAfter=300 # consider idle after 300 seconds
idleCpuAfter=100 # max cpu usage to enter high mode
idleCpuCount=10 #seconds to keep idle state(without interruptions) before starting high mode
count=0 #count for idle state, when counter reach idleCpuCount client will initiate high mode
while true; do
idleInSecondsCmd=$( who -s | perl -lane 'print "$F[0]\t" . 86400 * -A "/dev/$F[1]"'| sort -k2 -n | head -1 | cut -f2 )
idleInSecondsScreen=$(./getIdle)
cpuLoad=$(top -b -n 1 -u "$user" | awk 'NR>7 { sum += $9; } END { print user, sum; }')
echo "idleInSecondsCmd " + $idleInSecondsCmd # just for debug purposes.
echo "idleInSecondsScreen" + $idleInSecondsScreen # just for debug purposes.
echo "cpuLoad" + $cpuLoad # just for debug purposes.
if [[ ($idleInSecondsCmd -gt $idleAfter)
&& ($idleInSecondsScreen -gt $idleAfter)
&& ($(bc <<< "$cpuLoad <= $idleCpuAfter") -eq 1 )
&& $idle = false ]] ;then
count=$((count + 1))
echo $count
else
count=0
fi
if [[ $idle = false && ($count -gt $idleCpuCount) ]]; then
idle=true
setupLoad.sh 100
fi
if [[ ( $idleInSecondsCmd -lt $idleAfter
|| $idleInSecondsScreen -lt $idleAfter)
&& $idle = true ]] ; then
idle=false
setupLoad.sh 30
fi
sleep 1 # polling interval
done
is it the best approach?