Thursday, August 10, 2023

.::: Shell Script to Get CPU Memory Usage (%), Swap (%) & Local Check, all_check GTID Mirroring,IP & Hostname,uptime MariaDB in Unix, Linux & Solaris 10 from Local & Remote :::.

A. From Local
 
 1. create script to create cpu memory

[root@teguhth ~]# cat check_cpu_mem_usage.sh
#!/bin/bash

# Get CPU and Memory usage using top command
TOP_OUTPUT=$(top -b -n 1)

# Extract CPU usage percentage
CPU_USAGE=$(echo "$TOP_OUTPUT" | awk '/Cpu/ {print $2}')

# Extract total memory and used memory in kilobytes
TOTAL_MEMORY_KB=$(echo "$TOP_OUTPUT" | awk '/KiB Mem/ {print $4}')
USED_MEMORY_KB=$(echo "$TOP_OUTPUT" | awk '/KiB Mem/ {print $8}')

# Convert kilobytes to megabytes
TOTAL_MEMORY_MB=$(echo "scale=2; $TOTAL_MEMORY_KB / 1024" | bc)
USED_MEMORY_MB=$(echo "scale=2; $USED_MEMORY_KB / 1024" | bc)
# Extract memory usage percentage
MEMORY_USAGE=$(echo "$TOP_OUTPUT" | awk '/KiB Mem/ {print ($8 / $4) * 100}' | awk '{printf "%.2f", $0}')

echo "CPU Usage (%): $CPU_USAGE%"
echo "Memory Usage (%): ${MEMORY_USAGE}%"
echo "Used Memory: ${USED_MEMORY_MB}MB"
echo "Total Memory: ${TOTAL_MEMORY_MB}MB"


[root@teguhth ~]#

2. run query
[root@teguhth ~]# sh check_cpu_mem_usage.sh
Result checking on server with hostname < teguhth >
CPU Usage (%): 6.2%
Memory Usage (%): 23.08%
Used Memory: 224.40MB
Total Memory: 972.33MB
[root@teguhth ~]#
 


B. From Remote
1. enable keygen
 
ssh-keygen -t rsa
ssh-copy-id -i ~/.ssh/id_rsa root@10.10.10.8
 
2. create script to create cpu memory

 
[root@teguhth ~]# cat remote.sh
#!/bin/bash

# Get CPU and Memory usage using top command
TOP_OUTPUT=$(top -b -n 1)
Server=$(hostname)

# Extract CPU usage percentage
CPU_USAGE=$(echo "$TOP_OUTPUT" | awk '/Cpu/ {print $2}')

# Extract total memory and used memory in kilobytes
TOTAL_MEMORY_KB=$(echo "$TOP_OUTPUT" | awk '/KiB Mem/ {print $4}')
USED_MEMORY_KB=$(echo "$TOP_OUTPUT" | awk '/KiB Mem/ {print $8}')

# Convert kilobytes to megabytes
TOTAL_MEMORY_MB=$(echo "scale=2; $TOTAL_MEMORY_KB / 1024" | bc)
USED_MEMORY_MB=$(echo "scale=2; $USED_MEMORY_KB / 1024" | bc)
# Extract memory usage percentage
MEMORY_USAGE=$(echo "$TOP_OUTPUT" | awk '/KiB Mem/ {print ($8 / $4) * 100}' | awk '{printf "%.2f", $0}')

echo "Checking from Source $Server"
#echo "CPU Usage (%): $CPU_USAGE%"
#echo "Memory Usage (%): ${MEMORY_USAGE}%"
#echo "Used Memory: ${USED_MEMORY_MB}MB"
#echo "Total Memory: ${TOTAL_MEMORY_MB}MB"



echo "sh remote.sh <ip> <port>"
#!/bin/bash
# Remote machine details
remote_user="root"
remote_host=${1}
remote_password="root" # It's better to use SSH keys for authentication

# Function to get remote CPU and memory usage
remote_usage () {
     ssh "$remote_user@$remote_host" \
        "echo 'Result checking on Destination server with hostname < $Server >';\
echo 'CPU Usage (%): $CPU_USAGE''%' ; \
echo 'Memory Usage (%): ${MEMORY_USAGE}%';\
         echo 'Memory Usage: ' \$(free -m | grep 'Mem:' | awk '{print \$3}')'MB' ;\
         echo 'Memory Total: ' \$(free -m | grep 'Mem:' | awk '{print \$2}')'MB'"
}

remote_usage
[root@teguhth ~]#
 
3. Test
 

 
C. From Remote using custom port
[root@teguhth ~]# cat custom_check_cpu_mem_specifiport.sh
#!/bin/bash

