F5周/月巡检脚本

    通常F5(LTM)的巡检内容包括收集当前的ucs配置备份、收集qkview自检包、日志打包,记录当前node、pool、vs的总数量、可用数量、异常数量、为知数量等等。
node

    因为F5的底层Linux作了不少的限制,因此获取F5的不少信息只能经过snmp来实现,请保证F5的snmp使用的是v2c版本,团体名能够自行修改变量,容许127.0.0.1访问。
bash

#!/bin/bash

function line()
{
    cols=`tput cols`
    
    for l in `seq 1 ${cols}`
    do
    echo -n "="
    done
}

function define()
{
    user=`whoami`
    hostname=`uname -n`
    path=/tmp/inspection/
    public="public"
    datestamp=`date +%Y%m%d`
    timestamp=`date +%H%M%S`
    platform=`tmsh show sys hardware | egrep -A1 Platform | awk 'NR==2{print $2,$3}'`
    sn=`tmsh show sys hardware | grep "Chassis Serial" | awk '{print $3}'`
    version=`tmsh show sys version | grep -E "  Version" | awk '{print $2}'`
    uptime=`/usr/bin/uptime | awk -F '[,| ]+' '{print $4,$5,$6}'`
}

function check()
{
    if [ $user != "root" ]
    then
        echo "Please use root user ."
        exit 1
    fi
    
    if [ ! -d $path ]
    then
        mkdir -p $path
    fi
}

function select_version()
{
    bigpipe version > /dev/null 2>&1
    
    if [ $? != 0 ]
    then
        version11
    else
        version10
    fi
}

