设为首页 收藏本站
查看: 508|回复: 0

Shell-批量巡检服务器脚本

[复制链接]

尚未签到

发表于 2018-8-22 10:19:13 | 显示全部楼层 |阅读模式
#!/bin/bash  
########################################################################################
  
#Function:
  
#This script checks the system's information,disks's information,performance,etc...of the
  
#server
  
#
  
#Author:
  
#By Jack Wang
  
#
  
#Company:
  
#ShaanXi Great Wall Information Co.,Ltd.
  
########################################################################################
  

  
########################################################################################
  
#
  
#GatherServerIpAddress is the server's IP address that gather the checking log
  
#GatherServerPassword is the server's IP address that gather the checking log
  
#
  
########################################################################################
  
GatherServerIpAddress=$1
  
GatherServerPassword=$2
  

  
########################################################################################
  
#GetTheIpCommand is a command that you can get the IP address
  
########################################################################################
  
GetTheIpCommand=`ifconfig | grep '\([[:digit:]]\{1,3\}\.\)\{3\}[[:digit:]]\{1,3\}' --color=auto -o | sed -e '2,5d'`
  

  
########################################################################################
  
#LogName is a command that Your logs'name
  
########################################################################################
  
LogName=`ifconfig|grep '\([[:digit:]]\{1,3\}\.\)\{3\}[[:digit:]]\{1,3\}' --color=auto -o|sed -e '2,5d'``echo "-"``date +%Y%M%d`
  

  
########################################################################################
  
#
  
#GatherLogPath is a path that collecting log path
  
#LocalServerLogPath is local log path
  
#
  
########################################################################################
  
GatherServerLogPath="/tmp/GatherLogDirectory"
  
LocalServerLogPath="/tmp/LocalServerLogDirectory"
  

  

  
########################################################################################
  
#LinuxOsInformation is function that usege to collect OS's information
  
########################################################################################
  
