#!/bin/bash

firewalld_status=$(systemctl is-active firewalld)
##需要开放的端口
port_list=(2377/tcp 7946/tcp 7946/udp 4789/tcp 4789/udp 80/tcp 443/tcp 9000/tcp 5001/tcp 6789/tcp 6800-7300/tcp 7480/tcp 8080/tcp 7000/tcp)
not_open_list=()

function check_port() {
    #检查端口是否开放，没有开放的放入not open list数组中
    for port in ${port_list[*]}; do
        firewall-cmd --list-ports | grep "${port}" &>/dev/null
        if [ $? -ne 0 ]; then
            not_open_list[${#not_open_list[*]}]=${port}
        fi
    done
}

function main() {
    if [ "${firewalld_status}" = "inactive" ]; then
        checkStatus=OK
        msg="防火墙服务己关闭"
    else
        check_port
        if [ ${#not_open_list[*]} -eq 0 ]; then
            checkStatus=OK
            msg="防火墙端口己开放"
        else
            checkStatus=fail
            msg="以下所需要的端口没有开放：${not_open_list[*]}"
        fi
    fi
}

main

echo "{\"checkValue\":\"${msg}\", \"checkStatus\":\"${checkStatus}\", \"msg\":\"${msg}\"}"
