From 07fc2644a237187dd4c5680e88f4adadbf533603 Mon Sep 17 00:00:00 2001 From: Cody Hiar Date: Wed, 12 Aug 2015 12:06:23 -0600 Subject: Initial commit of the working files --- segments/cpu.sh | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100755 segments/cpu.sh (limited to 'segments/cpu.sh') diff --git a/segments/cpu.sh b/segments/cpu.sh new file mode 100755 index 0000000..637bcda --- /dev/null +++ b/segments/cpu.sh @@ -0,0 +1,22 @@ +# Prints the CPU usage: user% sys% idle. + +run_segment() { + if shell_is_linux; then + cpu_line=$(top -b -n 1 | grep "Cpu(s)" ) + cpu_user=$(echo "$cpu_line" | grep -Po "(\d+(.\d+)?)(?=%?\s?(us(er)?))") + cpu_system=$(echo "$cpu_line" | grep -Po "(\d+(.\d+)?)(?=%?\s?(sys?))") + cpu_idle=$(echo "$cpu_line" | grep -Po "(\d+(.\d+)?)(?=%?\s?(id(le)?))") + elif shell_is_osx; then + cpus_line=$(top -e -l 1 | grep "CPU usage:" | sed 's/CPU usage: //') + cpu_user=$(echo "$cpus_line" | awk '{print $1}' | sed 's/%//' ) + cpu_system=$(echo "$cpus_line" | awk '{print $3}'| sed 's/%//' ) + cpu_idle=$(echo "$cpus_line" | awk '{print $5}' | sed 's/%//' ) + fi + + if [ -n "$cpu_user" ] && [ -n "$cpu_system" ] && [ -n "$cpu_idle" ]; then + echo "${cpu_user}, ${cpu_system}, ${cpu_idle}" | awk -F', ' '{printf("%5.1f,%5.1f,%5.1f",$1,$2,$3)}' + return 0 + else + return 1 + fi +} -- cgit v1.2.3