shell脚本收集服务器基本信息并入库

前端之家收集整理的这篇文章主要介绍了shell脚本收集服务器基本信息并入库前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

# cat global.sh

--------------------------------------------------------------------------------------------------------

#!/bin/bash

#create by hexm @2016.6

DATE=`date +%Y%m%d`

TIME=`date '+%F %H:%M:%S'`

MysqL_USER="xxx"

MysqL_PASSWD="xxx"

MysqL_ADDRESS="10.88.2.111"

EXEC_LOG="/app/healthcheck/log/exec_$DATE.log"

#DATA_LOG="/app/healthcheck/log/data_$DATE.log"

SOURCE_FILE="/app/healthcheck"

DIST_FILE="/app/healthcheck"

SSH_KEY="/root/.ssh/id_rsa_gongy"

#IP=`ifconfig eth1|awk -F"[ :]+" 'NR==2{print $4}'`

DATA_LOG="/app/healthcheck/log/${IP}_data_$DATE.log"

INSERT_sql_LOG="/app/healthcheck/log/insertsql_$IP_$DATE.log"


-------------------------------------------------------------------------------------------

#catcheck_all.sh



#!/bin/bash

#create by hexm @2016.6

#date=`date +%Y%m%d`

#DIST_FILE=/app/healthcheck

#EXEC_LOG=/app/healthcheck/log/exec_$date.log


set -e


IP=`ifconfig eth1|awk -F"[ :]+" 'NR==2{print $4}'`


#加载变量

. /app/healthcheck/scripts/global.sh

#因rsync用户名有nobody和csocket两种,这里判断下防止被修改

CHECK_ALL_OWN=`ls -ld /app/healthcheck/ |awk '{print $3}'`

rsync_user=`cat /etc/rsyncd.conf|grep uid|awk -F"[ =]+" '{print $NF}'`

if [[ ${CHECK_ALL_OWN} != ${rsync_user} ]];then

chown ${rsync_user}.${rsync_user} /app/healthcheck -R

fi


#获取cpu使用率、cpu负载

TOP_INFO(){

#TOP_DETAIL=$(/usr/bin/top -bn1 )

#cpu_USE=$(echo -e "$TOP_DETAIL" |grep "cpu(s):" |awk '{print $2}' | tail -n1 |cut -d"%" -f 1)

TOP_DETAIL=$(/usr/bin/top -b -d 10 -n 2 )

cpu_USE=$(echo -e "$TOP_DETAIL" |grep "cpu(s):" |awk '{print $2}' | tail -n1 |cut -d"%" -f 1)

cpu_LOAD=$(uptime | awk -F"," '{print $(NF-2)}'| awk '{print $3}')

cpu_LOAD=$(echo -e "${cpu_LOAD}" | sed 's/[,]//g')

}


#获取可用内存率

MEM(){

Free_mem=`free -m|awk -F"[ :]+" 'NR==2{print $4+$6+$7}'`

Totol_mem=`free -m|grep Mem |awk '{print $2}'`

ava_mem=`echo "scale=2;$Free_mem/$Totol_mem*100" | bc`

MEM_FREE=${ava_mem/.*}

}


#获取读IO和写IO

IO(){

R_IO=`iostat -x|awk '/sda/{print $4}'|awk '{sum+=$1}END{print sum/NR}'`

W_IO=`iostat -x|awk '/sda/{print $5}'|awk '{sum+=$1}END{print sum/NR}'`

}


#获取磁盘可用率

#划分为"/"、"DFS"、"APP"、"FDFS"四种

DISK(){

DISK_USE=`df -h|awk "/\/$/"'{print $(NF-1)}'|cut -d"%" -f 1`

if [[ "${DISK_USE}" == "" ]];then

echo "$IP:无/目录" &>/dev/null

else

DISK_USE=${DISK_USE/.*}

DISK_FREE=$((100-$DISK_USE))

fi


DISK_DFS_USE=`df -h|awk "/\/dfs/"'{print$(NF-1)}'|cut -d"%" -f 1`

if [[ "${DISK_DFS_USE}" == "" ]];then

echo "$IP:无/dfs目录" &> /dev/null

else

DISK_DFS_USE=${DISK_DFS_USE/.*}

DISK_DFS_FREE=$((100-${DISK_DFS_USE}))

fi

DISK_APP_USE=`df -h|awk "/\/app/"'{print$(NF-1)}'|cut -d"%" -f 1`

if [[ "${DISK_APP_USE}" == "" ]];then

echo "$IP:无/app目录" &> /dev/null

else

DISK_APP_USE=${DISK_APP_USE/.*}

DISK_APP_FREE=$((100-$DISK_APP_USE))

fi


DISK_FDFS_USE=`df -h|awk "/\/fdfs/"'{print$(NF-1)}'|cut -d"%" -f 1`

if [[ "${DISK_FDFS_USE}" == "" ]];then

echo "$IP:无/fdfs目录" &> /dev/null

else

DISK_FDFS_USE=`echo $DISK_FDFS_USE|tr " " "\n"|awk '{sum+=$1}END{print sum/NR}'`

DISK_FDFS_USE=${DISK_FDFS_USE/.*}

DISK_FDFS_FREE=$((100-${DISK_FDFS_USE}))

fi

}


#获取网卡单秒流量

NETWORK_FLOW(){

FLAG=`ifstat 1 1|head -1|awk '{print $1}'`

if [ $FLAG == eth1 ];then

WAN_INPUT="None"

WAN_OUTPUT="None"

LAN_INPUT=`ifstat 1 1 |awk 'NR==3{print $1}'`

LAN_OUTPUT=`ifstat 1 1 |awk 'NR==3{print $2}'`

else

WAN_INPUT=`ifstat 1 1 |awk 'NR==3{print $1}'`

WAN_OUTPUT=`ifstat 1 1 |awk 'NR==3{print $2}'`

LAN_INPUT=`ifstat 1 1 |awk 'NR==3{print $3}'`

LAN_OUTPUT=`ifstat 1 1 |awk 'NR==3{print $4}'`

fi

}

MEM

TOP_INFO

DISK

NETWORK_FLOW

IO


#数据入库

TIME=`date '+%F %H:%M:%S'`

MysqL -u${MysqL_USER} -p${MysqL_PASSWD} -h${MysqL_ADDRESS} <<EOF

use db_devops;

insert into tbl_data(IP,cpu_USE,MEM_FREE,cpu_LOAD,WAN_INPUT,WAN_OUTPUT,LAN_INPUT,LAN_OUTPUT,DISK_FREE,DISK_DFS_FREE,DISK_APP_FREE,DISK_FDFS_FREE,R_IO,W_IO,SYS_TIME,sql_TIME)

values('${IP}','$cpu_USE','$MEM_FREE','$cpu_LOAD','${WAN_INPUT:-None}','${WAN_OUTPUT:-None}','${LAN_INPUT:-None}','${LAN_OUTPUT:-None}','${DISK_FREE:-None}','${DISK_DFS_FREE:-None}','${DISK_APP_FREE:-None}','${DISK_FDFS_FREE:-None}','${R_IO:-None}','${W_IO:-None}','$TIME',now());


EOF

猜你在找的Bash相关文章