当前位置:   article > 正文

linux 系统监控脚本

linux 系统监控脚本

1.对CPU的监控函数

  1. function GetCpu(){
  2. cpu_num=`grep -c "model name" /proc/cpuinfo`
  3. cpu_user=`top -b -n 1 | grep Cpu | awk '{print $2}' | cut -f 1 -d "%"`
  4. cpu_system=`top -b -n 1 | grep Cpu | awk '{print $4}' | cut -f 1 -d "%"`
  5. cpu_idle=`top -b -n 1 | grep Cpu | awk '{print $8}' | cut -f 1 -d "%"`
  6. cpu_iowait=`top -b -n 1 | grep Cpu | awk '{print $10}' | cut -f 1 -d "%"`
  7. cpu_interrupt=`vmstat -n 1 1 | sed -n 3p | awk '{print $11}'`
  8. cpu_context_switch=`vmstat -n 1 1 | sed -n 3p | awk '{print $12}'`
  9. cpu_load_15min=`uptime | awk '{print $12}' | cut -f 1 -d ','`
  10. cpu_load_5min=`uptime | awk '{print $11}' | cut -f 1 -d ','`
  11. cpu_load_1min=`uptime | awk '{print $10}' | cut -f 1 -d ','`
  12. color "cpu总核数:" "$cpu_num"
  13. color "用户空间占用CPU百分比:" "$cpu_user"
  14. color "内核空间占用CPU百分比:" "$cpu_system"
  15. color "空闲CPU百分比:" "$cpu_idle"
  16. color "等待输入输出占CPU百分比:" "$cpu_iowait"
  17. color "CPU中断次数:" "$cpu_interrupt"
  18. color "CPU上下文切换次数:" "$cpu_context_switch"
  19. color "CPU 15分钟前到现在的负载平均值:" "$cpu_load_15min"
  20. color "CPU 5分钟前到现在的负载平均值:" "$cpu_load_5min"
  21. color "CPU 1分钟前到现在的负载平均值:" "$cpu_load_1min"
  22. }

2.对内存的监控函数

  1. function GetMem(){
  2. mem_total=`free | grep Mem | awk '{print $2}'`
  3. mem_sys_used=`free | grep Mem | awk '{print $3}'`
  4. mem_sys_free=`free | grep Mem | awk '{print $4}'`
  5. mem_user_used=`free | sed -n 3p | awk '{print $3}'`
  6. mem_user_free=`free | sed -n 3p | awk '{print $4}'`
  7. mem_swap_total=`free | grep Swap | awk '{print $2}'`
  8. mem_swap_used=`free | grep Swap | awk '{print $3}'`
  9. mem_swap_free=`free | grep Swap | awk '{print $4}'`
  10. color "物理内存总量:" "$mem_total"
  11. color "已使用内存总量(操作系统):" "$mem_sys_used"
  12. color "剩余内存总量(操作系统):" "$mem_sys_free"
  13. color "已使用内存总量(应用程序):" "$mem_user_used"
  14. color "剩余内存总量(应用程序):" "$mem_user_free"
  15. color "交换分区总大小:" "$mem_swap_total"
  16. color "已使用交换分区大小:" "$mem_swap_used"
  17. color "剩余交换分区大小:" "$mem_swap_free"
  18. }

