]> git.ipfire.org Git - people/stevee/network.git/blob - src/functions/functions.cli
Fix creating new configs
[people/stevee/network.git] / src / functions / functions.cli
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
22 IDENT=" "
23
24 cli_help_requested() {
25 local argument="${1}"
26
27 if [ -n "${argument}" ]; then
28 if list_match ${argument} help -h --help; then
29 return ${EXIT_TRUE}
30 fi
31 fi
32
33 return ${EXIT_FALSE}
34 }
35
36 cli_run_help() {
37 local command="$@"
38
39 print "Run \"${command} help\" to get more information."
40 return ${EXIT_OK}
41 }
42
43 cli_device_headline() {
44 local device=${1}
45 assert isset device
46
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
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)
73 headline_prefix="Port ${device} ($(device_get_type ${device}))"
74 ;;
75 *)
76 headline_prefix="Device ${device} ($(device_get_type ${device}))"
77 ;;
78 esac
79
80 # Print the hook for all zones.
81 if [ "${type}" = "zone" ]; then
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})"
95 fi
96 cli_headline 1 "${headline_prefix}"
97
98 # Print the device status.
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}"
115
116 # Print the description title of the device.
117 case "${type}" in
118 port)
119 cli_print_fmt1 1 "Description" "$(port_get_description_title ${device})"
120 ;;
121
122 zone)
123 cli_print_fmt1 1 "Description" "$(zone_get_description_title ${device})"
124 ;;
125 esac
126
127 # Print the color of the device.
128 case "${type}" in
129 port)
130 cli_print_fmt1 1 "Color" "$(cli_color_bar $(port_get_color ${device}))"
131 ;;
132
133 zone)
134 cli_print_fmt1 1 "Color" "$(cli_color_bar $(zone_get_color ${device}))"
135 ;;
136 esac
137
138 if enabled long; then
139 cli_print_fmt1 1 "Address" "$(device_get_address ${device})"
140 fi
141 if device_is_up ${device}; then
142 cli_print_fmt1 1 "MTU" "$(device_get_mtu ${device})"
143 fi
144 if enabled long; then
145 device_is_promisc ${device} &>/dev/null
146 cli_print_fmt1 1 "Promisc" "$(cli_print_bool $?)"
147 fi
148
149 cli_space
150
151 # Print the device stats.
152 device_is_up ${device} && cli_device_stats 2 ${device}
153
154 if enabled long; then
155 # Virtual devices.
156 device_is_vlan ${device} && cli_device_vlan ${device}
157
158 # Bonded devices.
159 device_is_bonded ${device} && cli_device_bonded ${device}
160
161 # Bonding devices.
162 device_is_bonding ${device} && cli_device_bonding ${device}
163 fi
164 }
165
166 cli_device_stats() {
167 local level=${1}
168 local device=${2}
169
170 # This section will print statistical data from the device.
171 local packets bytes errors
172
173 cli_headline ${level} "Statistics"
174 local format="%-10s %9d packets %6s (%d errors)"
175
176 # RX
177 packets=$(device_get_rx_packets ${device})
178 bytes=$(device_get_rx_bytes ${device})
179 errors=$(device_get_rx_errors ${device})
180
181 cli_print ${level} "${format}" "Received" "${packets}" "$(beautify_bytes ${bytes})" "${errors}"
182
183 # TX
184 packets=$(device_get_tx_packets ${device})
185 bytes=$(device_get_tx_bytes ${device})
186 errors=$(device_get_tx_errors ${device})
187
188 cli_print ${level} "${format}" "Sent" "${packets}" "$(beautify_bytes ${bytes})" "${errors}"
189 cli_space
190 }
191
192 cli_device_vlan() {
193 local device=${1}
194
195 cli_headline 2 "VLAN"
196
197 cli_print_fmt1 2 "Parent" "$(vlan_get_parent ${device})"
198 cli_print_fmt1 2 "VID" "$(vlan_get_id ${device})"
199 cli_space
200 }
201
202 cli_device_bonded() {
203 local device=${1}
204
205 cli_headline 2 "Bonding information"
206
207 local master=$(bonding_slave_get_master ${port})
208 cli_print_fmt1 2 "Master" "${master}"
209
210 local mode=$(bonding_get_mode ${master})
211 if [ "${mode}" = "active-backup" ]; then
212 local active_slaves=$(bonding_get_slaves ${master} --active)
213 local active="false"
214 if list_match "${device}" ${active_slaves}; then
215 active="true"
216 fi
217 cli_print_fmt1 2 "Active slave" "$(cli_print_yesno ${active})"
218 fi
219
220 cli_space
221 }
222
223 cli_device_bonding() {
224 local device=${1}
225 assert isset device
226
227 cli_headline 2 "Bonding information"
228
229 local mode=$(bonding_get_mode ${device})
230
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}"
235 fi
236 cli_space
237
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.
242 local status
243 case "$(device_get_status ${slave})" in
244 ${STATUS_UP})
245 status=${MSG_DEVICE_STATUS_UP}
246 ;;
247 ${STATUS_DOWN})
248 status=${MSG_DEVICE_STATUS_DOWN}
249 ;;
250 ${STATUS_NOCARRIER})
251 status=${MSG_DEVICE_STATUS_NOCARRIER}
252 ;;
253 *)
254 status=${MSG_DEVICE_STATUS_UNKNOWN}
255 ;;
256 esac
257
258 if list_match "${slave}" ${active_slaves}; then
259 slave_suffix="(active)"
260 else
261 slave_suffix=""
262 fi
263 cli_print_fmt1 2 "Slave ${slave}" "${status} ${slave_suffix}"
264 done
265 cli_space
266 }
267
268 cli_device_show_queues() {
269 assert [ $# -eq 2 ]
270
271 local level=${1}
272 local device=${2}
273
274 cli_headline ${level} "Queues"
275 cli_print_fmt1 ${level} "Queue" "Processors"
276
277 local processors=$(system_get_processors)
278
279 local queue
280 for queue in $(device_get_queues ${device}); do
281 local smp_affinity=$(device_queue_get_smp_affinity ${device} ${queue})
282
283 local processor s=""
284 for (( processor = 0; processor < ${processors}; processor++ )); do
285 if ! isset smp_affinity || list_match ${processor} ${smp_affinity}; then
286 list_append s ${processor}
287 fi
288 done
289
290 cli_print_fmt1 ${level} "${queue}" "$(list_join s ", ")"
291 done
292 cli_space
293 }
294
295 cli_headline() {
296 local level=${1}
297 local format=${2}
298 shift 2
299
300 local ident=$(cli_ident ${level})
301
302 local out
303 printf -v out "${ident}${FONT_BOLD}${format}${FONT_RESET}\n" "$@"
304 printf "${out}"
305 }
306
307 cli_statusline() {
308 local level=${1}
309 shift
310
311 local head=${1}
312 shift
313
314 cli_print $(( ${level} - 1 )) "%-12s %s" "${head}" "$@"
315 }
316
317 cli_print() {
318 local level=${1}
319 local format=${2}
320 shift 2
321
322 local ident=$(cli_ident $(( ${level} + 1 )))
323
324 local out
325 printf -v out "${ident}${format}\n" "$@"
326 printf "${out}"
327 }
328
329 cli_print_fmt1() {
330 local level=${1}
331 shift
332
333 local space=$(( 34 - (${level} * ${#IDENT}) ))
334 local format="%-${space}s %s"
335
336 cli_print ${level} "${format}" "$@"
337 }
338
339 cli_print_bool() {
340 if [ "${1}" = "${EXIT_TRUE}" ]; then
341 echo "true"
342 else
343 echo "false"
344 fi
345 }
346
347 cli_print_yesno() {
348 if [ "${1}" = "${EXIT_TRUE}" ]; then
349 echo "yes"
350 else
351 echo "false"
352 fi
353 }
354
355 cli_print_enabled() {
356 enabled ${1}
357
358 cli_print_bool $?
359 }
360
361 cli_print_warning() {
362 local level=${1}
363 shift
364
365 cli_print ${level} "${CLR_YELLOW_B}%s${CLR_RESET}" "$@"
366 }
367
368 cli_space() {
369 printf "\n"
370 }
371
372 cli_ident() {
373 local level=${1}
374 assert isinteger level
375
376 local ident=""
377 while [ ${level} -gt 1 ]; do
378 ident="${ident}${IDENT}"
379 level=$(( ${level} - 1 ))
380 done
381
382 print "${ident}"
383 }
384
385 cli_yesno() {
386 local message="$@ [y/n] "
387 local yesno
388
389 while true; do
390 printf "\n${message}"
391 read yesno
392
393 # Check for "yes".
394 if list_match ${yesno} y Y yes YES Yes; then
395 return ${EXIT_TRUE}
396
397 # Check for "no".
398 elif list_match ${yesno} n N no NO No; then
399 return ${EXIT_FALSE}
400 fi
401 done
402 }
403
404 cli_get_key() {
405 local key="${1%%=*}"
406 echo "${key/--/}"
407 }
408
409 cli_get_val() {
410 echo "${@#*=}"
411 }
412
413 cli_get_bool() {
414 local value="$(cli_get_val "$@")"
415
416 if enabled value; then
417 print "true"
418 return ${EXIT_TRUE}
419 fi
420
421 print "false"
422 return ${EXIT_FALSE}
423 }
424
425 cli_usage() {
426 local command="$@"
427 local basename="$(basename ${0})"
428
429 if ! isset command; then
430 command="${basename} help"
431 fi
432
433 echo "The given command was not understood by ${basename}." >&2
434 echo "Please run '${command}' for detailed help." >&2
435 }
436
437 cli_show_man() {
438 local manpage=${1}
439 assert isset manpage
440
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."
444 exit ${EXIT_ERROR}
445 fi
446
447 man ${manpage}
448 }
449
450 cli_set_color() {
451 #Function to set the back and foreground color at once.
452 local fg=${1}
453 local bg=${2}
454
455 local i
456 for i in fg bg; do
457 # Skip if color is empty
458 [ -n "${!i}" ] || continue
459
460 # Skip for dash
461 [ "${!i}" = "-" ] && continue
462
463 color_set_shell ${i} ${!i}
464 done
465 }
466
467 cli_reset_color() {
468 #Reset the shell color.
469 printf "\e[0m"
470 }
471
472 cli_color_bar() {
473 # This function return some colored space
474 assert [ $# -eq 1 ]
475
476 local color=${1}
477 echo "$(cli_set_color - ${color}) ${CLR_RESET}"
478 }