码迷,mamicode.com
首页 > 系统相关 > 详细

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

时间:2017-02-08 14:58:11      阅读:352      评论:0      收藏:0      [点我收藏+]

标签:cpu   流量   内存   磁盘   pass   pts   负载   shell   user   

 

# cat check_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

# 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"

 

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

标签:cpu   流量   内存   磁盘   pass   pts   负载   shell   user   

原文地址:http://www.cnblogs.com/xiaoming279/p/6377952.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!