Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions scripts/backup.sh
Original file line number Diff line number Diff line change
@@ -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



45 changes: 45 additions & 0 deletions scripts/file_manager.sh
Original file line number Diff line number Diff line change
@@ -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


1 change: 1 addition & 0 deletions scripts/logs/file_manager.log
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
temp_file.tx has been deleted
15 changes: 15 additions & 0 deletions scripts/logs/system_report_17-04-26.log
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions scripts/logs/user_info.log
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Welcome, Gege! You are 21 years old--an adult, and from Nigeria.
28 changes: 28 additions & 0 deletions scripts/process_monitor.sh
Original file line number Diff line number Diff line change
@@ -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

37 changes: 37 additions & 0 deletions scripts/system_check.sh
Original file line number Diff line number Diff line change
@@ -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"
31 changes: 31 additions & 0 deletions scripts/user_info.sh
Original file line number Diff line number Diff line change
@@ -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