From: Michael Tremer Date: Sun, 10 Jun 2012 09:38:54 +0000 (+0000) Subject: Approach for better status output. X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=3cb2fc42e552f747b53123dfe6910664bede0c0a;p=people%2Fstevee%2Fnetwork.git Approach for better status output. --- diff --git a/functions.cli b/functions.cli index a884e327..579a263f 100644 --- a/functions.cli +++ b/functions.cli @@ -31,13 +31,81 @@ function cli_help_requested() { return ${EXIT_ERROR} } -function cli_zone_headline() { - local zone=${1} +function cli_device_headline() { + local device=${1} + assert isset device + + local type + if zone_exists ${device}; then + type="zone" + elif port_exists ${device}; then + type="port" + else + type="unknown" + fi + + local headline_prefix + case "${type}" in + zone) + headline_prefix="Zone ${device}" + ;; + port) + headline_prefix="Port ${device}" + ;; + *) + headline_prefix="Device ${device}" + ;; + esac + + cli_headline 1 "${headline_prefix}:" + + # Print the hook for all zones. + if [ "${type}" = "zone" ]; then + cli_print_fmt1 1 "Hook" "$(zone_get_hook ${device})" + fi + + # Print the device status. + local status=$(device_get_status ${device}) + cli_print_fmt1 1 "Status" "${STATUS_COLOUR[${status}]}${STATUS_TEXT[${status}]}${COLOUR_NORMAL}" + if device_is_up ${device}; then + cli_print_fmt1 1 "MTU" "$(device_get_mtu ${device})" + fi + + cli_space - cli_status_headline ${zone} + # Print the device stats. + device_is_up ${device} && cli_device_stats 2 ${device} +} + +function cli_device_stats() { + local level=${1} + local device=${2} + + # This section will print statistical data from the device. + local packets bytes errors + + cli_headline ${level} "Statistics" + local format="%-10s %9d packets %6s (%d errors)" + + # RX + packets=$(device_get_rx_packets ${device}) + bytes=$(device_get_rx_bytes ${device}) + errors=$(device_get_rx_errors ${device}) + + cli_print ${level} "${format}" "Received" "${packets}" "$(beautify_bytes ${bytes})" "${errors}" + + # TX + packets=$(device_get_tx_packets ${device}) + bytes=$(device_get_tx_bytes ${device}) + errors=$(device_get_tx_errors ${device}) + + cli_print ${level} "${format}" "Sent" "${packets}" "$(beautify_bytes ${bytes})" "${errors}" + cli_space } function cli_status_headline() { + echo "XXX THIS FUNCTION IS DEPRECATED" + local zone=${1} local state="${COLOUR_DOWN}DOWN${COLOUR_NORMAL}" @@ -94,6 +162,13 @@ function cli_print_yesno() { fi } +function cli_print_warning() { + local level=${1} + shift + + cli_print ${level} "${COLOUR_WARN}%s${COLOUR_NORMAL}" "$@" +} + function cli_space() { printf "\n" } diff --git a/functions.device b/functions.device index a15586ec..29be51c0 100644 --- a/functions.device +++ b/functions.device @@ -228,20 +228,17 @@ function device_get_type() { function device_get_status() { local device=${1} - assert isset device - local status=${STATUS_UNKNOWN} + local status=${STATUS_DOWN} - if ! device_has_carrier ${device}; then - status=${STATUS_NOCARRIER} - elif device_is_up ${device}; then + if device_is_up ${device}; then status=${STATUS_UP} - elif device_is_down ${device}; then - status=${STATUS_DOWN} - fi - assert isset status + if ! device_has_carrier ${device}; then + status=${STATUS_NOCARRIER} + fi + fi echo "${status}" } diff --git a/functions.stp b/functions.stp index 1d3efc69..21505a64 100644 --- a/functions.stp +++ b/functions.stp @@ -54,6 +54,22 @@ function stp_disable() { brctl stp ${bridge} off } +function stp_is_enabled() { + local bridge=${1} + assert isset bridge + + local state=$(__device_get_file ${bridge} bridge/stp_state) + + case "${state}" in + 0) + return ${EXIT_FALSE} + ;; + *) + return ${EXIT_TRUE} + ;; + esac +} + function stp_get_name() { local proto=${1} diff --git a/hooks/zones/6to4-tunnel b/hooks/zones/6to4-tunnel index cfa7cb92..a45f2f99 100755 --- a/hooks/zones/6to4-tunnel +++ b/hooks/zones/6to4-tunnel @@ -109,7 +109,7 @@ function _status() { local zone=${1} assert isset zone - cli_status_headline ${zone} + cli_device_headline ${zone} zone_config_read ${zone} @@ -119,11 +119,11 @@ function _status() { server_line="${server_line} (Hostname: ${server_hostname})" fi - cli_headline " Configuration:" - printf "${DEVICE_PRINT_LINE1}" "Server:" "${server_line}" - printf "${DEVICE_PRINT_LINE1}" "Endpoint IPv4 address:" "${LOCAL_ADDRESS}" - printf "${DEVICE_PRINT_LINE1}" "Endpoint IPv6 address:" "${LOCAL_ADDRESS6}" - echo + cli_headline 2 "Configuration" + cli_print_fmt1 2 "Server" "${server_line}" + cli_print_fmt1 2 "Endpoint IPv4 address" "${LOCAL_ADDRESS}" + cli_print_fmt1 2 "Endpoint IPv6 address" "${LOCAL_ADDRESS6}" + cli_space exit ${EXIT_OK} } diff --git a/hooks/zones/aiccu b/hooks/zones/aiccu index e867ff40..b49cb62d 100755 --- a/hooks/zones/aiccu +++ b/hooks/zones/aiccu @@ -94,36 +94,23 @@ function _down() { function _status() { local zone=${1} - assert isset zone - cli_status_headline ${zone} + cli_device_headline ${zone} zone_config_read ${zone} - cli_headline " Configuration:" - printf "${DEVICE_PRINT_LINE1}" "User:" "${USER}" - printf "${DEVICE_PRINT_LINE1}" "Secret:" "" - echo - printf "${DEVICE_PRINT_LINE1}" "Server:" "${SERVER}" - printf "${DEVICE_PRINT_LINE1}" "Protocol:" "${PROTOCOL}" + cli_headline 2 "Configuration" + cli_print_fmt1 2 "User" "${USER}" + cli_print_fmt1 2 "Secret" "" + cli_space + cli_print_fmt1 2 "Server" "${SERVER}" + cli_print_fmt1 2 "Protocol" "${PROTOCOL}" if isset TUNNEL_ID; then - echo - printf "${DEVICE_PRINT_LINE1}" "Tunnel ID:" "${TUNNEL_ID}" - fi - echo - printf "${DEVICE_PRINT_LINE1}" "Use default route?" "$(enabled DEFAULTROUTE && echo "enabled" || echo "disabled")" - printf "${DEVICE_PRINT_LINE1}" "Use peer DNS?" "$(enabled PEERDNS && echo "enabled" || echo "disabled")" - - # Exit if zone is down - if ! zone_is_up ${zone}; then - echo # Empty line - exit ${EXIT_ERROR} + cli_space + cli_print_fmt1 2 "Tunnel ID" "${TUNNEL_ID}" fi - - cli_headline " Protocol information:" - printf "${DEVICE_PRINT_LINE1}" "MTU:" "$(device_get_mtu ${zone})" - echo + cli_space exit ${EXIT_OK} } diff --git a/hooks/zones/bridge b/hooks/zones/bridge index 3db92aa8..074cfb33 100755 --- a/hooks/zones/bridge +++ b/hooks/zones/bridge @@ -154,8 +154,10 @@ function _down() { function _status() { local zone=${1} + assert isset zone - cli_status_headline ${zone} + # Print the default header. + cli_device_headline ${zone} # Exit if zone is down if ! zone_is_up ${zone}; then @@ -163,38 +165,44 @@ function _status() { exit ${EXIT_ERROR} fi - cli_headline " Spanning Tree Protocol information:" - local proto=$(stp_bridge_get_protocol ${zone}) - local ret=$? + cli_headline 2 "Spanning Tree Protocol information" + if stp_is_enabled ${zone}; then + local proto=$(stp_bridge_get_protocol ${zone}) - if [ ${ret} -eq 0 ]; then - printf "${DEVICE_PRINT_LINE1}" "Version:" "$(stp_get_name ${proto})" - printf "${DEVICE_PRINT_LINE1}" "ID:" $(stp_bridge_get_id ${zone}) - printf "${DEVICE_PRINT_LINE1}" "Priority:" $(stp_bridge_get_priority ${zone}) + cli_print_fmt1 2 "Version" "$(stp_get_name ${proto})" + cli_print_fmt1 2 "ID" "$(stp_bridge_get_id ${zone})" + cli_print_fmt1 2 "Priority" "$(stp_bridge_get_priority ${zone})" if stp_bridge_is_root ${zone}; then - echo -e " ${COLOUR_BOLD}This bridge is root.${COLOUR_NORMAL}" + cli_print 2 "This bridge is root." else - printf "${DEVICE_PRINT_LINE1}" "Designated root:" $(stp_bridge_get_designated_root ${zone}) - printf "${DEVICE_PRINT_LINE1}" "Root path cost:" $(stp_bridge_get_root_path_cost ${zone}) + cli_print_fmt1 2 "Designated root" \ + "$(stp_bridge_get_designated_root ${zone})" + cli_print_fmt1 2 "Root path cost" \ + "$(stp_bridge_get_root_path_cost ${zone})" fi - echo # Empty line + cli_space # Topology information - printf "${DEVICE_PRINT_LINE1}" "Topology changing:" $(stp_bridge_get_topology_change_detected ${zone}) - printf "${DEVICE_PRINT_LINE1}" "Topology change time:" $(beautify_time $(stp_bridge_get_topology_change_timer ${zone})) - printf "${DEVICE_PRINT_LINE1}" "Topology change count:" $(stp_bridge_get_topology_change_count ${zone}) + cli_print_fmt1 2 "Topology changing" \ + "$(stp_bridge_get_topology_change_detected ${zone})" + cli_print_fmt1 2 "Topology change time" \ + "$(beautify_time $(stp_bridge_get_topology_change_timer ${zone}))" + cli_print_fmt1 2 "Topology change count" \ + "$(stp_bridge_get_topology_change_count ${zone})" + cli_space else - echo -e " ${COLOUR_BOLD}STP is disabled.${COLOUR_NORMAL}" + cli_print 2 "Disabled" + cli_space fi - cli_headline " Ports:" + cli_headline 2 "Ports:" zone_ports_status ${zone} - cli_headline " Configurations:" + cli_headline 2 "Configurations:" zone_configs_cmd status ${zone} - echo # Empty line + cli_space exit ${EXIT_OK} } diff --git a/hooks/zones/isdn b/hooks/zones/isdn index 82d066fb..df948e17 100755 --- a/hooks/zones/isdn +++ b/hooks/zones/isdn @@ -171,47 +171,36 @@ function _down() { function _status() { local zone=${1} - assert isset zone - cli_status_headline ${zone} + cli_device_headline ${zone} zone_config_read ${zone} - cli_headline " Configuration:" - printf "${DEVICE_PRINT_LINE1}" "User:" "${USER}" - printf "${DEVICE_PRINT_LINE1}" "Secret:" "" - echo - printf "${DEVICE_PRINT_LINE1}" "MTU:" "${MTU}" - printf "${DEVICE_PRINT_LINE1}" "Use default route?" "$(enabled DEFAULTROUTE && echo "enabled" || echo "disabled")" - printf "${DEVICE_PRINT_LINE1}" "Use peer DNS?" "$(enabled PEERDNS && echo "enabled" || echo "disabled")" - echo + cli_headline 2 "Configuration:" + cli_print_fmt1 2 "User" "${USER}" + cli_print_fmt1 2 "Secret" "" + cli_space if device_exists ${zone}; then - cli_headline " ISDN information:" - printf "${DEVICE_PRINT_LINE1}" "L2 protocol:" "$(isdn_get_l2proto ${zone})" - printf "${DEVICE_PRINT_LINE1}" "L3 protocol:" "$(isdn_get_l3proto ${zone})" - printf "${DEVICE_PRINT_LINE1}" "Encapsulation:" "$(isdn_get_encap ${zone})" - echo + cli_headline 3 "ISDN information:" + cli_print_fmt1 3 "L2 protocol" "$(isdn_get_l2proto ${zone})" + cli_print_fmt1 3 "L3 protocol" "$(isdn_get_l3proto ${zone})" + cli_print_fmt1 3 "Encapsulation" "$(isdn_get_encap ${zone})" + cli_space fi # Exit if zone is down - if ! zone_is_up ${zone}; then - echo # Empty line - exit ${EXIT_ERROR} - fi + zone_is_up ${zone} || exit ${EXIT_ERROR} # XXX display time since connection started - cli_headline " Point-to-Point-over-Ethernet protocol:" - echo " IP-Address : $(routing_db_get ${zone} local-ip-address)" - echo " Gateway : $(routing_db_get ${zone} remote-ip-address)" - echo " DNS-Server : $(routing_db_get ${zone} dns)" - echo - echo " MAC-Remote : $(routing_db_get ${zone} remote-address)" - echo - echo " MTU : $(device_get_mtu ${zone})" - echo # Empty line + cli_headline 2 "Point-to-Point protocol" + cli_print_fmt1 2 "IP address" "$(routing_db_get ${zone} local-ip-address)" + cli_print_fmt1 2 "Gateway" "$(routing_db_get ${zone} remote-ip-address)" + cli_print_fmt1 2 "DNS servers" "$(routing_db_get ${zone} dns)" + cli_space + exit ${EXIT_OK} } diff --git a/hooks/zones/isdn-server b/hooks/zones/isdn-server index b8691e12..f5d1c7ab 100755 --- a/hooks/zones/isdn-server +++ b/hooks/zones/isdn-server @@ -187,10 +187,9 @@ function _down() { function _status() { local zone=${1} - assert isset zone - cli_status_headline ${zone} + cli_device_headline ${zone} zone_config_read ${zone} diff --git a/hooks/zones/modem b/hooks/zones/modem index d7582846..57a90e01 100755 --- a/hooks/zones/modem +++ b/hooks/zones/modem @@ -188,10 +188,9 @@ function _down() { function _status() { local zone=${1} - assert isset zone - cli_status_headline ${zone} + cli_device_headline ${zone} zone_config_read ${zone} diff --git a/hooks/zones/pppoe b/hooks/zones/pppoe index fb6b0a9c..df4bb463 100755 --- a/hooks/zones/pppoe +++ b/hooks/zones/pppoe @@ -240,26 +240,26 @@ function _discover() { function _status() { local zone=${1} - assert isset zone - cli_status_headline ${zone} + cli_device_headline ${zone} zone_config_read ${zone} - cli_headline " Configuration:" - printf "${DEVICE_PRINT_LINE1}" "User:" "${USER}" - printf "${DEVICE_PRINT_LINE1}" "Secret:" "" - echo - printf "${DEVICE_PRINT_LINE1}" "MTU:" "${MTU}" - printf "${DEVICE_PRINT_LINE1}" "IPv6:" "$(enabled IPV6 && echo "enabled" || echo "disabled")" - printf "${DEVICE_PRINT_LINE1}" "Use default route?" "$(enabled DEFAULTROUTE && echo "enabled" || echo "disabled")" - printf "${DEVICE_PRINT_LINE1}" "Use peer DNS?" "$(enabled PEERDNS && echo "enabled" || echo "disabled")" - echo - cli_headline " Ports:" + cli_headline 2 "Configuration" + cli_print_fmt1 2 "User" "${USER}" + cli_print_fmt1 2 "Secret" "" + cli_space + + enabled IPV6 &>/dev/null + local ipv6_enabled=$? + cli_print_fmt1 2 "IPv6?" "$(cli_print_bool ${ipv6_enabled})" + + cli_headline 2 "Ports" zone_ports_status ${zone} if [ -z "$(zone_get_ports ${zone})" ]; then - echo -e " ${COLOUR_WARN}No ports attached. Won't be able to start.${COLOUR_NORMAL}" + cli_print_warning "No ports attached. Won't be able to start." + cli_space fi # Exit if zone is down @@ -270,24 +270,33 @@ function _status() { # XXX display time since connection started - cli_headline " Point-to-Point-over-Ethernet protocol:" + cli_headline 2 "Point-to-Point-over-Ethernet protocol" local proto for proto in ${IP_SUPPORTED_PROTOCOLS}; do routing_db_exists ${zone} ${proto} || continue - if [ "${proto}" = "ipv6" ]; then - echo " Internet Protocol Version 6:" - elif [ "${proto}" = "ipv4" ]; then - echo " Internet Protocol Version 4:" - fi - echo " IP-Address : $(routing_db_get ${zone} ${proto} local-ip-address)" - echo " Gateway : $(routing_db_get ${zone} ${proto} remote-ip-address)" - echo " DNS-Server : $(routing_db_get ${zone} ${proto} dns)" - echo - echo " MAC-Remote : $(routing_db_get ${zone} ${proto} remote-address)" - echo + + local headline + case "${proto}" in + ipv6) + headline="Internet Protocol Version 6" + ;; + ipv4) + headline="Internet Protocol Version 4" + ;; + *) + headline="Unkown protocol" + ;; + esac + cli_headline 3 "${headline}" + + cli_print_fmt1 3 "IP address" "$(routing_db_get ${zone} ${proto} local-ip-address)" + cli_print_fmt1 3 "Gateway" "$(routing_db_get ${zone} ${proto} remote-ip-address)" + cli_print_fmt1 3 "DNS servers" "$(routing_db_get ${zone} ${proto} dns)" + cli_space + cli_print_fmt1 3 "MAC-Remote" "$(routing_db_get ${zone} ${proto} remote-address)" + cli_space done - echo " MTU : $(device_get_mtu ${zone})" - echo # Empty line + exit ${EXIT_OK} } diff --git a/hooks/zones/teredo b/hooks/zones/teredo index bfbe5711..1dd525e9 100755 --- a/hooks/zones/teredo +++ b/hooks/zones/teredo @@ -66,25 +66,25 @@ function _down() { function _status() { local zone=${1} - assert isset zone - cli_status_headline ${zone} + # Print a nice header. + cli_device_headline ${zone} zone_config_read ${zone} - cli_headline " Configuration:" - printf "${DEVICE_PRINT_LINE1}" "Server:" "${SERVER}" + cli_headline 2 "Configuration" + cli_print_fmt1 2 "Server" "${SERVER}" # Exit if zone is down if ! zone_is_up ${zone}; then - echo # Empty line + cli_space exit ${EXIT_ERROR} fi - cli_headline " Protocol information:" - printf "${DEVICE_PRINT_LINE1}" "MTU:" "$(device_get_mtu ${zone})" - echo + cli_headline 2 "Protocol information" + cli_print_fmt1 2 "MTU" "$(device_get_mtu ${zone})" + cli_space exit ${EXIT_OK} } diff --git a/hooks/zones/wireless b/hooks/zones/wireless index a9f62386..22f911f3 100755 --- a/hooks/zones/wireless +++ b/hooks/zones/wireless @@ -115,8 +115,9 @@ function _down() { function _status() { local zone=${1} + assert isset zone - cli_status_headline ${zone} + cli_device_headline ${zone} # Exit if zone is down if ! zone_is_up ${zone}; then diff --git a/network b/network index dbd5dabf..3e6b0935 100755 --- a/network +++ b/network @@ -115,26 +115,8 @@ function cli_device_status() { cli_print_fmt1 1 "MTU" "$(device_get_mtu ${device})" cli_space - # This section will print statistical data from the device. - local packets bytes errors - - cli_headline 2 "Statistics" - local format="%-10s %9d packets %6s (%d errors)" - - # RX - packets=$(device_get_rx_packets ${device}) - bytes=$(device_get_rx_bytes ${device}) - errors=$(device_get_rx_errors ${device}) - - cli_print 2 "${format}" "Received" "${packets}" "$(beautify_bytes ${bytes})" "${errors}" - - # TX - packets=$(device_get_tx_packets ${device}) - bytes=$(device_get_tx_bytes ${device}) - errors=$(device_get_tx_errors ${device}) - - cli_print 2 "${format}" "Sent" "${packets}" "$(beautify_bytes ${bytes})" "${errors}" - cli_space + # Print device statistics. + cli_device_stats 2 ${device} # Print some more information. device_has_carrier ${device} &>/dev/null