# Get CPU and Memory usage using top command
TOP_OUTPUT=$(top -b -n 1)
Server=$(hostname)

# Extract CPU usage percentage
CPU_USAGE=$(echo "$TOP_OUTPUT" | awk '/Cpu/ {print $2}')

# Extract total memory and used memory in kilobytes
TOTAL_MEMORY_KB=$(echo "$TOP_OUTPUT" | awk '/KiB Mem/ {print $4}')
USED_MEMORY_KB=$(echo "$TOP_OUTPUT" | awk '/KiB Mem/ {print $8}')

# Convert kilobytes to megabytes
TOTAL_MEMORY_MB=$(echo "scale=2; $TOTAL_MEMORY_KB / 1024" | bc)
USED_MEMORY_MB=$(echo "scale=2; $USED_MEMORY_KB / 1024" | bc)
# Extract memory usage percentage
MEMORY_USAGE=$(echo "$TOP_OUTPUT" | awk '/KiB Mem/ {print ($8 / $4) * 100}' | awk '{printf "%.2f", $0}')

echo "Checking from Source $Server"
#echo "CPU Usage (%): $CPU_USAGE%"
#echo "Memory Usage (%): ${MEMORY_USAGE}%"
#echo "Used Memory: ${USED_MEMORY_MB}MB"
#echo "Total Memory: ${TOTAL_MEMORY_MB}MB"



echo "sh remote.sh <ip> <port>"
#!/bin/bash
# Remote machine details
remote_user="root"
remote_host=${1}
portx=${2}
remote_password="root" # It's better to use SSH keys for authentication

# Function to get remote CPU and memory usage
remote_usage () {
     ssh "$remote_user@$remote_host" -p$portx \
        "echo 'Result checking on Destination server with hostname < $Server >';\
echo 'CPU Usage (%): $CPU_USAGE''%' ; \
echo 'Memory Usage (%): ${MEMORY_USAGE}%';\
         echo 'Memory Usage: ' \$(free -m | grep 'Mem:' | awk '{print \$3}')'MB' ;\
         echo 'Memory Total: ' \$(free -m | grep 'Mem:' | awk '{print \$2}')'MB'"
}

remote_usage
[root@teguhth ~]#

2. test
 

D. Local All check

[root@teguhth ~]# cat local_check.sh
#  cat check_cpu_mem_usage.sh
#!/bin/bash

# Get CPU and Memory usage using top command
TOP_OUTPUT=$(top -b -n 1)

Server=$(hostname)
# Extract CPU usage percentage
CPU_USAGE=$(echo "$TOP_OUTPUT" | awk '/Cpu/ {print $2}')

# Extract total memory and used memory in kilobytes
TOTAL_MEMORY_KB=$(echo "$TOP_OUTPUT" | awk '/KiB Mem/ {print $4}')
USED_MEMORY_KB=$(echo "$TOP_OUTPUT" | awk '/KiB Mem/ {print $8}')

# Convert kilobytes to megabytes
TOTAL_MEMORY_MB=$(echo "scale=2; $TOTAL_MEMORY_KB / 1024" | bc)
USED_MEMORY_MB=$(echo "scale=2; $USED_MEMORY_KB / 1024" | bc)

# Convert MB to GB
#TOTAL_MEMORY_GB=$(echo "scale=2;$TOTAL_MEMORY_KB / 1024 / 1024" | bc)
#USED_MEMORY_GB=$(($USED_MEMORY_KB / 1024 / 1024) | bc)
USED_MEMORY_GB=$(awk "BEGIN {printf \"%.2f\", ($USED_MEMORY_KB / 1024 / 1024)}")
TOTAL_MEMORY_GB=$(awk "BEGIN {printf \"%.2f\", ($TOTAL_MEMORY_KB / 1024 / 1024)}")

# Extract memory usage percentage
MEMORY_USAGE=$(echo "$TOP_OUTPUT" | awk '/KiB Mem/ {print ($8 / $4) * 100}' | awk '{printf "%.2f", $0}')


#!/bin/bash

#!/bin/bash
# Get Hostname
#server=$(hostname)
# Get the IP address using ifconfig, excluding the loopback interface
ip_address=$(ifconfig | awk '/inet / && $2 !~ /^127\./ {gsub("addr:","",$2); print $2}')
ip_address2=$(ip a | awk '/inet / && $2 !~ /^127\./ {gsub("addr:","",$2); print $2}')
# Print the IP address



# Menggunakan perintah 'free' untuk mendapatkan informasi tentang swap memory
swap_info=$(free -k | grep Swap)

