Добавлены конфиги tmux и zsh

This commit is contained in:
eKa
2019-09-18 00:17:47 +05:00
commit 7d0a7691c6
882 changed files with 67305 additions and 0 deletions

View File

@@ -0,0 +1,78 @@
#!/usr/bin/env bash
set -u
set -e
LC_NUMERIC=C
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$CURRENT_DIR/helpers.sh"
cpu_tmp_dir=$(tmux show-option -gqv "@sysstat_cpu_tmp_dir")
cpu_view_tmpl=$(get_tmux_option "@sysstat_cpu_view_tmpl" 'CPU:#[fg=#{cpu.color}]#{cpu.pused}#[default]')
cpu_medium_threshold=$(get_tmux_option "@sysstat_cpu_medium_threshold" "30")
cpu_stress_threshold=$(get_tmux_option "@sysstat_cpu_stress_threshold" "80")
cpu_color_low=$(get_tmux_option "@sysstat_cpu_color_low" "green")
cpu_color_medium=$(get_tmux_option "@sysstat_cpu_color_medium" "yellow")
cpu_color_stress=$(get_tmux_option "@sysstat_cpu_color_stress" "red")
get_cpu_color(){
local cpu_used=$1
if fcomp "$cpu_stress_threshold" "$cpu_used"; then
echo "$cpu_color_stress";
elif fcomp "$cpu_medium_threshold" "$cpu_used"; then
echo "$cpu_color_medium";
else
echo "$cpu_color_low";
fi
}
print_cpu_usage() {
local cpu_pused=$(get_cpu_usage_or_collect)
local cpu_color=$(get_cpu_color "$cpu_pused")
local cpu_view="$cpu_view_tmpl"
cpu_view="${cpu_view//'#{cpu.pused}'/$(printf "%.1f%%" "$cpu_pused")}"
cpu_view="${cpu_view//'#{cpu.color}'/$(echo "$cpu_color" | awk '{ print $1 }')}"
cpu_view="${cpu_view//'#{cpu.color2}'/$(echo "$cpu_color" | awk '{ print $2 }')}"
cpu_view="${cpu_view//'#{cpu.color3}'/$(echo "$cpu_color" | awk '{ print $3 }')}"
echo "$cpu_view"
}
get_cpu_usage_or_collect() {
local collect_cpu_metric="$cpu_tmp_dir/cpu_collect.metric"
# read cpu metric from file, otherwise 0 as a temporary null value, until first cpu metric is collected
[ -f "$collect_cpu_metric" ] && cat "$collect_cpu_metric" || echo "0.0"
start_cpu_collect_if_required >/dev/null 2>&1
}
start_cpu_collect_if_required() {
local collect_cpu_pidfile="$cpu_tmp_dir/cpu_collect.pid"
# check if cpu collect process is running, otherwise start it in background
if [ -f "$collect_cpu_pidfile" ] && ps -p "$(cat "$collect_cpu_pidfile")" > /dev/null 2>&1; then
return;
fi
jobs >/dev/null 2>&1
"$CURRENT_DIR/cpu_collect.sh" &>/dev/null &
if [ -n "$(jobs -n)" ]; then
echo "$!" > "${collect_cpu_pidfile}"
else
echo "Failed to start CPU collect job" >&2
exit 1
fi
}
main(){
print_cpu_usage
}
main

View File

