2

Based on this question, I would like to log the performance of a specific process, with a frequency of say one second, to a csv (comma separated value) log file.

Something like:

timestamp(unix),cpu_activity(%),mem_usage(B),network_activity(B)
1355407327,24.6,7451518,345
1355407328,27.6,7451535,12
1355407329,31.6,7451789,467
...
  • what value you want in network_activity ? – Rahul Patil Dec 13 '12 at 14:38
  • Oh, yeah it should probably be network_rx, and network_tx or so... – Theodor Dec 14 '12 at 8:29
1

i tried to get rx_bytes and tx_bytes but no luck, other things is working.. So you can use below script for the same

#!/bin/bash

# /sys/class/net/eth0/statistics/rx_bytes
# /sys/class/net/eth0/statistics/tx_bytes

Process="$1"

[[ -z $2 ]] && InterVal=1 || InterVal=$2

show_help() {
cat <<_EOF
Usage :
        $0  <ProcessName>  <Interval (Default 1s)>
_EOF
}

Show_Process_Stats() {
pgrep "${Process}" >/dev/null 2>&1 || { echo "Error: Process($1) it not Running.."; exit 1;};
while :
do
    # timestamp(unix),cpu_activity(%),mem_usage(B),network_activity(B)
    timestamp=$(date +%s)
    read cpu_activty mem_usage < <( ps --no-headers -o %cpu,rssize  -C "${Process}" )   
    echo "${timestamp}","${cpu_activty}","${mem_usage}"
    sleep $InterVal
done
}


Main() {
case $1 in
        ""|-h|--help)
                      show_help
                      ;;
                     *)
                      Show_Process_Stats
                      ;;
esac                      
}


Main $*
  • Great job! Of course, ps is the way to go. – Theodor Dec 15 '12 at 10:18
  • I'll get back to you if I figure out the network tx, rx – Theodor Dec 15 '12 at 11:12
0

what calculation should do with rx and tx ? i tried below things , please check and let us know what you want to accomplish.

#!/bin/bash

# /sys/class/net/eth0/statistics/rx_bytes
# /sys/class/net/eth0/statistics/tx_bytes

Process="$1"

[[ -z $2 ]] && InterVal=1 || InterVal=$2

show_help() {
cat <<_EOF
Usage :
                $0  <ProcessName>  <Interval (Default 1s)>
_EOF
}

Show_Process_Stats() {
pgrep "${Process}" >/dev/null 2>&1 || { echo "Error: Process($1) it not Running.."; exit 1;};
old_rx=0
old_tx=0
echo "timestamp,cpu_activty,mem_usage,rx_bytes,tx_bytes"
while :
do
        # timestamp(unix),cpu_activity(%),mem_usage(B),network_activity(B)
        timestamp=$(date +%s)
        raw_rx_bytes=$(< /sys/class/net/eth0/statistics/rx_bytes )
        raw_tx_bytes=$(< /sys/class/net/eth0/statistics/tx_bytes )
        rx_bytes=$(( $raw_rx_bytes - $old_rx ))
        tx_bytes=$(( $raw_tx_bytes - $old_tx ))
        read cpu_activty mem_usage < <( ps --no-headers -o %cpu,rssize  -C "${Process}" )
        echo "${timestamp}","${cpu_activty}","${mem_usage}","${rx_bytes}","${tx_bytes}"
        old_rx=${raw_rx_bytes}
        old_tx=${raw_tx_bytes}
        sleep $InterVal
done
}


Main() {
case $1 in
                ""|-h|--help)
                                          show_help
                                          ;;
                                         *)
                                          Show_Process_Stats
                                          ;;
esac
}


Main $*

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.