Determined Server Setup
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sysmon.sh 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #!/bin/bash
  2. # Josh's Automatic System Monitor
  3. # Written by Josh Mudge
  4. # Ad Mejorem Dei Glorium
  5. update=1
  6. version=v1.5.2a
  7. alpha=0
  8. dfh=$(df -h | grep '8[0-9]%')
  9. dfh2=$(df -h | grep '9[0-9]%')
  10. while [[ $# -gt 0 ]]
  11. do
  12. key="$1"
  13. case $key in
  14. --setup)
  15. shift # past argument
  16. setup=1
  17. ;;
  18. --no-update)
  19. update=0
  20. shift # past argument
  21. ;;
  22. --audit)
  23. audit=1
  24. shift # past argument
  25. ;;
  26. --email)
  27. email="$2"
  28. shift # past argument
  29. ;;
  30. -h|help)
  31. echo "dss-mon $version"
  32. echo "Usage: dss --monitor --email user@mailprovider.com"
  33. exit 1
  34. ;;
  35. -v|version)
  36. echo "dss $version"
  37. exit 1
  38. ;;
  39. *)
  40. # unknown option
  41. if test -z "${unknown}"
  42. then
  43. unknown=$1
  44. else
  45. echo "dss-mon $version"
  46. echo "Usage: dss --monitor --email user@mailprovider.com"
  47. exit 1
  48. fi
  49. ;;
  50. esac
  51. shift # past argument or value
  52. done
  53. if test $update = 1
  54. then
  55. sudo apt-get update
  56. sudo apt-get upgrade
  57. sudo apt-get install sysstat # Check if installed, then do this
  58. curl -s "https://git.coolaj86.com/josh/dss/raw/branch/master/setup.sh" | bash
  59. fi
  60. # Cleanup
  61. sudo apt-get clean
  62. # Security Audit (Tackled by dss init before setting this up.)
  63. # if test ! -f /home/.dssv1.7
  64. # then
  65. #
  66. # dss init
  67. #
  68. # fi
  69. auth=$(sudo cat /var/log/auth.log | grep "Invalid user")
  70. #auth2=$(sudo cat /var/log/auth.log | grep "Connection closed")
  71. if test $alpha = 1;
  72. then
  73. sudo apt-get autoremove
  74. fi
  75. # To setup email, point a domain name to your server using DNS.
  76. # Disable any firewall rules that block port 25 (You may have to go to a server admin panel or contact your system administrator)
  77. # Then run: sudo apt-get install mailutils
  78. # Open up /etc/hosts and make sure it has:
  79. # 127.0.1.1 mydomain.com myserverHOSTNAME
  80. # Select "Internet Site" and enter the domain you want it to send email from.
  81. # Then you can send email like this: echo "Body of email" | mail -s "subject" EMAILADDRESS
  82. if test ! -z "$auth" # If set to run automatically, don't run this check every time.
  83. then
  84. echo "Attacks found. Sending authentication log to $email"
  85. sudo cat /var/log/auth.log | grep "Invalid user" | mail -s "Invalid User Login" $email
  86. fi
  87. if test ! -z "$dfh"
  88. then
  89. echo "Disk usage is high, sending disk usage to $email"
  90. echo "$dfh" | mail -s "High Disk Usage" $email
  91. fi
  92. if test ! -z "$dfh2"
  93. then
  94. echo "Disk usage is critical, sending disk usage to $email"
  95. echo "$dfh2" | mail -s "Critical Disk Usage" $email
  96. fi
  97. for i in {1..300} # Do this 300 times.
  98. do
  99. CPU=$(mpstat 1 1 | awk '$3 ~ /CPU/ { for(i=1;i<=NF;i++) { if ($i ~ /%idle/) field=i } } $3 ~ /all/ { printf("%d",100 - $field) }') # Find CPU usage for the last 10 seconds. Code credit: Stackoverflow
  100. CPUT=$(($CPUT + $CPU)) # Add each 1 second record to the total.
  101. done
  102. CPURESULT=$(($CPUT / 300)) # Divide the total by 300 seconds to find average CPU usage over the last 5 minutes.
  103. if test $CPURESULT > 90
  104. then
  105. echo "CPU usage is quite high, sending report to $email"
  106. echo "$CPURESULT %" | mail -s "High CPU Usage" $email
  107. fi
  108. USEDRAM=$(free | grep Mem | awk '{print ($2 -$7) / $2 * 100.0}')
  109. if test $USEDRAM > 80
  110. then
  111. echo "RAM usage is quite high, sending report to $email"
  112. echo "$USEDRAM %" | mail -s "High RAM Usage" $email
  113. fi