Tuesday, January 23, 2024

.::: Shell Script to Get CPU Memory Usage (%), Swap (%) & Local Check, all_check GTID Mirroring,IP & Hostname,uptime MariaDB in Centos 9 Redhat 9 Rocky Linux 9 :::.

 1. local check

##[root@teguhth data]# cat local_check.sh
#  cat check_cpu_mem_usage.sh
#!/bin/bash
date
CPU=$(top -bn1 | grep load | awk '{printf "%.2f%%\t\t\n", $(NF-2)}')

Mem_Used_mb=$(free -m | awk 'NR==2{printf "%.f", $3 }')
Mem_Total_mb=$(free -m | awk 'NR==2{printf "%.f", $2 }')

Mem_Used_kb=$(free -m | awk 'NR==2{printf "%.f", $3*1024 }')
Mem_Total_kb=$(free -m | awk 'NR==2{printf "%.f", $2*1024 }')

Mem_Used_gb=$(free -m | awk 'NR==2{printf "%.f", $3/1024 }')
Mem_Total_gb=$(free -m | awk 'NR==2{printf "%.f", $2/1024 }')

Mem_Used_percent=$(free -m | awk 'NR==2{printf "%.2f%%\t\t", $3*100/$2 }')

Mem_swap_used_mb=$(free -m | awk 'NR==3{printf "%.f", $3 }')
Mem_swap_total_mb=$(free -m | awk 'NR==3{printf "%.f", $2 }')

Mem_swap_used_kb=$(free -m | awk 'NR==3{printf "%.f", $3*1024 }')
Mem_swap_total_kb=$(free -m | awk 'NR==3{printf "%.f", $2*1024 }')

Mem_swap_used_gb=$(free -m | awk 'NR==3{printf "%.f", $3/1024 }')
Mem_swap_total_gb=$(free -m | awk 'NR==3{printf "%.f", $2/1024 }')

Mem_swap_percent=$(free -m | awk 'NR==3{printf "%.2f%%\t\t", $3*100/$2 }')

Server=$(hostname)

#!/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


# 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"
echo "Memory Usage (%)       : $Mem_Used_percent"
echo "Swap Usage(%)          : $Mem_swap_percent"
echo "Used Memory            : $Mem_Used_gb in GB, $Mem_Used_mb in MB, $Mem_Used_kb in KB"
echo "Total Memory           : $Mem_Total_gb in GB, $Mem_Total_mb in MB,$Mem_Total_kb in KB"
echo "Used Swap              : $Mem_swap_used_gb in GB, $Mem_swap_used_mb in MB, $Mem_swap_used_kb in KB"
echo "Total Swap             : $Mem_swap_total_gb in GB, $Mem_swap_total_mb in MB,$Mem_swap_total_kb in KB"
echo ""
echo "disk info"
df -h
echo ""
echo "Copyright by           : Teguh Triharto"
echo "Website                : https://www.linkedin.com/in/teguhth"
echo ""

grep -E '^(VERSION|NAME)=' /etc/os-release

##[root@teguhth data]#
 


2. all check include 

#!/bin/bash
date
CPU=$(top -bn1 | grep load | awk '{printf "%.2f%%\t\t\n", $(NF-2)}')

Mem_Used_mb=$(free -m | awk 'NR==2{printf "%.f", $3 }')
Mem_Total_mb=$(free -m | awk 'NR==2{printf "%.f", $2 }')

Mem_Used_kb=$(free -m | awk 'NR==2{printf "%.f", $3*1024 }')
Mem_Total_kb=$(free -m | awk 'NR==2{printf "%.f", $2*1024 }')

Mem_Used_gb=$(free -m | awk 'NR==2{printf "%.f", $3/1024 }')
Mem_Total_gb=$(free -m | awk 'NR==2{printf "%.f", $2/1024 }')

Mem_Used_percent=$(free -m | awk 'NR==2{printf "%.2f%%\t\t", $3*100/$2 }')

Mem_swap_used_mb=$(free -m | awk 'NR==3{printf "%.f", $2 }')
Mem_swap_total_mb=$(free -m | awk 'NR==3{printf "%.f", $3 }')

Mem_swap_used_kb=$(free -m | awk 'NR==3{printf "%.f", $2*1024 }')
Mem_swap_total_kb=$(free -m | awk 'NR==3{printf "%.f", $3*1024 }')

Mem_swap_used_gb=$(free -m | awk 'NR==3{printf "%.f", $2/1024 }')
Mem_swap_total_gb=$(free -m | awk 'NR==3{printf "%.f", $3/1024 }')

Mem_swap_percent=$(free -m | awk 'NR==3{printf "%.2f%%\t\t", $3*100/$2 }')

Server=$(hostname)


#!/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"
echo "Memory Usage (%)       : $Mem_Used_percent"
echo "Swap Usage(%)          : $Mem_swap_percent"
echo "Used Memory            : $Mem_Used_gb in GB, $Mem_Used_mb in MB, $Mem_Used_kb in KB"
echo "Total Memory           : $Mem_Total_gb in GB, $Mem_Total_mb in MB,$Mem_Total_kb in KB"
echo "Total Swap             : $Mem_swap_total_gb in GB, $Mem_swap_total_mb in MB,$Mem_swap_total_kb in KB"
echo "Used Swap              : $Mem_swap_used_gb in GB, $Mem_swap_used_mb in MB, $Mem_swap_used_kb in 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