3.对磁盘IO的监控函数

  1. function GetDiskIo(){
  2. disk=sda
  3. read -p "请输入指定磁盘:(eg:sda)" input_disk
  4. if [ -z "$input_disk" ]; then
  5. echo "用户未输入,使用默认值 $disk"
  6. input_disk="$disk" # 由于未赋值,这里相当于保持默认
  7. else
  8. echo "用户输入的disk路径: $input_disk"
  9. fi
  10. echo "指定设备($input_disk)的统计信息"
  11. disk_sda_rs=`iostat -kx | grep $input_disk| awk '{print $4}'`
  12. disk_sda_ws=`iostat -kx | grep $input_disk| awk '{print $5}'`
  13. disk_sda_avgqu_sz=`iostat -kx | grep $input_disk| awk '{print $9}'`
  14. disk_sda_await=`iostat -kx | grep $input_disk| awk '{print $10}'`
  15. disk_sda_svctm=`iostat -kx | grep $input_disk| awk '{print $11}'`
  16. disk_sda_util=`iostat -kx | grep $input_disk| awk '{print $12}'`
  17. color "每秒向设备发起的读请求次数:" "$disk_sda_rs"
  18. color "每秒向设备发起的写请求次数:" "$disk_sda_ws"
  19. color "向设备发起的I/O请求队列长度平均值:" "$disk_sda_avgqu_sz"
  20. color "每次向设备发起的I/O请求平均时间:" "$disk_sda_await"
  21. color "向设备发起的I/O服务时间均值:" "$disk_sda_svctm"
  22. color "向设备发起I/O请求的CPU时间百分占比:" "$disk_sda_util"
  23. }

4.对线程状态的监控函数

  1. function GetPidstat(){
  2. #初始化变量
  3. Ptotal=0
  4. Rtotal=0
  5. Stotal=0
  6. Ttotal=0
  7. Dtotal=0
  8. Ztotal=0
  9. for pid in /proc/[1-9]*
  10. do
  11. #获取进程状态
  12. let Ptotal=Ptotal+1
  13. stat=$(cd $pid && cat stat| awk '{print $3}')
  14. case $stat in
  15. R)
  16. let Rtotal=Rtotal+1
  17. ;;
  18. S)
  19. let Stotal=Stotal+1
  20. ;;
  21. T)
  22. let Ttotal=Ttotal+1
  23. ;;
  24. D)
  25. let Dtotal=Dtotal+1
  26. ;;
  27. Z)
  28. let Ztotal=Ztotal+1
  29. ;;
  30. esac
  31. done
  32. color "当前进程总数为:" "$Ptotal"
  33. color "其中Running进程数为:" "$Rtotal"
  34. color "其中Sleeping进程数为:" "$Stotal"
  35. color "其中Stopped进程数为:" "$Ttotal"
  36. color "其中Disk sleep进程数为:" "$Dtotal"
  37. color "其中Zombies进程数为:" "$Ztotal"
  38. }

5.对网卡流量的监控函数

  1. function GetNet(){
  2. eth_name=`sar -n DEV 1 1|grep ens|awk 'NR==2{print $2}'`
  3. rxpck_num=`sar -n DEV 1 1|grep ens|awk 'NR==2{print $3}'`
  4. txpck_num=`sar -n DEV 1 1|grep ens|awk 'NR==2{print $4}'`
  5. rxkB_num=`sar -n DEV 1 1|grep ens|awk 'NR==2{print $5}'`
  6. txkB_num=`sar -n DEV 1 1|grep ens|awk 'NR==2{print $6}'`
  7. rxcmp_num=`sar -n DEV 1 1|grep ens|awk 'NR==2{print $7}'`
  8. txcmp_num=`sar -n DEV 1 1|grep ens|awk 'NR==2{print $8}'`
  9. rxmcst_num=`sar -n DEV 1 1|grep ens|awk 'NR==2{print $9}'`
  10. color "$eth_name每秒钟接收的数据包:" "$rxpck_num"
  11. color "$eth_name每秒钟发送的数据包:" "$txpck_num"
  12. color "$eth_name每秒钟接收的字节数:" "$rxkB_num"
  13. color "$eth_name每秒钟发送的字节数:" "$txkB_num"
  14. color "$eth_name每秒钟接收的压缩数据包:" "$rxcmp_num"
  15. color "$eth_name每秒钟发送的压缩数据包:" "$txcmp_num"
  16. color "$eth_name每秒钟接收的多播数据包:" "$rxmcst_num"
  17. }