# Memisahkan kolom-kolom dalam output
total_swap=$(echo $swap_info | awk '{print $2}')
used_swap=$(echo $swap_info | awk '{print $3}')
free_swap=$(echo $swap_info | awk '{print $4}')

# Menghitung persentase penggunaan swap
swap_percentage=$(awk "BEGIN {printf \"%.2f\", ($used_swap / $total_swap) * 100}")
usage_swapmb=$(awk "BEGIN {printf \"%.2f\", ($used_swap / 1024) }")
total_swapmb=$(awk "BEGIN {printf \"%.2f\", ($total_swap / 1024) }")
free_swapmb=$(awk "BEGIN {printf \"%.2f\", ($free_swap / 1024) }")

usage_swapgb=$(awk "BEGIN {printf \"%.2f\", ($used_swap / 1024/1024) }")
total_swapgb=$(awk "BEGIN {printf \"%.2f\", ($total_swap / 1024/1024) }")
free_swapgb=$(awk "BEGIN {printf \"%.2f\", ($free_swap / 1024/1024) }")

# Menampilkan informasi dalam KB dan persentase
echo ""
echo ".::: Result checking on server with hostname << $Server >> & << $ip_address2 >> :::."
echo ""
echo "Hostname Server        : $Server"
echo "IP Address(ifconfig)   : $ip_address"
echo "IP Address(ip a)       : $ip_address2"
echo "CPU Usage (%)          : $CPU_USAGE%"
echo "Memory Usage (%)       : ${MEMORY_USAGE}%"
echo "Swap Usage(%)          : $swap_percentage%"
echo "Used Memory            : ${USED_MEMORY_GB} GB,${USED_MEMORY_MB} MB,${USED_MEMORY_KB} KB"
echo "Total Memory           : ${TOTAL_MEMORY_GB} GB,${TOTAL_MEMORY_MB} MB,${TOTAL_MEMORY_KB} KB"
echo "Total Swap             : $total_swapgb GB,$total_swapmb MB,$total_swap KB"
echo "Used Swap              : $usage_swapgb GB,$usage_swapmb MB,$used_swap KB"
echo "Free Swap              : $free_swapgb GB,$free_swapmb MB,$free_swap KB"
echo ""
echo "disk info"
df -h
echo ""
echo "Copyright by           : Teguh Triharto"
echo "Website                : https://www.linkedin.com/in/teguhth"
echo ""

[root@teguhth ~]#

 


E. Get IP address

[root@teguhth ~]# cat ipserver.sh
#!/bin/bash
# Get Hostname
Server=$(hostname)
# Get the IP address using ifconfig, excluding the loopback interface
ip_address=$(ifconfig | awk '/inet / && $2 !~ /^127\./ {gsub("addr:","",$2); print $2}')
ip_address2=$(ip a | awk '/inet / && $2 !~ /^127\./ {gsub("addr:","",$2); print $2}')
# Print the IP address


echo ""
echo ".::: Result checking on server with hostname << $Server >> & << $ip_address2 >> :::."
echo ""
echo "Hostname Server        : $Server"
echo "IP Address(ifconfig)   : $ip_address"
echo "IP Address(ip a)       : $ip_address2"
echo ""

[root@teguhth ~]#
 
 
F. all check for mirroritng <gtid>
 
#!/bin/bash
# Get CPU and Memory usage using top command
TOP_OUTPUT=$(top -b -n 1)

# Extract CPU usage percentage
CPU_USAGE=$(echo "$TOP_OUTPUT" | awk '/Cpu/ {print $2}')

# Extract total memory and used memory in kilobytes
TOTAL_MEMORY_KB=$(echo "$TOP_OUTPUT" | awk '/KiB Mem/ {print $4}')
USED_MEMORY_KB=$(echo "$TOP_OUTPUT" | awk '/KiB Mem/ {print $8}')

# Convert kilobytes to megabytes
TOTAL_MEMORY_MB=$(echo "scale=2; $TOTAL_MEMORY_KB / 1024" | bc)
USED_MEMORY_MB=$(echo "scale=2; $USED_MEMORY_KB / 1024" | bc)
# Extract memory usage percentage
MEMORY_USAGE=$(echo "$TOP_OUTPUT" | awk '/KiB Mem/ {print ($8 / $4) * 100}' | awk '{printf "%.2f", $0}')

# Convert MB to GB
#TOTAL_MEMORY_GB=$(echo "scale=2;$TOTAL_MEMORY_KB / 1024 / 1024" | bc)
#USED_MEMORY_GB=$(($USED_MEMORY_KB / 1024 / 1024) | bc)
USED_MEMORY_GB=$(awk "BEGIN {printf \"%.2f\", ($USED_MEMORY_KB / 1024 / 1024)}")
TOTAL_MEMORY_GB=$(awk "BEGIN {printf \"%.2f\", ($TOTAL_MEMORY_KB / 1024 / 1024)}")