LinuxOsInformation(){
  
Hostname=`hostname`
  
UnameA=`uname -a`
  
OsVersion=`cat /etc/issue | sed '2,4d'`
  
Uptime=`uptime|awk '{print $3}'|awk -F "," '{print $1}'`
  
ServerIp=`ifconfig|grep "inet"|sed '2,4d'|awk -F ":" '{print $2}'|awk '{print $1}'`
  
ServerNetMask=`ifconfig|grep "inet"|sed '2,4d'|awk -F ":" '{print $4}'|awk '{print $1}'`
  
ServerGateWay=`netstat -r|grep "default"|awk '{print $2}'`
  
SigleMemoryCapacity=`dmidecode|grep -P -A5 "Memory\s+Device"|grep "Size"|grep -v "Range"|grep '[0-9]'|awk -F ":" '{print $2}'|sed 's/^[ \t]*//g'`
  
MaximumMemoryCapacity=`dmidecode -t 16|grep "Maximum Capacity"|awk -F ":" '{print $2}'|sed 's/^[ \t]*//g'`
  
NumberOfMemorySlots=`dmidecode -t 16|grep "Number Of Devices"|awk -F ":" '{print $2}'|sed 's/^[ \t]*//g'`
  
MemoryTotal=`cat /proc/meminfo|grep "MemTotal"|awk '{printf("MemTotal:%1.0fGB\n",$2/1024/1024)}'|awk -F ":" '{print $2}'`
  
PhysicalMemoryNumber=`dmidecode|grep -A16 "Memory Device"|grep "Size:"|grep -v "No Module Installed"|grep -v "Range Size:"|wc -l`
  
ProductName=`dmidecode|grep -A10 "System Information"|grep "Product Name"|awk -F ":" '{print $2}'|sed 's/^[ \t]*//g'`
  
SystemCPUInfomation=`cat /proc/cpuinfo|grep "name"|cut  -d: -f2|awk '{print "*"$1,$2,$3,$4}'|uniq -c|sed 's/^[ \t]*//g'`
  

  
echo -e "Hostname|$Hostname\nUnamea|$UnameA\nOsVersion|$OsVersion\nUptime|$Uptime\nServerIp|$ServerIp\nServerNetMask|$ServerNetMask\nServerGateWay|$ServerGateWay\nSigleMemoryCapacity|$SigleMemoryCapacity\nMaximumMemoryCapacity|$MaximumMemoryCapacity\nNumberOfMemorySlots|$NumberOfMemorySlots\nMemoryTotal|$MemoryTotal\nPhysicalMemoryNumber|$PhysicalMemoryNumber\nProductName|$ProductName\nSystemCPUInformation|$SystemCPUInfomation"
  

  
}
  

  
PerformanceInfomation (){
  
CPUIdle=`top -d 2 -n 1 -b|grep C[Pp][Uu]|grep id|awk '{print $5}'|awk -F "%" '{print $1}'`
  
CPUloadAverage=`top -d 2 -n 1 -b|grep "load average:"|awk -F ":" '{print $5}'|sed 's/^[ \t]*//g'`
  
ProcessNumbers=`top -d 2 -n 1 -b|grep "Tasks"|awk -F "[: ,]" '{print $3}'`
  
Proce***unning=`top -d 2 -n 1 -b|grep "Tasks"|awk -F "[: ,]" '{print $8}'`
  
ProcessSleeping=`top -d 2 -n 1 -b|grep "Tasks"|awk -F "[: ,]" '{print $11}'`
  
ProcessStoping=`top -d 2 -n 1 -b|grep "Tasks"|awk -F "[: ,]" '{print $16}'`
  
ProcessZombie=`top -d 2 -n 1 -b|grep "Tasks"|awk -F "[: ,]" '{print $21}'`
  
UserSpaceCPU=`top -d 2 -n 1 -b|grep 'C[Pp][Uu]'|head -1|awk -F "[: ,%]" '{print $4}'`
  
SystemSpaceCPU=`top -d 2 -n 1 -b|grep 'C[Pp][Uu]'|head -1|awk -F "[: ,%]" '{print $8}'`
  
ChangePriorityCPU=`top -d 2 -n 1 -b|grep 'C[Pp][Uu]'|head -1|awk -F "[: ,%]" '{print $12}'`
  
WaitingCPU=`top -d 2 -n 1 -b|grep 'C[Pp][Uu]'|head -1|awk -F "[: ,%]" '{print $19}'`
  
HardwareIRQCPU=`top -d 2 -n 1 -b|grep 'C[Pp][Uu]'|head -1|awk -F "[: ,%]" '{print $23}'`
  
SoftwareIRQCPU=`top -d 2 -n 1 -b|grep 'C[Pp][Uu]'|head -1|awk -F "[: ,%]" '{print $27}'`
  
MemUsed=`top -d 2 -n 1 -b|grep "Mem"|awk -F "[: ,]" '{print $11}'|tr -d "a-zA-Z"|awk '{printf("%dM\n",$1/1024)}'`
  
MemFreeP=`top -d 2 -n 1 -b|grep "Mem"|awk -F "[: ,]" '{print $16}'|tr -d "a-zA-Z"|awk '{printf("%dM\n",$1/1024)}'`
  
MemBuffersP=` top -d 2 -n 1 -b|grep "Mem"|awk -F "[: ,]" '{print $22}'|tr -d "a-zA-Z"|awk '{printf("%dM\n",$1/1024)}'`
  
CacheCachedP=`top -d 2 -n 1 -b|grep "Swap"|awk -F "[: ,]" '{print $24}'|tr -d "a-zA-Z"|awk '{printf("%dM\n",$1/1024)}'`
  
CacheTotal=`top -d 2 -n 1 -b|grep "Swap"|awk -F "[: ,]" '{print $4}'|tr -d "a-zA-Z"|awk '{printf("%dM\n",$1/1024)}'`
  
CacheUsed=`top -d 2 -n 1 -b|grep "Swap"|awk -F "[: ,]" '{print $14}'|tr -d "a-zA-Z"|awk '{printf("%dM\n",$1/1024)}'`
  
CacheFree=`top -d 2 -n 1 -b|grep "Swap"|awk -F "[: ,]" '{print $18}'|tr -d "a-zA-Z"|awk '{printf("%dM\n",$1/1024)}'`
  

  
echo -e "CPUIdle|$CPUIdle\nCPUloadAverage|$CPUloadAverage\nProcessNumbers|$ProcessNumbers\nProce***unning|$Proce***unning\nProcessSleeping|$ProcessSleeping\nProcessStoping|$ProcessStoping\nProcessZombie|$ProcessZombie\nUserSpaceCPU|$UserSpaceCPU\nSystemSpaceCPU|$SystemSpaceCPU\nChangePriorityCPU|$ChangePriorityCPU\nWaitingCPU|$WaitingCPU\nHardwareIRQCPU|$HardwareIRQCPU\nSoftwareIRQCPU|$SoftwareIRQCPU\nMemUsed|$MemUsed\nMemFreeP|$MemFreeP\nMemBuffersP|$MemBuffersP\nCacheCachedP|$CacheCachedP\nCacheTotal|$CacheTotal\nCacheUsed|$CacheUsed\nCacheFree|$CacheFree\n"
  
}
  

  
OprateSystemSec () {
  
echo '======================UserLogin======================'
  
w
  

  
echo '======================FileUsed======================='
  
df -ah
  

  
echo '======================dmesgError====================='
  
dmesg | grep error
  

  
echo '======================dmesgFail======================'
  
dmesg | grep Fail
  

  
echo '======================BootLog========================'
  
more /var/log/boot.log | grep -V "OK" | sed '1,6d'
  

  
echo '======================route -n======================='
  
route -n
  
echo '======================iptables -L===================='
  
iptables -L
  
echo '======================netstat -lntp=================='
  
netstat -lntp
  
echo '======================netstat -antp=================='
  
netstat -antp
  
echo '======================BootLog========================'
  
netstat -s
  
echo '======================netstat -s====================='
  
last
  
echo '======================du -sh /etc/==================='
  
du -sh /etc/
  
echo '======================du -sh /boot/=================='
  
du -sh /boot/
  
echo '======================du -sh /dev/==================='
  
du -sh /dev/
  
echo '======================df -h=========================='
  
df -h
  
echo '======================mount | column -t=============='
  
mount | column -t
  

  
}
  

  

  
TopAndVmstat(){
  
top -d 2 -n 1 -b
  
vmstat 1 10
  
}
  

  
CheckGatherLog(){
  

  
if [ -f "$LocalServerLogPath/$GetTheIpCommand.log" ];then
  
       rm -rf $LocalServerLogPath/$GetTheIpCommand.log
  
fi
  

  
if [ ! -x "$LocalServerLogPath" ];then
  
    mkdir "$LocalServerLogPath"
  
fi
  

  
if [ ! -f "$LocalServerLogPath/$GetTheIpCommand.log" ];then
  
    touch $LocalServerLogPath/$GetTheIpCommand.log
  
    LinuxOsInformation>>$LocalServerLogPath/$GetTheIpCommand.log
  
    PerformanceInfomation>>$LocalServerLogPath/$GetTheIpCommand.log
  
    OprateSystemSec>>$LocalServerLogPath/$GetTheIpCommand.log
  
    TopAndVmstat>>$LocalServerLogPath/$GetTheIpCommand.log
  
fi
  
}
  

  
CheckGatherLog
  

  
SCP_LOG_TO_GATHER_SERVER="scp $LocalServerLogPath/$GetTheIpCommand.log root@$GatherServerIpAddress:$GatherServerLogPath"
  

  
/usr/bin/expect

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-555001-1-1.html 上篇帖子: 通过shell将*.txt文本内容批量录入mysql 下篇帖子: shell 脚本快速部署dhcp服务器!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表