linux服务器巡检命令

发布于 2022-07-22  156 次阅读


系统基本信息

操作系统版本

uname -a

查看当前运行的系统信息

cat /proc/version

查看发行版本

cat /etc/issue

查看发行详细信息

yum install redhat-lsb -y

lsb_release -a

网络信息

网络配置信息

ifconfig -a

查看网络连接情况

ping www.baidu.com

CPU 信息

cpu 信息

cat /proc/cpuinfo

查看物理 CPU 的个数

cat /proc/cpuinfo| grep "physical id"| sort| uniq| wc -l

查看逻辑 CPU 的个数

cat /proc/cpuinfo| grep "processor"| wc -l

查看 CPU 的核心数

cat /proc/cpuinfo| grep "cpu cores"| uniq

内存信息

查看物理内存信息

cat /proc/meminfo

查看内存实际使用情况

free -m

硬盘信息

查看磁盘信息

cat /proc/partitions

查看文件系统信息

df -lh

巡检常用命令

CPU 负载情况

top

内存剩余使用率信息

free -m | grep Mem | awk '{print $4/$2*100}'

磁盘读写监控

yum install sysstat

iostat

环境变量监控

env

网络统计信息

ifconfig ens33 | grep 'RX packets' | awk -F '(' '{print $2}' | awk -F ')' '{print $1}'

ifconfig ens33 | grep 'TX packets' | awk -F '(' '{print $2}' | awk -F ')' '{print $1}'

开放端口检查

netstat -tunlp | grep LISTEN

查看当前登录用户信息

w

查看 ssh 登录失败日志

lastb

查看 ssh 登录成功日志

last

系统运行时间

uptime

系统重启日志

last reboot

系统账号检查

cat /etc/passwd 
cat /etc/shadow

查看系统定时任务

