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
127 changes: 127 additions & 0 deletions Challenges/Day_3/user_mamagement.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#Author : Apurv Samadder
#Date : Aug 2 2023
#Version: v1
#Day 3 Bash Scripting Challenge - User Account Management.



#!/bin/bash

#Create a user
add_user()
{
echo "enter username "
read username

#echo "Enter password"
#read pass

if grep "${username}" /etc/passwd >/dev/null 2>&1; then

echo "User alread present"

else
echo "enter password"
read pass
useradd -m -p $pass $username && echo "Successfully added user"

fi
}

#delete user

delete_user()
{

if [[ $(id -u) -ne 0 ]]; then
echo "This script must be run as root or with sudo privileges."
exit 1
fi

# Prompt the user for the username to delete
read -p "Enter the username you want to delete: " username

# Check if the username exists
if id "$username" &>/dev/null; then
# Delete the user and their home directory
userdel -r "$username"
echo "User '$username' and home directory deleted."
else
echo "User '$username' does not exist."
fi
}


reset_passwd()
{

if [[ $(id -u) -ne 0 ]]; then
echo "This script must be run as root or with sudo privileges."
exit 1
fi

# Prompt the user for the username to changes password
read -p "Enter the username you want to delete: " username

# Check if the username exists
if id "$username" &>/dev/null; then
# Changes the password if user exist
passwd "$username"
echo "User '$username' Password changed."
else
echo "User '$username' does not exist."
fi
}

list_user()
{
# Read uid column /etc/passwd
for userid in `awk -F: '{print $3}' /etc/passwd`
do
if ("$userid" >= 1000); then
echo "Valid User" :`cat /etc/passwd | grep $userid | awk -F: '{print $1,$3}'`
fi
done
}


user_mgmt()
{
echo "options"
echo "-c,--create create a new user accoutn"
echo "-d, --delete Delete and existing user account"
echo "-r, --reset Reset a password for existing user"
echo "-l, --list List all the user in system"
echo "-h, --help Display this help and exit"

read option

case $option in
-c|--create)
add_user
;;
-d|--delete)
echo "inside delete"
delete_user
;;
-r|--reset)
echo "inside reset"
reset_passwd
;;
-l|--list)
list_user
echo "inside list"
;;
-h|--help)
echo "inside help"
;;
*)
echo "please enter valid option"
user_mgmt
;;
esac
}

user_mgmt


105 changes: 105 additions & 0 deletions Challenges/Day_4/monitoring_metric.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
####################################
#Author : Apurv Samadder
#Date : Aug 3 2023
#Version: v1
#Day 4 Bash Scripting Challenge - Part_2_Monitoring_Metrics.md
####################################


#!/bin/bash

#Function to check get metrices

display_metric()
{

# Get CPU usage percentage
cpu_usage=$(top -bn 1 | awk '/%Cpu/{print $2}' | cut -d. -f1)

# Get memory usage percentage
mem_usage=$(free | awk '/Mem/{printf("%.2f", $3/$2 * 100)}')

# Get disk usage percentage of root partition
disk_usage=$(df -h / | awk '/\//{print $5}' | cut -d'%' -f1)

# Display metrics
echo "CPU Usage: $cpu_usage% Mem Usage: $mem_usage% Disk Space: $disk_usage%"

# Sleep for the specified interval (in seconds) before displaying metrics again
sleep_interval=2
sleep "$sleep_interval"
}



# Function to check and manage service
check_service() {
service_name="$1"

# Check if the service is running
if systemctl is-active --quiet "$service_name"; then
echo "**********************************"
echo "$service_name is running."
else
echo "**********************************"
echo "$service_name is not running."
read -p "Do you want to start $service_name? (y/n): " choice
if [[ "$choice" =~ ^[Yy]$ ]]; then
sudo systemctl start "$service_name"
echo "*********************************"
echo "Starting $service_name..."
else
echo "********************************"
echo "Service not started."
fi
fi
}


#User management function to get Input and work accordingly

user_mgmt()
{
echo "-------------------------------"
echo "**MAIN MENU**"
echo " 1. View System Metrics"
echo " 2. Monitor a specific service"
echo " 3. Exit"
echo " Please enter a valid option:"
echo "-------------------------------"
read option

case $option in
1)
# here we firstly display the metrics and wait for user input and again display metrics or menu
display_metric
while true; do
echo "Press Enter to continue & press 1 to for main menu"
read temp
if [ "$temp" == "1" ];
then
user_mgmt

else
display_metric
fi
done
;;
2)
read -p "Enter the name of the service: " service_name
check_service "$service_name"
user_mgmt
;;
3)
echo "Thanks for Visiting...."
sleep 5
exit
;;
*)
echo "please enter valid option"
user_mgmt
;;
esac
}

user_mgmt
54 changes: 54 additions & 0 deletions Challenges/Day_4/monitoring_pro.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#Author : Apurv Samadder
#Date : Aug 3 2023
#Version: v1
#Day 4 Bash Scripting Challenge - he script will check if the Nginx process is running and restart it if it's not.


#!/bin/bash

restart_serv(){
serv_active_restart=`sudo systemctl is-active $1`
case "$serv_active_restart" in
active)
#this will get printed in the 2nd or 3rd try if it wasn't running in the 1st or 2nd try
echo "Process $1 is running."
;;
failed|inactive)
echo "Process $1 is not running. Attempting to restart..."
#this will restart the process.
sudo systemctl restart $1
if [ systemctl is-active "$1" == true ] ;
then
echo "Service is started"
fi
;;
*)
esac
}

# Function to check and manage service
check_service() {
service_name="$1"

# Check if the service is running
if systemctl is-active --quiet "$service_name"; then
echo "**********************************"
echo "$service_name is running."
else
echo "**********************************"
echo "$service_name is not running."
read -p "Do you want to start $service_name? (y/n): " choice
if [[ "$choice" =~ ^[Yy]$ ]]; then
for i in {1..3}
do
restart_serv $service_name
sleep 2
done
else
echo "********************************"
echo "Service not started."
fi
fi
}
read -p "Enter the name of the service: " service_name
check_service "$service_name"