]> git.ipfire.org Git - people/stevee/network.git/blob - src/functions/functions.cli
Use autotools.
[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 headline_prefix="${headline_prefix} ($(zone_get_hook ${device}))"
83 fi
84 cli_headline 1 "${headline_prefix}"
85
86 # Print the device status.
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}"
103 if enabled long; then
104 cli_print_fmt1 1 "Address" "$(device_get_address ${device})"
105 fi
106 if device_is_up ${device}; then
107 cli_print_fmt1 1 "MTU" "$(device_get_mtu ${device})"
108 fi
109 if enabled long; then
110 device_is_promisc ${device} &>/dev/null
111 cli_print_fmt1 1 "Promisc" "$(cli_print_bool $?)"
112 fi
113
114 cli_space
115
116 # Print the device stats.
117 device_is_up ${device} && cli_device_stats 2 ${device}
118
119 if enabled long; then
120 # Virtual devices.
121 device_is_vlan ${device} && cli_device_vlan ${device}
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
129 }
130
131 function 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
155 }
156
157 function cli_device_vlan() {
158 local device=${1}
159
160 cli_headline 2 "VLAN"
161
162 cli_print_fmt1 2 "Parent" "$(vlan_get_parent ${device})"
163 cli_print_fmt1 2 "VID" "$(vlan_get_id ${device})"
164 cli_space
165 }
166
167 function 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
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
185 cli_space
186 }
187
188 function cli_device_bonding() {
189 local device=${1}
190 assert isset device
191
192 cli_headline 2 "Bonding information"
193
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
201 cli_space
202
203 local slave slave_suffix
204 local active_slaves=$(bonding_get_slaves ${device} --active)
205 for slave in $(bonding_get_slaves ${device}); do
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)"
225 else
226 slave_suffix=""
227 fi
228 cli_print_fmt1 2 "Slave ${slave}" "${status} ${slave_suffix}"
229 done
230 cli_space
231 }
232
233 function cli_headline() {
234 local level=${1}
235 local format=${2}
236 shift 2
237
238 local ident=$(cli_ident ${level})
239
240 local out
241 printf -v out "${ident}${CLR_BLACK_B}${format}${CLR_RESET}\n" "$@"
242 printf "${out}"
243 }
244
245 function cli_statusline() {
246 local level=${1}
247 shift
248
249 local head=${1}
250 shift
251
252 cli_print $(( ${level} - 1 )) "%-12s %s" "${head}" "$@"
253 }
254
255 function 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
267 function cli_print_fmt1() {
268 local level=${1}
269 shift
270
271 local space=$(( 34 - (${level} * ${#IDENT}) ))
272 local format="%-${space}s %s"
273
274 cli_print ${level} "${format}" "$@"
275 }
276
277 function cli_print_bool() {
278 if [ "${1}" = "${EXIT_TRUE}" ]; then
279 echo "true"
280 else
281 echo "false"
282 fi
283 }
284
285 function cli_print_yesno() {
286 if [ "${1}" = "${EXIT_TRUE}" ]; then
287 echo "yes"
288 else
289 echo "false"
290 fi
291 }
292
293 function cli_print_enabled() {
294 enabled ${1}
295
296 cli_print_bool $?
297 }
298
299 function cli_print_warning() {
300 local level=${1}
301 shift
302
303 cli_print ${level} "${CLR_YELLOW_B}%s${CLR_RESET}" "$@"
304 }
305
306 function cli_space() {
307 printf "\n"
308 }
309
310 function cli_ident() {
311 local level=${1}
312 assert isinteger level
313
314 local ident=""
315 while [ ${level} -gt 1 ]; do
316 ident="${ident}${IDENT}"
317 level=$(( ${level} - 1 ))
318 done
319
320 print "${ident}"
321 }
322
323 function cli_yesno() {
324 local message="$@ [y/n] "
325 local yesno
326
327 while true; do
328 printf "\n${message}"
329 read yesno
330
331 # Check for "yes".
332 if listmatch ${yesno} y Y yes YES Yes; then
333 return ${EXIT_TRUE}
334
335 # Check for "no".
336 elif listmatch ${yesno} n N no NO No; then
337 return ${EXIT_FALSE}
338 fi
339 done
340 }
341
342 function cli_get_key() {
343 local key="${1%%=*}"
344 echo "${key/--/}"
345 }
346
347 function cli_get_val() {
348 echo "${@#*=}"
349 }
350
351 function 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
363 function 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
375 function 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 }