sshpc_worker.sh
por EDSON - LAMBDA
—
última modificação
13/06/2023 11h33
sshpc_worker.sh — 3 KB
Conteúdo do arquivo
#!/bin/bash ######################################################################################### ## SSHPC - Cluster de processamento distribuído baseado em SSH ## ## ## ## Local: Laboratório Multiusuário de Bioinformática e Análise de Dados (LAMBDA) ## ## Centro de Biotecnologia (CBiotec), Universidade Federal da Paraíba (UFPB) ## ## Data: 30/05/2023 ## ## Coordenador: Edson Luiz Folador ## ## IC: Arthur Araújo de Lacerda ## ######################################################################################### readonly PROGNAME=$(basename "$0") readonly PROGDIR=$(readlink -m $(dirname "$0")) # debugging flag debugging() { ${debug} && echo -e "$1"; } echo_info(){ ### echoes info if -d if ${debug}; then echo "Laboratory: $lab" echo "MAC adress: $mac" echo "Client's port: $port" echo "Total cores: $cores" fi } check_sshpc_usage() { ### checks for sshpc processes # checks three times due to core usage variations for i in {1..3}; do local sshpc_user_usage=$(echo "$(ps -u sshpc -o %cpu | tail -n +2 | head -c -1 | tr -d ' ' | tr '\n' '+' | sed -E -e 's_\+_ & _g' | tr -d ' ')" | bc -l) # if there is no processes, then usage=0 if [[ -z ${sshpc_user_usage} ]]; then local sshpc_user_usage=0; fi # Checks sshpc's usage "100% = 1core" if (( $(echo "${sshpc_user_usage} > 15 " | bc -l ) )); then debugging "sshpc user cpu usage: ${sshpc_user_usage}% aborting!" exit 1 elif [[ "${i}" -eq 3 ]]; then debugging "sshpc user cpu usage: ${sshpc_user_usage}% proceding!" return 0 fi sleep 2 done } check_overall_usage() { ### checks total cpu usage for i in {1..3}; do local total_cpu_usage=$(echo "$(ps ax -o %cpu | tail -n +2 | head -c -1 | tr -d ' ' | tr '\n' '+' | sed -E -e 's_\+_ & _g' | tr -d ' ')" | bc -l) # if there is no processes, then usage=0 if [[ -z "${total_cpu_usage}" ]]; then local total_cpu_usage=0; fi # checks for cpu usage # If less then 50%, tries to connect to server if (( $(echo "${total_cpu_usage} < ${cores}*100/2" | bc -l) )); then debugging "total cpu usage: ${total_cpu_usage}% proceding!" return 0 else debugging "total cpu usage: ${total_cpu_usage}% aborting!" exit 1 fi sleep 2 done } connect() { ### Sends connection attempt local lab="$1" local mac="$2" local port="$3" local cores="$4" local server="$5" local used_cores=$(ps -axo pcpu | grep -Ev ' 0.0|\%' | tr '\n' '+' | sed -e 's/ //g' -e 's/^./(&/' -e 's/+$/)\/100\n/' | bc -l) local free_cores=$(echo "${cores}-${used_cores}" | bc -l | cut -f 1 -d '.') debugging "${cores} cores, sending available cores to server" debugging "loging in as sshpc_${lab}_${mac}_${free_cores} on port ${port}" debugging "${free_cores} cores available, sending available cores to server" # connects to server ssh -p ${port} -o StrictHostKeyChecking=no -o PasswordAuthentication=no sshpc_${lab}_${mac}_${free_cores}@${server} } main() { # Debug local debug=true while true; do case "$1" in -q | --quiet) debug=false; shift;; --) break;; *) break;; esac done # Config file source /home/sshpc/.bin/sshpc.conf echo_info if check_sshpc_usage; then if check_overall_usage; then connect "${lab}" "${mac}" "${port}" "${cores}" "${server}" fi fi } main "$@"