@@ -0,0 +1,53 @@
#!/usr/bin/env bash
LC_NUMERIC=C
set -u
set -e
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$CURRENT_DIR/helpers.sh"
refresh_interval=$(get_tmux_option "status-interval" "5")
samples_count="60"
cpu_metric_file="$(get_tmux_option "@sysstat_cpu_tmp_dir" "/dev/null")/cpu_collect.metric"
get_cpu_usage() {
if is_osx; then
if command_exists "iostat"; then
iostat -w "$refresh_interval" -c "$samples_count" \
| stdbuf -o0 awk 'NR > 2 { print 100-$(NF-3); }'
else
top -l "$samples_count" -s "$refresh_interval" -n 0 \
| sed -u -nr '/CPU usage/s/.*,[[:space:]]*([0-9]+[.,][0-9]*)%[[:space:]]*idle.*/\1/p' \
| stdbuf -o0 awk '{ print 100-$0 }'
fi
elif ! command_exists "vmstat"; then
if is_freebsd; then
vmstat -n "$refresh_interval" -c "$samples_count" \
| stdbuf -o0 awk 'NR>2 {print 100-$(NF-0)}'
else
vmstat -n "$refresh_interval" "$samples_count" \
| stdbuf -o0 awk 'NR>2 {print 100-$(NF-2)}'
fi
else
if is_freebsd; then
top -d"$samples_count" \
| sed -u -nr '/CPU:/s/.*,[[:space:]]*([0-9]+[.,][0-9]*)%[[:space:]]*id.*/\1/p' \
| stdbuf -o0 awk '{ print 100-$0 }'
else
top -b -n "$samples_count" -d "$refresh_interval" \
| sed -u -nr '/%Cpu/s/.*,[[:space:]]*([0-9]+[.,][0-9]*)[[:space:]]*id.*/\1/p' \
| stdbuf -o0 awk '{ print 100-$0 }'
fi
fi
}
main() {
get_cpu_usage | while read -r value; do
echo "$value" | tee "$cpu_metric_file"
done
}
main

View File

@@ -0,0 +1,76 @@
get_tmux_option() {
local option="$1"
local default_value="$2"
local option_value="$(tmux show-option -gqv "$option")"
if [ -z "$option_value" ]; then
echo "$default_value"
else
echo "$option_value"
fi
}
set_tmux_option() {
local option="$1"
local value="$2"
tmux set-option -gq "$option" "$value"
}
is_osx() {
[ $(uname) == "Darwin" ]
}
is_linux(){
[ $(uname -s) == "Linux" ]
}
is_freebsd() {
[ $(uname) == FreeBSD ]
}
command_exists() {
local command="$1"
type "$command" >/dev/null 2>&1
}
# because bash does not support floating-point math
# but awk does
calc() {
local stdin;
read -d '' -u 0 stdin;
awk "BEGIN { print $stdin }";
}
# "<" math operator which works with floats, once again based on awk
fcomp() {
awk -v n1="$1" -v n2="$2" 'BEGIN {if (n1<n2) exit 0; exit 1}'
}
# get_mem_usage* function returns values in KiB
# 1 - scale to KiB
# 1024 - scale to MiB
# 1048576 - scale to GiB
function get_size_scale_factor(){
local size_unit="$1"
case "$size_unit" in
G) echo 1048576;;
M) echo 1024;;
K) echo 1;;
esac
}
# Depending on scale factor, change precision
# 12612325K - no digits after floating point
# 1261M - no digits after floating point
# 1.1G - 1 digit after floating point
function get_size_format(){
local size_unit="$1"
case "$size_unit" in
G) echo '%.1f%s';;
M) echo '%.0f%s';;
K) echo '%.0f%s';;
esac
}

View File

@@ -0,0 +1,28 @@
#!/usr/bin/env bash
set -u
set -e
LC_NUMERIC=C
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$CURRENT_DIR/helpers.sh"
loadavg_per_cpu_core=$(get_tmux_option "@sysstat_loadavg_per_cpu_core" "true")
get_num_of_cores(){
is_osx && sysctl -n hw.ncpu || nproc
}
main(){
local num_cores=$([ "$loadavg_per_cpu_core" == "true" ] && get_num_of_cores || echo 1)
uptime | awk -v num_cores="$num_cores" '{
sub(/,$/, "", $(NF-2));
sub(/,$/, "", $(NF-1));
sub(/,$/, "", $NF);
printf "%.2f %.2f %.2f", $(NF-2)/num_cores, $(NF-1)/num_cores, $NF/num_cores
}'
}
main

View File

