Script functions.sh
Jump to navigation
Jump to search
#!/bin/sh # # /srv/config/functions.sh # # ######################### ############# VARIABILI ### Percorsi PATH="/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin"; CONF_PATH="/srv/config/etc"; LOG_PATH="/srv/config/log"; ### Colori NORMAL="\033[0;39m"; NORMALBACK="\033[0;49m"; WHITE="\033[1;37m"; WHITEBACK="\033[1;47m"; YELLOW="\033[1;33m"; YELLOWBACK="\033[1;43m"; GREEN="\033[1;32m"; GREENBACK="\033[1;42m"; CYAN="\033[1;36m"; CYANBACK="\033[1;46m"; BLUE="\033[1;34m"; BLUEBACK="\033[1;44m"; PINK="\033[1;35m"; PINKBACK="\033[1;45m"; RED="\033[1;31m"; REDBACK="\033[1;41m"; ### Caratteri UNDERLINE="\033[1;38m" COLS=`stty size | cut -d ' ' -f 2 2>/dev/null` if test $? -eq 0; then SPACE="\033["$(( ${COLS} /4*3 ))"G"; else SPACE="\033[60G"; fi; ######################### ############## FUNZIONI ### output all'utente echo_start() { echo -ne " ${GREEN}+${NORMAL} $1"; } echo_stop() { echo -ne " ${RED}-${NORMAL} $1"; } echo_success() { echo -e "${SPACE}${BLUE}[${GREEN} success ${BLUE}]${NORMAL}"; } echo_done() { echo -e "${SPACE}${BLUE}[${GREEN} done ${BLUE}]${NORMAL}"; } echo_unknown() { echo -e "${SPACE}${BLUE}[${RED} unknown ${BLUE}]${NORMAL}"; } echo_no_log() { echo -e "${SPACE}${BLUE}[${RED} no_log ${BLUE}]${NORMAL}"; } echo_no_conf() { echo -e "${SPACE}${BLUE}[${RED} no_conf ${BLUE}]${NORMAL}"; } echo_no_dir() { echo -e "${SPACE}${BLUE}[${RED} no_dir ${BLUE}]${NORMAL}"; } echo_no_other() { echo -e "${SPACE}${BLUE}[${RED} no_other ${BLUE}]${NORMAL}"; } echo_running() { echo -e "${SPACE}${BLUE}[${YELLOW} running ${BLUE}]${NORMAL}"; } echo_not_run() { echo -e "${SPACE}${BLUE}[${YELLOW} not_run ${BLUE}]${NORMAL}"; } echo_warning() { echo -e "${SPACE}${BLUE}[${WHITE} warning ${BLUE}]${NORMAL}"; } echo_failure() { echo -e "${SPACE}${BLUE}[${RED} failure ${BLUE}]${NORMAL}"; } echo_no_own() { echo -e "${SPACE}${BLUE}[${YELLOW} no-own! ${BLUE}]${NORMAL}"; } ### basename senza avere il pacchetto coreutils base_name() { TEXT="$1"; TEXT_OLD=""; while test "${TEXT}" != "${TEXT_OLD}"; do TEXT_OLD="${TEXT}"; TEXT=`echo ${TEXT} | cut -d '/' -f 2-`; if test -z "${TEXT}"; then echo "${TEXT_OLD}"; return 0; fi; done; echo "${TEXT}"; return 0; } # resize per i file di log resize() { FILE="$1" NEWSIZE="$2" if test -r "${FILE}"; then SIZE=`du -k "${FILE}" | cut -f 1`; if test ${SIZE} -gt ${NEWSIZE}; then dd if="${FILE}" of="${FILE}".tmp bs=1k skip=$(($SIZE-$NEWSIZE)) &> /dev/null; mv -f "${FILE}".tmp "${FILE}"; fi; fi; } # ciclo principale daemon_main() { case "${ACTION}" in start) echo -en "${WHITE}${TITLE}${NORMAL}..."; ;; stop) echo -en "${WHITE}${TITLE}${NORMAL}..."; ;; esac test ! -d "${LOG_PATH}" && echo_no_log && exit -2; # testo tutto quello che c'e' da testare N=1; while test -n "${PROG[$N]}"; do # controlla l'esistenza di un user, appoggiandosi sul filesystem touch $LOG_PATH/tmp_file &>/dev/null; chown "${OWN[$N]}:root" $LOG_PATH/tmp_file &> /dev/null; if test $? -ne 0; then echo_no_own; exit -4; fi; rm -f $LOG_PATH/tmp_file; # testo se il programma e' eseguibile test ! -x "${PROG[$N]}" && echo_unknown && exit -3; # se esistono le altre variabili le testo test -n "${FILE[$N]}" && test ! -r "${FILE[$N]}" && echo_no_conf && exit -5; test -n "${DIR[$N]}" && test ! -d "${DIR[$N]}" && echo_no_dir && exit -6; test -n "${OTHER[$N]}" && test ! -r "${OTHER[$N]}" && echo_no_other && exit -7; N=$(($N+1)); done; # eseguo la volonta' dell'admin case "${ACTION}" in start) # se l'output da dare all'utente non e' singolo, vado accapo test "${SINGLE_OUTPUT}" = "true" || echo # avvio i comandi nell' ordine in cui sono stati dichiarati N=1; while test -n "${PROG[$N]}"; do # se questo comando non va nascosto, lo mostro test "${HIDE_OUTPUT[$N]}" = "true" || echo_start "${START_OPT[$N]}"; if test "${DAEMON[$N]}" = "true"; then # controllo che non sia in esecuzione # NOTA: da rifare usando i pid di start!!!! pidof "${NAME[$N]}" &> /dev/null; if test $? -eq 0; then test "${HIDE_OUTPUT[$N]}" = "true" || echo_running; else su "${OWN[$N]}" -c "${START_OPT[$N]}" >> "${LOG_PATH}/${NAME[$N]}.log"; if test $? -eq 0; then test "${HIDE_OUTPUT[$N]}" = "true" || echo_success; else test "${HIDE_OUTPUT[$N]}" = "true" || echo_failure; fi; fi; else su "${OWN[$N]}" -c "${START_OPT[$N]}" >> "${LOG_PATH}/${NAME[$N]}.log"; if test $? -eq 0; then test "${HIDE_OUTPUT[$N]}" = "true" || echo_success; else test "${HIDE_OUTPUT[$N]}" = "true" || echo_failure; fi; fi; N=$(($N+1)); done; test "${SINGLE_OUTPUT}" = "true" && echo_success; ;; stop) # se l'output da dare all'utente non e' singolo, vado accapo test "${SINGLE_OUTPUT}" = "true" || echo # termino i comandi in ordine contrario a quello di avvio # NOTA: la variabile N presa dal check e' FONDAMENTALE N=$(($N-1)); while test ${N} -ne 0; do # se questo comando non va nascosto, lo mostro test "${HIDE_OUTPUT[$N]}" = "true" || echo_stop "${STOP_OPT[$N]}"; if test "${DAEMON[$N]}" = "true"; then # controllo che sia in esecuzione # NOTA: da rifare usando i pid di start!!!! pidof "${NAME[$N]}" &> /dev/null; if test $? -ne 0; then test "${HIDE_OUTPUT[$N]}" = "true" || echo_not_run; else su "${OWN[$N]}" -c "${STOP_OPT[$N]}" >> "${LOG_PATH}/${NAME[$N]}.log"; if test $? -eq 0; then test "${HIDE_OUTPUT[$N]}" = "true" || echo_success; else test "${HIDE_OUTPUT[$N]}" = "true" || echo_failure; fi; fi; else su "${OWN[$N]}" -c "${STOP_OPT[$N]}" >> "${LOG_PATH}/${NAME[$N]}.log"; if test $? -eq 0; then test "${HIDE_OUTPUT[$N]}" = "true" || echo_success; else test "${HIDE_OUTPUT[$N]}" = "true" || echo_failure; fi; fi; N=$(($N-1)); done; test "${SINGLE_OUTPUT}" = "true" && echo_success; ;; restart) echo ${0} stop; sleep 3s; ${0} start; ;; *) echo " Usage: ${0} {start|stop|restart}"; exit -1; ;; esac } # End