Skip to content

Commit 2a85770

Browse files
committed
Merge branch 'RafalLukawiecki/master'
2 parents 490d9b4 + a71c2cf commit 2a85770

File tree

4 files changed

+21
-6
lines changed

4 files changed

+21
-6
lines changed

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Features
1919
- OSX, Linux support
2020
- [ ] **TODO:** network I/O metric support
2121

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

2424

2525
Installation

scripts/cpu.sh

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,17 @@ get_cpu_usage() {
4545
fi
4646
else
4747
if command_exists "vmstat"; then
48-
vmstat -n "$refresh_interval" 2 | tail -n 1 | awk '{print 100-$(NF-2)}'
48+
if is_freebsd; then
49+
vmstat -n "$refresh_interval" -c 2 | tail -n 1 | awk '{print 100-$(NF-0)}'
50+
else
51+
vmstat -n "$refresh_interval" 2 | tail -n 1 | awk '{print 100-$(NF-2)}'
52+
fi
4953
else
50-
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 }'
54+
if is_freebsd; then
55+
top -d2 | sed -nr '/CPU:/s/.*,[[:space:]]*([0-9]+[.,][0-9]*)%[[:space:]]*id.*/\1/p' | tail -n 1 | awk '{ print 100-$0 }'
56+
else
57+
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 }'
58+
fi
5159
fi
5260
fi
5361
}
@@ -70,4 +78,4 @@ main(){
7078
print_cpu_usage
7179
}
7280

73-
main
81+
main

scripts/helpers.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ is_linux(){
2424
[ $(uname -s) == "Linux" ]
2525
}
2626

27+
is_freebsd() {
28+
[ $(uname) == FreeBSD ]
29+
}
2730

2831
command_exists() {
2932
local command="$1"

scripts/mem.sh

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ print_mem() {
4040
mem_usage=$(get_mem_usage_osx)
4141
elif is_linux; then
4242
mem_usage=$(get_mem_usage_linux)
43+
elif is_freebsd; then
44+
mem_usage=$(get_mem_usage_freebsd)
4345
fi
4446

4547
local size_scale="$(get_size_scale_factor "$size_unit")"
@@ -94,6 +96,10 @@ get_mem_usage_osx(){
9496
'
9597
}
9698

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

98104
# 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
99105

@@ -122,5 +128,3 @@ main() {
122128

123129
main
124130

125-
126-

0 commit comments

Comments
 (0)