@@ -0,0 +1,130 @@
#!/usr/bin/env bash
set -u
set -e
LC_NUMERIC=C
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$CURRENT_DIR/helpers.sh"
mem_view_tmpl=$(get_tmux_option "@sysstat_mem_view_tmpl" 'MEM:#[fg=#{mem.color}]#{mem.pused}#[default]')
mem_medium_threshold=$(get_tmux_option "@sysstat_mem_medium_threshold" "75")
mem_stress_threshold=$(get_tmux_option "@sysstat_mem_stress_threshold" "90")
mem_color_low=$(get_tmux_option "@sysstat_mem_color_low" "green")
mem_color_medium=$(get_tmux_option "@sysstat_mem_color_medium" "yellow")
mem_color_stress=$(get_tmux_option "@sysstat_mem_color_stress" "red")
size_unit=$(get_tmux_option "@sysstat_mem_size_unit" "G")
get_mem_color() {
local mem_pused=$1
if fcomp "$mem_stress_threshold" "$mem_pused"; then
echo "$mem_color_stress";
elif fcomp "$mem_medium_threshold" "$mem_pused"; then
echo "$mem_color_medium";
else
echo "$mem_color_low";
fi
}
print_mem() {
local mem_usage
local scale
local size_format
if is_osx; then
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")"
local size_format="$(get_size_format "$size_unit")"
# Extract free and used memory in MiB, calculate total and percentage
local mem_free=$(echo $mem_usage | awk -v scale="$size_scale" '{ print $1/scale }')
local mem_used=$(echo $mem_usage | awk -v scale="$size_scale" '{ print $2/scale }')
local mem_total=$(echo "$mem_free + $mem_used" | calc)
local mem_pused=$(echo "($mem_used / $mem_total) * 100" | calc)
local mem_pfree=$(echo "($mem_free / $mem_total) * 100" | calc)
# Calculate colors for mem and swap
local mem_color=$(get_mem_color "$mem_pused")
local mem_view="$mem_view_tmpl"
mem_view="${mem_view//'#{mem.used}'/$(printf "$size_format" "$mem_used" "$size_unit")}"
mem_view="${mem_view//'#{mem.pused}'/$(printf "%.0f%%" "$mem_pused")}"
mem_view="${mem_view//'#{mem.free}'/$(printf "$size_format" "$mem_free" "$size_unit")}"
mem_view="${mem_view//'#{mem.pfree}'/$(printf "%.0f%%" "$mem_pfree")}"
mem_view="${mem_view//'#{mem.total}'/$(printf "$size_format" "$mem_total" "$size_unit")}"
mem_view="${mem_view//'#{mem.color}'/$(echo "$mem_color" | awk '{ print $1 }')}"
mem_view="${mem_view//'#{mem.color2}'/$(echo "$mem_color" | awk '{ print $2 }')}"
mem_view="${mem_view//'#{mem.color3}'/$(echo "$mem_color" | awk '{ print $3 }')}"
echo "$mem_view"
}
# Report like it does htop on OSX:
# used = active + wired
# free = free + inactive + speculative + occupied by compressor
# see `vm_stat` command
get_mem_usage_osx(){
local page_size=$(sysctl -nq "vm.pagesize")
vm_stat | awk -v page_size=$page_size -F ':' '
BEGIN { free=0; used=0 }
/Pages active/ ||
/Pages wired/ {
gsub(/^[ \t]+|[ \t]+$/, "", $2); used+=$2;
}
/Pages free/ ||
/Pages inactive/ ||
/Pages speculative/ ||
/Pages occupied by compressor/ {
gsub(/^[ \t]+|[ \t]+$/, "", $2); free+=$2;
}
END { print (free * page_size)/1024, (used * page_size)/1024 }
'
}
# 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
# Method #2. If "MemAvailable" is provided by system, use it. This is more correct method, because we're not relying on fragile "free+buffer+cache" equation.
# See: Interpreting /proc/meminfo and free output for Red Hat Enterprise Linux 5, 6 and 7 - Red Hat Customer Portal - https://access.redhat.com/solutions/406773
# See: kernel/git/torvalds/linux.git - /proc/meminfo: provide estimated available memory - https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=34e431b0ae398fc54ea69ff85ec700722c9da773
get_mem_usage_linux(){
</proc/meminfo awk '
BEGIN { total=0; free=0; }
/MemTotal:/ { total=$2; }
/MemFree:/ { free+=$2; }
/Buffers:/ { free+=$2; }
/Cached:/ { free+=$2; }
/MemAvailable:/ { free=$2; exit;}
END { print free, total-free }
'
}
main() {
print_mem
}
main