cat /var/spool/cron/*

一键脚本

先安装依赖

yum -y install bc sysstat net-tools

脚本

#!/bin/bash
ipaddress=`ip a|grep "global"|awk '{print $2}' |awk -F/ '{print $1}'`
file_output=${ipaddress}'_os_linux_summary.html'
td_str=''
th_str=''
yum -y install bc sysstat net-tools;
sleep 10;
create_html_css(){
  echo -e "<html>
<head>
<style type="text/css">
    body        {font:12px Courier New,Helvetica,sansserif; color:black; background:White;}
    table,tr,td {font:12px Courier New,Helvetica,sansserif; color:Black; background:#FFFFCC; padding:0px 0px 0px 0px; margin:0px 0px 0px 0px;} 
    th          {font:bold 12px Courier New,Helvetica,sansserif; color:White; background:#0033FF; padding:0px 0px 0px 0px;} 
    h1          {font:bold 12pt Courier New,Helvetica,sansserif; color:Black; padding:0px 0px 0px 0px;} 
</style>
</head>
<body>"
}
create_html_head(){
echo -e "<h1>$1</h1>"
}
create_table_head1(){
  echo -e "<table width="68%" border="1" bordercolor="#000000" cellspacing="0px" style="border-collapse:collapse">"
}
create_table_head2(){
  echo -e "<table width="100%" border="1" bordercolor="#000000" cellspacing="0px" style="border-collapse:collapse">"
}
create_td(){
    td_str=`echo $1 | awk 'BEGIN{FS="|"}''{i=1; while(i<=NF) {print "<td>"$i"</td>";i++}}'`
}
create_th(){
    th_str=`echo $1|awk 'BEGIN{FS="|"}''{i=1; while(i<=NF) {print "<th>"$i"</th>";i++}}'`
}
create_tr1(){
  create_td "$1"
  echo -e "<tr>
    $td_str
  </tr>" >> $file_output
}
create_tr2(){
  create_th "$1"
  echo -e "<tr>
    $th_str
  </tr>" >> $file_output
}
create_tr3(){
  echo -e "<tr><td>
  <pre style=\"font-family:Courier New; word-wrap: break-word; white-space: pre-wrap; white-space: -moz-pre-wrap\" >
  `cat $1`
  </pre></td></tr>" >> $file_output
}
create_table_end(){
  echo -e "</table>"
}
create_html_end(){
  echo -e "</body></html>"
}
NAME_VAL_LEN=12
name_val () {
   printf "%+*s | %s\n" "${NAME_VAL_LEN}" "$1" "$2"
}
get_physics(){
    name_val "巡检时间" "`date`"
    name_val "主机名" "`uname -n`"
    name_val "系统版本" "`cat /etc/{oracle,redhat,SuSE,centos}-release 2>/dev/null|sort -ru|head -n1`"
    name_val "内核版本" "`uname -r`"
    name_val "架构" "CPU=`lscpu|grep Architecture|awk -F: '{print $2}'|sed 's/^[[:space:]]*//g'`;OS=`getconf LONG_BIT`-bit"
}
get_cpuinfo () {
   file="/proc/cpuinfo"
   virtual=`grep -c ^processor "${file}"`
   physical=`grep 'physical id' "${file}" | sort -u | wc -l`
   cores=`grep 'cpu cores' "${file}" | head -n 1 | cut -d: -f2`
   model=`grep "model name" "${file}"|sort -u|awk -F: '{print $2}'`
   speed=`grep -i "cpu MHz" "${file}"|sort -u|awk -F: '{print $2}'`
   cache=`grep -i "cache size" "${file}"|sort -u|awk -F: '{print $2}'`
   SysCPUIdle=`vmstat | sed -n '$ p' | awk '{print $15}'`
   [ "${physical}" = "0" ] && physical="${virtual}"
   [ -z "${cores}" ] && cores=0
   cores=$((${cores} * ${physical}));
   htt=""
   if [ ${cores} -gt 0 -a $cores -lt $virtual ]; then htt=yes; else htt=no; fi
   name_val "线程" "physical = ${physical}, cores = ${cores}, virtual = ${virtual}, hyperthreading = ${htt}"
   name_val "cpu型号" "${physical} x ${model}"
   name_val "速度" "${virtual} x ${speed} MHz"
   name_val "缓存" "${virtual} x ${cache}"
   name_val "CPU空闲率(%)" "${SysCPUIdle}%"
}
get_netinfo(){
   echo "interface | status | ipadds     |      mtu    |  Speed     |     Duplex" >>/tmp/tmpnet_h1_`date +%y%m%d`.txt
   for ipstr in `ifconfig -a|grep ": flags"|awk  '{print $1}'|sed 's/.$//'`
   do
      ipadds=`ifconfig ${ipstr}|grep -w inet|awk '{print $2}'`
      mtu=`ifconfig ${ipstr}|grep mtu|awk '{print $NF}'`
      speed=`ethtool ${ipstr}|grep Speed|awk -F: '{print $2}'`
      duplex=`ethtool ${ipstr}|grep Duplex|awk -F: '{print $2}'`
      echo "${ipstr}"  "up" "${ipadds}" "${mtu}" "${speed}" "${duplex}"\
      |awk '{print $1,"|", $2,"|", $3,"|", $4,"|", $5,"|", $6}'  >>/tmp/tmpnet1_`date +%y%m%d`.txt
   done
}
get_cpuuse(){
   echo "#######################################  cpu使用率  #######################################" >>/tmp/tmp_cpuuse_`date +%y%m%d`.txt
   mpstat -P ALL 10 8 >>/tmp/tmp_cpuuse_`date +%y%m%d`.txt
}
get_connections (){
  filemax=`cat /proc/sys/fs/file-max`
  name_val "Number of concurrent connections" "${filemax}"
}
get_ulimitinfo(){
   echo "#######################################  系统限制最大进程数  #######################################" >>/tmp/tmp_ulimitinfo_`date +%y%m%d`.txt
   ulimit -a >>/tmp/tmp_ulimitinfo_`date +%y%m%d`.txt
   echo "#######################################  env  #######################################" >>/tmp/tmp_ulimitinfo_`date +%y%m%d`.txt
   env >>/tmp/tmp_ulimitinfo_`date +%y%m%d`.txt
}
get_meminfo(){
   echo "Locator   |Size     |Speed       |Form Factor  | Type      |    Type Detail" >>/tmp/tmpmem3_h1_`date +%y%m%d`.txt
   dmidecode| grep -v "Memory Device Mapped Address"|grep -A12 -w "Memory Device" \
   |egrep "Locator:|Size:|Speed:|Form Factor:|Type:|Type Detail:" \
   |awk -F: '/Size|Type|Form.Factor|Type.Detail|^[\t ]+Locator/{printf("|%s", $2)}/^[\t ]+Speed/{print "|" $2}' \
   |grep -v "No Module Installed" \
   |awk -F"|" '{print $4,"|", $2,"|", $7,"|", $3,"|", $5,"|", $6}' >>/tmp/tmpmem3_t1_`date +%y%m%d`.txt
   free -glht >>/tmp/tmpmem2_`date +%y%m%d`.txt
   memtotal=`vmstat -s | head -1 | awk '{print $1}'`
   avm=`vmstat -s| sed -n '3p' | awk '{print $1}'`
   name_val "Mem_used_rate(%)" "`echo "100*${avm}/${memtotal}" | bc`%" >>/tmp/tmpmem1_`date +%y%m%d`.txt

}
get_diskinfo(){
   echo "Filesystem        |Type   |Size |  Used  | Avail | Use%  | Mounted on | Opts" >>/tmp/tmpdisk_h1_`date +%y%m%d`.txt
   df -ThP|grep -v tmpfs|sed '1d'|sort >/tmp/tmpdf1_`date +%y%m%d`.txt
   mount -l|awk '{print $1,$6}'|grep ^/|sort >/tmp/tmpdf2_`date +%y%m%d`.txt
   join /tmp/tmpdf1_`date +%y%m%d`.txt /tmp/tmpdf2_`date +%y%m%d`.txt\
   |awk '{print $1,"|", $2,"|", $3,"|", $4,"|", $5,"|", $6,"|", $7,"|", $8}' >>/tmp/tmpdisk_t1_`date +%y%m%d`.txt 
   lsblk >>/tmp/tmpdisk1_`date +%y%m%d`.txt 
   for disk in `ls -l /sys/block|awk '{print $9}'|sed '/^$/d'|grep -v fd`
   do
      echo "${disk}" `cat /sys/block/${disk}/queue/scheduler`  >>/tmp/tmpdisk2_`date +%y%m%d`.txt 
   done
  echo "==========df -h============" >>/tmp/tmpdisk2_`date +%y%m%d`.txt  
  df -h >>/tmp/tmpdisk2_`date +%y%m%d`.txt 
  echo "==========mount============" >>/tmp/tmpdisk2_`date +%y%m%d`.txt  
  mount >>/tmp/tmpdisk2_`date +%y%m%d`.txt 
  echo "==========fdisk -l============" >>/tmp/tmpdisk2_`date +%y%m%d`.txt  
  fdisk -l >>/tmp/tmpdisk2_`date +%y%m%d`.txt 


   pvs >>/tmp/tmpdisk3_`date +%y%m%d`.txt
   echo "======================  =====  =====  =====  =====  =====  ==========  =======" >>/tmp/tmpdisk3_`date +%y%m%d`.txt
   vgs >>/tmp/tmpdisk3_`date +%y%m%d`.txt
   echo "======================  =====  =====  =====  =====  =====  ==========  =======" >>/tmp/tmpdisk3_`date +%y%m%d`.txt
   lvs >>/tmp/tmpdisk3_`date +%y%m%d`.txt
}
   ##xiao changge 
