信息采集上报

  1. 通过Shell脚本实现服务器CPU、内存、PCI设备、登录日志等信息定时采集和自主上报
  2. Shell脚本比Agent更加轻量化,无侵入式实现定时采集、自主上报,对业务几乎无影响,用户侧无感知

1、定时上报

搭建httpd文件服务器后,将采集脚本上传至对应目录,待采集服务器下载执行即可实现自主上报

  • 被采集服务器可使用curl或者wget进行脚本文件下载和执行
  • 被采集服务器可使用crontab进行定时任务,指定每天12点定时执行脚本,并把执行脚本的日志写入指定文件
  • crontab使用方式可自行Google
# 脚本下载方式
wget http://101.43.144.88/get_info.sh | bash
或curl –O http://101.43.144.88/get_info.sh | bash

# 打开定时任务配置文件
crontab -e

# 指定每天12点定时执行脚本,并把执行脚本的日志写入对应文件 test.log
0 12 * * * sh demo.sh > result.log

2、数据采集

2.1、系统基础信息采集

#!/bin/bash
# 用于开发测试环境操作系统相关信息采集

# 获取系统基础信息
function getSystemInfo() {

    Brand=$(dmidecode -s system-manufacturer | tail -n 1)     # 品牌    Tencent Cloud
    Model=$(dmidecode -s system-product-name | tail -n 1)     # 型号    CVM
    SN=$(dmidecode -s system-serial-number | tail -n 1)       # 序列号  8541d005-a671-4736-902a-bd9b3e9a1afd
    #    UseFor="xxx产品"                  # 用户填写
    CpuModel=$(cat /proc/cpuinfo | grep -i "model name"| head -n 1 | awk -F':' '{print $2}' | sed 's/ //g')     # CPU型号 Intel(R)Xeon(R)Platinum8255CCPU@2.50GHz
    CpuNumber=$(cat /proc/cpuinfo | grep -i "model name" | wc -l)                        # CPU数量 2
    free -h
    if [ $? -eq 0 ]; then
        Memory=$(free -h |grep Mem| awk -F ":  *" '{print $2}'|awk -F "  *" '{print $1}')       # 最大内存
    else
        # 适配Centos6
        number=$(free | grep Mem| awk -F ":  *" '{print $2}'| awk -F "  *" '{print $1}')       # 最大内存
        Reverse ${number} > temp.ini
        Memory=$(cat temp.ini)
    fi
    dmidecode -s system-product-name | tail -n 1 | grep -i "VM"                          # 平台类型 KVM


    # 2. 上报基础环境信息
    body="{\"SN\":\"$SN\", \"Brand\":\"$Brand\", \"Model\":\"$Model\", \"Memory\":\"$Memory\", \"PlatformType\":\"$PlatformType\", \"CpuModel\":\"$CpuModel\", \"CpuNumber\":\"$CpuNumber\"}"
#    echo "JSON body :" $body

    ret=$(curl -H "System:api-gateway" -H "Token:czc27be93ecsz53a53fc2a8443aeebe0" "http://127.0.0.1:8000/api/v1/config/esunny/meta/info" -XPOST -d "$body" -H "Content-Type: application/json")
#    echo "MetaInfo result:" $ret

    # 错误码不是0就表示失败,0代表成功
    ErrCode=$(echo $ret  | sed 's/,/\n/g' | grep "ErrCode" | sed 's/:/\n/g' | sed '1d')
    if [[ x"0" != x"$ErrCode" ]]; then
        echo "error| up MetaInfo to server error $ret"
        exit 1
    fi
}

2.2、登陆日志采集

# 获取设备登录日志
function getLoginINfo() {
    SN=$(dmidecode -s system-serial-number | tail -n 1)  # 序列号  8541d005-a671-4736-902a-bd9b3e9a1afd
    last -f /var/log/wtmp | head -3 | sed -e '/reboot/d' > temp.ini          #  root  pts/1   123.149.240.130  Mon May 29 13:48 - 16:07  (02:18)
    cat temp.ini | while read line
    do
        UserName=$(echo ${line} | awk '{print $1}')                                     # 登录用户名
        UserIp=$(echo ${line} | awk '{print $3}')                                       # 登录源IP
        StartTime=$(echo ${line} | awk '{print $5,$6,$7}' | sed 's/:/ /g')              # 登录时间
        EndTime=$(echo ${line} | awk '{print $5,$6,$9}' | sed 's/:/ /g')                # 结束时间
        Duration=$(echo ${line} | awk '{print $10}' | sed 's/(//g' | sed 's/)//g')      # 持续时长

        body="{\"SN\":\"$SN\", \"UserName\":\"$UserName\", \"UserIp\":\"$UserIp\", \"StartTime\":\"$StartTime\", \"EndTime\":\"$EndTime\", \"Duration\":\"$Duration\"}"
#        echo "JSON body :" $body

        ret=$(curl -H "System:api-gateway" -H "Token:czc27be93ecsz53a53fc2a8443aeebe0" "http://127.0.0.1:8000/api/v1/config/esunny/loginInfo" -XPOST -d "$body" -H "Content-Type: application/json")
#        echo "result:" $ret

        ErrCode=$(echo $ret  | sed 's/,/\n/g' | grep "ErrCode" | sed 's/:/\n/g' | sed '1d')
        if [[ x"0" != x"$ErrCode" ]]; then
            echo "error| up Login Info to server error $ret"
            exit 1
        fi
    done
}

