Skip to content

Added basic support for FreeBSD #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Features
- OSX, Linux support
- [ ] **TODO:** network I/O metric support

Tested on: OS X El Capitan 10.11.5, Ubuntu 14 LTS, CentOS 7
Tested on: OS X El Capitan 10.11.5, Ubuntu 14 LTS, CentOS 7, FreeBSD 11.1.


Installation
Expand Down
14 changes: 11 additions & 3 deletions scripts/cpu.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,17 @@ get_cpu_usage() {
fi
else
if command_exists "vmstat"; then
vmstat -n "$refresh_interval" 2 | tail -n 1 | awk '{print 100-$(NF-2)}'
if is_freebsd; then
vmstat -n "$refresh_interval" -c 2 | tail -n 1 | awk '{print 100-$(NF-0)}'
else
vmstat -n "$refresh_interval" 2 | tail -n 1 | awk '{print 100-$(NF-2)}'
fi
else
top -b -n 2 -d "$refresh_interval" | sed -nr '/%Cpu/s/.*,[[:space:]]*([0-9]+[.,][0-9]*)[[:space:]]*id.*/\1/p' | tail -n 1 | awk '{ print 100-$0 }'
if is_freebsd; then
top -d2 | sed -nr '/CPU:/s/.*,[[:space:]]*([0-9]+[.,][0-9]*)%[[:space:]]*id.*/\1/p' | tail -n 1 | awk '{ print 100-$0 }'
else
top -b -n 2 -d "$refresh_interval" | sed -nr '/%Cpu/s/.*,[[:space:]]*([0-9]+[.,][0-9]*)[[:space:]]*id.*/\1/p' | tail -n 1 | awk '{ print 100-$0 }'
fi
fi
fi
}
Expand All @@ -70,4 +78,4 @@ main(){
print_cpu_usage
}

main
main
3 changes: 3 additions & 0 deletions scripts/helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ is_linux(){
[ $(uname -s) == "Linux" ]
}

is_freebsd() {
[ $(uname) == FreeBSD ]
}

command_exists() {
local command="$1"
Expand Down
8 changes: 6 additions & 2 deletions scripts/mem.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ print_mem() {
mem_usage=$(get_mem_usage_osx)
elif is_linux; then
mem_usage=$(get_mem_usage_linux)
elif is_freebsd; then
mem_usage=$(get_mem_usage_freebsd)
fi

local size_scale="$(get_size_scale_factor "$size_unit")"
Expand Down Expand Up @@ -94,6 +96,10 @@ get_mem_usage_osx(){
'
}

# Relies on vmstat, but could also be done with top on FreeBSD
get_mem_usage_freebsd(){
vmstat -H | tail -n 1 | awk '{ print $5, $4 }'
}

# Method #1. Sum up free+buffers+cached, treat it as "available" memory, assuming buff+cache can be reclaimed. Note, that this assumption is not 100% correct, buff+cache most likely cannot be 100% reclaimed, but this is how memory calculation is used to be done on Linux

Expand Down Expand Up @@ -122,5 +128,3 @@ main() {

main