#!/bin/bash
Hertz=$(/usr/bin/getconf CLK_TCK)
pid=$(ps -e | grep "\<binlogdev_idx\>" | awk '{print $1}')
if [ -z "${pid}" ]; then
    echo "`date`: process binlogdev_idx is not exist" >> $LOG
    exit 0
elif [[ ! -d /proc/${pid} ]]; then
    echo "`date`: /proc/${pid} is not a dict"
    exit 1
fi
sys_uptime_0=$(cat /proc/uptime | awk '{print $1}')
spent_time_0=$(cat /proc/${pid}/stat | awk '{print $14+$15+$16+$17}')

sleep 1
sys_uptime_1=$(cat /proc/uptime | awk '{print $1}')
spent_time_1=$(cat /proc/${pid}/stat | awk '{print $14+$15+$16+$17}')

## example: is 0.7000, not 70 for 70%
cpu_usage=$(echo "scale=4; ((${spent_time_1}-${spent_time_0})/${Hertz})/(${sys_uptime_1}-${sys_uptime_0})" | bc )

# example: is 70 for 70%
cpu_usage_percent=$(echo "${spent_time_1} ${spent_time_0} ${Hertz} ${sys_uptime_1} ${sys_uptime_0}"|awk '{ printf "%0.2f\n", ($1-$2)/$3/($4-$5)*100}')

status='[{"value":'$cpu_usage_percent', "tags":"pid='$pid'"}]'
echo -n "$status"

