-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcputop
More file actions
executable file
·84 lines (75 loc) · 2.07 KB
/
cputop
File metadata and controls
executable file
·84 lines (75 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env bash
function cpulook/initialize-cpudir {
unset -f "$FUNCNAME"
local script=$0
if [[ -h $script ]]; then
local path=$(realpath "$0" || readlink -f "$0" || /bin/readlink -f "$script") 2>/dev/null
[[ $path ]] && script=$path
fi
[[ $script =~ ^(/?)(.*/)[^/]*$ ]]
local script_dir=${BASH_REMATCH[1]:-$PWD/}${BASH_REMATCH[2]}
script_dir=${script_dir%/}
if [[ -d ${script_dir:-/} ]]; then
cpudir=$script_dir
elif local dir=${XDG_DATA_HOME:-$HOME/.local/share}/cpulook; [[ -d $dir ]]; then
cpudir=$dir
else
cpudir=$script_dir
fi
# Go to the parent directory until we find $cpudir/lib/cpudef.bash
while
local common=$cpudir/lib/cpudef.bash
if [[ -f $common ]]; then
if [[ ! -r $common ]]; then
printf '%s\n' "$script: permission denied for the cpulook directory." >&2
return 1
fi
source "$common"
return "$?"
fi
[[ $cpudir == */* ]]
do
cpudir=${cpudir%/*}
done
cpudir=
printf '%s\n' "$script: failed to detect the cpulook directory." >&2
return 1
}
cpulook/initialize-cpudir || exit "$?"
##----CPULOOK_COMMON_HEADER_END----
## @fn cputop/read-arguments args...
## @var[out] flags
## @var[out] host
function cputop/read-arguments {
flags= host=
local host_set=
while (($#)); do
case $1 in
(-a)
flags=a$flags ;; # show all processes
(*)
if [[ $host_set ]]; then
cpulook/print "$cpulook_prog(arg \"$1\"): host is already specified." >&2
flags=E$flags
fi
host=$1 host_set=1 ;;
esac
shift
done
if [[ ! $host_set ]]; then
cpulook/print "$cpulook_prog(args): host is not specified!" >&2
flags=E$flags
fi
[[ $flags != *E* ]]
}
function cputop {
local flags host
cputop/read-arguments "$@" || return 2
cpulook/parse-host "$host" || return 1
if [[ $flags == *a* ]]; then
rsh.dispatch "$host" "COLUMNS=$((COLUMNS-1)) top -cb -n 1"
else
rsh.dispatch "$host" "COLUMNS=$((COLUMNS-1)) top -cb -n 1" | awk '$1=="PID"{head=1;print;next} !head||$8!="S"||$9!=0{print}'
fi | cpulook/view
}
cputop "$@"