Linux 系統健康巡檢指令碼

路途中的人2012發表於2016-12-28
客戶應用系統伺服器要做健康檢查,作業系統為Red Hat Enterprise Linux 5.6,資料庫為oracle 10g,寫了如下shell指令碼:
  1. #!/bin/bash
  2. echo "You are logged in as `whoami`";
  3. if [ `whoami` != root ]; then
  4. echo "Must be logged on as root to run this script."
  5. exit
  6. fi
  7. CHECK_DATE=`date +%F`
  8. echo "Running script. at `date`"
  9. CHECK_REPORT_PATH=/tmp/getinfo
  10. ls -d $CHECK_REPORT_PATH
  11. if [ $? -gt 0 ]
  12. then
  13. mkdir $CHECK_REPORT_PATH
  14. fi
  15. chmod -R 777 $CHECK_REPORT_PATH
  16. CURRENT_DIR=`pwd`
  17. echo "#################### Check Start ####################"
  18. HOST=`hostname`
  19. echo "********Hostname Check**********" >$CHECK_REPORT_PATH/Report_$CHECK_DATE
  20. echo "Hostname is $HOSTNAME" >>$CHECK_REPORT_PATH/Report_$CHECK_DATE
  21. echo -e "----------------------------------------------------\n">>$CHECK_REPORT_PATH/Report_$CHECK_DATE
  22. #系統版本檢查
  23. echo "********OS Level**********" >>$CHECK_REPORT_PATH/Report_$CHECK_DATE
  24. uname -a >>$CHECK_REPORT_PATH/Report_$CHECK_DATE
  25. echo -e "----------------------------------------------------\n">>$CHECK_REPORT_PATH/Report_$CHECK_DATE
  26. #系統日誌檢查
  27. echo "********System log*******" >>$CHECK_REPORT_PATH/Report_$CHECK_DATE
  28. cat /var/log/messages >>$CHECK_REPORT_PATH/Report_$CHECK_DATE
  29. echo -e "----------------------------------------------------\n">>$CHECK_REPORT_PATH/Report_$CHECK_DATE
  30. #檔案系統檢查
  31. echo "********File System**********" >>$CHECK_REPORT_PATH/Report_$CHECK_DATE
  32. df -h >>$CHECK_REPORT_PATH/Report_$CHECK_DATE
  33. echo "over 75%" >>$CHECK_REPORT_PATH/Report_$CHECK_DATE
  34. echo -e "----------------------------------------------------\n">>$CHECK_REPORT_PATH/Report_$CHECK_DATE
  35. df -h |grep '[7-9][5-9]%'>>$CHECK_REPORT_PATH/Report_$CHECK_DATE
  36. #網路地址配置資訊
  37. echo "********System /etc/hosts********" >>$CHECK_REPORT_PATH/Report_$CHECK_DATE
  38. cat /etc/hosts >>$CHECK_REPORT_PATH/Report_$CHECK_DATE
  39. echo -e "----------------------------------------------------\n">>$CHECK_REPORT_PATH/Report_$CHECK_DATE
  40. #檢視記憶體使用情況
  41. echo "**********mem check************" >>$CHECK_REPORT_PATH/Report_$CHECK_DATE
  42. free >>$CHECK_REPORT_PATH/Report_$CHECK_DATE
  43. echo -e "----------------------------------------------------\n">>$CHECK_REPORT_PATH/Report_$CHECK_DATE
  44. vmstat >>$CHECK_REPORT_PATH/Report_$CHECK_DATE
  45. echo -e "----------------------------------------------------\n">>$CHECK_REPORT_PATH/Report_$CHECK_DATE
  46. #檢視磁碟讀寫情況
  47. echo "********io check**********" >>$CHECK_REPORT_PATH/Report_$CHECK_DATE
  48. iostat >>$CHECK_REPORT_PATH/Report_$CHECK_DATE
  49. echo -e "----------------------------------------------------\n">>$CHECK_REPORT_PATH/Report_$CHECK_DATE
  50. #網路檢查
  51. echo "********network route check**********" >>$CHECK_REPORT_PATH/Report_$CHECK_DATE
  52. netstat -rn >>$CHECK_REPORT_PATH/Report_$CHECK_DATE
  53. echo "********network check**********" >>$CHECK_REPORT_PATH/Report_$CHECK_DATE
  54. ifconfig -a >>$CHECK_REPORT_PATH/Report_$CHECK_DATE
  55. echo -e "----------------------------------------------------\n">>$CHECK_REPORT_PATH/Report_$CHECK_DATE
  56. #環境變數檢查
  57. echo "********env info****************" >>$CHECK_REPORT_PATH/Report_$CHECK_DATE
  58. env >>$CHECK_REPORT_PATH/Report_$CHECK_DATE
  59. echo -e "----------------------------------------------------\n">>$CHECK_REPORT_PATH/Report_$CHECK_DATE
  60. #使用者組檢查
  61. echo "********group info****************" >>$CHECK_REPORT_PATH/Report_$CHECK_DATE
  62. cat /etc/group >>$CHECK_REPORT_PATH/Report_$CHECK_DATE
  63. echo -e "----------------------------------------------------\n">>$CHECK_REPORT_PATH/Report_$CHECK_DATE
  64. #使用者密碼檢查
  65. echo "********user info****************" >>$CHECK_REPORT_PATH/Report_$CHECK_DATE
  66. cat /etc/passwd >>$CHECK_REPORT_PATH/Report_$CHECK_DATE
  67. echo -e "----------------------------------------------------\n">>$CHECK_REPORT_PATH/Report_$CHECK_DATE
  68. #檢查作業系統kernel資訊
  69. echo "********System kernel info****************" >>$CHECK_REPORT_PATH/Report_$CHECK_DATE
  70. su - root -c $CURRENT_DIR/check_kernel.sh >>$CHECK_REPORT_PATH/Report_$CHECK_DATE
  71. echo -e "----------------------------------------------------\n">>$CHECK_REPORT_PATH/Report_$CHECK_DATE
  72. #檢查作業系統定時任務
  73. echo "********System crontab info****************" >>$CHECK_REPORT_PATH/Report_$CHECK_DATE
  74. crontab -l >>$CHECK_REPORT_PATH/Report_$CHECK_DATE
  75. echo -e "----------------------------------------------------\n">>$CHECK_REPORT_PATH/Report_$CHECK_DATE
  76. #檢查系統裸裝置屬主
  77. echo "******** RAW****************" >>$CHECK_REPORT_PATH/Report_$CHECK_DATE
  78. ls -l /dev/sd* >>$CHECK_REPORT_PATH/Report_$CHECK_DATE
  79. echo -e "----------------------------------------------------\n">>$CHECK_REPORT_PATH/Report_$CHECK_DATE
  80. #檢查伺服器上執行的資料庫資訊
  81. echo "********get oracle info****************" >>$CHECK_REPORT_PATH/Report_$CHECK_DATE
  82. Instance=`ps -ef|grep -v grep|grep oracle|awk '{print $1}'`
  83. for i in ${Instance}
  84. do
  85. su - $i -c $CHECK_REPORT_PATH/getdbinfo.sh
  86. done
  87. echo -e "----------------------------------------------------\n">>$CHECK_REPORT_PATH/Report_$CHECK_DATE
  88. echo "#################### Check End! ####################"

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29067253/viewspace-2131572/,如需轉載,請註明出處,否則將追究法律責任。

相關文章