diff --git a/low_disk/.gitrepo b/low_disk/.gitrepo index 30036fe..52e8ae2 100644 --- a/low_disk/.gitrepo +++ b/low_disk/.gitrepo @@ -6,7 +6,7 @@ [subrepo] remote = https://github.com/LukeShirnia/Low_Disk.git branch = master - commit = e082e2d7b46a1c6ef4828f9d54524a387ec1c5c5 - parent = 2814f11ffe56fad25bd87ff7734d2886626e5a3e + commit = 92dca879629f15891fc5a963f426fb1fcced1e7b + parent = 26fc5def139d07c1d767490213a16218a6d99425 method = merge cmdver = 0.4.1 diff --git a/low_disk/README.md b/low_disk/README.md index 6ff062b..e6756ad 100644 --- a/low_disk/README.md +++ b/low_disk/README.md @@ -1,89 +1,209 @@ # Low_Disk -This disk usage script is designed to gather information on a Linux devices filesystem usage and report on: -- Filesystem Information (Space and Inode usage) +This disk usage script is designed to gather information on a Linux devices filesystem. +You can either run a breakdown of a filesystem `disk usage` or `inode usage`. + +## Disk Usage Breadown Summary +- Filesystem Information Overview - Largest Directories - Largest Files - Volume group space - Large Open Files - /home/rack usage -Use the `-b` flag for output with bbcode tags +## Inode Usage Breakdown Summary +- Filesystem Information Overview +- Storage Device Behind Filesystem +- Top Inode Consumers +- Bytes Per Inode +- Disk space usage +- Elapsed Time (time taken to calculate breakdown) +- Server time as script completion -## Usage ## +## Help ``` -Usage: disk_usage_check.sh [-f] [-b] [-h] - -f filesystem Specify a Filesystem - -b Print with bbcode - -h Print help (usage) +Usage: disk_usage_check.sh [-f] [-i] [-b] [-h] + -i, --inode Display Inode breakdown + -f, --filesystem <filesystem> Specify a Filesystem + -b, --bbcode Print with bbcode + -h, --help Print help (usage) ``` -## Example Output ## - +## Disk Usage Breadown ``` +$ ./disk_usage_check.sh + ============================================================ == / Filesystem Information == ============================================================ -Filesystem Type Size Used Avail Use% Mounted on -/dev/mapper/vglocal20170715-root00 ext4 130G 8.9G 121G 7% / +Filesystem Type Size Used Avail Use% Mounted on +/dev/mapper/fedora-root ext4 146G 59G 80G 43% / + +Filesystem Type Inodes IUsed IFree IUse% Mounted on +/dev/mapper/fedora-root ext4 9732096 1346900 8385196 14% / -Filesystem Type Inodes IUsed IFree IUse% Mounted on -/dev/mapper/vglocal20170715-root00 ext4 8634368 229822 8404546 3% / +============================================================ + == Server Time at start == +============================================================ + +Fri 11 Sep 12:23:57 BST 2020 ============================================================ == Largest Directories == ============================================================ -8.5G / -3.6G /var -3.4G /usr -2.7G /var/cache -1.4G /opt -1.1G /usr/local +59G / +30G /var +25G /var/lib +11G /home +8.9G /home/luke +8.7G /usr +8.1G /root +8.0G /root/backups_desktop +4.2G /var/log +3.4G /usr/share ============================================================ == Largest Files == ============================================================ -253.23M /var/cache/yum/x86_64/7Server/rhel-x86_64-server-7/gen/primary.xml.sqlite -250.77M /var/cache/yum/x86_64/7Server/rhel-x86_64-server-7/gen/filelists.xml -220.10M /var/cache/yum/x86_64/7Server/rhel-x86_64-server-optional-7/gen/filelists.xml -199.96M /var/cache/yum/x86_64/7Server/rhel-x86_64-server-7/gen/primary.xml -162.35M /var/cache/yum/x86_64/7Server/rhel_base/gen/primary_db.sqlite -157.98M /var/lib/rpm/Packages -139.54M /var/cache/yum/x86_64/7Server/rs-epel/gen/filelists.xml -137.99M /var/cache/yum/x86_64/7Server/rhel-x86_64-server-7/gen/filelists.xml.sqlite -101.13M /usr/lib/locale/locale-archive -100M /var/lib/mysql/ib_logfile1 -100M /var/lib/mysql/ib_logfile0 -90.38M /var/cache/yum/x86_64/7Server/rhel-x86_64-server-optional-7/gen/filelists.xml.sqlite -68.21M /opt/dell/srvadmin/sbin/lx32/invcol -63.64M /var/cache/yum/x86_64/7Server/rs-epel/gen/filelists.xml.sqlite -63.02M /opt/dell/srvadmin/lib64/openmanage/jre/lib/rt.jar -59.26M /opt/dell/srvadmin/lib64/openmanage/jre/lib/amd64/libjfxwebkit.so -55.68M /var/cache/yum/x86_64/7Server/rs-epel/gen/filelists_db.sqlite -43.55M /opt/dell/srvadmin/sbin/lx64/invcol -42.85M /var/cache/yum/x86_64/7Server/rhel-x86_64-server-7/packages/kernel-3.10.0-693.el7.x86_64.rpm -37.30M /var/cache/yum/x86_64/7Server/rhel-x86_64-server-7/packages/kernel-3.10.0-514.26.2.el7.x86_64.rpm +1061.86M /home/luke/Downloads/Streetlight.Online.mp4 +640.12M /root/backups_desktop/error.log +535.58M /root/backups_desktop/daily/test/home/luke/development/github/git_Luke/out-of-memory/messages +535.58M /home/luke/development/github/git_Luke/out-of-memory/messages +505.55M /root/backups_desktop/daily/desktop.2020-08-28-15:30:01.tar.gz.gpg +503.56M /root/backups_desktop/daily/desktop.2020-08-29-15:30:01.tar.gz.gpg +467.61M /root/backups_desktop/daily/desktop.2020-09-09-15:30:02.tar.gz.gpg +459.72M /root/backups_desktop/daily/desktop.2020-09-08-15:30:01.tar.gz.gpg +457.75M /root/backups_desktop/daily/desktop.2020-09-02-15:30:01.tar.gz.gpg +455.59M /root/backups_desktop/daily/desktop.2020-09-01-15:30:01.tar.gz.gpg +455.38M /root/backups_desktop/daily/desktop.2020-09-07-15:30:02.tar.gz.gpg +453.94M /root/backups_desktop/weekly/desktop.2020-09-04-15:00:02.tar.gz.gpg +453.93M /root/backups_desktop/daily/desktop.2020-09-04-15:30:01.tar.gz.gpg +453.27M /root/backups_desktop/daily/desktop.2020-09-03-15:30:01.tar.gz.gpg +412.92M /root/backups_desktop/daily/test/test.tar.gz +300.00M /root/backups_desktop/daily/test/home/luke/development/github/git_Luke/out-of-memory/oom_logs/large +300.00M /root/backups_desktop/daily/test/home/luke/development/github/git_Luke/oom_new_notgit/oom_logs/messages.verylarge +300.00M /home/luke/development/github/git_Luke/out-of-memory/oom_logs/large +300M /home/luke/development/github/git_Luke/oom_new_notgit/oom_logs/messages.verylarge +230.55M /usr/share/atom/resources/app.asar ============================================================ == Volume Group Usage == ============================================================ - VG #PV #LV #SN Attr VSize VFree - vglocal20170715 1 3 0 wz--n- 135.63g 4.00m + VG #PV #LV #SN Attr VSize VFree + fedora 1 1 0 wz--n- 148.43g 0 ============================================================ == OK Check List == -============================================================ +============================================================ The following have been checked and are ok: -[OK] No deleted files over 1GB -[OK] /home/rack smaller than 1GB: 58 MB +[OK] No open DELETED files over 500MB +[WARNING] /home/rack does not appear to exist + +============================================================ + == Elapsed Time == +============================================================ + +0h:0m:56s + +============================================================ + == Server Time at completion == +============================================================ + +Fri 11 Sep 12:24:53 BST 2020 + +============================================================ +``` +## Inode Usage Breadown +``` +$ ./disk_usage_check.sh --inode + + +============================================================ + == / Filesystem Information == +============================================================ + +Checking Inodes. Please note this could take a while to run... + +============================================================ + == Server Time at start == +============================================================ + +Fri 11 Sep 12:27:47 BST 2020 + +============================================================ + == Inode Information for [ / ] == +============================================================ + +Filesystem Type Inodes IUsed IFree IUse% Mounted on +/dev/mapper/fedora-root ext4 9732096 1346902 8385194 14% / + +============================================================ + == Storage device behind filesystem [ / ] == +============================================================ + +/dev/mapper/fedora-root + +============================================================ + == Top inode Consumers on [ / ] == +============================================================ + +inode-Count Path + 814,406 /var + 813,177 /var/lib + 808,320 /var/lib/docker + 771,441 /var/lib/docker/overlay2 + 256,737 /home + 255,527 /usr + 179,418 /home/luke + 146,649 /usr/share + 69,939 /home/luke/.cache + 68,990 /usr/lib + 53,524 /root + 49,277 /root/backups_desktop + 49,272 /root/backups_desktop/daily + 49,262 /root/backups_desktop/daily/test + 49,233 /root/backups_desktop/daily/test/home + 42,162 /home/venv + 38,952 /home/luke/development + 35,370 /usr/lib/python3.8 + 35,369 /usr/lib/python3.8/site-packages + 35,107 /home/luke/.cache/google-chrome + +============================================================ + == Bytes per Inode format for [ / ] == +============================================================ + +15.0 KB per inode! + +============================================================ + == Disk space [ / ] == +============================================================ + +Filesystem Type Size Used Avail Use% Mounted on +/dev/mapper/fedora-root ext4 146G 59G 80G 43% / + +Filesystem Type Inodes IUsed IFree IUse% Mounted on +/dev/mapper/fedora-root ext4 9732096 1346905 8385191 14% / + +============================================================ + == Elapsed Time == +============================================================ + +0h:1m:38s + +============================================================ + == Server Time at completion == +============================================================ + +Fri 11 Sep 12:29:25 BST 2020 ============================================================ ``` diff --git a/low_disk/disk_usage_check.sh b/low_disk/disk_usage_check.sh index 8fc60ac..15b80d8 100644 --- a/low_disk/disk_usage_check.sh +++ b/low_disk/disk_usage_check.sh @@ -6,35 +6,59 @@ BREAK="============================================================" NotRun=() # Array to store all commands not run during script -PrintFirstHeader(){ - if [[ $bbcode = 'True' ]]; then - echo -ne "\n$BREAK \n \t == $1 == \n$BREAK \n\n"; - echo "[code]" - else - echo -ne "\n$BREAK \n \t == $1 == \n$BREAK \n\n"; - fi -} PrintHeader() { # Common header used throughout script - if [[ $bbcode = 'True' ]]; then + if [[ "$bbcode" = "True" ]] && [[ "$2" == "NoTags" ]]; then + echo + echo -ne "$BREAK \n \t ==[b] $1 [/b]== \n$BREAK \n\n"; + elif [[ "$bbcode" = "True" ]] && [[ "$2" == "NoOpen" ]]; then echo "[/code]" echo - echo -ne "$BREAK \n \t == $1 == \n$BREAK \n\n"; + echo -ne "$BREAK \n \t ==[b] $1 [/b]== \n$BREAK \n\n"; + elif [[ "$bbcode" = "True" ]] && [[ "$2" == "NoClose" ]]; then + echo + echo -ne "$BREAK \n \t ==[b] $1 [/b]== \n$BREAK \n\n"; + echo '[code]' + elif [[ "$bbcode" = "True" ]]; then + echo "[/code]" + echo + echo -ne "$BREAK \n \t ==[b] $1 [/b]== \n$BREAK \n\n"; echo '[code]' else echo -ne "\n$BREAK \n \t == $1 == \n$BREAK \n\n"; fi } usage() { # Print script usage function - echo "Usage: $0 [-f] [-b] [-h]" - echo " -f filesystem Specify a Filesystem" - echo " -b Print with bbcode" - echo " -h Print help (usage)" + echo "Usage: $0 [-f] [-i] [-b] [-h]" + echo " -i, --inode Display Inode breakdown" + echo " -f, --filesystem <filesystem> Specify a Filesystem" + echo " -b, --bbcode Print with bbcode" + echo " -h, --help Print help (usage)" exit 1 } +start_time() { + intStartTime=$(date +%s) + PrintHeader "Server Time at start" "NoClose" + date +} +end_time() { + intEndTime=$(date +%s); + intDuration=$((intEndTime-intStartTime)); + PrintHeader "Elapsed Time" "NoClose" + printf '%dh:%dm:%ds\n' $(($intDuration/3600)) $(($intDuration%3600/60)) $(($intDuration%60)); + + PrintHeader "Server Time at completion" + date; + if [[ $bbcode = 'True' ]]; then + echo "[/code]" + fi +} filesystem_overview() { df -PTh $filesystem; echo df -PTi $filesystem; + if [[ $bbcode = 'True' ]]; then + echo "[/code]" + fi } large_directories() { if [[ ! $( du -hcx --max-depth=2 $filesystem 2>/dev/null | grep -P '^([0-9]\.*)*G(?!.*(\btotal\b|\./$))' ) ]]; then @@ -85,10 +109,7 @@ home_rack() { # Check disk usage in /home/rack fi } NotRun() { # Print a list of commands not run at the end of the script - if [[ $bbcode = 'True' ]]; then - echo "[/code]" - fi - echo -ne "\n$BREAK \n \t == "OK Check List" == \n$BREAK \n\n"; + PrintHeader "OK Checklist" "NoOpen" echo "The following have been checked and are ok: " echo @@ -114,10 +135,73 @@ NotRun() { # Print a list of commands not run at the end of the script esac done } +check_inodes() { + intNumFiles=20; + intDepth=5; + strFsMount=$(df -P $filesystem | awk '$1 !~ /Filesystem/ {print $6}'); + + resize 2&> /dev/null; + + start_time + + PrintHeader "Inode Information for [ $strFsMount ]" + + df -PTi $strFsMount | column -t; + strFsDev=$(df -P $PWD | awk '$0 !~ /Filesystem/ {print $1}'); + PrintHeader "Storage device behind filesystem [ $strFsMount ]" + echo $strFsDev; + PrintHeader "Top inode Consumers on [ $strFsMount ]" + + awk '{ printf "%11s \t %-30s\n", $1, $2 }' <(echo "inode-Count Path"); + awk '{ printf "%11'"'"'d \t %-30s\n", $1, $2 } ' <( + strMounts="$(findmnt -o TARGET -rn | sed 's/^\s*/\^/g' | sed 's/$/\$|/g' | tr -d '\n' | sed 's/|$/\n/')"; + find $strFsMount -maxdepth $intDepth -xdev -type d -print0 2>/dev/null | while IFS= read -rd '' i; + do if ! echo $i | grep -E "$strMounts"; + then echo "$(find "$i" -xdev | wc -l ) $i "; + fi; + done | sort -n -r | head -n $intNumFiles + ) ; + + PrintHeader "Bytes per Inode format for [ $strFsMount ]" + echo "$(printf "%.1f\n" $(echo "$(tune2fs -l $strFsDev | awk -F ": *" '$1 ~ /Inode count/ { inodecount = $2 }; $1 == "Block count" {printf "%s", $2}; $1 == "Block size" {printf "%s", " * " $2 " / " inodecount };' | tr -d '\n') /1024" | bc)) KB per inode"'!'; + + PrintHeader "Disk space [ $strFsMount ]" + filesystem_overview + + end_time + +} + + +# Allow for long options. Code based off https://stackoverflow.com/a/30026641 +for arg in "$@"; do + shift + case "$arg" in + "--help") + set -- "$@" "-h" + ;; + "--inode") + set -- "$@" "-i" + ;; + "--bbcode") + set -- "$@" "-b" + ;; + "--filesystem") + set -- "$@" "-f" + ;; + "--"*) + echo "Invalid option: ${arg}" 1>&2 + usage ${arg} + exit 2 + ;; + *) + set -- "$@" "$arg" + esac +done # Checking the script arguments and assigning the appropriate $filesystem -while getopts ":f:hb" opt; do +while getopts ":f:ihb" opt; do case ${opt} in f ) filesystem=$OPTARG @@ -125,13 +209,16 @@ while getopts ":f:hb" opt; do b ) bbcode='True' ;; + i ) + inode='True' + ;; \? ) echo "Invalid option: $OPTARG" 1>&2 usage exit 1 ;; : ) - echo "Invalid option: -$OPTARG requires an argument" 1>&2 + echo "Invalid option: -f, --filesystem requires an argument" 1>&2 exit 1 ;; h ) @@ -155,33 +242,36 @@ echo ############################################################################################## -PrintFirstHeader "$filesystem Filesystem Information" - -filesystem_overview - -PrintHeader "Largest Directories" - -large_directories - -PrintHeader "Largest Files" - -largest_files - -# Check to see if logical volumes are being used -logical_volumes - -if [ "$( which lsof 2>/dev/null )" ]; then - # Check open deleted filed - lsof_check_open_deleted +# If inode is specified, we only want to get a breakdown of the systems inodes +if [[ "$inode" = "True" ]]; then + PrintHeader "$filesystem Filesystem Information" "NoTags" + echo "Checking Inodes. Please note this could take a while to run..." + check_inodes +# If inode is not specified, run a normal filesystem breakdown else - NotRun+=("lsof_large_open_deleted"); -fi + PrintHeader "$filesystem Filesystem Information" "NoClose" + filesystem_overview + start_time + PrintHeader "Largest Directories" + large_directories + PrintHeader "Largest Files" + largest_files + # Check to see if logical volumes are being used + logical_volumes -# Run home_rack function to check disk usage -home_rack + if [ "$( which lsof 2>/dev/null )" ]; then + # Check open deleted filed + lsof_check_open_deleted + else + NotRun+=("lsof_large_open_deleted"); + fi -# Print commands/sections not run -NotRun + # Run home_rack function to check disk usage + home_rack + # Print commands/sections not run + NotRun + end_time +fi echo echo $BREAK