]> git.ipfire.org Git - people/ms/network.git/blame - functions.cli
Make the print function print strings that start with a dash.
[people/ms/network.git] / functions.cli
CommitLineData
1848564d
MT
1#!/bin/bash
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
5# Copyright (C) 2010 Michael Tremer & Christian Schmidt #
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 <http://www.gnu.org/licenses/>. #
19# #
20###############################################################################
21
1d7bc4f3 22function cli_help_requested() {
866de228
MT
23 local argument="${1}"
24
25 if [ -n "${argument}" ]; then
26 if listmatch ${argument} help -h --help; then
1d7bc4f3
MT
27 return ${EXIT_OK}
28 fi
866de228 29 fi
1d7bc4f3
MT
30
31 return ${EXIT_ERROR}
32}
33
3cb2fc42
MT
34function cli_device_headline() {
35 local device=${1}
36 assert isset device
37
fe8e6d69
MT
38 local long=0
39 shift
40 while [ $# -gt 0 ]; do
41 case "${1}" in
42 --long)
43 long=1
44 ;;
45 esac
46 shift
47 done
48
3cb2fc42
MT
49 local type
50 if zone_exists ${device}; then
51 type="zone"
52 elif port_exists ${device}; then
53 type="port"
54 else
55 type="unknown"
56 fi
57
58 local headline_prefix
59 case "${type}" in
60 zone)
61 headline_prefix="Zone ${device}"
62 ;;
63 port)
fe8e6d69 64 headline_prefix="Port ${device} ($(device_get_type ${device}))"
3cb2fc42
MT
65 ;;
66 *)
fe8e6d69 67 headline_prefix="Device ${device} ($(device_get_type ${device}))"
3cb2fc42
MT
68 ;;
69 esac
70
3cb2fc42
MT
71 # Print the hook for all zones.
72 if [ "${type}" = "zone" ]; then
8e3508ac 73 headline_prefix="${headline_prefix} ($(zone_get_hook ${device}))"
3cb2fc42 74 fi
8e3508ac 75 cli_headline 1 "${headline_prefix}"
3cb2fc42
MT
76
77 # Print the device status.
8e3508ac
MT
78 local status
79 case "$(device_get_status ${device})" in
80 ${STATUS_UP})
81 status=${MSG_DEVICE_STATUS_UP}
82 ;;
83 ${STATUS_DOWN})
84 status=${MSG_DEVICE_STATUS_DOWN}
85 ;;
86 ${STATUS_NOCARRIER})
87 status=${MSG_DEVICE_STATUS_NOCARRIER}
88 ;;
89 *)
90 status=${MSG_DEVICE_STATUS_UNKNOWN}
91 ;;
92 esac
93 cli_print_fmt1 1 "Status" "${status}"
fe8e6d69
MT
94 if enabled ${long}; then
95 cli_print_fmt1 1 "Address" "$(device_get_address ${device})"
96 fi
3cb2fc42
MT
97 if device_is_up ${device}; then
98 cli_print_fmt1 1 "MTU" "$(device_get_mtu ${device})"
99 fi
fe8e6d69
MT
100 if enabled ${long}; then
101 device_is_promisc ${device} &>/dev/null
102 cli_print_fmt1 1 "Promisc" "$(cli_print_bool $?)"
103 fi
3cb2fc42
MT
104
105 cli_space
ec63256a 106
3cb2fc42
MT
107 # Print the device stats.
108 device_is_up ${device} && cli_device_stats 2 ${device}
fe8e6d69
MT
109
110 if enabled ${long}; then
111 # Virtual devices.
112 device_is_virtual ${device} && cli_device_virtual ${device}
113
114 # Bonded devices.
115 device_is_bonded ${device} && cli_device_bonded ${device}
116
117 # Bonding devices.
118 device_is_bonding ${device} && cli_device_bonding ${device}
119 fi
3cb2fc42
MT
120}
121
122function cli_device_stats() {
123 local level=${1}
124 local device=${2}
125
126 # This section will print statistical data from the device.
127 local packets bytes errors
128
129 cli_headline ${level} "Statistics"
130 local format="%-10s %9d packets %6s (%d errors)"
131
132 # RX
133 packets=$(device_get_rx_packets ${device})
134 bytes=$(device_get_rx_bytes ${device})
135 errors=$(device_get_rx_errors ${device})
136
137 cli_print ${level} "${format}" "Received" "${packets}" "$(beautify_bytes ${bytes})" "${errors}"
138
139 # TX
140 packets=$(device_get_tx_packets ${device})
141 bytes=$(device_get_tx_bytes ${device})
142 errors=$(device_get_tx_errors ${device})
143
144 cli_print ${level} "${format}" "Sent" "${packets}" "$(beautify_bytes ${bytes})" "${errors}"
145 cli_space
ec63256a
MT
146}
147
fe8e6d69
MT
148function cli_device_virtual() {
149 local device=${1}
150
151 cli_headline 2 "VLAN"
152
153 cli_print_fmt1 2 "Parent" "$(virtual_get_parent ${device})"
154 cli_print_fmt1 2 "VID" "$(virtual_get_id ${device})"
155 cli_space
156}
157
158function cli_device_bonded() {
159 local device=${1}
160
161 cli_headline 2 "Bonding information"
162
163 local master=$(bonding_slave_get_master ${port})
164 cli_print_fmt1 2 "Master" "${master}"
165
166 local active
167 [ "$(bonding_get_active_slave ${master})" = "${port}" ]
168 cli_print_fmt1 2 "Active slave" "$(cli_print_yesno $?)"
169 cli_space
170}
171
172function cli_device_bonding() {
173 local device=${1}
174
175 cli_headline 2 "Bonding information"
176
177 cli_print_fmt1 2 "Mode" "$(bonding_get_mode ${port})"
178 # XXX lacp rate
179 cli_space
180
181 local slave slave_prefix
182 local slave_active=$(bonding_get_active_slave ${device})
183 for slave in $(bonding_get_slaves ${device}); do
184 if [ "${slave_active}" = "${slave}" ]; then
185 slave_prefix="Slave (active)"
186 else
187 slave_prefix="Slave"
188 fi
189 cli_print_fmt1 2 "${slave_prefix}" "${slave}"
190 done
191 cli_space
192}
193
8e3508ac
MT
194function cli_headline() {
195 local level=${1}
196 local format=${2}
197 shift 2
9178284d 198
8e3508ac 199 local ident=$(cli_ident ${level})
9178284d 200
8e3508ac
MT
201 local out
202 printf -v out "${ident}${COLOUR_BOLD}${format}${COLOUR_NORMAL}\n" "$@"
203 printf "${out}"
9178284d
MT
204}
205
8e3508ac 206function cli_statusline() {
ec63256a
MT
207 local level=${1}
208 shift
209
8e3508ac
MT
210 local head=${1}
211 shift
ec63256a 212
8e3508ac 213 cli_print $(( ${level} - 1 )) "%-12s %s" "${head}" "$@"
ec63256a
MT
214}
215
216function cli_print() {
217 local level=${1}
218 local format=${2}
219 shift 2
220
221 local ident=$(cli_ident $(( ${level} + 1 )))
222
223 local out
224 printf -v out "${ident}${format}\n" "$@"
225 printf "${out}"
226}
227
228function cli_print_fmt1() {
229 local level=${1}
230 shift
231
232 local space=$(( 30 - (${level} * 4) ))
233 local format="%-${space}s %s"
234
235 cli_print ${level} "${format}" "$@"
236}
237
238function cli_print_bool() {
239 if [ "${1}" = "${EXIT_TRUE}" ]; then
240 echo "true"
241 else
242 echo "false"
243 fi
244}
245
246function cli_print_yesno() {
247 if [ "${1}" = "${EXIT_TRUE}" ]; then
248 echo "yes"
249 else
250 echo "false"
251 fi
252}
253
3cb2fc42
MT
254function cli_print_warning() {
255 local level=${1}
256 shift
257
258 cli_print ${level} "${COLOUR_WARN}%s${COLOUR_NORMAL}" "$@"
259}
260
ec63256a
MT
261function cli_space() {
262 printf "\n"
263}
264
265function cli_ident() {
266 local level=${1}
267 shift
268
269 local ident=""
270 while [ ${level} -gt 1 ]; do
271 ident="${ident} "
272 level=$(( ${level} - 1 ))
273 done
274
275 echo "${ident}"
9178284d 276}
f90e550b
MT
277
278function cli_yesno() {
39f552f5 279 local message="$@ [y/n] "
f90e550b
MT
280 local yesno
281
39f552f5
MT
282 while true; do
283 printf "\n${message}"
284 read yesno
f90e550b 285
39f552f5
MT
286 # Check for "yes".
287 if listmatch ${yesno} y Y yes YES Yes; then
288 return ${EXIT_TRUE}
f90e550b 289
39f552f5
MT
290 # Check for "no".
291 elif listmatch ${yesno} n N no NO No; then
292 return ${EXIT_FALSE}
293 fi
294 done
f90e550b 295}
d76f5107
MT
296
297function cli_get_key() {
298 local key="${1%%=*}"
299 echo "${key/--/}"
300}
301
302function cli_get_val() {
e9ea243e 303 echo "${@##*=}"
d76f5107 304}
de28a630
MT
305
306function cli_usage() {
307 local command="$@"
308 local basename="$(basename ${0})"
309
310 if ! isset command; then
311 command="${basename} help"
312 fi
313
314 echo "The given command was not understood by ${basename}." >&2
315 echo "Please run '${command}' for detailed help." >&2
316}
317
318function cli_show_man() {
319 local manpage=${1}
320 assert isset manpage
321
322 if ! binary_exists man; then
323 error "The man package is not installed on this system."
324 error "Please install 'man' in order to view the help."
325 exit ${EXIT_ERROR}
326 fi
327
328 man ${manpage}
329}