Saturday, March 16, 2024

.::: Script to check cpu & memory usage service in linux include custome service, mariadb, mysql, mongodb :::.


1. check custome cpu usage and memory usage


[root@teguhth special]# cat custom_service.sh

#!/bin/bash

echo "run custom_service.sh <name_service>"
PIDX=${1}
# Mendapatkan PID dari proses mariadbd
MARIADB_PID=$(pgrep $PIDX)

# Mendapatkan informasi tentang proses mariadbd dari top
TOP_INFO=$(top -n 1 -b -p "$MARIADB_PID" | tail -n +8 | head -n 1)
Mem_Total_mb=$(free -m | awk 'NR==2{printf "%.f", $2 }')
Mem_Total_gb=$(free -m | awk 'NR==2{printf "%.2f", $2/1024 }')

# Mengekstrak nilai CPU dan penggunaan memori dari output top
CPU_USAGE=$(echo "$TOP_INFO" | awk '{print $9}')
MEMORY_USAGE=$(echo "$TOP_INFO" | awk '{print $10}')
MEMORY_B=$(echo "$TOP_INFO" | awk '{print $6 }')

echo "CPU Usage(%) $PIDX   : ${CPU_USAGE}%"
echo "Memory Usage(%) $PIDX: ${MEMORY_USAGE}%"
echo "Memory Usage $PIDX   : ${MEMORY_B}"
echo "Memory Total Server   : $Mem_Total_mb MB or $Mem_Total_gb GB"

[root@teguhth special]#


2. run custom_service.sh

sh custom_service.sh mariadb
sh custom_service.sh mongod
 

3. check cpu usage mariadb mysql

[root@teguhth special]# cat maria_usage.sh

#!/bin/bash

# Mendapatkan PID dari proses mariadbd
MARIADB_PID=$(pgrep 'mariadbd|mysqld|mysql|mysqld')

# Mendapatkan informasi tentang proses mariadbd dari top
TOP_INFO=$(top -n 1 -b -p "$MARIADB_PID" | tail -n +8 | head -n 1)
Mem_Total_mb=$(free -m | awk 'NR==2{printf "%.f", $2 }')
Mem_Total_gb=$(free -m | awk 'NR==2{printf "%.2f", $2/1024 }')

# Mengekstrak nilai CPU dan penggunaan memori dari output top
CPU_USAGE=$(echo "$TOP_INFO" | awk '{print $9}')
MEMORY_USAGE=$(echo "$TOP_INFO" | awk '{print $10}')
MEMORY_B=$(echo "$TOP_INFO" | awk '{print $6 }')

echo "CPU Usage(%) MariaDB/MySQL   : ${CPU_USAGE}%"
echo "Memory Usage(%) MariaDB/MySQL: ${MEMORY_USAGE}%"
echo "Memory Usage MariaDB/MySQL   : ${MEMORY_B}"
echo "Memory Total Server          : $Mem_Total_mb MB or $Mem_Total_gb GB"
[root@teguhth special]#

 

4. check cpu usage mongodb

[root@teguhth special]# cat mongo_usage.sh

#!/bin/bash

# Mendapatkan PID dari proses mariadbd
MARIADB_PID=$(pgrep 'mongo|mongod')

# Mendapatkan informasi tentang proses mariadbd dari top
TOP_INFO=$(top -n 1 -b -p "$MARIADB_PID" | tail -n +8 | head -n 1)
Mem_Total_mb=$(free -m | awk 'NR==2{printf "%.f", $2 }')
Mem_Total_gb=$(free -m | awk 'NR==2{printf "%.2f", $2/1024 }')

# Mengekstrak nilai CPU dan penggunaan memori dari output top
CPU_USAGE=$(echo "$TOP_INFO" | awk '{print $9}')
MEMORY_USAGE=$(echo "$TOP_INFO" | awk '{print $10}')
MEMORY_B=$(echo "$TOP_INFO" | awk '{print $6 }')

echo "CPU Usage(%) MongoDB   : ${CPU_USAGE}%"
echo "Memory Usage(%) MongoDB: ${MEMORY_USAGE}%"
echo "Memory Usage MongoDB   : ${MEMORY_B}"
echo "Memory Total Server    : $Mem_Total_mb MB or $Mem_Total_gb GB"
[root@teguhth special]#
 


No comments:

Post a Comment

Popular Posts