]>
Commit | Line | Data |
---|---|---|
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 |
22 | IDENT=" " |
23 | ||
1c6a4e30 | 24 | cli_help_requested() { |
866de228 MT |
25 | local argument="${1}" |
26 | ||
27 | if [ -n "${argument}" ]; then | |
8c9205b1 | 28 | if list_match ${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 | 36 | cli_run_help() { |
4fedddef MT |
37 | local command="$@" |
38 | ||
39 | print "Run \"${command} help\" to get more information." | |
40 | return ${EXIT_OK} | |
41 | } | |
42 | ||
1c6a4e30 | 43 | cli_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}" | |
2e3317f4 | 115 | |
f7b8eb73 JS |
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 | ||
2e3317f4 JS |
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 | ||
f8f476d8 | 138 | if enabled long; then |
fe8e6d69 MT |
139 | cli_print_fmt1 1 "Address" "$(device_get_address ${device})" |
140 | fi | |
3cb2fc42 MT |
141 | if device_is_up ${device}; then |
142 | cli_print_fmt1 1 "MTU" "$(device_get_mtu ${device})" | |
143 | fi | |
f8f476d8 | 144 | if enabled long; then |
fe8e6d69 MT |
145 | device_is_promisc ${device} &>/dev/null |
146 | cli_print_fmt1 1 "Promisc" "$(cli_print_bool $?)" | |
147 | fi | |
3cb2fc42 MT |
148 | |
149 | cli_space | |
ec63256a | 150 | |
3cb2fc42 MT |
151 | # Print the device stats. |
152 | device_is_up ${device} && cli_device_stats 2 ${device} | |
fe8e6d69 | 153 | |
f8f476d8 | 154 | if enabled long; then |
fe8e6d69 | 155 | # Virtual devices. |
7951525a | 156 | device_is_vlan ${device} && cli_device_vlan ${device} |
fe8e6d69 MT |
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 | |
3cb2fc42 MT |
164 | } |
165 | ||
1c6a4e30 | 166 | cli_device_stats() { |
3cb2fc42 MT |
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 | |
ec63256a MT |
190 | } |
191 | ||
1c6a4e30 | 192 | cli_device_vlan() { |
fe8e6d69 MT |
193 | local device=${1} |
194 | ||
195 | cli_headline 2 "VLAN" | |
196 | ||
7951525a MT |
197 | cli_print_fmt1 2 "Parent" "$(vlan_get_parent ${device})" |
198 | cli_print_fmt1 2 "VID" "$(vlan_get_id ${device})" | |
fe8e6d69 MT |
199 | cli_space |
200 | } | |
201 | ||
1c6a4e30 | 202 | cli_device_bonded() { |
fe8e6d69 MT |
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 | ||
d1e71061 MT |
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 | ||
fe8e6d69 MT |
220 | cli_space |
221 | } | |
222 | ||
1c6a4e30 | 223 | cli_device_bonding() { |
fe8e6d69 | 224 | local device=${1} |
d1e71061 | 225 | assert isset device |
fe8e6d69 MT |
226 | |
227 | cli_headline 2 "Bonding information" | |
228 | ||
d1e71061 MT |
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 | |
fe8e6d69 MT |
236 | cli_space |
237 | ||
d1e71061 MT |
238 | local slave slave_suffix |
239 | local active_slaves=$(bonding_get_slaves ${device} --active) | |
fe8e6d69 | 240 | for slave in $(bonding_get_slaves ${device}); do |
d1e71061 MT |
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)" | |
fe8e6d69 | 260 | else |
d1e71061 | 261 | slave_suffix="" |
fe8e6d69 | 262 | fi |
d1e71061 | 263 | cli_print_fmt1 2 "Slave ${slave}" "${status} ${slave_suffix}" |
fe8e6d69 MT |
264 | done |
265 | cli_space | |
266 | } | |
267 | ||
6529dfaa MT |
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 | ||
1c6a4e30 | 295 | cli_headline() { |
8e3508ac MT |
296 | local level=${1} |
297 | local format=${2} | |
298 | shift 2 | |
9178284d | 299 | |
8e3508ac | 300 | local ident=$(cli_ident ${level}) |
9178284d | 301 | |
8e3508ac | 302 | local out |
ded49a6b | 303 | printf -v out "${ident}${FONT_BOLD}${format}${FONT_RESET}\n" "$@" |
8e3508ac | 304 | printf "${out}" |
9178284d MT |
305 | } |
306 | ||
1c6a4e30 | 307 | cli_statusline() { |
ec63256a MT |
308 | local level=${1} |
309 | shift | |
310 | ||
8e3508ac MT |
311 | local head=${1} |
312 | shift | |
ec63256a | 313 | |
8e3508ac | 314 | cli_print $(( ${level} - 1 )) "%-12s %s" "${head}" "$@" |
ec63256a MT |
315 | } |
316 | ||
1c6a4e30 | 317 | cli_print() { |
ec63256a MT |
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 | ||
1c6a4e30 | 329 | cli_print_fmt1() { |
ec63256a MT |
330 | local level=${1} |
331 | shift | |
332 | ||
e6993835 | 333 | local space=$(( 34 - (${level} * ${#IDENT}) )) |
ec63256a MT |
334 | local format="%-${space}s %s" |
335 | ||
336 | cli_print ${level} "${format}" "$@" | |
337 | } | |
338 | ||
1c6a4e30 | 339 | cli_print_bool() { |
ec63256a MT |
340 | if [ "${1}" = "${EXIT_TRUE}" ]; then |
341 | echo "true" | |
342 | else | |
343 | echo "false" | |
344 | fi | |
345 | } | |
346 | ||
1c6a4e30 | 347 | cli_print_yesno() { |
ec63256a MT |
348 | if [ "${1}" = "${EXIT_TRUE}" ]; then |
349 | echo "yes" | |
350 | else | |
351 | echo "false" | |
352 | fi | |
353 | } | |
354 | ||
1c6a4e30 | 355 | cli_print_enabled() { |
e145f826 MT |
356 | enabled ${1} |
357 | ||
358 | cli_print_bool $? | |
359 | } | |
360 | ||
1c6a4e30 | 361 | cli_print_warning() { |
3cb2fc42 MT |
362 | local level=${1} |
363 | shift | |
364 | ||
fcbf6823 | 365 | cli_print ${level} "${CLR_YELLOW_B}%s${CLR_RESET}" "$@" |
3cb2fc42 MT |
366 | } |
367 | ||
1c6a4e30 | 368 | cli_space() { |
ec63256a MT |
369 | printf "\n" |
370 | } | |
371 | ||
1c6a4e30 | 372 | cli_ident() { |
ec63256a | 373 | local level=${1} |
111c94e2 | 374 | assert isinteger level |
ec63256a MT |
375 | |
376 | local ident="" | |
377 | while [ ${level} -gt 1 ]; do | |
e145f826 | 378 | ident="${ident}${IDENT}" |
ec63256a MT |
379 | level=$(( ${level} - 1 )) |
380 | done | |
381 | ||
111c94e2 | 382 | print "${ident}" |
9178284d | 383 | } |
f90e550b | 384 | |
1c6a4e30 | 385 | cli_yesno() { |
39f552f5 | 386 | local message="$@ [y/n] " |
f90e550b MT |
387 | local yesno |
388 | ||
39f552f5 MT |
389 | while true; do |
390 | printf "\n${message}" | |
391 | read yesno | |
f90e550b | 392 | |
39f552f5 | 393 | # Check for "yes". |
8c9205b1 | 394 | if list_match ${yesno} y Y yes YES Yes; then |
39f552f5 | 395 | return ${EXIT_TRUE} |
f90e550b | 396 | |
39f552f5 | 397 | # Check for "no". |
8c9205b1 | 398 | elif list_match ${yesno} n N no NO No; then |
39f552f5 MT |
399 | return ${EXIT_FALSE} |
400 | fi | |
401 | done | |
f90e550b | 402 | } |
d76f5107 | 403 | |
1c6a4e30 | 404 | cli_get_key() { |
d76f5107 MT |
405 | local key="${1%%=*}" |
406 | echo "${key/--/}" | |
407 | } | |
408 | ||
1c6a4e30 | 409 | cli_get_val() { |
204bb6ad | 410 | echo "${@#*=}" |
d76f5107 | 411 | } |
de28a630 | 412 | |
1c6a4e30 | 413 | cli_get_bool() { |
08e40c8c MT |
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 | ||
1c6a4e30 | 425 | cli_usage() { |
de28a630 MT |
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 | ||
1c6a4e30 | 437 | cli_show_man() { |
de28a630 MT |
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 | } | |
2e3317f4 JS |
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 | } |