2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2010 Michael Tremer & Christian Schmidt #
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. #
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. #
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/>. #
20 ###############################################################################
24 cli_help_requested
() {
27 if [ -n "${argument}" ]; then
28 if list_match
${argument} help -h --help; then
39 print
"Run \"${command} help\" to get more information."
43 cli_device_headline
() {
49 while [ $# -gt 0 ]; do
59 if zone_exists
${device}; then
61 elif port_exists
${device}; then
70 headline_prefix
="Zone ${device}"
73 headline_prefix
="Port ${device} ($(device_get_type ${device}))"
76 headline_prefix
="Device ${device} ($(device_get_type ${device}))"
80 # Print the hook for all zones.
81 if [ "${type}" = "zone" ]; then
83 zone_is_enabled
"${device}"
93 local hook
="$(zone_get_hook "${device}")"
94 headline_prefix
="${headline_prefix} (${enabled}, ${hook})"
96 cli_headline
1 "${headline_prefix}"
98 # Print the device status.
100 case "$(device_get_status ${device})" in
102 status
=${MSG_DEVICE_STATUS_UP}
105 status
=${MSG_DEVICE_STATUS_DOWN}
108 status
=${MSG_DEVICE_STATUS_NOCARRIER}
111 status
=${MSG_DEVICE_STATUS_UNKNOWN}
114 cli_print_fmt1
1 "Status" "${status}"
116 # Print the description title of the device.
119 cli_print_fmt1
1 "Description" "$(port_get_description_title ${device})"
123 cli_print_fmt1
1 "Description" "$(zone_get_description_title ${device})"
127 # Print the color of the device.
130 cli_print_fmt1
1 "Color" "$(cli_color_bar $(port_get_color ${device}))"
134 cli_print_fmt1
1 "Color" "$(cli_color_bar $(zone_get_color ${device}))"
138 if enabled long
; then
139 cli_print_fmt1
1 "Address" "$(device_get_address ${device})"
141 if device_is_up
${device}; then
142 cli_print_fmt1
1 "MTU" "$(device_get_mtu ${device})"
144 if enabled long
; then
145 device_is_promisc
${device} &>/dev
/null
146 cli_print_fmt1
1 "Promisc" "$(cli_print_bool $?)"
151 # Print the device stats.
152 device_is_up
${device} && cli_device_stats
2 ${device}
154 if enabled long
; then
156 device_is_vlan
${device} && cli_device_vlan
${device}
159 device_is_bonded
${device} && cli_device_bonded
${device}
162 device_is_bonding
${device} && cli_device_bonding
${device}
170 # This section will print statistical data from the device.
171 local packets bytes errors
173 cli_headline
${level} "Statistics"
174 local format
="%-10s %9d packets %6s (%d errors)"
177 packets
=$
(device_get_rx_packets
${device})
178 bytes
=$
(device_get_rx_bytes
${device})
179 errors
=$
(device_get_rx_errors
${device})
181 cli_print
${level} "${format}" "Received" "${packets}" "$(beautify_bytes ${bytes})" "${errors}"
184 packets=$(device_get_tx_packets ${device})
185 bytes=$(device_get_tx_bytes ${device})
186 errors=$(device_get_tx_errors ${device})
188 cli_print ${level} "${format}" "Sent" "${packets}" "$(beautify_bytes ${bytes})" "${errors}"
195 cli_headline
2 "VLAN"
197 cli_print_fmt1
2 "Parent" "$(vlan_get_parent ${device})"
198 cli_print_fmt1
2 "VID" "$(vlan_get_id ${device})"
202 cli_device_bonded
() {
205 cli_headline
2 "Bonding information"
207 local master
=$
(bonding_slave_get_master
${port})
208 cli_print_fmt1
2 "Master" "${master}"
210 local mode
=$
(bonding_get_mode
${master})
211 if [ "${mode}" = "active-backup" ]; then
212 local active_slaves
=$
(bonding_get_slaves
${master} --active)
214 if list_match
"${device}" ${active_slaves}; then
217 cli_print_fmt1
2 "Active slave" "$(cli_print_yesno ${active})"
223 cli_device_bonding
() {
227 cli_headline
2 "Bonding information"
229 local mode
=$
(bonding_get_mode
${device})
231 cli_print_fmt1
2 "Mode" "${mode}"
232 if [ "${mode}" = "802.3ad" ]; then
233 local lacp_rate
=$
(bonding_get_lacp_rate
${device})
234 cli_print_fmt1
2 "LACP rate" "${lacp_rate}"
238 local slave slave_suffix
239 local active_slaves
=$
(bonding_get_slaves
${device} --active)
240 for slave
in $
(bonding_get_slaves
${device}); do
241 # Print the device status.
243 case "$(device_get_status ${slave})" in
245 status
=${MSG_DEVICE_STATUS_UP}
248 status
=${MSG_DEVICE_STATUS_DOWN}
251 status
=${MSG_DEVICE_STATUS_NOCARRIER}
254 status
=${MSG_DEVICE_STATUS_UNKNOWN}
258 if list_match
"${slave}" ${active_slaves}; then
259 slave_suffix
="(active)"
263 cli_print_fmt1
2 "Slave ${slave}" "${status} ${slave_suffix}"
268 cli_device_show_queues
() {
274 cli_headline
${level} "Queues"
275 cli_print_fmt1
${level} "Queue" "Processors"
277 local processors
=$
(system_get_processors
)
280 for queue
in $
(device_get_queues
${device}); do
281 local smp_affinity
=$
(device_queue_get_smp_affinity
${device} ${queue})
284 for (( processor
= 0; processor
< ${processors}; processor
++ )); do
285 if ! isset smp_affinity || list_match
${processor} ${smp_affinity}; then
286 list_append s
${processor}
290 cli_print_fmt1
${level} "${queue}" "$(list_join s ", ")"
300 local ident
=$
(cli_ident
${level})
303 printf -v out
"${ident}${FONT_BOLD}${format}${FONT_RESET}\n" "$@"
314 cli_print $
(( ${level} - 1 )) "%-12s %s" "${head}" "$@"
322 local ident
=$
(cli_ident $
(( ${level} + 1 )))
325 printf -v out
"${ident}${format}\n" "$@"
333 local space
=$
(( 34 - (${level} * ${#IDENT}) ))
334 local format
="%-${space}s %s"
336 cli_print
${level} "${format}" "$@"
340 if [ "${1}" = "${EXIT_TRUE}" ]; then
348 if [ "${1}" = "${EXIT_TRUE}" ]; then
355 cli_print_enabled
() {
361 cli_print_warning
() {
365 cli_print
${level} "${CLR_YELLOW_B}%s${CLR_RESET}" "$@
"
374 assert isinteger level
377 while [ ${level} -gt 1 ]; do
378 ident="${ident}${IDENT}"
379 level=$(( ${level} - 1 ))
386 local message="$@
[y
/n
] "
390 printf "\n${message}"
394 if list_match ${yesno} y Y yes YES Yes; then
398 elif list_match ${yesno} n N no NO No; then
414 local value="$
(cli_get_val
"$@")"
416 if enabled value; then
427 local basename="$
(basename ${0})"
429 if ! isset command; then
430 command="${basename} help"
433 echo "The given
command was not understood by
${basename}.
" >&2
434 echo "Please run
'${command}' for detailed
help.
" >&2
441 if ! binary_exists man; then
442 error "The man package is not installed on this system.
"
443 error "Please
install 'man' in order to view the
help.
"
451 #Function to set the back and foreground color at once.
457 # Skip if color is empty
458 [ -n "${!i}" ] || continue
461 [ "${!i}" = "-" ] && continue
463 color_set_shell ${i} ${!i}
468 #Reset the shell color.
473 # This function return some colored space
477 echo "$
(cli_set_color
- ${color}) ${CLR_RESET}"