Tuesday, 7 April 2020

Sample bash script to generate system report

Generate system report

vim /usr/local/src/test_my_scripts/system_report.sh
#!/bin/bash
# System Report
greentext="\033[32m"
bold="\033[1m"
normal="\033[0m"
freespace=$(df -h / | grep -E "\/$" | awk '{print $4}')
logdate=$(date +"%Y%m%d")
path=/var/log/script
logfile="$path$logdate"_report.log

echo -e $bold"Quick system report for "$greentext"$HOSTNAME"$normal
printf "\tSystem type:\t%s\n" $MACHTYPE
printf "\tBash Version:\t%s\n" $BASH_VERSION
printf "\tFree Space:\t%s\n" $freespace
printf "\tFiles in dir:\t%s\n" $(ls | wc -l)
printf "\tGenerated on:\t%s\n" $(date +"%m/%d/%y")
echo -e $greentext"A summary of this info has been saved to $logfile"$normal

cat <<- EOF > $logfile
        This report was generated using Bash script.
EOF

printf "\tGenerated on:\t%s\n" $(date +"%m/%d/%y") >> $logfile
printf "\tFree Space:\t%s\n" $freespace >> $logfile
printf "\tFiles in dir:\t%s\n" $(ls | wc -l) >> $logfile

sh /usr/local/src/test_my_scripts/system_report.sh
Quick system report for localhost.localdomain
        System type:    x86_64-redhat-linux-gnu
        Bash Version:   4.2.46(2)-release
        Free Space:     15G
        Files in dir:   6
        Generated on:   04/07/20
A summary of this info has been saved to /var/log/script20200407_report.log

cat /var/log/script20200407_report.log
This report was generated using Bash script.
        Generated on:   04/07/20
        Free Space:     15G
        Files in dir:   6

No comments:

Post a comment