2.3、PCI设备信息采集

# 获取PCI设备信息
function getPCINfo() {
    SN=$(dmidecode -s system-serial-number | tail -n 1)   # 序列号  8541d005-a671-4736-902a-bd9b3e9a1afd
    lspci -nn
    if [ $? -ne 0 ]; then
        echo "skip lspci info "
    else
        lspci -nn > temp.ini                      # PCI设备 00:00.0 Host bridge [0600]: Intel Corporation 440FX - 82441FX PMC [Natoma] [8086:1237] (rev 02)
        cat temp.ini | while read line
        do
            PCIDevice=${line}
            body="{\"SN\":\"$SN\", \"PCIDevice\":\"$PCIDevice\"}"
    #        echo "JSON body :" $body

            ret=$(curl -H "System:api-gateway" -H "Token:czc27be93ecsz53a53fc2a8443aeebe0" "http://127.0.0.1:8000/api/v1/config/esunny/pciDevice" -XPOST -d "$body" -H "Content-Type: application/json")
    #        echo "result:" $ret

            ErrCode=$(echo $ret  | sed 's/,/\n/g' | grep "ErrCode" | sed 's/:/\n/g' | sed '1d')
            if [[ x"0" != x"$ErrCode" ]]; then
                echo "error| up PCIDevice Info to server error $ret"
                exit 1
            fi
            sleep 0.1
        done
    fi

}

2.4、逻辑磁盘信息采集

# 获取逻辑磁盘信息
function getLogicDiskInfo() {
    SN=$(dmidecode -s system-serial-number | tail -n 1)    # 序列号  8541d005-a671-4736-902a-bd9b3e9a1afd
    df -h
    if [ $? -ne 0 ]; then
        echo "skip logicDisk info "
    else
        df -h  > temp.ini                          # 逻辑磁盘 /dev/vda1        59G   16G   42G  28% /
        sed -i '1d' temp.ini

        cat temp.ini | while read line
        do

            Filesystem=$(echo ${line} | awk '{print $1}')
            Size=$(echo ${line} | awk '{print $2}')
            Used=$(echo ${line} | awk '{print $3}')
            Avail=$(echo ${line} | awk '{print $4}')
            UsePercent=$(echo ${line} | awk '{print $5}')
            Mounted=$(echo ${line} | awk '{print $6}')
            body="{\"SN\":\"$SN\", \"Filesystem\":\"$Filesystem\", \"Size\":\"$Size\", \"Used\":\"$Used\", \"Avail\":\"$Avail\", \"UsePercent\":\"$UsePercent\", \"Mounted\":\"$Mounted\"}"
    #        echo "JSON body :" $body

            ret=$(curl -H "System:api-gateway" -H "Token:czc27be93ecsz53a53fc2a8443aeebe0" "http://127.0.0.1:8000/api/v1/config/esunny/logicDisk" -XPOST -d "$body" -H "Content-Type: application/json")
    #        echo "result:" $ret

            ErrCode=$(echo $ret  | sed 's/,/\n/g' | grep "ErrCode" | sed 's/:/\n/g' | sed '1d')
            if [[ x"0" != x"$ErrCode" ]]; then
                echo "error| up LogicalDisk Info to server error $ret"
                exit 1
            fi
            sleep 0.1
        done
    fi
}

2.5、Centos6老旧系统适配

# dmidecode 显示格式适配
dmidecode -s system-manufacturer | tail -n 1

# /dev/mem访问权限问题
sudo chmod 777 /dev/mem

# 内存显示
free 得到数值后进行换算

3、功能测试

  • sh执行采集脚本文件,出现success标志,同时数据库中多个表出现对应数据,create时间和脚本执行时间一致

服务器管理平台开发(4)- 信息采集上报_sed

服务器管理平台开发(4)- 信息采集上报_ide_02

服务器管理平台开发(4)- 信息采集上报_运维_03

End

公众号平台更改了推送规则,如果不想错过内容,请点击“赞”,感谢支持!