From 77911acfb44cd511cd62d1cf9b12102a6c242a2c Mon Sep 17 00:00:00 2001 From: m0duspwnens Date: Fri, 11 Sep 2020 18:28:53 -0400 Subject: [PATCH 1/2] so-status module --- salt/_modules/so.py | 4 ++ salt/common/tools/sbin/so-status | 113 ++++++++++++++++++++++--------- 2 files changed, 84 insertions(+), 33 deletions(-) create mode 100644 salt/_modules/so.py diff --git a/salt/_modules/so.py b/salt/_modules/so.py new file mode 100644 index 000000000..b581468c8 --- /dev/null +++ b/salt/_modules/so.py @@ -0,0 +1,4 @@ +#!py + +def status(): + return __salt__['cmd.run']('/sbin/so-status') \ No newline at end of file diff --git a/salt/common/tools/sbin/so-status b/salt/common/tools/sbin/so-status index 8658f4757..9b95a1016 100755 --- a/salt/common/tools/sbin/so-status +++ b/salt/common/tools/sbin/so-status @@ -14,8 +14,6 @@ # # You should have received a copy of the GNU General Public License # along with this program. If not, see . -{%- from 'common/maps/so-status.map.jinja' import docker with context %} -{%- set container_list = docker['containers'] | sort | unique %} if ! [ "$(id -u)" = 0 ]; then echo "This command must be run as root" @@ -39,7 +37,7 @@ declare -a container_state_list=() declare -a expected_container_list=() -# {% raw %} +# compare_lists() { local found=0 @@ -68,13 +66,17 @@ compare_lists() { done } -# {% endraw %} +# create_expected_container_list() { - {% for item in container_list -%} - expected_container_list+=("{{ item }}") - {% endfor -%} -} + expected_container_list+=("so-curator") + expected_container_list+=("so-elasticsearch") + expected_container_list+=("so-filebeat") + expected_container_list+=("so-logstash") + expected_container_list+=("so-nginx") + expected_container_list+=("so-telegraf") + expected_container_list+=("so-wazuh") + } populate_container_lists() { systemctl is-active --quiet docker @@ -93,7 +95,7 @@ populate_container_lists() { for line in "${docker_raw_list[@]}"; do container_name="$( echo $line | sed -e 's/Name:\(.*\),State:\(.*\)/\1/' )" # Get value in the first search group (container names) container_state="$( echo $line | sed -e 's/Name:\(.*\),State:\(.*\)/\2/' )" # Get value in the second search group (container states) - + temp_container_name_list+=( "${container_name}" ) temp_container_state_list+=( "${container_state}" ) done @@ -122,7 +124,7 @@ parse_status() { printf $ERROR_STRING && return 1 } -# {% raw %} +# print_line() { local service_name=${1} @@ -149,33 +151,78 @@ print_line() { printf "%s \n" " ]" } -main() { - local focus_color="\e[1;34m" - printf "\n" - printf "${focus_color}%b\e[0m" "Checking Docker status\n\n" +non_term_print_line() { + local service_name=${1} + local service_state="$( parse_status ${2} )" - systemctl is-active --quiet docker - if [[ $? = 0 ]]; then - print_line "Docker" "running" - else - print_line "Docker" "exited" - fi + local PADDING_CONSTANT=10 - populate_container_lists - - printf "\n" - printf "${focus_color}%b\e[0m" "Checking container statuses\n\n" - - local num_containers=${#container_name_list[@]} - - for i in $(seq 0 $(($num_containers - 1 ))); do - print_line ${container_name_list[$i]} ${container_state_list[$i]} + printf " $service_name " + for i in $(seq 0 $(( 40 - $PADDING_CONSTANT - ${#service_name} - ${#service_state} ))); do + printf "-" done - - printf "\n" + printf " [ " + printf "$service_state" + printf "%s \n" " ]" } -# {% endraw %} +main() { + + # if running from salt + if [ "$TERM" == 'dumb' ]; then + printf "\n" + printf "Checking Docker status\n\n" + + systemctl is-active --quiet docker + if [[ $? = 0 ]]; then + non_term_print_line "Docker" "running" + else + non_term_print_line "Docker" "exited" + fi + + populate_container_lists + + printf "\n" + printf "Checking container statuses\n\n" + + local num_containers=${#container_name_list[@]} + + for i in $(seq 0 $(($num_containers - 1 ))); do + non_term_print_line ${container_name_list[$i]} ${container_state_list[$i]} + done + + printf "\n" + + # else if running from a terminal + else + + local focus_color="\e[1;34m" + printf "\n" + printf "${focus_color}%b\e[0m" "Checking Docker status\n\n" + + systemctl is-active --quiet docker + if [[ $? = 0 ]]; then + print_line "Docker" "running" + else + print_line "Docker" "exited" + fi + + populate_container_lists + + printf "\n" + printf "${focus_color}%b\e[0m" "Checking container statuses\n\n" + + local num_containers=${#container_name_list[@]} + + for i in $(seq 0 $(($num_containers - 1 ))); do + print_line ${container_name_list[$i]} ${container_state_list[$i]} + done + + printf "\n" + fi +} + +# -main +main \ No newline at end of file From b93d1496316ff972ffd260b127683aba472d5a79 Mon Sep 17 00:00:00 2001 From: m0duspwnens Date: Mon, 14 Sep 2020 09:36:26 -0400 Subject: [PATCH 2/2] fix so-status --- salt/common/tools/sbin/so-status | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/salt/common/tools/sbin/so-status b/salt/common/tools/sbin/so-status index 9b95a1016..b3aa582ce 100755 --- a/salt/common/tools/sbin/so-status +++ b/salt/common/tools/sbin/so-status @@ -14,6 +14,8 @@ # # You should have received a copy of the GNU General Public License # along with this program. If not, see . +{%- from 'common/maps/so-status.map.jinja' import docker with context %} +{%- set container_list = docker['containers'] | sort | unique %} if ! [ "$(id -u)" = 0 ]; then echo "This command must be run as root" @@ -37,7 +39,7 @@ declare -a container_state_list=() declare -a expected_container_list=() -# +# {% raw %} compare_lists() { local found=0 @@ -66,17 +68,13 @@ compare_lists() { done } -# +# {% endraw %} create_expected_container_list() { - expected_container_list+=("so-curator") - expected_container_list+=("so-elasticsearch") - expected_container_list+=("so-filebeat") - expected_container_list+=("so-logstash") - expected_container_list+=("so-nginx") - expected_container_list+=("so-telegraf") - expected_container_list+=("so-wazuh") - } + {% for item in container_list -%} + expected_container_list+=("{{ item }}") + {% endfor -%} +} populate_container_lists() { systemctl is-active --quiet docker @@ -124,7 +122,7 @@ parse_status() { printf $ERROR_STRING && return 1 } -# +# {% raw %} print_line() { local service_name=${1} @@ -222,7 +220,7 @@ main() { fi } -# +# {% endraw %} main \ No newline at end of file