那些年写过的shell拿出来干死你丫的
#!/bin/bash
# Linux Check Script
# author :liupeng
dat=`date +%Y-%m-%d`
mem=`free -m |grep "Mem" |awk -F ' ' '{print $2}'`
vers=`cat /etc/centos-release`
ipp=`ifconfig eno1 |grep 'inet' |awk -F ' ' '{print $2}' |sed -n 1p`
hn=`hostname`
pycpu=`cat /proc/cpuinfo | grep "physical id"|sort | uniq | wc -l`
locpu=`cat /proc/cpuinfo | grep "processor"| wc -l`
centosversion=`awk '{print $(NF-1)}' /etc/centos-release`
resultfile="/tmp/${hn}_${dat}_${ipp}.txt"
Physical_CPUs=`grep "physical id" /proc/cpuinfo| sort | uniq | wc -l`
Virt_CPUs=`grep "processor" /proc/cpuinfo | wc -l`
CPU_Kernels=`grep "cores" /proc/cpuinfo|uniq| awk -F ': ' '{print $2}'`
CPU_Type=`grep "model name" /proc/cpuinfo | awk -F ': ' '{print $2}' | sort | uniq`
CPU_Arch=`uname -m`
SELinux=`/usr/sbin/sestatus | grep "SELinux status: " | awk '{print $3}'`
uptime=`uptime | sed 's/.*up \([^,]*\), .*/\1/'`
mys=`ps -ef |grep mysql |grep -v 'grep' |wc -l`
ora=`ps -ef |grep ora |grep -v 'grep' |wc -l`
mongo=`ps -ef | grep mongo | grep -v 'grep' |wc -l `
elasticsearch=`ps -ef | grep elasticsearch | grep -v 'grep' | wc -l`
redis=`ps -ef | grep redis | grep -v 'grep' | wc -l`
hadoop=`ps -ef |grep hadoop | grep -v 'grep' |wc -l `
storm=`ps -ef |grep storm | grep -v 'grep' |wc -l `
hazelcast=`ps -ef |grep hazelcast | grep -v 'grep' |wc -l `
keepalived=`ps -ef |grep keepalived | grep -v 'grep' |wc -l `
varnish=`ps -ef | grep varnish| grep -v 'grep'| wc -l`
rabbitmq=`ps -ef | grep rabbitmq | grep -v 'grep' | wc -l`
disk=`df -h`
function getjiemian()
{
echo " "
echo " "
echo " ******************************服务器,数据库日常巡检*****************************"
echo " "
echo "-- 服务器巡检完毕 !......"
echo "-- 服务器当前日期:${dat}"
echo "-- 主机名:${hn}"
echo " "
echo "========================================================================="
echo "1.内存(总):${mem} MB"
echo "2.操作系统版本:${vers}"
echo "3.IP地址: ${ipp}"
echo "4.物理CPU个数:${pycpu}"
echo "5.逻辑CPU个数:${locpu}"
echo "6.CPU型号:${CPU_Type}"
echo "7.CPU架构:${CPU_Arch}"
echo "8.SELINUX:${SELinux}"
echo "9.系统运行时间:${uptime}"
echo "10.系统使用存储:${disk}"
echo "========================================================================="
sleep 5
}
function getmemstatus()
{
echo ""
echo ""
echo "************************** 内存检查 **************************"
if [[ $centosVersion < 7 ]];then
free -m
else
free -h
fi
}
function getdiskstatus()
{
echo ""
echo ""
echo "************************** 磁盘检查 **************************"
df -hiP | sed 's/Mounted on/Mounted/'> /tmp/inode
df -hTP | sed 's/Mounted on/Mounted/'> /tmp/disk
join /tmp/disk /tmp/inode | awk '{print $1,$2,"|",$3,$4,$5,$6,"|",$8,$9,$10,$11,"|",$12}'| column -t
}
function getservicestatus()
{
echo ""
echo ""
echo "************************** 服务检查 **************************"
echo ""
if [[ $centosVersion > 7 ]];then
#conf=$(systemctl list-unit-files --type=service --state=enabled --no-pager | grep "enabled")
process=$(systemctl list-units --type=service --state=running --no-pager | grep ".service")
else
#conf=$(/sbin/chkconfig | grep -E ":on|:启用")
process=$(/sbin/service --status-all 2>/dev/null | grep -E "is running|正在运行")
fi
echo "正在运行的服务"
echo "--------------"
echo "${process}"
}
function getprocessstatus()
{
echo ""
echo ""
echo "************************** 进程检查 **************************"
if [ $(ps -ef | grep defunct | grep -v grep | wc -l) -ge 1 ];then
echo ""
echo "僵尸进程";
echo "--------"
ps -ef | head -n1
ps -ef | grep defunct | grep -v grep
fi
echo ""
echo "内存占用TOP10"
echo "-------------"
echo -e "PID %MEM RSS COMMAND
$(ps aux | awk '{print $2, $4, $6, $11}' | sort -k3rn | head -n 10 )"| column -t
echo ""
echo "CPU占用TOP10"
echo "------------"
top b -n1 | head -17 | tail -11
}
function getoracle()
{
echo ""
echo ""
echo "************************** oracle数据库检查 **************************"
if [ ${ora} -gt 0 ];then
echo ""
echo "1.是否存在Oracle数据库";
echo "-------------------"
echo "YES,Oracle Database Is Running!"
else
echo ""
echo "1.是否存在Oracle数据库";
echo "-------------------"
echo "No,Oracle Database Is Not Running!"
fi
}
function getmysql()
{
echo ""
echo ""
echo "************************** mysql数据库检查 **************************"
if [ ${mys} -gt 0 ];then
echo ""
echo "2.是否存在MySQL数据库";
echo "-------------------"
echo "YES,MySQL Database Is Running!"
else
echo ""
echo "2.是否存在MySQL数据库";
echo "-------------------"
echo "No,MySQL Database Is Not Running!"
fi
}
function getmongo()
{
echo ""
echo ""
echo "************************** mongo数据库检查 **************************"
if [ ${mongo} -gt 0 ];then
echo ""
echo "1.是否存在mongo数据库";
echo "-------------------"
echo "YES,Mongo Database Is Running!"
else
echo ""
echo "1.是否存在mongo数据库";
echo "-------------------"
echo "No,Mongo Database Is Not Running!"
fi
}
function getelasticsearch()
{
echo ""
echo ""
echo "************************** ElasticSearch引擎检查 **************************"
if [ ${elasticsearch} -gt 0 ];then
echo ""
echo "2.是否存在ElasticSearch引擎";
echo "-------------------"
echo "YES,ElasticSearch Is Running!"
else
echo ""
echo "2.是否存在ElasticSearch引擎";
echo "-------------------"
echo "No,ElasticSearch Is Not Running!"
fi
}
function getredis()
{
echo ""
echo ""
echo "************************** redis检查 **************************"
if [ ${redis} -gt 0 ];then
echo ""
echo "1.是否存在redis数据库";
echo "-------------------"
echo "YES,redis Database Is Running!"
else
echo ""
echo "1.是否存在Redis数据库";
echo "-------------------"
echo "No,Redis Database Is Not Running!"
fi
}
function gethadoop()
{
echo""
echo""
echo "************************** hadoop检查 **************************"
if [ ${hadoop} -gt 0 ];then
echo ""
echo "2.是否存在 hadoop";
echo "-------------------"
echo "YES,hadoop Is Running!"
else
echo ""
echo "2.是否存在hadoop";
echo "-------------------"
echo "No,hadoop Is Not Running!"
fi
}
function getstorm()
{
echo""
echo""
echo "************************** hadoop检查 **************************"
if [ ${storm} -gt 0 ];then
echo ""
echo "3.是否存在 storm";
echo "-------------------"
echo "YES,storm Is Running!"
else
echo ""
echo "3.是否存在storm";
echo "-------------------"
echo "No,storm Is Not Running!"
fi
}
function getkeepalived()
{
echo""
echo""
echo "************************** keeplived检查 **************************"
if [ ${keepalived} -gt 0 ];then
echo ""
echo "是否存在 keepalived";
echo "-------------------"
echo "YES,keepalived Is Running!"
else
echo ""
echo "是否存在keepalived";
echo "-------------------"
echo "No,keepalived Is Not Running!"
fi
}
function getvarnish()
{
echo""
echo""
echo "************************** keeplived检查 **************************"
if [ ${varnish} -gt 0 ];then
echo ""
echo "5.是否存在 varnish";
echo "-------------------"
echo "YES,varnish Is Running!"
else
echo ""
echo "5.是否存在varnish";
echo "-------------------"
echo "No,varnish Is Not Running!"
fi
}
function gethazelcast()
{
echo""
echo""
echo "************************** hazelcast检查 **************************"
if [ ${hazelcast} -gt 0 ];then
echo ""
echo "5.是否存在 hazelcast";
echo "-------------------"
echo "YES,hazelcast Is Running!"
else
echo ""
echo "5.是否存在varnish";
echo "-------------------"
echo "No,hazelcast Is Not Running!"
fi
}
function getrabbitmq()
{
echo""
echo""
echo "************************** rabbitmq检查 **************************"
if [ ${rabbitmq} -gt 0 ];then
echo ""
echo "6.是否存在 rabbitmq";
echo "-------------------"
echo "YES,rabbitmq Is Running!"
else
echo ""
echo "6.是否存在rabbitmq";
echo "-------------------"
echo "No,rabbitmq Is Not Running!"
fi
}
function getdockerstatus()
{
echo "************************** docker status ****************************"
docker ps
}
function getmessege()
{
err=`egrep -rn "error" /var/log/messages* |awk '{print $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14}' | sed -n '/^*ERROR/p'`
echo "************************** 操作系统错误日志 **************************"
echo "操作系统错误日志";
echo "-------------------"
echo ${err}
}
#执行函数掉用
getjiemian;
function check()
{
getjiemian
getmemstatus
getdiskstatus
getservicestatus
getprocessstatus
getdatabase
getmiddleware
}
#执行检查并保存检查结果
check > ${resultfile}
echo " "
echo " "
echo "检查结果:"
echo "---------"
echo "${resultfile}"
echo " "
echo " "
#sed '/^SQL/d' -i /tmp/${resultfile}
学之如大海汪洋,薄积厚发,后发制人,大学之道也。