• 环境检查脚本


    环境检查脚本:

    根据提供的配置文件,读取本地的服务配置是否达标,以及是否能与远程的服务器实现端口的通信:

    脚本:

    #!/bin/bash
    #date:2017-08-25
    #注意当前目录下要有env_list.conf配置文件
    
    #system info
    system_info() { 
    echo "*********************************************************************************" 
    echo "system info:" 
    echo  
    echo "System-release : `cat /etc/redhat-release`" 
    echo "Kernel-release : `uname -a|awk '{print $1,$3}'`" 
    echo "Server-Model : `dmidecode | grep "Product Name:"|sed -n '1p'|awk -F': ' '{print $2}'`" 
    } 
    #CPU info 
    cpu_info() { 
    echo "**********************************************************************************" 
    local_cpu_core=`cat /proc/cpuinfo | grep "processor" | sort -u| wc -l `
    if [[ $1 -gt $local_cpu_core ]]; then
        echo -e "33[31m WARRING,local CPU logic core is $local_cpu_core33[0m" 
    else
        echo -e "33[32m OK,The local CPU logic core is $local_cpu_core33[0m" 
    
    fi
    }
    #memory info 
    mem_info() { 
    echo "**********************************************************************************" 
    max_check_total_memory=`echo $1|sed -e 's#G##g' -e 's#g##g' -e 's#M##g' -e 's#m##g'` 
    min_check_total_memory=`echo "$max_check_total_memory * 0.9"|bc`                                      #获取文件里面设置的内存的最小值,然后和本地的内存比较,小于本地内存就是正常的。
    local_total_memory=`free -h|grep Mem|awk '{print $2}'|sed -e 's#G##' -e 's#G##'`
    if [ `expr $local_total_memory > $min_check_total_memory` -eq 0 ]; then
        echo -e "33[31m WARRING,The local total memory  is ${local_total_memory}G33[0m"
    else
        echo -e "33[32m OK,The local total memory  is ${local_total_memory}G33[0m"
    fi
    }  
    #disk and partitions 
    partition_info() { 
    echo "**********************************************************************************" 
    echo 
    df -h
    echo 
    #分区(单位为M)
    ezdata_test=`df -h | grep '/ezdata'| awk -v OFS=',' '{print $6,$2}'`
    ezdata=$ezdata_test,$check_data_store,ezdata
    var_test=`df -h | grep '/var'| awk -v OFS=',' '{print $6,$2}'`
    var=$var_test,$check_log_store,var
    opt_test=`df -h | grep '/opt'| awk -v OFS=',' '{print $6,$2}'`
    opt=$opt_test,$check_package_store,opt
    gen_test=`df -h | grep -w '/'| awk -v OFS=',' '{print $6,$2}'`
    gen=$gen_test,$check_gen_store,gen
    array=($ezdata $var $opt $gen)                                      # 把需要检查的分区加入数组,每个元素的格式为:/,26G,10G,gen /是从df里面取出来,26G是本地/的存储大小,10G配置文件里设置的存储大小,gen标识符,后面提示引用
    for data in ${array[@]};do
        num=`echo $data|awk -F ',' '{print NF}'`
        par=`echo $data|awk -F ',' '{print $NF}'`
        if [[ $num -eq 3 ]]; then                                        # 如果一个元素通过逗号拆分出来后个数等于3,说明该系统没有改分区。
            echo -e "33[31m WARRING, Partition $par does not exist33[0m"
        fi          
        if [[ $num -eq 4 ]]; then                                        # 如果一个元素的通过逗号拆分出来后个数等于4,说明该系统有该分区。
            local_size=`echo $data|awk -F ',' '{print $2}'|sed -e 's#G##g' -e 's#g##g' -e 's#M##g' -e 's#m##g'`  
            set_size=`echo $data|awk -F ',' '{print $3}'|sed -e 's#G##g' -e 's#g##g' -e 's#M##g' -e 's#m##g'`
            if [[ local_size -gt set_size ]];then                                                                     # 通过比较该分区存储大小和配置文件中设置的分区的存储大小是否符合要求
            echo -e "33[32m OK,$par partition is ${local_size}G,larger than ${set_size}G33[0m"
            else
            echo -e "33[31m WARRING, $par partition is ${local_size}G,less than ${set_size}G33[0m"
            fi
        fi
    done 
     
    }
    
    #check port
    check_port() {
    echo "************************checking $1*********************************"
    check_ip_list=$2                                         # 获取需要检查的IP
    port_list=$3                                             # 获取需要检查的port
    check_ip_array=(${check_ip_list//,/ })                   # 把获取到的IP转换成数组,方便后边遍历
    for check_ip in ${check_ip_array[@]};do             
        for i in  ${port_list[@]};do                         # 每个IP遍历所有需要检查的port
    #         nohup timeout 5 tcpdump -i any host $check_ip  -n >> check_port.log 2>&1 &       
    #         nohup timeout 5 telnet $check_ip $i  >> /dev/null 2>&1 &
            nohup timeout 5 tcpdump -i any host $check_ip and port $i  -n >> check_port.log 2>&1 &
            sleep 1
            timeout 5 telnet $check_ip $i  >> /dev/null 2>&1
            sleep 5
            cat check_port.log |grep ack |grep -v sackOK | grep $check_ip >> /dev/null
            if [[ $? -ne 0 ]]; then
                echo -e "33[31m WARRING,This host can not communicate with $1($check_ip):$i33[0m"
            else
     
                echo -e "33[32m OK,This host can communicate with $1($check_ip):$i33[0m"
            fi
            rm -rf check_port.log
        done
        echo "---$5($4) check $1($check_ip) success ---"
    done
    }
    
    four_in_one () {
        system_info 
        cpu_info $check_CPU
        mem_info $check_memory
        partition_info 
    }
    
    main () {
    four_in_one
    
    if [[ $server_role == 'collector server' ]]; then
        check_port=(9092 9999 10051 10050)
        check_port "transfer server" "$transfer_ip"  "${check_port[*]}" $local_ip $server_role
        check_port=(10000 3306)
        check_port "analyze server" "$analyze_ip" "${check_port[*]}" $local_ip $server_role
    fi
    if [[ $server_role == 'transfer server' ]]; then
        check_port=(9092 9999 10050 10051)
        check_port "collector server" "$collector_ip" "${check_port[*]}" $local_ip $server_role
        check_port=(9092)
        check_port "analyze server" "$analyze_ip" "${check_port[*]}" $local_ip $server_role
    fi
    if [[ $server_role == 'analyze server' ]]; then
        check_port=(10000 3306)
        check_port "collector server" "$collector_ip" "${check_port[*]}" $local_ip $server_role
        check_port=(9092)
        check_port "transfer server" "$transfer_ip" "${check_port[*]}" $local_ip $server_role
    fi
    }
    source env_list.conf
    main |tee ${local_ip}_system.log

    配置文件env_list.conf:

    # 环境检查脚本读取的相关信息文件。
    server_role='analyze server' # 服务器角色有4种选择:transfer server、collector server、analyze server、all_in_one
    local_ip='192.168.174.143'    # 本地地址的IP
    check_memory=5G    # 输入内存大小
    check_CPU=1         # 输入CPU核数
    check_data_store=10G # 输入数据分区大小
    check_log_store=10G  # 输入日志分区大小
    check_package_store=10G # 输入安装包分区大小
    check_gen_store=10G   # 输入根分区大小
    transfer_ip=,3.3.3.3,4.4.4192.168.174.1.4   
    analyze_ip=192.168.174.1,3.3.3.3,4.4.4.4    
    collector_ip=192.168.174.1,3.3.3.3,4.4.4.4  
  • 相关阅读:
    A Bug's Life POJ 2492
    First Training
    洛谷----P1162 填涂颜色
    软考知识点梳理--软件测试
    软考知识点梳理--面向对象方法
    软考知识点梳理--国家信息化体系六要素
    软考知识点梳理--电子政务
    软考知识点梳理--激励理论
    软考知识点梳理--承建方项目论证
    软考知识点梳理--项目成本类型
  • 原文地址:https://www.cnblogs.com/hel7512/p/12350335.html
Copyright © 2020-2023  润新知