]> git.ipfire.org Git - people/ms/network.git/blame - src/functions/functions.cli
Use autotools.
[people/ms/network.git] / src / functions / 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
e145f826
MT
22IDENT=" "
23
1d7bc4f3 24function cli_help_requested() {
866de228
MT
25 local argument="${1}"
26
27 if [ -n "${argument}" ]; then
28 if listmatch ${argument} help -h --help; then
1d7bc4f3
MT
29 return ${EXIT_OK}
30 fi
866de228 31 fi
1d7bc4f3
MT
32
33 return ${EXIT_ERROR}
34}
35
4fedddef
MT
36function cli_run_help() {
37 local command="$@"
38
39 print "Run \"${command} help\" to get more information."
40 return ${EXIT_OK}
41}
42
3cb2fc42
MT
43function cli_device_headline() {
44 local device=${1}
45 assert isset device
46
fe8e6d69
MT
47 local long=0
48 shift
49 while [ $# -gt 0 ]; do
50 case "${1}" in
51 --long)
52 long=1
53 ;;
54 esac
55 shift
56 done
57
3cb2fc42
MT
58 local type
59 if zone_exists ${device}; then
60 type="zone"
61 elif port_exists ${device}; then
62 type="port"
63 else
64 type="unknown"
65 fi
66
67 local headline_prefix
68 case "${type}" in
69 zone)
70 headline_prefix="Zone ${device}"
71 ;;
72 port)
fe8e6d69 73 headline_prefix="Port ${device} ($(device_get_type ${device}))"
3cb2fc42
MT
74 ;;
75 *)
fe8e6d69 76 headline_prefix="Device ${device} ($(device_get_type ${device}))"
3cb2fc42
MT
77 ;;
78 esac
79
3cb2fc42
MT
80 # Print the hook for all zones.
81 if [ "${type}" = "zone" ]; then
8e3508ac 82 headline_prefix="${headline_prefix} ($(zone_get_hook ${device}))"
3cb2fc42 83 fi
8e3508ac 84 cli_headline 1 "${headline_prefix}"
3cb2fc42
MT
85
86 # Print the device status.
8e3508ac
MT
87 local status
88 case "$(device_get_status ${device})" in
89 ${STATUS_UP})
90 status=${MSG_DEVICE_STATUS_UP}
91 ;;
92 ${STATUS_DOWN})
93 status=${MSG_DEVICE_STATUS_DOWN}
94 ;;
95 ${STATUS_NOCARRIER})
96 status=${MSG_DEVICE_STATUS_NOCARRIER}
97 ;;
98 *)
99 status=${MSG_DEVICE_STATUS_UNKNOWN}
100 ;;
101 esac
102 cli_print_fmt1 1 "Status" "${status}"
f8f476d8 103 if enabled long; then
fe8e6d69
MT
104 cli_print_fmt1 1 "Address" "$(device_get_address ${device})"
105 fi
3cb2fc42
MT
106 if device_is_up ${device}; then
107 cli_print_fmt1 1 "MTU" "$(device_get_mtu ${device})"
108 fi
f8f476d8 109 if enabled long; then
fe8e6d69
MT
110 device_is_promisc ${device} &>/dev/null
111 cli_print_fmt1 1 "Promisc" "$(cli_print_bool $?)"
112 fi
3cb2fc42
MT
113
114 cli_space
ec63256a 115
3cb2fc42
MT
116 # Print the device stats.
117 device_is_up ${device} && cli_device_stats 2 ${device}
fe8e6d69 118
f8f476d8 119 if enabled long; then
fe8e6d69 120 # Virtual devices.
7951525a 121 device_is_vlan ${device} && cli_device_vlan ${device}
fe8e6d69
MT
122
123 # Bonded devices.
124 device_is_bonded ${device} && cli_device_bonded ${device}
125
126 # Bonding devices.
127 device_is_bonding ${device} && cli_device_bonding ${device}
128 fi
3cb2fc42
MT
129}
130
131function cli_device_stats() {
132 local level=${1}
133 local device=${2}
134
135 # This section will print statistical data from the device.
136 local packets bytes errors
137
138 cli_headline ${level} "Statistics"
139 local format="%-10s %9d packets %6s (%d errors)"
140
141 # RX
142 packets=$(device_get_rx_packets ${device})
143 bytes=$(device_get_rx_bytes ${device})
144 errors=$(device_get_rx_errors ${device})
145
146 cli_print ${level} "${format}" "Received" "${packets}" "$(beautify_bytes ${bytes})" "${errors}"
147
148 # TX
149 packets=$(device_get_tx_packets ${device})
150 bytes=$(device_get_tx_bytes ${device})
151 errors=$(device_get_tx_errors ${device})
152
153 cli_print ${level} "${format}" "Sent" "${packets}" "$(beautify_bytes ${bytes})" "${errors}"
154 cli_space
ec63256a
MT
155}
156
7951525a 157function cli_device_vlan() {
fe8e6d69
MT
158 local device=${1}
159
160 cli_headline 2 "VLAN"
161
7951525a
MT
162 cli_print_fmt1 2 "Parent" "$(vlan_get_parent ${device})"
163 cli_print_fmt1 2 "VID" "$(vlan_get_id ${device})"
fe8e6d69
MT
164 cli_space
165}
166
167function cli_device_bonded() {
168 local device=${1}
169
170 cli_headline 2 "Bonding information"
171
172 local master=$(bonding_slave_get_master ${port})
173 cli_print_fmt1 2 "Master" "${master}"
174
d1e71061
MT
175 local mode=$(bonding_get_mode ${master})
176 if [ "${mode}" = "active-backup" ]; then
177 local active_slaves=$(bonding_get_slaves ${master} --active)
178 local active="false"
179 if list_match "${device}" ${active_slaves}; then
180 active="true"
181 fi
182 cli_print_fmt1 2 "Active slave" "$(cli_print_yesno ${active})"
183 fi
184
fe8e6d69
MT
185 cli_space
186}
187
188function cli_device_bonding() {
189 local device=${1}
d1e71061 190 assert isset device
fe8e6d69
MT
191
192 cli_headline 2 "Bonding information"
193
d1e71061
MT
194 local mode=$(bonding_get_mode ${device})
195
196 cli_print_fmt1 2 "Mode" "${mode}"
197 if [ "${mode}" = "802.3ad" ]; then
198 local lacp_rate=$(bonding_get_lacp_rate ${device})
199 cli_print_fmt1 2 "LACP rate" "${lacp_rate}"
200 fi
fe8e6d69
MT
201 cli_space
202
d1e71061
MT
203 local slave slave_suffix
204 local active_slaves=$(bonding_get_slaves ${device} --active)
fe8e6d69 205 for slave in $(bonding_get_slaves ${device}); do
d1e71061
MT
206 # Print the device status.
207 local status
208 case "$(device_get_status ${slave})" in
209 ${STATUS_UP})
210 status=${MSG_DEVICE_STATUS_UP}
211 ;;
212 ${STATUS_DOWN})
213 status=${MSG_DEVICE_STATUS_DOWN}
214 ;;
215 ${STATUS_NOCARRIER})
216 status=${MSG_DEVICE_STATUS_NOCARRIER}
217 ;;
218 *)
219 status=${MSG_DEVICE_STATUS_UNKNOWN}
220 ;;
221 esac
222
223 if list_match "${slave}" ${active_slaves}; then
224 slave_suffix="(active)"
fe8e6d69 225 else
d1e71061 226 slave_suffix=""
fe8e6d69 227 fi
d1e71061 228 cli_print_fmt1 2 "Slave ${slave}" "${status} ${slave_suffix}"
fe8e6d69
MT
229 done
230 cli_space
231}
232
8e3508ac
MT
233function cli_headline() {
234 local level=${1}
235 local format=${2}
236 shift 2
9178284d 237
8e3508ac 238 local ident=$(cli_ident ${level})
9178284d 239
8e3508ac 240 local out
a45d972f 241 printf -v out "${ident}${CLR_BLACK_B}${format}${CLR_RESET}\n" "$@"
8e3508ac 242 printf "${out}"
9178284d
MT
243}
244
8e3508ac 245function cli_statusline() {
ec63256a
MT
246 local level=${1}
247 shift
248
8e3508ac
MT
249 local head=${1}
250 shift
ec63256a 251
8e3508ac 252 cli_print $(( ${level} - 1 )) "%-12s %s" "${head}" "$@"
ec63256a
MT
253}
254
255function cli_print() {
256 local level=${1}
257 local format=${2}
258 shift 2
259
260 local ident=$(cli_ident $(( ${level} + 1 )))
261
262 local out
263 printf -v out "${ident}${format}\n" "$@"
264 printf "${out}"
265}
266
267function cli_print_fmt1() {
268 local level=${1}
269 shift
270
e6993835 271 local space=$(( 34 - (${level} * ${#IDENT}) ))
ec63256a
MT
272 local format="%-${space}s %s"
273
274 cli_print ${level} "${format}" "$@"
275}
276
277function cli_print_bool() {
278 if [ "${1}" = "${EXIT_TRUE}" ]; then
279 echo "true"
280 else
281 echo "false"
282 fi
283}
284
285function cli_print_yesno() {
286 if [ "${1}" = "${EXIT_TRUE}" ]; then
287 echo "yes"
288 else
289 echo "false"
290 fi
291}
292
e145f826
MT
293function cli_print_enabled() {
294 enabled ${1}
295
296 cli_print_bool $?
297}
298
3cb2fc42
MT
299function cli_print_warning() {
300 local level=${1}
301 shift
302
fcbf6823 303 cli_print ${level} "${CLR_YELLOW_B}%s${CLR_RESET}" "$@"
3cb2fc42
MT
304}
305
ec63256a
MT
306function cli_space() {
307 printf "\n"
308}
309
310function cli_ident() {
311 local level=${1}
111c94e2 312 assert isinteger level
ec63256a
MT
313
314 local ident=""
315 while [ ${level} -gt 1 ]; do
e145f826 316 ident="${ident}${IDENT}"
ec63256a
MT
317 level=$(( ${level} - 1 ))
318 done
319
111c94e2 320 print "${ident}"
9178284d 321}
f90e550b
MT
322
323function cli_yesno() {
39f552f5 324 local message="$@ [y/n] "
f90e550b
MT
325 local yesno
326
39f552f5
MT
327 while true; do
328 printf "\n${message}"
329 read yesno
f90e550b 330
39f552f5
MT
331 # Check for "yes".
332 if listmatch ${yesno} y Y yes YES Yes; then
333 return ${EXIT_TRUE}
f90e550b 334
39f552f5
MT
335 # Check for "no".
336 elif listmatch ${yesno} n N no NO No; then
337 return ${EXIT_FALSE}
338 fi
339 done
f90e550b 340}
d76f5107
MT
341
342function cli_get_key() {
343 local key="${1%%=*}"
344 echo "${key/--/}"
345}
346
347function cli_get_val() {
204bb6ad 348 echo "${@#*=}"
d76f5107 349}
de28a630 350
08e40c8c
MT
351function cli_get_bool() {
352 local value="$(cli_get_val "$@")"
353
354 if enabled value; then
355 print "true"
356 return ${EXIT_TRUE}
357 fi
358
359 print "false"
360 return ${EXIT_FALSE}
361}
362
de28a630
MT
363function cli_usage() {
364 local command="$@"
365 local basename="$(basename ${0})"
366
367 if ! isset command; then
368 command="${basename} help"
369 fi
370
371 echo "The given command was not understood by ${basename}." >&2
372 echo "Please run '${command}' for detailed help." >&2
373}
374
375function cli_show_man() {
376 local manpage=${1}
377 assert isset manpage
378
379 if ! binary_exists man; then
380 error "The man package is not installed on this system."
381 error "Please install 'man' in order to view the help."
382 exit ${EXIT_ERROR}
383 fi
384
385 man ${manpage}
386}