6.实现效果

  1. #!/bin/bash
  2. function color() {
  3. local a=$1
  4. local b=$2
  5. echo -e "\033[1;32m$a\033[0m \033[1;31m$b\033[0m"
  6. }
  7. function GetIp(){
  8. IP=`ifconfig | grep inet | grep -vE 'inet6|127.0.0.1' | awk '{print $2}'`
  9. echo "IP地址:"$IP
  10. }
  11. #检查CPU利用率
  12. function GetCpu(){
  13. cpu_num=`grep -c "model name" /proc/cpuinfo`
  14. cpu_user=`top -b -n 1 | grep Cpu | awk '{print $2}' | cut -f 1 -d "%"`
  15. cpu_system=`top -b -n 1 | grep Cpu | awk '{print $4}' | cut -f 1 -d "%"`
  16. cpu_idle=`top -b -n 1 | grep Cpu | awk '{print $8}' | cut -f 1 -d "%"`
  17. cpu_iowait=`top -b -n 1 | grep Cpu | awk '{print $10}' | cut -f 1 -d "%"`
  18. cpu_interrupt=`vmstat -n 1 1 | sed -n 3p | awk '{print $11}'`
  19. cpu_context_switch=`vmstat -n 1 1 | sed -n 3p | awk '{print $12}'`
  20. cpu_load_15min=`uptime | awk '{print $12}' | cut -f 1 -d ','`
  21. cpu_load_5min=`uptime | awk '{print $11}' | cut -f 1 -d ','`
  22. cpu_load_1min=`uptime | awk '{print $10}' | cut -f 1 -d ','`
  23. color "cpu总核数:" "$cpu_num"
  24. color "用户空间占用CPU百分比:" "$cpu_user"
  25. color "内核空间占用CPU百分比:" "$cpu_system"
  26. color "空闲CPU百分比:" "$cpu_idle"
  27. color "等待输入输出占CPU百分比:" "$cpu_iowait"
  28. color "CPU中断次数:" "$cpu_interrupt"
  29. color "CPU上下文切换次数:" "$cpu_context_switch"
  30. color "CPU 15分钟前到现在的负载平均值:" "$cpu_load_15min"
  31. color "CPU 5分钟前到现在的负载平均值:" "$cpu_load_5min"
  32. color "CPU 1分钟前到现在的负载平均值:" "$cpu_load_1min"
  33. }
  34. #检查内存使用率
  35. function GetMem(){
  36. mem_total=`free | grep Mem | awk '{print $2}'`
  37. mem_sys_used=`free | grep Mem | awk '{print $3}'`
  38. mem_sys_free=`free | grep Mem | awk '{print $4}'`
  39. mem_user_used=`free | sed -n 3p | awk '{print $3}'`
  40. mem_user_free=`free | sed -n 3p | awk '{print $4}'`
  41. mem_swap_total=`free | grep Swap | awk '{print $2}'`
  42. mem_swap_used=`free | grep Swap | awk '{print $3}'`
  43. mem_swap_free=`free | grep Swap | awk '{print $4}'`
  44. color "物理内存总量:" "$mem_total"
  45. color "已使用内存总量(操作系统):" "$mem_sys_used"
  46. color "剩余内存总量(操作系统):" "$mem_sys_free"
  47. color "已使用内存总量(应用程序):" "$mem_user_used"
  48. color "剩余内存总量(应用程序):" "$mem_user_free"
  49. color "交换分区总大小:" "$mem_swap_total"
  50. color "已使用交换分区大小:" "$mem_swap_used"
  51. color "剩余交换分区大小:" "$mem_swap_free"
  52. }
  53. #检查磁盘IO状态
  54. function GetDiskIo(){
  55. disk=sda
  56. read -p "请输入指定磁盘:(eg:sda)" input_disk
  57. if [ -z "$input_disk" ]; then
  58. echo "用户未输入,使用默认值 $disk"
  59. input_disk="$disk" # 由于未赋值,这里相当于保持默认
  60. else
  61. echo "用户输入的disk路径: $input_disk"
  62. fi
  63. echo "指定设备($input_disk)的统计信息"
  64. disk_sda_rs=`iostat -kx | grep $input_disk| awk '{print $4}'`
  65. disk_sda_ws=`iostat -kx | grep $input_disk| awk '{print $5}'`
  66. disk_sda_avgqu_sz=`iostat -kx | grep $input_disk| awk '{print $9}'`
  67. disk_sda_await=`iostat -kx | grep $input_disk| awk '{print $10}'`
  68. disk_sda_svctm=`iostat -kx | grep $input_disk| awk '{print $11}'`
  69. disk_sda_util=`iostat -kx | grep $input_disk| awk '{print $12}'`
  70. color "每秒向设备发起的读请求次数:" "$disk_sda_rs"
  71. color "每秒向设备发起的写请求次数:" "$disk_sda_ws"
  72. color "向设备发起的I/O请求队列长度平均值:" "$disk_sda_avgqu_sz"
  73. color "每次向设备发起的I/O请求平均时间:" "$disk_sda_await"
  74. color "向设备发起的I/O服务时间均值:" "$disk_sda_svctm"
  75. color "向设备发起I/O请求的CPU时间百分占比:" "$disk_sda_util"
  76. }
  77. function GetPidstat(){
  78. #初始化变量
  79. Ptotal=0
  80. Rtotal=0
  81. Stotal=0
  82. Ttotal=0
  83. Dtotal=0
  84. Ztotal=0
  85. for pid in /proc/[1-9]*
  86. do
  87. #获取进程状态
  88. let Ptotal=Ptotal+1
  89. stat=$(cd $pid && cat stat| awk '{print $3}')
  90. case $stat in
  91. R)
  92. let Rtotal=Rtotal+1
  93. ;;
  94. S)
  95. let Stotal=Stotal+1
  96. ;;
  97. T)
  98. let Ttotal=Ttotal+1
  99. ;;
  100. D)
  101. let Dtotal=Dtotal+1
  102. ;;
  103. Z)
  104. let Ztotal=Ztotal+1
  105. ;;
  106. esac
  107. done
  108. color "当前进程总数为:" "$Ptotal"
  109. color "其中Running进程数为:" "$Rtotal"
  110. color "其中Sleeping进程数为:" "$Stotal"
  111. color "其中Stopped进程数为:" "$Ttotal"
  112. color "其中Disk sleep进程数为:" "$Dtotal"
  113. color "其中Zombies进程数为:" "$Ztotal"
  114. }
  115. function GetNet(){
  116. eth_name=`sar -n DEV 1 1|grep ens|awk 'NR==2{print $2}'`
  117. rxpck_num=`sar -n DEV 1 1|grep ens|awk 'NR==2{print $3}'`
  118. txpck_num=`sar -n DEV 1 1|grep ens|awk 'NR==2{print $4}'`
  119. rxkB_num=`sar -n DEV 1 1|grep ens|awk 'NR==2{print $5}'`
  120. txkB_num=`sar -n DEV 1 1|grep ens|awk 'NR==2{print $6}'`
  121. rxcmp_num=`sar -n DEV 1 1|grep ens|awk 'NR==2{print $7}'`
  122. txcmp_num=`sar -n DEV 1 1|grep ens|awk 'NR==2{print $8}'`
  123. rxmcst_num=`sar -n DEV 1 1|grep ens|awk 'NR==2{print $9}'`
  124. color "$eth_name每秒钟接收的数据包:" "$rxpck_num"
  125. color "$eth_name每秒钟发送的数据包:" "$txpck_num"
  126. color "$eth_name每秒钟接收的字节数:" "$rxkB_num"
  127. color "$eth_name每秒钟发送的字节数:" "$txkB_num"
  128. color "$eth_name每秒钟接收的压缩数据包:" "$rxcmp_num"
  129. color "$eth_name每秒钟发送的压缩数据包:" "$txcmp_num"
  130. color "$eth_name每秒钟接收的多播数据包:" "$rxmcst_num"
  131. }
  132. echo "---------------------开始检查CPU----------------------"
  133. GetCpu
  134. echo "---------------------开始检查内存---------------------"
  135. GetMem
  136. echo "---------------------开始检查磁盘IO-------------------"
  137. GetDiskIo
  138. echo "---------------------开始检查进程状态-----------------"
  139. GetPidstat
  140. echo "---------------------开始检查网卡流量-----------------"
  141. GetNet

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/680821
推荐阅读
相关标签
  

闽ICP备14008679号