[root@localhost ~]# vim自动巡检脚本.sh
#!/bin/bash
###################################################################
# Functions: this script from polling system status
# Info: be suitable for CentOS/RHEL 6/7
# Changelog:
# 2018-01-28 Hetl initial commit
###################################################################
#set path env,if not set will some command not found in crontab
# export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
# source /etc/profile
#set user to run this script
# report_user='monitor'
# run this script use monitor
# if [ `whoami` != "$report_user" ];then
# echo " only monitor can run this script"
# exit 1
# fi
# define log path
# LOGPATH=/home/$report_user/var
# [ -d $LOGPATH ] || mkdir -p $LOGPATH
# RESULTFILE="$LOGPATH/HostDailyCheck-`hostname`-`date +%Y%m%d`.txt"
# check system version,6 or 7 ?
OS_Version=$(awk '{print $(NF-1)}' /etc/redhat-release)
# define globle variable
report_DateTime='' #日期
report_SystemVersion='' #系统版本
report_Hostname='' #主机名
report_IP='' #主机IP
report_CpuUser='' #用户空间占用CPU百分比
report_CpuSys='' #内核空间占用CPU百分比
report_CpuIdle='' #CPU空闲率
report_CpuUsed='' #CPU使用率
report_MemALL='' #内存大小
report_MemFree='' #内存剩余
report_MenRate='' #使用率
report_DiskInfo='' #磁盘信息
report_DefunctProsess='' #僵尸进程数量
report_ProcessCount='' #系统进程数
report_ProcessStatus='' #特定进程状态
function getSystemStatus(){
report_DateTime=$(date +"%F %T")
report_SystemVersion=$(cat /etc/redhat-release)
report_Hostname=$(hostname)
}
function getCpuStatus(){
if [[ $OS_Version < 7 ]];then
report_CpuUser=$(top -n 1|grep 'Cpu(s)'|awk '{print $2}'|sed 's/us\,//')
report_CpuSys=$(top -n 1|grep 'Cpu(s)'|awk '{print $3}'|sed 's/sy\,//')
report_CpuIdle=$(top -n 1|grep 'Cpu(s)'|awk '{print $5}'|sed 's/id\,//')
report_CpuUsed='' #建议程序计算
else
report_CpuUser=$(top -n 1|grep 'Cpu(s)'|awk '{print $2}')%
report_CpuSys=$(top -n 1|grep 'Cpu(s)'|awk '{print $4}')%
report_CpuIdle=$(top -n 1|grep 'Cpu(s)'|awk '{print $8}')%
report_CpuUsed='' #建议程序计算
fi
}
function getIpInfo(){
#report_IP=$(ip -f inet addr | grep -v 127.0.0.1 | grep inet | awk '{print $NF,$2}' | tr '\n' ',' | sed 's/,$//')
if [[ $OS_Version < 7 ]];then
report_IP=$(ifconfig|grep "inet addr:"|grep -v "127.0.0.1"|cut -d: -f2|awk '{print $1}')
else
report_IP=$(ifconfig|grep -w inet|grep -v "127.0.0.1"|awk '{print $2}')
fi
}
function getMemInfo(){
report_MemALL=$(grep MemTotal /proc/meminfo|awk {'print $2'})
report_MemFree=$(grep MemFree /proc/meminfo|awk {'print $2'})
report_MenRate='' #使用率--建议程序计算
}
function getDiskInfo(){
report_DiskInfo=$(df -Ph |grep -vE 'boot|run|tempfs|Filesystem|文件系统'|grep -vw '0%'|awk '{print $6,$5,$4}')
}
function getProcessNum(){
#僵尸进程数量
report_DefunctProsess="$(ps -ef | grep defunct | grep -v grep|wc -l)"
#进程总数
report_ProcessCount="$(ps aux|wc -l)"
}
#获取单个进程的状态,参数1:程序名,参数2:筛选关键字
function getSingleProcessStats(){
if [ $# -eq 1 ];then
count=$(ps -ef | grep $1 | grep -v 'grep' | wc -l)
elif [[ $# -eq 2 ]]; then
count=$(ps -ef | grep $1 |grep $2| grep -v 'grep' |wc -l)
else
count="ERROR"
fi
if [[ $count = "ERROR" ]]; then
report_ProcessStatus=$report_ProcessStatus$1" ""getSingleProcessStats()参数错误""|"
elif [[ count -ge 1 ]]; then
report_ProcessStatus=$report_ProcessStatus$1" ""YES""|"
else
report_ProcessStatus=$report_ProcessStatus$1" ""NO""|"
fi
}
function getAllProcessStats(){
getSingleProcessStats java CheungSSH aaa
getSingleProcessStats flume
getSingleProcessStats systemd
getSingleProcessStats netns
}
#统一监控平台OVO接口测试
function OVOWebServiceStatus(){
nodeName=`echo -n "192.168.56.200" | base64`
alertName=`echo -n "TEST" | base64`
severity=`echo -n "ALARM" | base64`
Message=`echo -n "OVO接口测试,无需报障,谢谢!" | base64`
#获取当前时间,yyyyMMddHHmmSS
arisingTime=`date +%Y%m%d%H%M%S`
arisingTime=`echo -n "${arisingTime}" | base64`
#目前接口至通过agentId="OVO"的告警
agentId=`echo -n "OVO" | base64`
curl http://192.168.56.200:8089/collectorAlertService/collectAlert/$nodeName/$alertName/$severity/$Message/$arisingTime/$agentId
if [ $? -eq 0 ];
then
ImmsWebServiceStatus="ACCESS"
echo $ImmsWebServiceStatus
fi
}
function getJson(){
json="{
\"DateTime\":\"$report_DateTime\",
\"report_Hostname\":\"$report_Hostname\",
\"report_IP\":\"$report_IP\",
\"report_SystemVersion\":\"$report_SystemVersion\",
\"report_CpuUser\":\"$report_CpuUser\",
\"report_CpuSys\":\"$report_CpuSys\",
\"report_CpuIdle\":\"$report_CpuIdle\",
\"report_MemALL\":\"$report_MemALL\",
\"report_MemFree\":\"$report_MemFree\",
\"report_DiskInfo\":\"$report_DiskInfo\",
\"report_DefunctProsess\":\"$report_DefunctProsess\",
\"report_ProcessCount\":\"$report_ProcessCount\",
\"report_ProcessStatus\":\"$report_ProcessStatus\"
}"
echo "$json"
}
function main(){
getSystemStatus
getCpuStatus
getIpInfo
getDiskInfo
getMemInfo
getProcessNum
getAllProcessStats
getJson
}
#main > $RESULTFILE
main
shell脚本的效果图
[root@localhost ~]# sh zhuhaiyan.sh
{
"DateTime":"2018-07-22 13:46:03",
"report_Hostname":"room9pc01",
"report_IP":"176.130.4.34
192.168.4.254
192.168.2.254
201.1.1.254
201.1.2.254
172.25.254.250
172.25.0.250
192.168.6.254
192.168.122.1",
"report_SystemVersion":"CentOS Linux release 7.4.1708 (Core) ",
"report_CpuUser":"28.8%",
"report_CpuSys":"0.0%",
"report_CpuIdle":"97.0%",
"report_MemALL":"16147372",
"report_MemFree":"12032600",
"report_DiskInfo":"/ 75% 29G
/dev/shm 2% 7.6G
/var/ftp/openstack-ext 100% 0
/var/lib/libvirt/images 59% 77G
/var/ftp/centos7 100% 0
/var/www/html/rhel7 100% 0
/var/ftp/rhel7 100% 0
/var/ftp/openstack 100% 0",
"report_DefunctProsess":"0",
"report_ProcessCount":"248",
"report_ProcessStatus":"java getSingleProcessStats()参数错误|flume NO|systemd YES|netns YES|"
}
--------------------