Sample Header Ad - 728x90

(Solaris) Ram CPU monitoring script is grabbing incorrect cpu ram utilization values

-1 votes
1 answer
37 views
#!/bin/bash

host=$(hostname)
email="abc.@xyz.com"  # Change to your desired email
subject="Attention!!! Health check Failed on $host"
echo $(date)

# CPU use threshold
cpu_threshold=0

# Memory idle threshold
mem_threshold=0

#--- CPU
cpu_usage () {
    # Get CPU idle percentage
    cpu_idle=$(prstat -Z 1 1 | awk 'NR==2 {print $8}' | tr -d '%')
    
    # Check if cpu_idle is a valid number
    if ! [[ "$cpu_idle" =~ ^[0-9]+$ ]]; then
        echo "Error: Invalid CPU idle value: $cpu_idle"
        cpu_use=0
    else
        cpu_use=$((100 - cpu_idle))
    fi

    cpu_flag=0
    echo "CPU utilization: $cpu_use%"
    if [ "$cpu_use" -gt "$cpu_threshold" ]; then
        echo "CPU warning!!!"
        cpu_flag=1
    else
        echo "CPU ok!!!"
    fi
}

#--- Memory
mem_usage () {
    mem_total=$(kstat -m | grep "physmem" | awk '{print $2}')  # Total memory in bytes
    mem_free=$(kstat -m | grep "freemem" | awk '{print $2}')   # Free memory in bytes

    if [[ -z "$mem_total" || "$mem_total" -eq 0 ]]; then
        echo "Failed to retrieve total memory."
        mem_total=0
        mem_free=0
    fi

    # Convert bytes to GB
    mem_total_gb=$((mem_total / 1024 / 1024))
    mem_free_gb=$((mem_free / 1024 / 1024))

    echo "Total memory: $mem_total_gb GB"
    echo "Free memory: $mem_free_gb GB"

    if [ "$mem_total" -gt 0 ]; then
        per_mem=$(( ((mem_total - mem_free)) * 100 / mem_total ))
    else
        per_mem=0
    fi

    echo "Memory space remaining: $mem_free_gb GB"
    mem_flag=0
    if [ "$per_mem" -gt "$mem_threshold" ]; then
        echo "Memory warning!!!"
        mem_flag=1
    else
        echo "Memory ok!!!"
    fi
}

out() {
    if [ "$cpu_flag" -eq 1 ] && [ "$mem_flag" -eq 1 ]; then 
        printf "
Hello Team,

Please check RAM and CPU utilization on $host.
Current RAM Percentage: $per_mem%%
Current CPU Percentage: $cpu_use%%

Thanks " > /tmp/health.txt
    elif [ "$cpu_flag" -eq 1 ]; then 
        printf "
Hello Team,

Please check CPU utilization on $host.
Current CPU Percentage: $cpu_use%%

Thanks " > /tmp/health.txt
    elif [ "$mem_flag" -eq 1 ]; then 
        printf "
Hello Team,

Please check RAM utilization on $host.
Current RAM Percentage: $per_mem%%

Thanks " > /tmp/health.txt
    fi
}

mail() {
    if [ "$cpu_flag" -eq 1 ] || [ "$mem_flag" -eq 1 ]; then
        /usr/sbin/sendmail -t <
It gives the following output. Despite using kstat, it throws this error. What am I doing wrong here?
Fri Oct 11 06:51:21 CDT 2024
Error: Invalid CPU idle value: 17:25:22
CPU utilization: 0%
CPU ok!!!
Usage:
kstat [ -qlp ] [ -T d|u ] [ -c class ]
      [ -m module ] [ -i instance ] [ -n name ] [ -s statistic ]
      [ interval [ count ] ]
kstat [ -qlp ] [ -T d|u ] [ -c class ]
      [ module:instance:name:statistic ... ]
      [ interval [ count ] ]
Usage:
kstat [ -qlp ] [ -T d|u ] [ -c class ]
      [ -m module ] [ -i instance ] [ -n name ] [ -s statistic ]
      [ interval [ count ] ]
kstat [ -qlp ] [ -T d|u ] [ -c class ]
      [ module:instance:name:statistic ... ]
      [ interval [ count ] ]
Failed to retrieve total memory.
Total memory: 0 GB
Free memory: 0 GB
Memory space remaining: 0 GB
Memory ok!!!
The CPU utilization is getting grabbed correctly now, only the memory usage is not reported correctly. I am not sure which module to use with kstat command, so I used prtconf as well but that didn't capture it either.
Asked by Navdeep Singh (37 rep)
Oct 11, 2024, 11:59 AM
Last activity: Oct 11, 2024, 01:02 PM