Debian Codegraph @

libexec/container/top

raw symbols SCIP
1 #!/bin/sh
2
3 # Copyright (C) 2014-2026 Daniel Baumann <[email protected]>
4 #
5 # SPDX-License-Identifier: GPL-3.0+
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <https://www.gnu.org/licenses/>.
19
20 set -e
21
22 PROJECT="open-infrastructure"
23 SOFTWARE="compute-tools"
24 PROGRAM="container"
25 COMMAND="$(basename ${0})"
26
27 Parameters ()
28 {
29 GETOPT_LONGOPTIONS="delay:,"
30 GETOPT_OPTIONS="d:,"
31
32 PARAMETERS="$(getopt --longoptions ${GETOPT_LONGOPTIONS} --name=${COMMAND} --options ${GETOPT_OPTIONS} --shell sh -- ${@})"
33
34 if [ "${?}" != "0" ]
35 then
36 echo "'${COMMAND}': getopt exit" >&2
37 exit 1
38 fi
39
40 eval set -- "${PARAMETERS}"
41
42 while true
43 do
44 case "${1}" in
45 -d|--delay)
46 DELAY="${2}"
47 shift 2
48 ;;
49
50 --)
51 shift 1
52 break
53 ;;
54
55 *)
56 echo "'${COMMAND}': getopt error" >&2
57 exit 1
58 ;;
59 esac
60 done
61 }
62
63 Usage ()
64 {
65 echo "Usage: ${PROGRAM} ${COMMAND} [-d|--delay DELAY]" >&2
66 echo
67 echo "See ${COMMAND}(1), ${PROGRAM}(1) and ${PROJECT}(7) for more information."
68
69 exit 1
70 }
71
72 Parameters "${@}"
73
74 DELAY="${DELAY:-1}"
75 HOST="$(hostname -f 2> /dev/null || hostname)"
76
77 # Pre hooks
78 for FILE in "${HOOKS}/pre-${COMMAND}".* "${HOOKS}/${NAME}.pre-${COMMAND}"
79 do
80 if [ -x "${FILE}" ]
81 then
82 "${FILE}"
83 fi
84 done
85
86 # Run
87 Top ()
88 {
89 HOME="$(tput cup 0 0)"
90 ED="$(tput ed)"
91 EL="$(tput el)"
92
93 printf '%s%s' "${HOME}" "${ED}"
94
95 while true
96 do
97 ROWS="$(tput lines)"
98 COLS="$(tput cols)"
99 CMD="${@}"
100
101 ${SHELL:=sh} -c "${CMD}" | head -n ${ROWS} | while IFS= read LINE
102 do
103 printf '%-*.*s%s\n' ${COLS} ${COLS} "${LINE}" "${EL}"
104 done
105
106 printf '%s%s' "${ED}" "${HOME}"
107 sleep ${DELAY}
108 done
109 }
110
111 trap 'clear' EXIT HUP INT QUIT TERM
112
113 Top "${PROGRAM} list && printf '%-59s %-19s\n' \" Host: ${HOST}\" \"\$(date +%Y-%m-%d\ %H:%M:%S)\""
114
115 # Post hooks
116 for FILE in "${HOOKS}/post-${COMMAND}".* "${HOOKS}/${NAME}.post-${COMMAND}"
117 do
118 if [ -x "${FILE}" ]
119 then
120 "${FILE}"
121 fi
122 done