Advertisement
Guest User

sysinfo.sh OpenWrt

a guest
Aug 23rd, 2016
486
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 13.70 KB | None | 0 0
  1. #!/bin/sh
  2. #
  3. # sysinfo.sh dla OpenWRT AA Gelip 2016
  4. #
  5. #    1.00    CJ    Pierwsza wersja kodu
  6. #    1.01    RD    Drobna przebudowa
  7. #    1.02    RD    Korekta błędu wy¶w. zajeto¶ci Flash-a, dodanie kolorów
  8. #    1.03    RD    Dodanie nazwy routera, zmiana formatowania
  9. #    1.04    RD    Kosmetyka, sugestie @mikhnal. Zmiana przetwarzania info. o wan.
  10. #    1.05    RD    Zmiana algorytmu pobierania danych dla wan i lan
  11. #    1.06    RD    Parametryzacja kolorów i pojawiania się podkre¶leń
  12. #    1.07    RD    Modyfikacja zwi±zana z poprawnym wy¶wietlaniem interfejsu dla prot.3g
  13. #    1.08    RD    Modyfikacja wy¶wietlania DNS-ów dla wan, dodanie uptime dla interfejsów
  14. #    1.09    RD    Dodanie statusu "Down" dla wył±czonego wifi, zmiana wy¶wietlania dla WLAN(sta)
  15. #    1.10    RD    Korekta wy¶wietlania dla WLAN(sta)
  16. #    1.11    RD    Korekta wy¶wietlania stanu pamięci, sugestie @dopsz
  17. #    1.12    RD    Zmiana kolejno¶ci wy¶wietlania warto¶ci stanu pamięci + kosmetyka
  18. #    1.12.1  G     Zmiana wy¶wietlania 'Machine & Name' na 'System type'
  19. #
  20. # Destination /sbin/sysinfo.sh
  21. #
  22. . /usr/share/libubox/jshn.sh
  23.  
  24. local Width=70
  25. local StartRuler="1"
  26. local EndRuler="1"
  27. local Rouler
  28. local NormalColor
  29. local MachineColor
  30. local ValueColor
  31. local AddrColor
  32. local RXTXColor
  33.  
  34. initialize() { # <Script Parameters>
  35.     local ColorMode="1"
  36.     for Parameter in $@; do
  37.         case  $Parameter  in
  38.         -m) ColorMode="0";;
  39.         -sr) StartRuler="0";;
  40.         -er) EndRuler="0";;
  41.         -w1) Width=80;;
  42.         -w2) Width=100;;
  43.         -w3) Width=120;;
  44.         -h|*)    
  45.             echo "Usage: $0 - [parameter]"
  46.             echo "    -h    : This help."
  47.             echo "    -m    : Display mono version."
  48.             echo "    -sr    : Without start horizontal ruler."    
  49.             echo "    -er    : Without end horizontal ruler."    
  50.             exit 1;;
  51.         esac
  52.     done
  53.     if [ "$ColorMode" == "1" ]; then
  54.         NormalColor="\e[0m"
  55.         MachineColor="\e[0;33m"
  56.         ValueColor="\e[1;36m"
  57.         AddrColor="\e[1;31m"
  58.         RXTXColor="\e[2;32m"
  59.     else
  60.         NormalColor="\e[0m"
  61.         MachineColor="\e[7m"
  62.         ValueColor="\e[1m"
  63.         AddrColor="\e[4m"
  64.         RXTXColor="\e[1m"
  65.     fi
  66.     local i
  67.     for i in $(seq $(expr $Width + 4 )); do
  68.         Rouler="$Rouler-";
  69.     done
  70. }
  71.  
  72. human_readable() { # <Number of bytes>
  73.     if [ $1 -gt 0 ]; then
  74.         printf "$(awk -v n=$1 'BEGIN{for(i=split("B KB MB GB TB PB",suffix);s<1;i--)s=n/(2**(10*i));printf (int(s)==s)?"%.0f%s":"%.1f%s",s,suffix[i+2]}')"
  75.     else
  76.         printf "0B"
  77.     fi
  78. }
  79.  
  80. device_rx_tx() { # <Device>
  81.     local RXTX=$(awk -v Device=$1 '$1==Device ":"{printf "%.0f\t%.0f",$2,$10}' /proc/net/dev)
  82.     [ "$RXTX" != "" ] && printf ", rx/tx: $RXTXColor$(human_readable $(echo "$RXTX" | cut -f 1))$NormalColor/$RXTXColor$(human_readable $(echo "$RXTX" | cut -f 2))$NormalColor"
  83. }
  84.  
  85. uptime_str() { # <Time in Seconds>
  86.     local Uptime=$1
  87.     if [ $Uptime -gt 0 ]; then
  88.         local Days=$(expr $Uptime / 60 / 60 / 24)
  89.         local Hours=$(expr $Uptime / 60 / 60 % 24)
  90.         local Minutes=$(expr $Uptime / 60 % 60)
  91.         local Seconds=$(expr $Uptime % 60)
  92.         if [ $Days -gt 0 ]; then
  93.             Days=$(printf "%dd " $Days)
  94.         else
  95.             Days=""
  96.         fi
  97.         printf "$Days%02d:%02d:%02d" $Hours $Minutes $Seconds
  98.     fi
  99. }
  100.  
  101. print_line() { # <String to Print>, [[<String to Print>] ...]
  102.     local Line="$@"
  103.     printf " | %-${Width}s |\r | $Line\n"
  104. }
  105.  
  106. print_horizontal_ruler() {
  107.     printf " $Rouler\n"
  108. }
  109.  
  110. print_machine() {
  111.     local Machine=$(awk -F: '/system type/ {print $2}' /proc/cpuinfo)
  112.     print_line     "System type:$MachineColor$Machine$NormalColor"
  113. }
  114.  
  115. print_times() {
  116.     local SysUptime=$(cut -d. -f1 /proc/uptime)
  117.     local Uptime=$(uptime_str $SysUptime)
  118.     local Now=$(date +'%Y-%m-%d %H:%M:%S')
  119.     print_line     "System uptime: $ValueColor$Uptime$NormalColor,"\
  120.                 "Now: $ValueColor$Now$NormalColor"
  121. }
  122.  
  123. print_loadavg() {
  124.     local LoadAvg=$(awk '{printf"'$ValueColor'%s'$NormalColor', '$ValueColor'%s'$NormalColor', '$ValueColor'%s'$NormalColor'",$1,$2,$3}' /proc/loadavg)
  125.     print_line "System load: $LoadAvg"
  126. }
  127.  
  128. print_flash() {
  129.     local Flash=$(df -k /overlay | awk '/overlay/{printf "%.0f\t%.0f\t%.1f\t%.0f",$2*1024,$3*1024,($2>0)?$3/$2*100:0,$4*1024}')
  130.     local Total=$(echo "$Flash" | cut -f 1)
  131.     local Used=$(echo "$Flash" | cut -f 2)
  132.     local UsedPercent=$(echo "$Flash" | cut -f 3)
  133.     local Free=$(echo "$Flash" | cut -f 4)
  134.     print_line     "Flash:"\
  135.                 "total: $ValueColor$(human_readable $Total)$NormalColor,"\
  136.                 "used: $ValueColor$(human_readable $Used)$NormalColor, $ValueColor$UsedPercent$NormalColor%%,"\
  137.                 "free: $ValueColor$(human_readable $Free)$NormalColor"
  138. }
  139.  
  140. print_memory() {
  141.     local Memory=$(awk 'BEGIN{Total=0;Free=0}$1~/^MemTotal:/{Total=$2}$1~/^MemFree:|^Buffers:|^Cached:/{Free+=$2}END{Used=Total-Free;printf"%.0f\t%.0f\t%.1f\t%.0f",Total*1024,Used*1024,(Total>0)?((Used/Total)*100):0,Free*1024}' /proc/meminfo)
  142.     local Total=$(echo "$Memory" | cut -f 1)
  143.     local Used=$(echo "$Memory" | cut -f 2)
  144.     local UsedPercent=$(echo "$Memory" | cut -f 3)
  145.     local Free=$(echo "$Memory" | cut -f 4)
  146.     print_line "Memory:"\
  147.                 "total: $ValueColor$(human_readable $Total)$NormalColor,"\
  148.                 "used: $ValueColor$(human_readable $Used)$NormalColor, $ValueColor$UsedPercent$NormalColor%%,"\
  149.                 "free: $ValueColor$(human_readable $Free)$NormalColor"
  150. }
  151.  
  152. print_wan() {
  153.     local Zone
  154.     local Device
  155.     for Zone in $(uci -q show firewall | grep .masq= | cut -f2 -d.); do
  156.         for Device in $(uci -q get firewall.$Zone.network); do
  157.             local Status="$(ubus call network.interface.$Device status 2>/dev/null)"
  158.             if [ "$Status" != "" ]; then
  159.                 local State=""
  160.                 local Iface=""
  161.                 local Uptime=""
  162.                 local IP4=""
  163.                 local IP6=""
  164.                 local Subnet4=""
  165.                 local Subnet6=""
  166.                 local Gateway4=""
  167.                 local Gateway6=""
  168.                 local DNS=""
  169.                 local Protocol=""
  170.                 json_load "${Status:-{}}"
  171.                 json_get_var State up
  172.                 json_get_var Uptime uptime
  173.                 json_get_var Iface l3_device
  174.                 json_get_var Protocol proto
  175.                 if json_get_type Status ipv4_address && [ "$Status" = array ]; then
  176.                     json_select ipv4_address
  177.                     json_get_type Status 1
  178.                     if [ "$Status" = object ]; then
  179.                         json_select 1
  180.                         json_get_var IP4 address
  181.                         json_get_var Subnet4 mask
  182.                         [ "$IP4" != "" ] && [ "$Subnet4" != "" ] && IP4="$IP4/$Subnet4"
  183.                     fi
  184.                 fi
  185.                 json_select
  186.                 if json_get_type Status ipv6_address && [ "$Status" = array ]; then
  187.                     json_select ipv6_address
  188.                     json_get_type Status 1
  189.                     if [ "$Status" = object ]; then
  190.                         json_select 1
  191.                         json_get_var IP6 address
  192.                         json_get_var Subnet6 mask
  193.                         [ "$IP6" != "" ] && [ "$Subnet6" != "" ] && IP6="$IP6/$Subnet6"
  194.                     fi
  195.                 fi
  196.                 json_select
  197.                 if json_get_type Status route && [ "$Status" = array ]; then
  198.                     json_select route
  199.                     local Index="1"
  200.                     while json_get_type Status $Index && [ "$Status" = object ]; do
  201.                         json_select "$((Index++))"
  202.                         json_get_var Status target
  203.                         case "$Status" in
  204.                             0.0.0.0)
  205.                                 json_get_var Gateway4 nexthop;;
  206.                             ::)
  207.                                 json_get_var Gateway6 nexthop;;
  208.                         esac
  209.                         json_select ".."
  210.                     done    
  211.                 fi
  212.                 json_select
  213.                 if json_get_type Status dns_server && [ "$Status" = array ]; then
  214.                     json_select dns_server
  215.                     local Index="1"
  216.                     while json_get_type Status $Index && [ "$Status" = string ]; do
  217.                         json_get_var Status "$((Index++))"
  218.                         DNS="${DNS:+$DNS }$Status"
  219.                     done
  220.                 fi
  221.                 if [ "$State" == "1" ]; then
  222.                     [ "$IP4" != "" ] && print_line     "WAN: $AddrColor$IP4$NormalColor($Iface),"\
  223.                                                     "gateway: $AddrColor${Gateway4:-n/a}$NormalColor"
  224.                     [ "$IP6" != "" ] && print_line    "WAN: $AddrColor$IP6$NormalColor($Iface),"\
  225.                                                     "gateway: $AddrColor${Gateway6:-n/a}$NormalColor"
  226.                     print_line    "proto: $ValueColor${Protocol:-n/a}$NormalColor,"\
  227.                                 "uptime: $ValueColor$(uptime_str $Uptime)$NormalColor$(device_rx_tx $Iface)"
  228.                     [ "$DNS" != "" ] && print_line "dns: $AddrColor$DNS$NormalColor"
  229.                 fi
  230.             fi
  231.         done
  232.     done
  233. }
  234.  
  235. print_lan() {
  236.     local Device="lan"
  237.     local State
  238.     local Iface
  239.     local IP4
  240.     local IP6
  241.     local Subnet4
  242.     local Subnet6
  243.     local Status="$(ubus call network.interface.$Device status 2>/dev/null)"
  244.     if [ "$Status" != "" ]; then
  245.         json_load "${Status:-{}}"
  246.         json_get_var State up
  247.         json_get_var Iface device
  248.         if json_get_type Status ipv4_address && [ "$Status" = array ]; then
  249.             json_select ipv4_address
  250.             json_get_type Status 1
  251.             if [ "$Status" = object ]; then
  252.                 json_select 1
  253.                 json_get_var IP4 address
  254.                 json_get_var Subnet4 mask
  255.                 [ "$IP4" != "" ] && [ "$Subnet4" != "" ] && IP4="$IP4/$Subnet4"
  256.             fi
  257.         fi
  258.         json_select
  259.         if json_get_type Status ipv6_address && [ "$Status" = array ]; then
  260.             json_select ipv6_address
  261.             json_get_type Status 1
  262.             if [ "$Status" = object ]; then
  263.                 json_select 1
  264.                 json_get_var IP6 address
  265.                 json_get_var Subnet6 mask
  266.                 [ "$IP6" != "" ] && [ "$Subnet6" != "" ] && IP6="$IP6/$Subnet6"
  267.             fi
  268.         fi
  269.         [ "$IP4" != "" ] && print_line "LAN: $AddrColor$IP4$NormalColor"
  270.         [ "$IP6" != "" ] && print_line "LAN: $AddrColor$IP6$NormalColor"
  271.     fi
  272. }
  273.  
  274. print_wlan() {
  275.     local Iface
  276.     for Iface in $(uci -q show wireless | grep device=radio | cut -f2 -d.); do
  277.         local Device=$(uci -q get wireless.$Iface.device)
  278.         local SSID=$(uci -q get wireless.$Iface.ssid)
  279.         local IfaceDisabled=$(uci -q get wireless.$Iface.disabled)
  280.         local DeviceDisabled=$(uci -q get wireless.$Device.disabled)
  281.         if [ -n "$SSID" ] && [ "$IfaceDisabled" != "1" ] && [ "$DeviceDisabled" != "1" ]; then
  282.             local Mode=$(uci -q -P /var/state get wireless.$Iface.mode)
  283.             local Channel=$(uci -q get wireless.$Device.channel)
  284.             local RadioIface=$(uci -q -P /var/state get wireless.$Iface.ifname)
  285.             local Connection="Down"
  286.             if [ -n "$RadioIface" ]; then
  287.                 if [ "$Mode" == "ap" ]; then
  288.                     Connection="$(iw dev $RadioIface station dump | grep Station | wc -l)"
  289.                 else
  290.                     Connection="$(iw dev $RadioIface link | awk 'BEGIN{FS=": ";Signal="";Bitrate=""} $1~/signal/ {Signal=$2} $1~/tx bitrate/ {Bitrate=$2}END{print Signal" "Bitrate}')"
  291.                 fi
  292.             fi
  293.             if [ "$Mode" == "ap" ]; then
  294.                 print_line    "WLAN: $ValueColor$SSID$NormalColor($Mode),"\
  295.                             "ch: $ValueColor${Channel:-n/a}$NormalColor,"\
  296.                             "conn: $ValueColor$Connection$NormalColor$(device_rx_tx $RadioIface)"
  297.             else
  298.                 print_line    "WLAN: $ValueColor$SSID$NormalColor($Mode),"\
  299.                             "ch: $ValueColor${Channel:-n/a}$NormalColor"
  300.                 print_line    "conn: $ValueColor$Connection$NormalColor$(device_rx_tx $RadioIface)"
  301.             fi
  302.         fi
  303.     done
  304. }
  305.  
  306. print_vpn() {
  307.     local VPN
  308.     for VPN in $(uci -q show openvpn | grep .ca= | cut -f2 -d.); do
  309.         local Device=$(uci -q get openvpn.$VPN.dev)
  310.         local Enabled=$(uci -q get openvpn.$VPN.enabled)
  311.         if [ "$Enabled" == "1" ] || [ "$Enabled" == "" ]; then
  312.             local Mode=$(uci -q get openvpn.$VPN.mode)
  313.             local Connection="n/a"
  314.             if [ "$Mode" == "server" ]; then
  315.                 Mode="$ValueColor$VPN$NormalColor(svr):$(uci -q get openvpn.$VPN.port)"
  316.                 Status=$(uci -q get openvpn.$VPN.status)
  317.                 Connection=$(awk 'BEGIN{FS=",";c=0;l=0}{if($1=="Common Name")l=1;else if($1=="ROUTING TABLE")exit;else if (l==1) c=c+1}END{print c}' $Status)
  318.             else
  319.                 Mode="$ValueColor$VPN$NormalColor(cli)"
  320.                 Connection="Down"
  321.                 ifconfig $Device &>/dev/null && Connection="Up"
  322.             fi
  323.             print_line    "VPN: $Mode,"\
  324.                         "conn: $ValueColor$Connection$NormalColor$(device_rx_tx $Device)"
  325.         fi
  326.     done
  327. }
  328.  
  329. initialize $@
  330. [ "$StartRuler" == "1" ] && print_horizontal_ruler
  331. print_machine
  332. print_times
  333. print_loadavg
  334. print_flash
  335. print_memory
  336. print_wan
  337. print_lan
  338. print_wlan
  339. print_vpn
  340. [ "$EndRuler" == "1" ] && print_horizontal_ruler
  341. exit 0
  342. # Done.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement