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