#!/bin/bash

# Menggunakan perintah 'free' untuk mendapatkan informasi tentang swap memory
swap_info=$(free -k | grep Swap)

# Memisahkan kolom-kolom dalam output
total_swap=$(echo $swap_info | awk '{print $2}')
used_swap=$(echo $swap_info | awk '{print $3}')
free_swap=$(echo $swap_info | awk '{print $4}')

# Menghitung persentase penggunaan swap
swap_percentage=$(awk "BEGIN {printf \"%.2f\", ($used_swap / $total_swap) * 100}")
usage_swapmb=$(awk "BEGIN {printf \"%.2f\", ($used_swap / 1024) }")
total_swapmb=$(awk "BEGIN {printf \"%.2f\", ($total_swap / 1024) }")
free_swapmb=$(awk "BEGIN {printf \"%.2f\", ($free_swap / 1024) }")

usage_swapgb=$(awk "BEGIN {printf \"%.2f\", ($used_swap / 1024/1024) }")
total_swapgb=$(awk "BEGIN {printf \"%.2f\", ($total_swap / 1024/1024) }")
free_swapgb=$(awk "BEGIN {printf \"%.2f\", ($free_swap / 1024/1024) }")

#!/bin/bash
# Get Hostname
Server=$(hostname)
# Get the IP address using ifconfig, excluding the loopback interface
ip_address=$(ifconfig | awk '/inet / && $2 !~ /^127\./ {gsub("addr:","",$2); print $2}')
ip_address2=$(ip a | awk '/inet / && $2 !~ /^127\./ {gsub("addr:","",$2); print $2}')
# Print the IP address

# Menampilkan informasi dalam KB dan persentase

# Menampilkan informasi dalam KB dan persentase
echo ""
echo ".::: Result checking on server with hostname << $Server >> & << $ip_address2 >> :::."
echo ""
echo "Hostname Server        : $Server"
echo "IP Address(ifconfig)   : $ip_address"
echo "IP Address(ip a)       : $ip_address2"
echo "CPU Usage (%)          : $CPU_USAGE%"
echo "Memory Usage (%)       : ${MEMORY_USAGE}%"
echo "Swap Usage(%)          : $swap_percentage%"
echo "Used Memory            : ${USED_MEMORY_GB} GB,${USED_MEMORY_MB} MB,${USED_MEMORY_KB} KB"
echo "Total Memory           : ${TOTAL_MEMORY_GB} GB,${TOTAL_MEMORY_MB} MB,${TOTAL_MEMORY_KB} KB"
echo "Total Swap             : $total_swapgb GB,$total_swapmb MB,$total_swap KB"
echo "Used Swap              : $usage_swapgb GB,$usage_swapmb MB,$used_swap KB"
echo "Free Swap              : $free_swapgb GB,$free_swapmb MB,$free_swap KB"
echo ""
echo "Check GTID Replication"

COUNTER=0

while [ $COUNTER -lt 2 ]; do
    let COUNTER=COUNTER+1

        TGL=$(date +%Y"-"%m"-"%d" "%H":"%M":"%S" ")
        CEK=`mysql -uroot -proot -e"show slave status \G;" | grep Seconds_Behind_Master `
        RESULT=$(echo $CEK | awk '{print $2}')
        if [[ $RESULT != "0" ]] ; then
                echo $TGL" --> $CEK " >> cek_rep.log
                #echo $TGL" --> Seconds_Behind_Master : "$CEK >> cek_rep.log
        fi

        echo "no: $COUNTER : $CEK "
sleep 1 #slepp or wait
done

echo ""
echo "Resume Processlist: "
mysql -uroot -proot -e "show status where variable_name like 'threads%'";

echo ""
echo "Uptime MariaDB"
mysql -uroot -proot -e "select @@hostname,@@version,now() - interval variable_value second as 'MySQL started on',TIME_FORMAT(SEC_TO_TIME(VARIABLE_VALUE ),'%Hh %im')  as Uptime from information_schema.global_status where variable_name='Uptime';";

echo ""
echo "disk info df -h"
df -h

echo ""
echo "maxscale info"
maxadmin list servers

echo ""
echo "tail -10 mysql log"
tail -10 /var/lib/mysql/mysql_error.log

echo ""
echo "Maxscale log"
tail -10 /var/log/maxscale/maxscale.log

 


No comments:

Post a Comment

Popular Posts