mirror of
https://github.com/Security-Onion-Solutions/securityonion.git
synced 2025-12-06 09:12:45 +01:00
Merge pull request #6496 from JamesMConroy/so-staus-tty
so-staus detects tty
This commit is contained in:
@@ -15,10 +15,6 @@
|
|||||||
# You should have received a copy of the GNU General Public License
|
# You should have received a copy of the GNU General Public License
|
||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
if ! [ "$(id -u)" = 0 ]; then
|
|
||||||
echo "This command must be run as root"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
display_help() {
|
display_help() {
|
||||||
cat <<HELP_USAGE
|
cat <<HELP_USAGE
|
||||||
@@ -100,10 +96,15 @@ create_expected_container_list() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# {% raw %}
|
||||||
populate_container_lists() {
|
populate_container_lists() {
|
||||||
|
# TODO: check exit code directly, not with $?
|
||||||
systemctl is-active --quiet docker
|
systemctl is-active --quiet docker
|
||||||
|
|
||||||
if [[ $? = 0 ]]; then
|
if [[ $? = 0 ]]; then
|
||||||
|
# TODO: look into using docker templates instead of curl and jq
|
||||||
|
# Ex docker ps --format "{{.Names}}\t{{.State}}"
|
||||||
|
# TODO: convert the output to an associtive array
|
||||||
mapfile -t docker_raw_list < <(curl -s --unix-socket /var/run/docker.sock http:/v1.40/containers/json?all=1 \
|
mapfile -t docker_raw_list < <(curl -s --unix-socket /var/run/docker.sock http:/v1.40/containers/json?all=1 \
|
||||||
| jq -c '.[] | { Name: .Names[0], State: .State }' \
|
| jq -c '.[] | { Name: .Names[0], State: .State }' \
|
||||||
| tr -d '/{"}')
|
| tr -d '/{"}')
|
||||||
@@ -167,60 +168,55 @@ parse_status() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# {% raw %}
|
|
||||||
|
|
||||||
print_line() {
|
print_line() {
|
||||||
local service_name=${1}
|
local service_name="${1}"
|
||||||
local service_state="$( parse_status ${1} ${2} )"
|
local service_state="" ; service_state="$( parse_status "${1}" "${2}" )"
|
||||||
local columns=$(tput cols)
|
# XXX: What will we do if tput isn't avalable?
|
||||||
local state_color="\e[0m"
|
local line=""
|
||||||
|
local PADDING_CONSTANT=""
|
||||||
|
local columns=35 # value used if not printing to a tty
|
||||||
|
|
||||||
local PADDING_CONSTANT=15
|
if (( __tty == 1 )); then
|
||||||
|
local reset_attr; reset_attr="$(tput sgr0)" # reset all attributes
|
||||||
|
local bold; bold="$(tput bold)"
|
||||||
|
local red; red="$(tput setaf 1)"
|
||||||
|
local green; green="$(tput setaf 2)"
|
||||||
|
local yellow; yellow="$(tput setaf 3)"
|
||||||
|
PADDING_CONSTANT=15 # whitespace + brackets + 1
|
||||||
|
|
||||||
if [[ $service_state = "$ERROR_STRING" ]] || [[ $service_state = "$MISSING_STRING" ]]; then
|
columns=$(tput cols)
|
||||||
state_color="\e[1;31m"
|
fi
|
||||||
|
|
||||||
|
# construct a line of '------' so that the names and states are all aligned
|
||||||
|
linewidth=$(( columns - PADDING_CONSTANT - ${#service_name} - ${#service_state} ))
|
||||||
|
for i in $(seq 0 "${linewidth}"); do
|
||||||
|
line="${line}-"
|
||||||
|
done
|
||||||
|
|
||||||
|
if [[ $service_state = "$ERROR_STRING" ]] \
|
||||||
|
|| [[ $service_state = "$MISSING_STRING" ]]; then
|
||||||
|
state_color="${red:-}"
|
||||||
if [[ "$EXITCODE" -eq 0 ]]; then
|
if [[ "$EXITCODE" -eq 0 ]]; then
|
||||||
EXITCODE=1
|
EXITCODE=1
|
||||||
fi
|
fi
|
||||||
elif [[ $service_state = "$SUCCESS_STRING" ]]; then
|
elif [[ $service_state = "$SUCCESS_STRING" ]]; then
|
||||||
state_color="\e[1;32m"
|
state_color="${green:-}"
|
||||||
elif [[ $service_state = "$PENDING_STRING" ]] || [[ $service_state = "$DISABLED_STRING" ]] || [[ $service_state = "$STARTING_STRING" ]] || [[ $service_state = "$WAIT_START_STRING" ]]; then
|
elif [[ $service_state = "$PENDING_STRING" ]] \
|
||||||
state_color="\e[1;33m"
|
|| [[ $service_state = "$DISABLED_STRING" ]] \
|
||||||
|
|| [[ $service_state = "$STARTING_STRING" ]] \
|
||||||
|
|| [[ $service_state = "$WAIT_START_STRING" ]]; then
|
||||||
|
state_color="${yellow:-}"
|
||||||
EXITCODE=2
|
EXITCODE=2
|
||||||
fi
|
fi
|
||||||
|
|
||||||
printf " $service_name "
|
service_state="${bold:-}${state_color:-}${service_state}${reset_attr:-}"
|
||||||
for i in $(seq 0 $(( $columns - $PADDING_CONSTANT - ${#service_name} - ${#service_state} ))); do
|
line="${bold:-}${state_color:-}${line:-}${reset_attr:-}"
|
||||||
printf "${state_color}%b\e[0m" "-"
|
printf " %s %s [ %s ] \n" "${service_name}" "${line:-}" "${service_state}"
|
||||||
done
|
|
||||||
printf " [ "
|
|
||||||
printf "${state_color}%b\e[0m" "$service_state"
|
|
||||||
printf "%s \n" " ]"
|
|
||||||
}
|
|
||||||
|
|
||||||
non_term_print_line() {
|
|
||||||
local service_name=${1}
|
|
||||||
local service_state="$( parse_status ${1} ${2} )"
|
|
||||||
|
|
||||||
if [[ $service_state = "$ERROR_STRING" ]] || [[ $service_state = "$MISSING_STRING" ]]; then
|
|
||||||
if [[ "$EXITCODE" -eq 0 ]]; then
|
|
||||||
EXITCODE=1
|
|
||||||
fi
|
|
||||||
elif [[ $service_state = "$PENDING_STRING" ]] || [[ $service_state = "$DISABLED_STRING" ]] || [[ $service_state = "$STARTING_STRING" ]] || [[ $service_state = "$WAIT_START_STRING" ]]; then
|
|
||||||
EXITCODE=2
|
|
||||||
fi
|
|
||||||
|
|
||||||
printf " $service_name "
|
|
||||||
for i in $(seq 0 $(( 35 - ${#service_name} - ${#service_state} ))); do
|
|
||||||
printf "-"
|
|
||||||
done
|
|
||||||
printf " [ "
|
|
||||||
printf "$service_state"
|
|
||||||
printf "%s \n" " ]"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
main() {
|
main() {
|
||||||
|
is_tty
|
||||||
# if running from salt
|
# if running from salt
|
||||||
if [ "$CALLER" == 'salt-call' ] || [ "$CALLER" == 'salt-minion' ]; then
|
if [ "$CALLER" == 'salt-call' ] || [ "$CALLER" == 'salt-minion' ]; then
|
||||||
printf "\n"
|
printf "\n"
|
||||||
@@ -228,20 +224,19 @@ main() {
|
|||||||
|
|
||||||
systemctl is-active --quiet docker
|
systemctl is-active --quiet docker
|
||||||
if [[ $? = 0 ]]; then
|
if [[ $? = 0 ]]; then
|
||||||
non_term_print_line "Docker" "running"
|
print_line "Docker" "running"
|
||||||
else
|
else
|
||||||
non_term_print_line "Docker" "exited"
|
print_line "Docker" "exited"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
populate_container_lists
|
populate_container_lists
|
||||||
|
|
||||||
printf "\n"
|
printf "\nChecking container statuses\n\n"
|
||||||
printf "Checking container statuses\n\n"
|
|
||||||
|
|
||||||
local num_containers=${#container_name_list[@]}
|
local num_containers=${#container_name_list[@]}
|
||||||
|
|
||||||
for i in $(seq 0 $(($num_containers - 1 ))); do
|
for i in $(seq 0 $(($num_containers - 1 ))); do
|
||||||
non_term_print_line ${container_name_list[$i]} ${container_state_list[$i]}
|
print_line ${container_name_list[$i]} ${container_state_list[$i]}
|
||||||
done
|
done
|
||||||
|
|
||||||
printf "\n"
|
printf "\n"
|
||||||
@@ -257,9 +252,12 @@ main() {
|
|||||||
else
|
else
|
||||||
print_or_parse="print_line"
|
print_or_parse="print_line"
|
||||||
|
|
||||||
local focus_color="\e[1;34m"
|
if (( __tty == 1 )) ; then
|
||||||
printf "\n"
|
local bold; bold="$(tput bold)"
|
||||||
printf "${focus_color}%b\e[0m" "Checking Docker status\n\n"
|
local focus_color; focus_color="$(tput setaf 4)"
|
||||||
|
local reset_attr; reset_attr="$(tput sgr0)" # reset all attributes
|
||||||
|
fi
|
||||||
|
printf "\n${bold}${focus_color:-}%s${reset_attr:-}\n\n" "Checking Docker status"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
systemctl is-active --quiet docker
|
systemctl is-active --quiet docker
|
||||||
@@ -272,8 +270,7 @@ main() {
|
|||||||
populate_container_lists
|
populate_container_lists
|
||||||
|
|
||||||
if [ "$QUIET" = false ]; then
|
if [ "$QUIET" = false ]; then
|
||||||
printf "\n"
|
printf "\n${bold}${focus_color:-}%s${reset_attr:-}\n\n" "Checking container statuses"
|
||||||
printf "${focus_color}%b\e[0m" "Checking container statuses\n\n"
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
local num_containers=${#container_name_list[@]}
|
local num_containers=${#container_name_list[@]}
|
||||||
@@ -288,20 +285,30 @@ main() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
is_tty() {
|
||||||
|
__tty=0
|
||||||
|
[ -t 1 ] && __tty=1
|
||||||
|
# don't print colors if NO_COLOR is set to anything
|
||||||
|
[ "${#NO_COLOR}" -ne 0 ] && __tty=0
|
||||||
|
}
|
||||||
|
|
||||||
# {% endraw %}
|
# {% endraw %}
|
||||||
|
|
||||||
|
if ! [ "$(id -u)" = 0 ]; then
|
||||||
|
echo "${0}: This command must be run as root"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
while getopts ':hq' OPTION; do
|
while getopts ':hq' OPTION; do
|
||||||
case "$OPTION" in
|
case "$OPTION" in
|
||||||
h)
|
h)
|
||||||
display_help
|
display_help
|
||||||
exit 0
|
exit 0
|
||||||
;;
|
;;
|
||||||
q)
|
q) QUIET=true ;;
|
||||||
QUIET=true
|
|
||||||
;;
|
|
||||||
\?)
|
\?)
|
||||||
display_help
|
display_help
|
||||||
exit 0
|
exit 1
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
|||||||
Reference in New Issue
Block a user