View File

@@ -0,0 +1,95 @@
#!/usr/bin/env bash
set -u
set -e
LC_NUMERIC=C
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$CURRENT_DIR/helpers.sh"
swap_view_tmpl=$(get_tmux_option "@sysstat_swap_view_tmpl" 'SW:#[fg=#{swap.color}]#{swap.pused}#[default]')
swap_medium_threshold=$(get_tmux_option "@sysstat_swap_medium_threshold" "25")
swap_stress_threshold=$(get_tmux_option "@sysstat_swap_stress_threshold" "75")
swap_color_low=$(get_tmux_option "@sysstat_swap_color_low" "green")
swap_color_medium=$(get_tmux_option "@sysstat_swap_color_medium" "yellow")
swap_color_stress=$(get_tmux_option "@sysstat_swap_color_stress" "red")
size_unit=$(get_tmux_option "@sysstat_swap_size_unit" "G")
get_swap_color() {
local swap_pused=$1
if fcomp "$swap_stress_threshold" "$swap_pused"; then
echo "$swap_color_stress";
elif fcomp "$swap_medium_threshold" "$swap_pused"; then
echo "$swap_color_medium";
else
echo "$swap_color_low";
fi
}
print_swap() {
local swap_usage
if is_osx; then
swap_usage=$(get_swap_usage_osx)
elif is_linux; then
swap_usage=$(get_swap_usage_linux)
fi
local size_scale="$(get_size_scale_factor "$size_unit")"
local size_format="$(get_size_format "$size_unit")"
# Extract swap free and used in MiB, calculate total and percentage
local swap_free=$(echo $swap_usage | awk -v scale="$size_scale" '{ print $1/scale }')
local swap_used=$(echo $swap_usage | awk -v scale="$size_scale" '{ print $2/scale }')
local swap_total=$(echo "$swap_free + $swap_used" | calc)
local swap_pused=$(echo "($swap_used / $swap_total) * 100" | calc)
local swap_pfree=$(echo "($swap_free / $swap_total) * 100" | calc)
# Calculate colors for mem and swap
local swap_color=$(get_swap_color "$swap_pused")
local swap_view="$swap_view_tmpl"
swap_view="${swap_view//'#{swap.used}'/$(printf "$size_format" "$swap_used" "$size_unit")}"
swap_view="${swap_view//'#{swap.pused}'/$(printf "%.0f%%" "$swap_pused")}"
swap_view="${swap_view//'#{swap.free}'/$(printf "$size_format" "$swap_free" "$size_unit")}"
swap_view="${swap_view//'#{swap.pfree}'/$(printf "%.0f%%" "$swap_pfree")}"
swap_view="${swap_view//'#{swap.total}'/$(printf "$size_format" "$swap_total" "$size_unit")}"
swap_view="${swap_view//'#{swap.color}'/$(echo "$swap_color" | awk '{ print $1 }')}"
swap_view="${swap_view//'#{swap.color2}'/$(echo "$swap_color" | awk '{ print $2 }')}"
swap_view="${swap_view//'#{swap.color3}'/$(echo "$swap_color" | awk '{ print $3 }')}"
echo "$swap_view"
}
get_swap_usage_osx(){
# assume swap size in MB
local swap_used=$(sysctl -nq vm.swapusage | awk -F ' ' '{ print $2 }' | awk -F '=' '{gsub(/^[ ]|[M]$/, "", $2); printf "%d", $2 * 1024 }')
local swap_free=$(sysctl -nq vm.swapusage | awk -F ' ' '{ print $3 }' | awk -F '=' '{gsub(/^[ ]|[M]$/, "", $2); printf "%d", $2 * 1024 }')
printf "%s %s" "$swap_free" "$swap_used"
}
get_swap_usage_linux(){
</proc/meminfo awk '
BEGIN { total=0; free=0; }
/SwapTotal:/ { total=$2; }
/SwapFree:/ { free=$2; }
END { print free, total-free }
'
}
main() {
print_swap
}
main