Last active
December 11, 2015 17:48
-
-
Save hamstar/4637461 to your computer and use it in GitHub Desktop.
print out the mem and cpu used by a process (or multiple processes with the same name) for munin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| function get_cpu_usage() { | |
| cpu=`ps auwx|grep "$1"|grep -v 'grep'|tr -s ' ' ' ' | cut -d' ' -f3|awk '{s+=$1}END{print s}' `; | |
| } | |
| function get_mem_usage() { | |
| mem=`ps auwx|grep "$1"|grep -v 'grep'|tr -s ' ' ' ' | cut -d' ' -f4|awk '{s+=$1}END{print s}' `; | |
| } | |
| function get_io_usage() { | |
| iotmp="/tmp/$1.io"; | |
| pids=`ps auwx|grep "$1"|grep -v 'grep'|tr -s ' ' ' ' | cut -d' ' -f2`; | |
| echo -n "" > $iotmp; | |
| for pid in $pids; do | |
| pidstat -d -p $pid 1 1|grep Average|tr -s ' ' ' ' |cut -d' ' -f3-4 >> $iotmp; | |
| done; | |
| kbrs=`cat $iotmp|cut -d' ' -f1|awk '{s+=$1}END{print s}'`; | |
| kbws=`cat $iotmp|cut -d' ' -f2|awk '{s+=$1}END{print s}'`; | |
| } | |
| function print_values() { | |
| echo "mem.value $mem"; | |
| echo "cpu.value $cpu"; | |
| echo "kbrs.value $kbrs"; | |
| echo "kbws.value $kbws"; | |
| } | |
| function print_fields() { | |
| echo graph_args -l 0 | |
| echo graph_category system | |
| echo graph_title Process Statistics; | |
| echo graph_info Information on the process | |
| echo graph_vlabel; | |
| echo mem.label Memory %; | |
| echo cpu.label CPU %; | |
| echo kbrs.label IO Read kB/s; | |
| echo kbrs.label IO Write kB/s; | |
| } | |
| case $1 in | |
| config) | |
| print_fields; | |
| exit 0;; | |
| esac | |
| get_cpu_usage apache2 | |
| get_mem_usage apache2 | |
| get_io_usage apache2 | |
| print_values; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment