so-status module

This commit is contained in:
m0duspwnens
2020-09-11 18:28:53 -04:00
parent 2d508d9e57
commit 77911acfb4
2 changed files with 84 additions and 33 deletions

4
salt/_modules/so.py Normal file
View File

@@ -0,0 +1,4 @@
#!py
def status():
return __salt__['cmd.run']('/sbin/so-status')

View File

@@ -14,8 +14,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/>.
{%- from 'common/maps/so-status.map.jinja' import docker with context %}
{%- set container_list = docker['containers'] | sort | unique %}
if ! [ "$(id -u)" = 0 ]; then if ! [ "$(id -u)" = 0 ]; then
echo "This command must be run as root" echo "This command must be run as root"
@@ -39,7 +37,7 @@ declare -a container_state_list=()
declare -a expected_container_list=() declare -a expected_container_list=()
# {% raw %} #
compare_lists() { compare_lists() {
local found=0 local found=0
@@ -68,12 +66,16 @@ compare_lists() {
done done
} }
# {% endraw %} #
create_expected_container_list() { create_expected_container_list() {
{% for item in container_list -%} expected_container_list+=("so-curator")
expected_container_list+=("{{ item }}") expected_container_list+=("so-elasticsearch")
{% endfor -%} 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() { populate_container_lists() {
@@ -122,7 +124,7 @@ parse_status() {
printf $ERROR_STRING && return 1 printf $ERROR_STRING && return 1
} }
# {% raw %} #
print_line() { print_line() {
local service_name=${1} local service_name=${1}
@@ -149,7 +151,51 @@ print_line() {
printf "%s \n" " ]" printf "%s \n" " ]"
} }
non_term_print_line() {
local service_name=${1}
local service_state="$( parse_status ${2} )"
local PADDING_CONSTANT=10
printf " $service_name "
for i in $(seq 0 $(( 40 - $PADDING_CONSTANT - ${#service_name} - ${#service_state} ))); do
printf "-"
done
printf " [ "
printf "$service_state"
printf "%s \n" " ]"
}
main() { 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" local focus_color="\e[1;34m"
printf "\n" printf "\n"
printf "${focus_color}%b\e[0m" "Checking Docker status\n\n" printf "${focus_color}%b\e[0m" "Checking Docker status\n\n"
@@ -173,9 +219,10 @@ main() {
done done
printf "\n" printf "\n"
fi
} }
# {% endraw %} #
main main