#!/bin/sh
#
# nxcheckload - sample script for calculation of the load for a node.
#
# Version 0.5
#
# Under GPL
#
# Jonathan "Arrouan" ROUZAUD-CORNABAS (rouzaud.jonathan@gmail.com)
#
# Fabian Franz <FreeNX@fabian-franz.de>
#
# 0.5
#  - Rewrote huge parts
#
# Change between 0.3 and 0.4
#   - Add of TMP_FILE
#   - Add of lock file to avoid two run at once.
#
# Change between 0.2 and 0.3
#   - SMP support.
#

if [ "$1" != "" ]
then
	# Connect to a remote node
	
	# Note: This is a ssh sample, you'll need to tweak it for your setup
	#       and setup the secret keys for yourself.
	
	#exec $COMMAND_SSH nxcal@"$1" "$PATH_BIN/nxcheckload"
	
	# Note: This is a netcat example. You need to have nxcheckload running through 
	# netpipes or netcat like follows:
	# 	node1:~$ faucet 9876 -io $PATH_BIN/nxcheckload
	#
	# Connect to another node running the load-service on some port.
	#
	
	#exec $COMMAND_NETCAT "$1" 9876

	# Same as loadbalance_rr_random
	# pick a node by random.
	
	ping -c 1 $1 1>/dev/null 2>/dev/null                                                                                                                                       
        if [ $? -eq 0 ]                                                                                                                                                             
        then                                                                                                                                                                        
                exec ssh x2gobroker@"$1" "/usr/bin/nxcheckload"                                                                                                                       
        else                                                                                                                                                                        
            echo "1"                                                                                                                                                                
        fi 

	#echo $RANDOM
	exit 0
fi

# Be sure to use C numeric for calculations
export LC_NUMERIC=C

# The 3 variables of load from uptime
# Средняя нагрузка системы LA
#LOADXX=$(awk '{ printf("(100-%s)+(100-%s)+(100-%s)\n", $1, $2, $3); }' /proc/loadavg | bc -q)
LOADXX=$(awk '{ printf("((%s+%s+%s)*100/3)\n", $1, $2, $3); }' /proc/loadavg | bc -q)

if [ $LOADXX -eq 0 ]; then
    LOADXX="1"
fi

# Add of total memory and free memory
#Mt=$(awk 'BEGIN { N=0 } /MemTotal|MemFree/ { N+=$2; } END { print N }' /proc/meminfo)

# NBCPU = number of CPU
# Количество цпу
NBCPU=$(cat /proc/cpuinfo | grep ^processor | wc -l)

# CPU = Mhz of the CPU
# Мощности процов
#CPU=$(cat /proc/cpuinfo | grep "cpu MHz" | head -n1 | cut -d':' -f2 | cut -d' ' -f2)
CPU=$(cat /proc/cpuinfo | grep "model name" | awk '{print $9*1000}' | cut -d'G' -f1 | tail -n1 | bc -q)

# Number of Xorg already launch and running.
# Количество запущенных сессий
#NUMBX=$(ps aux | grep Xorg | grep -v grep | wc -l)
NUMBX=$(ps aux | grep x2goagent | grep -v grep | wc -l)

if [ $NUMBX -eq 0 ]; then
    NUMBX="1"
fi

# Final calcul of the number of load.
# (Количество ЦПУ * МОЩНОСТЬ ЦПУ) / НА СРЕДНЮЮ НАГРУЗКУ СИСТЕМЫ / Количество сессий
#echo "100 * $LOADXX + $Mt + ( $NBCPU * $CPU ) / 100 + $UNB * 100" | bc -q | cut -d. -f1
echo "( $NBCPU * $CPU * 100 ) / $LOADXX / $NUMBX" | bc -q
