info_distro.sh 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #!/bin/bash
  2. # LinuxGSM info_distro.sh function
  3. # Author: Daniel Gibbs
  4. # Website: https://linuxgsm.com
  5. # Description: Variables providing useful info on the Operating System such as disk and performace info.
  6. # Used for command_details.sh, command_debug.sh and alert.sh.
  7. local function_selfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  8. ### Distro information
  9. ## Distro
  10. # Returns architecture, kernel and distro/os.
  11. arch=$(uname -m)
  12. kernel=$(uname -r)
  13. if [ -n "$(command -v lsb_release)" ]; then
  14. distroname=$(lsb_release -s -d)
  15. elif [ -f "/etc/os-release" ]; then
  16. distroname=$(grep PRETTY_NAME /etc/os-release | sed 's/PRETTY_NAME=//g' | tr -d '="')
  17. elif [ -f "/etc/debian_version" ]; then
  18. distroname="Debian $(cat /etc/debian_version)"
  19. elif [ -f "/etc/redhat-release" ]; then
  20. distroname=$(cat /etc/redhat-release)
  21. else
  22. distroname="$(uname -s) $(uname -r)"
  23. fi
  24. if [ -f "/etc/os-release" ]; then
  25. distroversion=$(grep VERSION_ID /etc/os-release | sed 's/VERSION_ID=//g' | sed 's/\"//g')
  26. distroid=$(grep ID /etc/os-release | grep -v _ID | grep -v ID_ | sed 's/ID=//g')
  27. elif [ -n "$(command -v yum)" ]; then
  28. distroversion=$(rpm -qa \*-release | grep -Ei "oracle|redhat|centos" | cut -d"-" -f3)
  29. fi
  30. ## Glibc version
  31. # e.g: 1.17
  32. glibcversion="$(ldd --version | sed -n '1s/.* //p')"
  33. ## tmux version
  34. # e.g: tmux 1.6
  35. if [ -z "$(command -V tmux 2>/dev/null)" ]; then
  36. tmuxv="${red}NOT INSTALLED!${default}"
  37. else
  38. if [ "$(tmux -V|sed "s/tmux //" | sed -n '1 p' | tr -cd '[:digit:]')" -lt "16" ] 2>/dev/null; then
  39. tmuxv="$(tmux -V) (>= 1.6 required for console log)"
  40. else
  41. tmuxv=$(tmux -V)
  42. fi
  43. fi
  44. ## Uptime
  45. uptime=$(</proc/uptime)
  46. uptime=${uptime/[. ]*/}
  47. minutes=$(( uptime/60%60 ))
  48. hours=$(( uptime/60/60%24 ))
  49. days=$(( uptime/60/60/24 ))
  50. ### Performance information
  51. ## Average server load
  52. load=$(uptime|awk -F 'load average: ' '{ print $2 }')
  53. ## Memory information
  54. # Available RAM and swap.
  55. # Older versions of free do not support -h option.
  56. if [ "$(free -h > /dev/null 2>&1; echo $?)" -ne "0" ]; then
  57. humanreadable="-m"
  58. else
  59. humanreadable="-h"
  60. fi
  61. physmemtotal=$(free ${humanreadable} | awk '/Mem:/ {print $2}')
  62. physmemtotalmb=$(free -m | awk '/Mem:/ {print $2}')
  63. physmemused=$(free ${humanreadable} | awk '/Mem:/ {print $3}')
  64. physmemfree=$(free ${humanreadable} | awk '/Mem:/ {print $4}')
  65. physmemcached=$(free ${humanreadable} | awk '/cache:/ {print $4}')
  66. if [ -z "${physmemcached}" ]; then
  67. physmemcached=$(free ${humanreadable} | awk '/Mem:/ {print $5}')
  68. fi
  69. swaptotal=$(free ${humanreadable} | awk '/Swap:/ {print $2}')
  70. swapused=$(free ${humanreadable} | awk '/Swap:/ {print $3}')
  71. swapfree=$(free ${humanreadable} | awk '/Swap:/ {print $4}')
  72. ### Disk information
  73. ## Available disk space on the partition.
  74. filesystem=$(df -hP "${rootdir}" | grep -v "Filesystem" | awk '{print $1}')
  75. totalspace=$(df -hP "${rootdir}" | grep -v "Filesystem" | awk '{print $2}')
  76. usedspace=$(df -hP "${rootdir}" | grep -v "Filesystem" | awk '{print $3}')
  77. availspace=$(df -hP "${rootdir}" | grep -v "Filesystem" | awk '{print $4}')
  78. ## LinuxGSM used space total.
  79. rootdirdu=$(du -sh "${rootdir}" 2> /dev/null | awk '{print $1}')
  80. if [ -z "${rootdirdu}" ]; then
  81. rootdirdu="0M"
  82. fi
  83. ## LinuxGSM used space in serverfiles dir.
  84. serverfilesdu=$(du -sh "${serverfiles}" 2> /dev/null | awk '{print $1}')
  85. if [ -z "${serverfilesdu}" ]; then
  86. serverfilesdu="0M"
  87. fi
  88. ## LinuxGSM used space total minus backup dir.
  89. rootdirduexbackup=$(du -sh --exclude="${backupdir}" "${serverfiles}" 2> /dev/null | awk '{print $1}')
  90. if [ -z "${rootdirduexbackup}" ]; then
  91. rootdirduexbackup="0M"
  92. fi
  93. ## Backup info
  94. if [ -d "${backupdir}" ]; then
  95. # Used space in backups dir.
  96. backupdirdu=$(du -sh "${backupdir}" | awk '{print $1}')
  97. # If no backup dir, size is 0M
  98. if [ -z "${backupdirdu}" ]; then
  99. backupdirdu="0M"
  100. fi
  101. # number of backups set to 0 by default
  102. backupcount=0
  103. # If there are backups in backup dir.
  104. if [ $(find "${backupdir}" -name "*.tar.gz" | wc -l) -ne "0" ]; then
  105. # number of backups.
  106. backupcount=$(find "${backupdir}"/*.tar.gz | wc -l)
  107. # most recent backup.
  108. lastbackup=$(find "${backupdir}"/*.tar.gz | head -1)
  109. # date of most recent backup.
  110. lastbackupdate=$(date -r "${lastbackup}")
  111. # no of days since last backup.
  112. lastbackupdaysago=$(( ( $(date +'%s') - $(date -r "${lastbackup}" +'%s') )/60/60/24 ))
  113. # size of most recent backup.
  114. lastbackupsize=$(du -h "${lastbackup}" | awk '{print $1}')
  115. fi
  116. fi
  117. # External IP address
  118. if [ -z "${extip}" ]; then
  119. extip=$(${curlpath} -m 3 ifconfig.co > "${tmpdir}/extip.txt" 2>/dev/null)
  120. if [ $? -ne 0 ]; then
  121. if [ -f "${tmpdir}/extip.txt" ]; then
  122. echo "${tmpdir}/extip.txt"
  123. else
  124. echo "x.x.x.x"
  125. fi
  126. fi
  127. fi