get_topproc(){
   #os load
   echo "#######################################  网络流量情况  #######################################" >>/tmp/tmpload_`date +%y%m%d`.txt
   sar -n DEV 10 6 >>/tmp/tmpload_`date +%y%m%d`.txt
   echo "#######################################  ip add  #######################################" >>/tmp/tmpload_`date +%y%m%d`.txt
   ip add >>/tmp/tmpload_`date +%y%m%d`.txt
   echo "#######################################  ss -t -a  #######################################" >>/tmp/tmpload_`date +%y%m%d`.txt
   ss -t -a  >>/tmp/tmpload_`date +%y%m%d`.txt
   echo "#######################################  netstat -lntp  #######################################" >>/tmp/tmpload_`date +%y%m%d`.txt
   netstat -lntp  >>/tmp/tmpload_`date +%y%m%d`.txt
   echo "#######################################  netstat -anp  #######################################" >>/tmp/tmpload_`date +%y%m%d`.txt
   netstat -anp  >>/tmp/tmpload_`date +%y%m%d`.txt
   echo "#######################################  netstat -rn  #######################################" >>/tmp/tmpload_`date +%y%m%d`.txt
   netstat -rn  >>/tmp/tmpload_`date +%y%m%d`.txt


   echo "#######################################  系统资源变化  #######################################" >>/tmp/tmptop_`date +%y%m%d`.txt
   vmstat -S M 10 6  >>/tmp/tmptop_`date +%y%m%d`.txt
   mpstat 1 5 >>/tmp/tmptop_`date +%y%m%d`.txt
   echo "#######################################  消耗CPU前十排行  #######################################" >>/tmp/tmptop_`date +%y%m%d`.txt
   ps aux|head -1 >>/tmp/tmptop_`date +%y%m%d`.txt
   ps aux|grep -v PID|sort -rn -k +3|head  >>/tmp/tmptop_`date +%y%m%d`.txt
   sleep 2;
   echo "#######################################  消耗内存前十排行  #######################################" >>/tmp/tmptop_`date +%y%m%d`.txt
   ps aux|head -1  >>/tmp/tmptop_`date +%y%m%d`.txt
   ps aux|grep -v PID|sort -rn -k +4|head  >>/tmp/tmptop_`date +%y%m%d`.txt
   sleep 2;
   echo "#######################################  TOP资源监控  #######################################" >>/tmp/tmptop_`date +%y%m%d`.txt
   top -bn1 -o "%CPU"|sed  -n '1,80p' >>/tmp/tmptop_`date +%y%m%d`.txt
   sleep 2;
  echo "#######################################  显示进程信息ps -ef  #######################################" >>/tmp/tmptop_`date +%y%m%d`.txt
   ps -ef >>/tmp/tmptop_`date +%y%m%d`.txt
   sleep 2;   

   #top i/o
   echo "#######################################  磁盘io情况  #######################################" >>/tmp/tmptopio_`date +%y%m%d`.txt
   iostat -k -d 10 5  >>/tmp/tmptopio_`date +%y%m%d`.txt
}
get_crontablist(){
   crontab -l >>/tmp/tmp_crontab_`date +%y%m%d`.txt
  if [ -s /tmp/tmp_crontab_`date +%y%m%d`.txt ] ; then 
    echo 'ths file is not empyt and file info'
  else
    echo '#### 无定时任务 ####' >>/tmp/tmp_crontab_`date +%y%m%d`.txt
  fi
}
get_crontab_content(){
   crontab_content_log=/tmp/tmp_crontab_content_`date +%y%m%d`.txt
   crontab -l|awk -F ' ' '{ print $NF}' >>$crontab_content_log


   contrab_num=`crontab -l|awk -F ' ' '{ print $NF}'|wc -l`
   if [ $contrab_num -ne 0 ];then
   count=1
   while [ $count -le $contrab_num ]
     do
       echo "#######################################  获取系统定时任务脚本 $count 内容开始  #######################################" >>/tmp/tmp_crontab_shellcontent_`date +%y%m%d`.txt
       cat `sed -n -e "${count}p" $crontab_content_log` >>/tmp/tmp_crontab_shellcontent_`date +%y%m%d`.txt
       echo -e "\n#######################################  获取系统定时任务脚本 $count 内容结束  #######################################\n" >>/tmp/tmp_crontab_shellcontent_`date +%y%m%d`.txt
     count=$[${count}+1]
     done
   else
     echo '#### 无定时执行脚本 ####' >>/tmp/tmp_crontab_shellcontent_`date +%y%m%d`.txt
   fi
}
create_html(){
  rm -rf $file_output
  touch $file_output
  create_html_css >> $file_output

  create_html_head "系统基本信息" >> $file_output
  create_table_head1 >> $file_output
  get_physics >>/tmp/tmpos_summ_`date +%y%m%d`.txt
  while read line
  do
    create_tr1 "$line" 
  done < /tmp/tmpos_summ_`date +%y%m%d`.txt
  create_table_end >> $file_output

  create_html_head "cpu信息" >> $file_output
  create_table_head1 >> $file_output
  get_cpuinfo >>/tmp/tmp_cpuinfo_`date +%y%m%d`.txt
  while read line
  do
    create_tr1 "$line" 
  done < /tmp/tmp_cpuinfo_`date +%y%m%d`.txt  
  create_table_end >> $file_output


  create_html_head "ip网络信息" >> $file_output
  create_table_head1 >> $file_output
  get_netinfo
  while read line
  do
    create_tr2 "$line" 
  done < /tmp/tmpnet_h1_`date +%y%m%d`.txt
  while read line
  do
    create_tr1 "$line" 
  done < /tmp/tmpnet1_`date +%y%m%d`.txt
  create_table_end >> $file_output


  create_html_head "cpu使用率" >> $file_output
  create_table_head1 >> $file_output
  get_cpuuse
  create_tr3 "/tmp/tmp_cpuuse_`date +%y%m%d`.txt"
  create_table_end >> $file_output


  create_html_head "连接数信息" >> $file_output
  create_table_head1 >> $file_output
  get_connections >>/tmp/tmp_connections_`date +%y%m%d`.txt
  while read line
  do
    create_tr1 "$line" 
  done < /tmp/tmp_connections_`date +%y%m%d`.txt  
  create_table_end >> $file_output


  create_html_head "系统限制信息" >> $file_output
  create_table_head1 >> $file_output
  get_ulimitinfo
  create_tr3 "/tmp/tmp_ulimitinfo_`date +%y%m%d`.txt"
  create_table_end >> $file_output  


  create_html_head "内存使用信息" >> $file_output
  create_table_head1 >> $file_output
  get_meminfo
  while read line
  do
    create_tr1 "$line" 
  done < /tmp/tmpmem1_`date +%y%m%d`.txt
  create_table_end >> $file_output

  create_table_head1 >> $file_output
  create_tr3 "/tmp/tmpmem2_`date +%y%m%d`.txt"
  create_table_end >> $file_output

  create_table_head1 >> $file_output
  while read line
  do
    create_tr2 "$line" 
  done < /tmp/tmpmem3_h1_`date +%y%m%d`.txt

  while read line
  do
    create_tr1 "$line" 
  done < /tmp/tmpmem3_t1_`date +%y%m%d`.txt
  create_table_end >> $file_output

  create_html_head "磁盘使用信息" >> $file_output
  create_table_head1 >> $file_output
  get_diskinfo
  while read line
  do
    create_tr2 "$line" 
  done < /tmp/tmpdisk_h1_`date +%y%m%d`.txt
  while read line
  do
    create_tr1 "$line" 
  done < /tmp/tmpdisk_t1_`date +%y%m%d`.txt
  create_table_end >> $file_output

  create_table_head1 >> $file_output
  create_tr3 "/tmp/tmpdisk1_`date +%y%m%d`.txt"
  create_table_end >> $file_output

  create_table_head1 >> $file_output
  create_tr3 "/tmp/tmpdisk2_`date +%y%m%d`.txt"
  create_table_end >> $file_output

  create_table_head1 >> $file_output
  create_tr3 "/tmp/tmpdisk3_`date +%y%m%d`.txt"
  create_table_end >> $file_output


  create_html_head "网络流量情况" >> $file_output
  create_table_head1 >> $file_output
  get_topproc
  create_tr3 "/tmp/tmpload_`date +%y%m%d`.txt"
  create_table_end >> $file_output

  create_html_head "性能相关监控" >> $file_output
  create_table_head1 >> $file_output
  sed -i 's/</----/g' "/tmp/tmptop_`date +%y%m%d`.txt"  >>/tmp/tmptop_`date +%y%m%d`.txt
  create_tr3 "/tmp/tmptop_`date +%y%m%d`.txt"
  create_table_end >> $file_output

  create_html_head "磁盘io情况" >> $file_output
  create_table_head1 >> $file_output
  create_tr3 "/tmp/tmptopio_`date +%y%m%d`.txt"
  create_table_end >> $file_output


  create_html_head "定时任务信息" >> $file_output
  create_table_head1 >> $file_output
  get_crontablist
  create_tr3 "/tmp/tmp_crontab_`date +%y%m%d`.txt"
  create_table_end >> $file_output


  create_html_head "定时任务脚本内容" >> $file_output
  create_table_head1 >> $file_output
  get_crontab_content
  create_tr3 "/tmp/tmp_crontab_shellcontent_`date +%y%m%d`.txt"
  create_table_end >> $file_output


  create_html_end >> $file_output
  sed -i 's/BORDER=1/width="68%" border="1" bordercolor="#000000" cellspacing="0px" style="border-collapse:collapse"/g' $file_output
  rm -rf /tmp/tmp*_`date +%y%m%d`.txt
}
# This script must be executed as root
RUID=`id|awk -F\( '{print $1}'|awk -F\= '{print $2}'`
if [ ${RUID} != "0" ];then
    echo"This script must be executed as root"
    exit 1
fi
PLATFORM=`uname`
if [ ${PLATFORM} = "HP-UX" ] ; then
    echo "This script does not support HP-UX platform for the time being"
exit 1
elif [ ${PLATFORM} = "SunOS" ] ; then
    echo "This script does not support SunOS platform for the time being"
exit 1
elif [ ${PLATFORM} = "AIX" ] ; then
    echo "This script does not support AIX platform for the time being"
exit 1
elif [ ${PLATFORM} = "Linux" ] ; then
  create_html
fi

参考链接:

https://mp.weixin.qq.com/s/w1qQ0DHDlVmhRN8ceGS5Kg
https://blog.csdn.net/helloworld104/article/details/106819948/

本文作者:R0A1NG

原文链接:https://www.roaing.com/linux_ins.html


啦啦啦!