Instructions to set up a basic LAMP+SSH server environment
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.

117 lines
3.0 KiB

5 years ago
  1. #!/bin/bash
  2. # Author: Pekka Helenius (~Fincer), 2018
  3. # Collect system statistics at regular intervals
  4. # Check if the following file exists. If not, exit the script.
  5. [[ $(ls /etc/default/sysstat) ]] || exit
  6. # Enable stats
  7. [[ $(cat /etc/default/sysstat | grep "ENABLED=\"true\"") ]] || sudo sed -i 's/ENABLED.*/ENABLED="true"/' /etc/default/sysstat
  8. # How many days we take stats
  9. DAYS=2
  10. DATE_INTERVAL=$(echo $(date +%Y-%m-%d)_$(date +%Y-%m-%d -d "+$DAYS days"))
  11. # Interval of stats in seconds
  12. INTERVAL=20
  13. # Logs dir
  14. SARDIR=$HOME/sar_statistics
  15. SARSTAT_FILE=$SARDIR/sar_stats_$DATE_INTERVAL.file
  16. PIDSTAT_DIR=$SARDIR/pidstats
  17. ##############
  18. [[ -d $PIDSTAT_DIR ]] || mkdir -p $PIDSTAT_DIR
  19. DAYS_SEC=$(( DAYS * 60 * 60 * 24))
  20. COUNT=$(( DAYS_SEC / INTERVAL ))
  21. ##############
  22. # Ignore HANGUP signal. By setting this, we continue running commands presented below though this script has exited
  23. # Alternatively, use 'nohup' before any command execution. NOTE: 'nohup' doesn't work with functions
  24. trap "" HUP
  25. # Gather data for analysis
  26. # alternatively use 'nohup' as a prefix for this command
  27. sar $INTERVAL $COUNT -o $SARSTAT_FILE &>/dev/null &
  28. disown
  29. ##########################################
  30. # How to print out statistics?
  31. # CPU
  32. # sar -u -f $SARSTAT_FILE
  33. # Memory
  34. # sar -r -f $SARSTAT_FILE
  35. # Mounted filesystems
  36. # sar -F MOUNT / -f $SARSTAT_FILE
  37. # Swap space stats
  38. # sar -S -f $SARSTAT_FILE
  39. # Network devices
  40. # sar -n DEV -f $SARSTAT_FILE
  41. # Network devices - errors
  42. # sar -n EDEV -f $SARSTAT_FILE
  43. # Network IPv4 sockets
  44. # sar -n SOCK -f $SARSTAT_FILE
  45. # Network IPv6 sockets
  46. # sar -n SOCK6 -f $SARSTAT_FILE
  47. # Network IPv4 traffic statistics
  48. # sar -n IP -f $SARSTAT_FILE
  49. # Network IPv4 traffic statistics - errors
  50. # sar -n EIP -f $SARSTAT_FILE
  51. # Network IPv6 traffic statistics
  52. # sar -n IP6 -f $SARSTAT_FILE
  53. # Network IPv6 traffic statistics - errors
  54. # sar -n EIP6 -f $SARSTAT_FILE
  55. # Network TCP protocol statistics
  56. # sar -n TCP -f $SARSTAT_FILE
  57. # Network TCP protocol statistics - errors
  58. # sar -n ETCP -f $SARSTAT_FILE
  59. # Inode, file etc. stats
  60. # sar -v -f $SARSTAT_FILE
  61. # Tasks statistics
  62. # sar -w -f $SARSTAT_FILE
  63. ##########################################
  64. # Pidstat
  65. function pidstats() {
  66. while [[ $DAYS_SEC -ge 0 ]]; do
  67. pidstat -d >> $PIDSTAT_DIR/pidstat_stats_io_$DATE_INTERVAL
  68. # pidstat -R >> $PIDSTAT_DIR/pidstat_stats_realtime_$DATE_INTERVAL
  69. pidstat -r >> $PIDSTAT_DIR/pidstat_stats_pagefaults_$DATE_INTERVAL
  70. pidstat -s >> $PIDSTAT_DIR/pidstat_stats_stacks_$DATE_INTERVAL
  71. pidstat -u >> $PIDSTAT_DIR/pidstat_stats_cpu-tasks_$DATE_INTERVAL
  72. pidstat -v >> $PIDSTAT_DIR/pidstat_stats_kerneltables_$DATE_INTERVAL
  73. # We want to interrupt this loop if previous pidstat commands fail.
  74. if [[ ! $? == 0 ]]; then
  75. break 1
  76. fi
  77. DAYS_SEC=$(( DAYS_SEC - INTERVAL ))
  78. sleep $INTERVAL
  79. done
  80. }
  81. pidstats &
  82. echo -e "Collecting statistics with 'sar' and 'pidstat' commands for the following $DAYS days. All commands will stop at $(date '+%H:%M on %Y-%m-%d' -d '+2 days')."
  83. disown