Você está aqui: Página Inicial > Contents > Documentos > sshpc > sshpc_summary.sh
conteúdo

sshpc_summary.sh

por EDSON - LAMBDA última modificação 13/06/2023 11h32

text/x-sh sshpc_summary.sh — 5 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"))
readonly SSHPC_DIRECTORY="/home/sshpc"

debugging() { ${debug} && echo -e "$1"; }

### RUN SUMMARY
get_run_files_amount() {
  debugging "\nFiles in the run directory:"
  find /home/sshpc/run/ -type f | wc -l
}

get_run_directories_amount() {
  debugging "\nDirectories in the run directory:"
  find /home/sshpc/run/ -type d | wc -l
}

### RUNNING SUMMARY
get_running_amout() {
  debugging "\nCurrent processes in progress:"
  find /home/sshpc/running/ -type f | wc -l
}

get_running_nodes_amount() {
  debugging "\nCurrent amount of nodes computing:"
  ps aux | grep "sshpc_run" | head -n -1 | wc -l
}

get_running_nodes() {
  debugging "\nCurrent nodes computing:"
  ps aux | grep "sshpc_run" | head -n -1 | sed -E 's/\ +/\t/g' | cut -f14-17
}

### RUNNED SUMMARY
get_runned_day_data() {
  if [[ -n ${year} ]]; then
    debugging "\nData runned in ${day}/${month}/${year}:"
    find "${SSHPC_DIRECTORY}/runned" -type f -name "${current_log_file}" | xargs grep "${day}/${month}"
  else
    debugging "\nData runned till today"
    find "${SSHPC_DIRECTORY}/runned" -type f | xargs cat
  fi
}

get_runned_day_amount(){
  if [[ -n ${year} ]]; then
    debugging "\nData count runned in ${day}/${month}/${year}:"
    find "${SSHPC_DIRECTORY}/runned" -type f -name "${current_log_file}" | xargs grep "${day}/${month}" -c
  else
    debugging "\nData runned amount till today"
    find "${SSHPC_DIRECTORY}/runned" -type f | xargs cat | wc -l
  fi
}

get_runned_day_data_pernode() {
  if [[ -n ${year} ]]; then
    debugging "\nData runned per node in ${day}/${month}/${year}:"
    find "${SSHPC_DIRECTORY}/runned" -type f -name "${current_log_file}" | sudo xargs grep "${day}/${month}" | cut -f 3,5 | sed 's/_/\t/' | cut -f 1,2 | sort -k2,2 | uniq -f 1 -c | sed -E -e 's/ +//' -e 's/ +/\t/' | sort -nr
  else
    debugging "\nData runned per node till today"
    find "${SSHPC_DIRECTORY}/runned" -type f | sudo xargs cat | cut -f 3,5 | sed 's/_/\t/' | cut -f 1,2 | sort -k2,2 | uniq -f 1 -c | sed -E -e 's/ +//' -e 's/ +/\t/' | sort -nr
  fi 
}

### ARGS PARSING
parse_options() {
  case "$t_type" in
    "0" | "") run="1"; runned="1"; running="1";;
    "1") run="1";;
    "2") running="1";;
    "3") runned="1";;
    "4") sshpc_log="1";;
    "5") auth_log="1";;
    *) echo "-t option: $t_type not valid"; exit 1;;
  esac
}

parse_date() {
  # Fulldate
  if [[ "${date_input}" == "today" ]]; then
    date_today=$(date +"%d%m%Y")
    day=${date_today:0:2}
    month=${date_today:2:2}
    year=${date_today:4}
  elif [[ "${date_input}" =~ ^((0[0-9]|1[0-9]|2[0-9]|3[0-1])(0[0-9]|1[0-2])20[0-9]{2})$ ]]; then
    day=${date_input:0:2}
    month=${date_input:2:2}
    year=${date_input:4}
  # day of the month
  elif [[ "${date_input}" =~ ^(0[0-9]|1[0-9]|2[0-9]|3[0-1])(0[0-9]|1[0-2])$ ]]; then
    day=${date_input:0:2}
    month=${date_input:2:2}
    year=$(date +"%Y")
  # days past
  elif [[ "${date_input}" =~ ^-[0-9]+$ ]]; then
    date_today=$(date --date "-${date_input} day" +"%d%m%Y")
    day_temp=${date_today:0:2}
    day=${date_today:0:2}
    month=${date_today:2:2}
    year=${date_today:4}
  fi
  current_log_file="${year}${month}_sshpclog.txt"
}

execute_options() {
   if [[ -n $run ]]; then
    get_run_files_amount
    get_run_directories_amount
   fi 

   if [[ -n $runned ]]; then
    [[ -n ${g_type} ]] && get_runned_day_data 
    get_runned_day_amount 
    get_runned_day_data_pernode 
   fi 

   if [[ -n $running ]] && [[ "${date_input}" == "today" ]]; then
    get_running_amout
    get_running_nodes_amount
    get_running_nodes
   fi 
  
   if [[ -n $sshpc_log ]] ; then
    debugging "\nCurrent sshpc.log"
    cat /home/sshpc/sshpc.log 
   fi 

   if [[ -n $auth_log ]] ; then
    debugging "\nCurrent auth.log"
    sudo tail -500 /var/log/auth.log | grep -P "^[a-zA-Z]+ ${day} .* (?!root)Invalid user sshpc_"
   fi 
} 

main() {
  debug=true
  while true; do
    case "$1" in
      -t) [[ -n $2 ]] && [[ "$2" == *"-"* ]] && echo "-t option: $2 not valid" && exit 1 || t_type=$2; shift; shift;;
      -g) g_type="1"; shift;;
      -d) if [[ -z $2 ]] || [[ "$2" =~ ^-[a-z]$ ]]; then date_input="today"; else date_input=$2; shift; fi; shift;;
      -q) debug=false; shift;;
      -*) echo "$1 :not such option"; exit 1;;
      *) break;;
    esac
  done
  parse_date
  parse_options
  execute_options
}

main $@