]> git.ipfire.org Git - people/ms/network.git/commitdiff
Approach for better status output.
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 10 Jun 2012 09:38:54 +0000 (09:38 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 10 Jun 2012 09:38:54 +0000 (09:38 +0000)
13 files changed:
functions.cli
functions.device
functions.stp
hooks/zones/6to4-tunnel
hooks/zones/aiccu
hooks/zones/bridge
hooks/zones/isdn
hooks/zones/isdn-server
hooks/zones/modem
hooks/zones/pppoe
hooks/zones/teredo
hooks/zones/wireless
network

index a884e327332599b762c113f80e7b066a58b2b55f..579a263fc986202f4bd804924e44d0e3f6cf1522 100644 (file)
@@ -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"
 }
index a15586ecf7ec08a71f6ec76ff5e2f68e63dc5f27..29be51c08d66b6b855c0edbc1e6c95f831db75b2 100644 (file)
@@ -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}"
 }
index 1d3efc690e084bfb9e29f064c2f2008c922fc4f4..21505a648cc80c7a8975996840ac95b43818916b 100644 (file)
@@ -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}
 
index cfa7cb929815a8786380129ed6faae9feceb6a20..a45f2f999220354134223d9c8488dc0c87b97521 100755 (executable)
@@ -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}
 }
index e867ff4091dd7a83b41ad0c294abfb095f47540c..b49cb62d7dd407711b61ded045b60d9b687cdca7 100755 (executable)
@@ -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:" "<hidden>"
-       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" "<hidden>"
+       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}
 }
index 3db92aa832a1e817b100b23c76c1d4b27ae7fbd7..074cfb338e95aade83536a8d8f345e2fe531f7ca 100755 (executable)
@@ -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}
 }
 
index 82d066fbf98cb550c0cf1b7f2b84c973c441b477..df948e179537124d3f3b7b9a83ea16d92ec8a4c3 100755 (executable)
@@ -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:" "<hidden>"
-       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" "<hidden>"
+       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}
 }
 
index b8691e1290b3c578357d7fb708c88cfb63714252..f5d1c7ab776d61ed6183b74a0d83c065d6383d35 100755 (executable)
@@ -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}
 
index d7582846e76d5f2a109049b13374b3ba0f24a94c..57a90e01171b10d418fb3ecc02dcefd3d0d19002 100755 (executable)
@@ -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}
 
index fb6b0a9c761644c48e6e27674e08c36389dad8fa..df4bb46328923f2eca7dfeef3841404ef6630ed7 100755 (executable)
@@ -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:" "<hidden>"
-       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" "<hidden>"
+       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}
 }
 
index bfbe5711433a7844914186ce44b376dedca0dba6..1dd525e9af97adcd59edd79769457000fe67807e 100755 (executable)
@@ -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}
 }
index a9f6238663929425bee3abf0c16fd9d6342b53d9..22f911f34bbae0870544714154e19f513d237a33 100755 (executable)
@@ -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 dbd5dabf590adfd102422c5be333dcc358b25417..3e6b09358f911627d11eacce509ad0a8cebb2982 100755 (executable)
--- 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