From c2b123c2cedca2f6a07ec9f3b2708fd8addcde6b Mon Sep 17 00:00:00 2001 From: ApurvDevops Date: Wed, 2 Aug 2023 20:47:56 +0200 Subject: [PATCH 1/3] Day 3 challenge --- Challenges/Day_3/user_mamagement.sh | 127 ++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100755 Challenges/Day_3/user_mamagement.sh diff --git a/Challenges/Day_3/user_mamagement.sh b/Challenges/Day_3/user_mamagement.sh new file mode 100755 index 0000000..358817e --- /dev/null +++ b/Challenges/Day_3/user_mamagement.sh @@ -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 + + From 54a44e08fc54cf8be42e7ecb9fc37bc069925f3e Mon Sep 17 00:00:00 2001 From: ApurvDevops Date: Thu, 3 Aug 2023 21:38:17 +0200 Subject: [PATCH 2/3] Adding Day_4 Tassk1_ monitoring_metric.sh --- Challenges/Day_4/monitoring_metric.sh | 105 ++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100755 Challenges/Day_4/monitoring_metric.sh diff --git a/Challenges/Day_4/monitoring_metric.sh b/Challenges/Day_4/monitoring_metric.sh new file mode 100755 index 0000000..04ce911 --- /dev/null +++ b/Challenges/Day_4/monitoring_metric.sh @@ -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 From f7fbb2be8665154c9cf9107b252199b76ee004a8 Mon Sep 17 00:00:00 2001 From: ApurvDevops Date: Thu, 3 Aug 2023 21:38:42 +0200 Subject: [PATCH 3/3] Adding Day_4 Tassk2_ monitoring_.sh --- Challenges/Day_4/monitoring_pro.sh | 54 ++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100755 Challenges/Day_4/monitoring_pro.sh diff --git a/Challenges/Day_4/monitoring_pro.sh b/Challenges/Day_4/monitoring_pro.sh new file mode 100755 index 0000000..2fa0c7b --- /dev/null +++ b/Challenges/Day_4/monitoring_pro.sh @@ -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"