]> git.ipfire.org Git - people/stevee/network.git/blob - src/functions/functions.cli
network status: Show if zones are enabled to start at boot
[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 function cli_help_requested() {
25 local argument="${1}"
26
27 if [ -n "${argument}" ]; then
28 if listmatch ${argument} help -h --help; then
29 return ${EXIT_OK}
30 fi
31 fi
32
33 return ${EXIT_ERROR}
34 }
35
36 function cli_run_help() {
37 local command="$@"
38
39 print "Run \"${command} help\" to get more information."
40 return ${EXIT_OK}
41 }
42
43 function 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 if enabled long; then
116 cli_print_fmt1 1 "Address" "$(device_get_address ${device})"
117 fi
118 if device_is_up ${device}; then
119 cli_print_fmt1 1 "MTU" "$(device_get_mtu ${device})"
120 fi
121 if enabled long; then
122 device_is_promisc ${device} &>/dev/null
123 cli_print_fmt1 1 "Promisc" "$(cli_print_bool $?)"
124 fi
125
126 cli_space
127
128 # Print the device stats.
129 device_is_up ${device} && cli_device_stats 2 ${device}
130
131 if enabled long; then
132 # Virtual devices.
133 device_is_vlan ${device} && cli_device_vlan ${device}
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
141 }
142
143 function cli_device_stats() {
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
167 }
168
169 function cli_device_vlan() {
170 local device=${1}
171
172 cli_headline 2 "VLAN"
173
174 cli_print_fmt1 2 "Parent" "$(vlan_get_parent ${device})"
175 cli_print_fmt1 2 "VID" "$(vlan_get_id ${device})"
176 cli_space
177 }
178
179 function cli_device_bonded() {
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
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
197 cli_space
198 }
199
200 function cli_device_bonding() {
201 local device=${1}
202 assert isset device
203
204 cli_headline 2 "Bonding information"
205
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
213 cli_space
214
215 local slave slave_suffix
216 local active_slaves=$(bonding_get_slaves ${device} --active)
217 for slave in $(bonding_get_slaves ${device}); do
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)"
237 else
238 slave_suffix=""
239 fi
240 cli_print_fmt1 2 "Slave ${slave}" "${status} ${slave_suffix}"
241 done
242 cli_space
243 }
244
245 function cli_headline() {
246 local level=${1}
247 local format=${2}
248 shift 2
249
250 local ident=$(cli_ident ${level})
251
252 local out
253 printf -v out "${ident}${CLR_BLACK_B}${format}${CLR_RESET}\n" "$@"
254 printf "${out}"
255 }
256
257 function cli_statusline() {
258 local level=${1}
259 shift
260
261 local head=${1}
262 shift
263
264 cli_print $(( ${level} - 1 )) "%-12s %s" "${head}" "$@"
265 }
266
267 function cli_print() {
268 local level=${1}
269 local format=${2}
270 shift 2
271
272 local ident=$(cli_ident $(( ${level} + 1 )))
273
274 local out
275 printf -v out "${ident}${format}\n" "$@"
276 printf "${out}"
277 }
278
279 function cli_print_fmt1() {
280 local level=${1}
281 shift
282
283 local space=$(( 34 - (${level} * ${#IDENT}) ))
284 local format="%-${space}s %s"
285
286 cli_print ${level} "${format}" "$@"
287 }
288
289 function cli_print_bool() {
290 if [ "${1}" = "${EXIT_TRUE}" ]; then
291 echo "true"
292 else
293 echo "false"
294 fi
295 }
296
297 function cli_print_yesno() {
298 if [ "${1}" = "${EXIT_TRUE}" ]; then
299 echo "yes"
300 else
301 echo "false"
302 fi
303 }
304
305 function cli_print_enabled() {
306 enabled ${1}
307
308 cli_print_bool $?
309 }
310
311 function cli_print_warning() {
312 local level=${1}
313 shift
314
315 cli_print ${level} "${CLR_YELLOW_B}%s${CLR_RESET}" "$@"
316 }
317
318 function cli_space() {
319 printf "\n"
320 }
321
322 function cli_ident() {
323 local level=${1}
324 assert isinteger level
325
326 local ident=""
327 while [ ${level} -gt 1 ]; do
328 ident="${ident}${IDENT}"
329 level=$(( ${level} - 1 ))
330 done
331
332 print "${ident}"
333 }
334
335 function cli_yesno() {
336 local message="$@ [y/n] "
337 local yesno
338
339 while true; do
340 printf "\n${message}"
341 read yesno
342
343 # Check for "yes".
344 if listmatch ${yesno} y Y yes YES Yes; then
345 return ${EXIT_TRUE}
346
347 # Check for "no".
348 elif listmatch ${yesno} n N no NO No; then
349 return ${EXIT_FALSE}
350 fi
351 done
352 }
353
354 function cli_get_key() {
355 local key="${1%%=*}"
356 echo "${key/--/}"
357 }
358
359 function cli_get_val() {
360 echo "${@#*=}"
361 }
362
363 function cli_get_bool() {
364 local value="$(cli_get_val "$@")"
365
366 if enabled value; then
367 print "true"
368 return ${EXIT_TRUE}
369 fi
370
371 print "false"
372 return ${EXIT_FALSE}
373 }
374
375 function cli_usage() {
376 local command="$@"
377 local basename="$(basename ${0})"
378
379 if ! isset command; then
380 command="${basename} help"
381 fi
382
383 echo "The given command was not understood by ${basename}." >&2
384 echo "Please run '${command}' for detailed help." >&2
385 }
386
387 function cli_show_man() {
388 local manpage=${1}
389 assert isset manpage
390
391 if ! binary_exists man; then
392 error "The man package is not installed on this system."
393 error "Please install 'man' in order to view the help."
394 exit ${EXIT_ERROR}
395 fi
396
397 man ${manpage}
398 }