From b049232a9d9f01416f1d756eac82b2e969e0e524 Mon Sep 17 00:00:00 2001 From: Gardouille Date: Wed, 28 Jan 2015 17:47:55 +0100 Subject: [PATCH] Dynmotd: rename the occur_num function into get_pattern_count get_pattern_count: add a expected value and compare this one and the number of occurrence to choose the color. --- dynmotd | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/dynmotd b/dynmotd index e4f080a..7a60026 100755 --- a/dynmotd +++ b/dynmotd @@ -68,12 +68,16 @@ service_info() { ## Return the number of occurrence a pattern is present in a file # and a color (red:>0, green:=0) -occur_num() { +get_pattern_count() { local GREP_PAT="${1}" local FILE="${2}" + local EXP_VAL="${3}" - NUM=$(grep "${GREP_PAT}" "${FILE}" | wc -l) - if [ "${NUM}" = "0" ]; then + # Count the pattern in the file + NUM=$(grep -E "${GREP_PAT}" "${FILE}" | wc -l) + + # If $EXP_VAL exist ++ the $NUM and $EXP_VAL are equal + if [ "${EXP_VAL}" ] && [ "${NUM}" = "${EXP_VAL}" ]; then MSG="${GREEN}${NUM}" else MSG="${REDB}${NUM}" @@ -113,15 +117,14 @@ SSH_USER_LOGIN=$(grep 'session opened' "${AUTH_LOG_FILE}" | awk '/sshd/' | awk " MOTD_ORIG='/etc/motd.orig' # If an original Motd exist, print it if [ -f ${MOTD_ORIG} ]; then - printf "%b" "${RESET}${MAGENTA}+++++++++++++++++++++++: ${WHITE}MoTD${MAGENTA} :++++++++++++++++++++++++${RESET}" + printf '%b' "${RESET}${MAGENTA}+++++++++++++++++++++++: ${WHITE}MoTD${MAGENTA} :++++++++++++++++++++++++${RESET}" while read -r line; do printf '\n%s' "${line}" done < ${MOTD_ORIG} fi - -printf "%b" "\n${RESET}${MAGENTA}+++++++++++++++++++: ${WHITE}System Data${MAGENTA} :+++++++++++++++++++++${RESET} +printf '%b' "\n${RESET}${MAGENTA}+++++++++++++++++++: ${WHITE}System Data${MAGENTA} :+++++++++++++++++++++${RESET} ${MAGENTA}+ ${WHITE}Hostname\t${MAGENTA}= ${GREEN}${HOSTNAME} ${MAGENTA}+ ${WHITE}Addresses\t${MAGENTA}= ${GREEN}${NET_ADDR} ${MAGENTA}+ ${WHITE}Kernel\t${MAGENTA}= ${GREEN}${KERNEL_VER} @@ -131,7 +134,7 @@ ${MAGENTA}+ ${WHITE}Memory\t${MAGENTA}= ${GREEN}${MEM_FREE}(free)/${MEM_TOTAL}(t ${MAGENTA}+ ${WHITE}Swap\t\t${MAGENTA}= ${GREEN}${SWAP_FREE}(free)/${SWAP_TOTAL}(total) kB" -printf "%b" "\n${RESET}${MAGENTA}++++++++++++++++++++: ${WHITE}User Data${MAGENTA} :++++++++++++++++++++++${RESET} +printf '%b' "\n${RESET}${MAGENTA}++++++++++++++++++++: ${WHITE}User Data${MAGENTA} :++++++++++++++++++++++${RESET} ${MAGENTA}+ ${WHITE}Username\t${MAGENTA}= ${GREEN}${USERNAME} ${MAGENTA}+ ${WHITE}Sessions\t${MAGENTA}= ${GREEN}${USER_SESSION} ${MAGENTA}+ ${WHITE}Processes\t${MAGENTA}= ${GREEN}$PROC_COUNT of ${PROC_LIMIT} MAX" @@ -145,11 +148,11 @@ printf '%b' "\n${MAGENTA}++++++++++++: ${WHITE}Authentication Information${MAGEN #done # Count the number of failed ssh authentication -SSH_FAIL_LOGIN=$(occur_num 'sshd.*Failed' "${AUTH_LOG_FILE}") +SSH_FAIL_LOGIN=$(get_pattern_count 'sshd.*Failed' "${AUTH_LOG_FILE}" '0') printf '%b' "\n${MAGENTA}+ ${WHITE}SSH fail\t${MAGENTA}= ${SSH_FAIL_LOGIN} fail(s) this week" # Count the number of failed sudo authentication -SUDO_FAIL=$(occur_num 'sudo.*authentication failure' "${AUTH_LOG_FILE}") -SUDO_3_FAIL=$(occur_num 'sudo.*3 incorrect password' "${AUTH_LOG_FILE}") +SUDO_FAIL=$(get_pattern_count 'sudo.*authentication failure' "${AUTH_LOG_FILE}" '0') +SUDO_3_FAIL=$(get_pattern_count 'sudo.*3 incorrect password' "${AUTH_LOG_FILE}" '0') printf '%b' "\n${MAGENTA}+ ${WHITE}Sudo fail\t${MAGENTA}= ${GREEN}${SUDO_FAIL} fail(s) this week ${MAGENTA}+ ${WHITE}Sudo 3 fails\t${MAGENTA}= ${GREEN}${SUDO_3_FAIL} fail(s) this week"