1. #!bin/bash
    2. #Nailing alarm: disk, memory, CPU, web status code monitoring alarm - WJ (Version 1.1)
    3. # Debian/Ubuntu Linux systems. (May 10, 2019)
    4. # (GNU/General Public License version 2.0)
    5. # For Centos 7 and up, Linux lzone_zkong_cluster_01 3.10.0-862.el7.x86_64.
    6. # ...And away we go!
    7. set -ue
    8. Container_PATH="/var/lib/docker/containers/"
    9. Time=`date '+%F %T'`
    10. url="http://127.0.0.1:9999/user/getNewPublicKey?loginType=1"
    11. Hostname=`hostname`
    12. webhook="https://oapi.dingtalk.com/robot/send?access_token=4d15f6354cdddf9498f90131084c72e32e1bc3e028df8e8ae11201392b672840"
    13. Host_IP=`/usr/sbin/ip a | grep inet | grep -v inet6 | grep -v '127.0.0.1' | awk '{print $2}' | awk -F / '{print$1}'`
    14. Check_Network(){
    15. massage=`ping -c 3 www.baidu.com 2>/dev/null`
    16. if [ $? = 0 ]; then
    17. echo "Smooth network"
    18. else
    19. echo "$message"
    20. exit 1
    21. fi
    22. }
    23. logError()
    24. {
    25. echo -n '[Error]: ' $*
    26. echo -en '\033[120G \033[31m' && echo [ Error ]
    27. echo -en '\033[0m'
    28. }
    29. function Item_Mem(){
    30. Mem_Total=`free -m | awk -F '[ :]+' 'NR==2{print $2}'`
    31. Mem_Used=`free -m | awk -F '[ :]+' 'NR==2{print $3}'`
    32. Monitoring_Indicators=`awk 'BEGIN{printf "%.0f\n",('$Mem_Used'/'$Mem_Total')*100}'`
    33. Alarm_Subject="内存使用率告警"
    34. if [[ "$Monitoring_Indicators" > 80 ]];then
    35. SendMsgToDingding
    36. if [ $? != 0 ];then
    37. logError
    38. exit -1
    39. fi
    40. fi
    41. }
    42. Item_Disk () {
    43. Alarm_Subject="磁盘使用率告警"
    44. Monitoring_Indicators=`df | egrep -n '/dev/vda1|/dev/sda3' | awk '{print $5}' | sed 's/[^0-9\.]//g'`
    45. if [[ "$Monitoring_Indicators" > 85 ]]
    46. then
    47. SendMsgToDingding
    48. if [ $? != 0 ];then
    49. logError
    50. exit -1
    51. fi
    52. else
    53. echo "disk ok!!!" >/dev/null 2>&1
    54. fi
    55. }
    56. Item_Cpu () {
    57. Alarm_Subject="CPU使用率告警"
    58. cpu_us=`vmstat | awk '{print $13}' | sed -n '$p'`
    59. cpu_sy=`vmstat | awk '{print $14}' | sed -n '$p'`
    60. cpu_id=`vmstat | awk '{print $15}' | sed -n '$p'`
    61. Monitoring_Indicators=$(($cpu_us+$cpu_sy))
    62. if(($Monitoring_Indicators > 80))
    63. then
    64. SendMsgToDingding
    65. if [ $? != 0 ];then
    66. logError
    67. exit -1
    68. fi
    69. fi
    70. }
    71. function SendMsgToDingding() {
    72. Pname=`cat /etc/profile|grep Name|awk -F "=" '{print $2}'`
    73. curl $webhook -H 'Content-Type: application/json' -d "
    74. {
    75. 'msgtype': 'text',
    76. 'text': {
    77. 'content': '告警主题:$Pname-$Alarm_Subject \n钉钉关键字: 1 \n告警时间: $Time \n告警主机名: $Hostname \n主机IP: $Host_IP \n当前监控指标: $Monitoring_Indicators \n'
    78. },
    79. 'at': {
    80. 'isAtAll': false
    81. }
    82. }"
    83. }
    84. Running() {
    85. Check_Network
    86. Item_Mem
    87. Item_Disk
    88. Item_Cpu
    89. }
    90. Running