function version10()
{
    fstime=`bigpipe failover show | awk '{print $4,$5}'`
    
    snmp_check_version10
    snmp
    
    tar zcf ${path}log_${hostname}_${datestamp}.tar.gz /var/log/* >/dev/null 2>&1
    tmsh save sys ucs /var/local/ucs/ucs_${hostname}_${datestamp}.ucs >/dev/null 2>&1
    cp /var/local/ucs/ucs_${hostname}_${datestamp}.ucs $path
    qkview > /dev/null 2>&1
    mv /var/tmp/${hostname}.tgz $path
    
    logfilt
    output
    backup
}

function version11()
{
    fstime=`tmsh show sys failover | awk '{print $4,$5}'`
    
    snmp_check_version11
    snmp
    tar zcf ${path}log_${hostname}_${datestamp}.tar.gz /var/log/* >/dev/null 2>&1
    tmsh save sys ucs /var/local/ucs/ucs_${hostname}_${datestamp}.ucs >/dev/null 2>&1
    cp /var/local/ucs/ucs_${hostname}_${datestamp}.ucs $path
    qkview > /dev/null 2>&1
    mv /var/tmp/${hostname}.qkview $path
    
    logfilt
    output
    backup
}

function snmp_check_version10()
{
    numstart=`awk '/^snmpd {$/{print NR}' /config/bigip_sys.conf`
    numstop=`awk '/^sshd {$/{print NR}' /config/bigip_sys.conf`
    numend=`echo | awk  "{print ${numstop}-1}"`
    numcheck=`sed -n "${numstart},${numend}p" /config/bigip_sys.conf | grep -E "127.0.0.1|0.0.0.0/0.0.0.0" | wc -l`
    
    if [ $numcheck = 0 ]
    then
        echo "Please allow 127.0.0.1 snmp ."
        exit 2
    fi
}

function snmp_check_version11()
{
    numstart=`awk '/^sys snmp {$/{print NR}' /config/bigip_base.conf`
    numstop=`awk '/^    communities {$/{print NR}' /config/bigip_base.conf`
    numend=`echo | awk "{print ${numstop}-1}"`
    numcheck=`sed -n "${numstart},${numend}p" /config/bigip_base.conf | grep -E " 127. |0.0.0.0/0.0.0.0" | wc -l`
    
    if [ $numcheck = 0 ]
    then
        echo "Please allow 127.0.0.1 snmp ."
        exit 2
    fi
}

function snmp()
{
    vs_red=`snmpwalk -v 2c -c $public localhost .1.3.6.1.4.1.3375.2.2.10.13.2.1.2|grep -c "red"`
    vs_green=`snmpwalk -v 2c -c $public localhost .1.3.6.1.4.1.3375.2.2.10.13.2.1.2|grep -c "green"`
    vs_blue=`snmpwalk -v 2c -c $public localhost .1.3.6.1.4.1.3375.2.2.10.13.2.1.2|grep -c "blue"`
    vs_total=`snmpwalk -v 2c -c $public localhost .1.3.6.1.4.1.3375.2.2.10.13.2.1.2|grep -c "F5-BIGIP-LOCAL-MIB"`
    vs_yellow=`snmpwalk -v 2c -c $public localhost .1.3.6.1.4.1.3375.2.2.10.13.2.1.2|grep -c "yellow"`
    pool_green=`snmpwalk -v 2c -c $public localhost .1.3.6.1.4.1.3375.2.2.5.5.2.1.2|grep -c "green"`
    pool_blue=`snmpwalk -v 2c -c $public localhost .1.3.6.1.4.1.3375.2.2.5.5.2.1.2|grep -c "blue"`
    pool_red=`snmpwalk -v 2c -c $public localhost .1.3.6.1.4.1.3375.2.2.5.5.2.1.2|grep -c "red"`
    pool_yellow=`snmpwalk -v 2c -c $public localhost .1.3.6.1.4.1.3375.2.2.5.5.2.1.2|grep -c "yellow"`
    pool_total=`snmpwalk -v 2c -c $public localhost .1.3.6.1.4.1.3375.2.2.5.5.2.1.2|grep -c "F5-BIGIP-LOCAL-MIB"`
    node_blue=`snmpwalk -v 2c -c $public localhost .1.3.6.1.4.1.3375.2.2.4.3.2.1.3|grep -c "blue"`
    node_green=`snmpwalk -v 2c -c $public localhost .1.3.6.1.4.1.3375.2.2.4.3.2.1.3|grep -c "green"`
    node_red=`snmpwalk -v 2c -c $public localhost .1.3.6.1.4.1.3375.2.2.4.3.2.1.3|grep -c "red"`
    node_yellow=`snmpwalk -v 2c -c $public localhost .1.3.6.1.4.1.3375.2.2.4.3.2.1.3|grep -c "yellow"`
    node_total=`snmpwalk -v 2c -c $public localhost .1.3.6.1.4.1.3375.2.2.4.3.2.1.3|grep -c "F5-BIGIP-LOCAL-MIB"`
}

function logfilt()
{
    gunzip -c /var/log/ltm.{30..1}.gz > ${path}log.tmp > /dev/null 2>&1
    cat /var/log/ltm >>  ${path}log.tmp
    grep -E "err|crit|alert|emery" ${path}log.tmp > ${path}logfilt.log
    numerr=`cat ${path}logfilt.log | wc -l`
}

function output()
{
    line > ${path}inspection.log
    echo -e "Date:\t\t${datestamp} ${timestamp}" >> ${path}inspection.log
    echo -e "Hostname:\t${hostname}" >> ${path}inspection.log
    echo -e "Platform:\t${platform}" >> ${path}inspection.log
    echo -e "Serial Number:\t${sn}" >> ${path}inspection.log
    echo -e "Version:\t${version}" >> ${path}inspection.log
    echo -e "Run time:\t${uptime}" >> ${path}inspection.log
    echo "" >> ${path}inspection.log
    echo -e "Virtual Server Status:\ttotal ${vs_total}\t\tAvailable ${vs_green}\t\tUnavailable ${vs_yellow}\t\tOffine ${vs_red}\t\tUNknow ${vs_blue}" >> ${path}inspection.log
    echo -e "Pool Status:\t\ttotal ${pool_total}\t\tAvailable ${pool_green}\t\tUnavailable ${pool_yellow}\t\tOffine ${pool_red}\t\tUNknow ${pool_blue}" >> ${path}inspection.log
    echo -e "Node Status:\t\ttotal ${node_total}\t\tAvailable ${node_green}\t\tUnavailable ${node_yellow}\t\tOffine ${node_red}\t\tUNknow ${node_blue}" >> ${path}inspection.log
    echo "" >> ${path}inspection.log
    echo -e "The recent failover time :\t${fstime}" >> ${path}inspection.log
    echo -e "The err log number :\t\t${numerr}" >> ${path}inspection.log
    line >> ${path}inspection.log
}

function backup()
{
    tar zcf ${path}inspection_backup_${datestamp}_${timestamp}.tar.gz `find ${path} -type f` > /dev/null 2>&1
    find $path -type f ! -name "inspection.log" ! -name "inspection_backup_${datestamp}_${timestamp}.tar.gz" ! -name "$0" | xargs rm -f {}
}

function main()
{
    define
    check
    select_version
}

main


v10版本:ssh

[root@f5_10_2_4:Active] config # sh f5_inspection.sh 
[root@f5_10_2_4:Active] config # cd /tmp/inspection/
[root@f5_10_2_4:Active] inspection # ls
inspection_backup_20150118_103743.tar.gz  inspection.log
[root@f5_10_2_4:Active] inspection # cat inspection.log 
=====================================================================================================================================================================================================
Date:           20150118 103743
Hostname:       f5_10_2_4.com
Platform:       BIG-IP Virtual
Serial Number:  564de91c-9657-08a3-d054cd969f84
Version:        10.2.4
Run time:       1 day 22:20
Virtual Server Status:  total 1         Available 0             Unavailable 0           Offine 0                UNknow 0
Pool Status:            total 1         Available 0             Unavailable 0           Offine 0                UNknow 0
Node Status:            total 1         Available 0             Unavailable 0           Offine 0                UNknow 0
The recent failover time :      49355d 23:29:34
The err log number :            28
=====================================================================================================================================================================================================
[root@f5_10_2_4:Active] inspection #


v11版本:ide

[root@f5_11_5_3:Active:Standalone] config # sh f5_inspection.sh 
[root@f5_11_5_3:Active:Standalone] config # cd /tmp/inspection/
[root@f5_11_5_3:Active:Standalone] inspection # ls
inspection_backup_20150118_104028.tar.gz  inspection.log
[root@f5_11_5_3:Active:Standalone] inspection # cat inspection.log 
=====================================================================================================================================================================================================
Date:           20150118 104028
Hostname:       f5_11_5_3.com
Platform:       BIG-IP Virtual
Serial Number:  564d9078-7ad9-5e47-64b737aab97c
Version:        11.5.3
Run time:       1 day 13:06
Virtual Server Status:  total 1         Available 0             Unavailable 0           Offine 0                UNknow 0
Pool Status:            total 1         Available 0             Unavailable 0           Offine 0                UNknow 0
Node Status:            total 1         Available 0             Unavailable 0           Offine 0                UNknow 0
The recent failover time :      0d 00:04:39
The err log number :            132
=====================================================================================================================================================================================================
[root@f5_11_5_3:Active:Standalone] inspection #


PS:v10版本的上次切换时间显示,是虚拟机的问题,真机并没有问题。spa


最后拿到的压缩包中,是ucs配置备份,qkview自检文件,/var/log目录下的全部日志文件,过滤ltm日志文件中全部包括err等错误关键字的日志条目,还有就是单独出现的这个inspection.log文件。pwa