diff --git a/scripts/backup.sh b/scripts/backup.sh new file mode 100755 index 0000000..681443c --- /dev/null +++ b/scripts/backup.sh @@ -0,0 +1,22 @@ +#!/bin/bash + +# Back up file Scripts directory +# Today's Timestam +timestamp=$(date +%d-%m-%y) + +# Path to target directory to be backed up +target_dir='/home/gege/devops-bash-toolkit-assestment/scripts' + +# Path to destination directory +destination_dir='/home/gege/scripts_backup_'$timestamp'.tar.gz' + +# Accept directory as input +if [[ ! -d $1 ]] then + echo "Argument is not a directory." + exit +else + cp -r ${target_dir} ${destination_dir} +fi + + + diff --git a/scripts/file_manager.sh b/scripts/file_manager.sh new file mode 100755 index 0000000..a072c0d --- /dev/null +++ b/scripts/file_manager.sh @@ -0,0 +1,45 @@ +#!/bin/bash +# Check the following actions run with this scrips + +action=$1 +filename=$2 + +# Using a case statement to check each action and possibility. +case $action in + create) + if [ -e $filename ]; then + echo "File already exists." + else + touch "$filename" + echo "File has been created." + fi + ;; + delete) + if [ -e $filename ]; then + rm "$filename" + echo "$filename has been deleted" + else + echo "$filename does not exist" + fi + ;; + list) + if [ -e $filename ]; then + ls + else + echo "$filename does not exist" + fi + ;; + rename) + if [ -e $filename ]; then + mv "$filename" "temp_file.tx" + echo "$filename has been renamed to temp_file" + else + echo "Failed to rename file" + fi + ;; + *) + echo "Unkown action: try: ./file_manager.sh {create} {delete} {list}" + ;; +esac + + diff --git a/scripts/logs/file_manager.log b/scripts/logs/file_manager.log new file mode 100644 index 0000000..70ca51d --- /dev/null +++ b/scripts/logs/file_manager.log @@ -0,0 +1 @@ +temp_file.tx has been deleted diff --git a/scripts/logs/system_report_17-04-26.log b/scripts/logs/system_report_17-04-26.log new file mode 100644 index 0000000..dc0dcf2 --- /dev/null +++ b/scripts/logs/system_report_17-04-26.log @@ -0,0 +1,15 @@ + total used free shared buff/cache available +Mem: 3855 446 3396 3 145 3408 +Swap: 1024 0 1024 + +System uptime: 20:50:12 up 2:40, 1 user, load average: 0.00, 0.00, 0.00 + +Disk usage is at 1 which is below the threshold of 80 + +Total running processes is: 27 + +0.5 +0.4 +0.3 +0.3 +0.3 diff --git a/scripts/logs/user_info.log b/scripts/logs/user_info.log new file mode 100644 index 0000000..517d9a9 --- /dev/null +++ b/scripts/logs/user_info.log @@ -0,0 +1 @@ +Welcome, Gege! You are 21 years old--an adult, and from Nigeria. diff --git a/scripts/process_monitor.sh b/scripts/process_monitor.sh new file mode 100755 index 0000000..fe07d2e --- /dev/null +++ b/scripts/process_monitor.sh @@ -0,0 +1,28 @@ +#!/bin/bash +# This script monitors a process + +services=("nginx" "ssh" "docker") +found=false + +# Iterate through array to check for a running process +for ps in ${services[@]}; do + if [[ $1 == "$ps" ]]; then + found=true + # check if process is running + if pgrep -x "$ps" > /dev/null; then + echo "$ps process is running." + else + echo "$ps process is not running." + echo "Attempting to install process..." + sudo apt install "$ps" + fi + # Exit the loop after find and processing service + break + fi +done + +# Notify if argument doesn't match the array +if ! $found; then + echo "$1 is not a valid argument" +fi + diff --git a/scripts/system_check.sh b/scripts/system_check.sh new file mode 100755 index 0000000..8282818 --- /dev/null +++ b/scripts/system_check.sh @@ -0,0 +1,37 @@ +#!/bin/bash +set -euo pipefail + +# Redirect input to a .log file +ts=$(date +%d-%m-%y) + +# Set Threshold +threshold=80 + +# Display Disk usage +diskuse=$(df -P / | awk 'NR>1 {print $5}' | sed 's/%//') + +# Display memory usage +free_memory=$(free -m) +echo "$free_memory" +echo "" + +# Display CPU loadtime +sys_uptime=$(uptime) +echo "System uptime: $sys_uptime" +echo "" +# Check if the disk usage exceeds the threshold +if [[ "$diskuse" -gt "$threshold" ]]; then + echo "Warning! Disk usage is at $diskuse% which exceeds the threshold of $threshold%" +else + echo "Disk usage is at $diskuse which is below the threshold of $threshold" +fi +echo "" + +# count total running processes +ps_count=$(ps aux | wc -l) +echo "Total running processes is: $ps_count" +echo "" + +# Display top 5 memory consuming processes +max_memory=$(ps aux | sort -k4 -nr | awk 'NR>=1 {print $4}' | head -n 5) +echo "$max_memory" diff --git a/scripts/user_info.sh b/scripts/user_info.sh new file mode 100755 index 0000000..914bb11 --- /dev/null +++ b/scripts/user_info.sh @@ -0,0 +1,31 @@ +#!/bin/bash +# User information requirement script +# prompt the user for name, age, and country. + +# Save output to logs/user_info.log + +while true; do + echo "Hello, please enter your name, age, country:" + read name age country + +# Redirect stdout and stderr to user_info.log +exec > logs/user_info.log 2>&1 + +# Verify strings are not empty and age is numeric. + if [[ -n "$name" ]] && [[ -n "$age" ]] && [[ -n "$country" ]] && [[ "$age" =~ ^[0-9]+$ ]]; then + # Age is numeric. Continue with the script. + if [[ "$age" -lt 18 ]]; then + # echo a greeting message to user. + echo "Welcome, $name! You are $age years old--a minor, and from $country." + elif [[ "$age" -ge 18 ]]; then + echo "Welcome, $name! You are $age years old--an adult, and from $country." + else + echo "Welcome, $name! You are over $age years old--a senior, and from $country." + break + fi + break + else + echo "Age must be a numeric value." + fi +done +