sysinfo.sh 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. #!/bin/sh
  2. #
  3. # sysinfo.sh dla OpenWRT AA Cezary Jackiewicz 2013
  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.13 RD Dodanie info o dhcp w LAN, zmiana sposobu wy¶wietlania informacji o LAN
  19. # 1.14 RD Dodanie informacji o ostatnich 5 b³êdach
  20. # 1.15 RD Zmiana stderr
  21. # 1.16 RD Dodanie wy¶wietlania informacji o swap
  22. # 1.17 RD Zmiana wyliczania informacji o flash
  23. # 1.18 RD Zmiana wy¶wietlania informacji o flash
  24. # 1.19 RD Zmiana wy¶wietlania informacji o sprzêcie
  25. # 1.20 RD Zmiana wy¶wietlania informacji o sprzêcie
  26. # 1.21 RD Dopasowyanie szeroko¶ci do zawarto¶ci /etc/banner
  27. # 1.22 RD Dodanie wyœwietlania w HTML-u
  28. #
  29. # Destination /sbin/sysinfo.sh
  30. #
  31. . /usr/share/libubox/jshn.sh
  32. local Width=0
  33. local StartRuler="1"
  34. local EndRuler="1"
  35. local LastErrors="1"
  36. local NormalColor=""
  37. local MachineColor=""
  38. local ValueColor=""
  39. local AddrColor=""
  40. local RXTXColor=""
  41. local ErrorColor=""
  42. local ExtraName=""
  43. local ExtraValue=""
  44. local HTML=""
  45. initialize() { # <Script Parameters>
  46. local ColorMode="c"
  47. if [ ! -z "$REQUEST_METHOD" ]; then
  48. HTML="1"
  49. ColorMode="html"
  50. StartRuler=""
  51. EndRuler="1"
  52. fi
  53. [ -e /etc/banner ] && Width=$(awk 'BEGIN{max=0}{if(length($0)>max)max=length($0)}END{print max}' /etc/banner 2>/dev/null)
  54. while [ -n "$1" ]; do
  55. case "$1" in
  56. -h|--help) echo -e "Usage: $0 [-h|--help] [[-m|--mono]|[-bw|-black-white]|[-c2|--color-2]] [-sr|--no-start-ruler] [-er|--no-end-ruler]"\
  57. "[-w N|--width N] [-en Name|--extra-name Name] [-ev Value|--extra-value Value] [-le|--no-last-err]"\
  58. "\n\t-h\t\tThis help,"\
  59. "\n\t-m\t\tDisplay mono version,"\
  60. "\n\t-bw\t\tDisplay black-white version,"\
  61. "\n\t-c2\t\tDisplay alternative color version 2,"\
  62. "\n\t-sr\t\tWithout start horizontal ruler,"\
  63. "\n\t-er\t\tWithout end horizontal ruler,"\
  64. "\n\t-w N\t\tSet width of text area to N characters (minimum 60)"\
  65. "\n\t-en Name\tPrint extra name"\
  66. "\n\t-ev Value\tPrint extra value"\
  67. "\n\t-le\t\tDon't display last errors"
  68. exit 1;;
  69. -m|--mono) ColorMode="m";;
  70. -bw|--black-white) ColorMode="bw";;
  71. -c2|--color-2) ColorMode="c2";;
  72. -sr|--no-start-ruler) StartRuler="0";;
  73. -er|--no-end-ruler) EndRuler="0";;
  74. -w|--width) shift; Width=$1;;
  75. -en|--extra-name) while [ -n "$2" ] && [ "${2:0:1}" != "-" ]; do
  76. shift
  77. [ "$ExtraName" != "" ] && ExtraName="$ExtraName "
  78. ExtraName="$ExtraName$1"
  79. done;;
  80. -ev|--extra-value) while [ -n "$2" ] && [ "${2:0:1}" != "-" ]; do
  81. shift
  82. [ "$ExtraValue" != "" ] && ExtraValue="$ExtraValue "
  83. ExtraValue="$ExtraValue$1"
  84. done;;
  85. -le|--no-last-err) LastErrors="0";;
  86. *) echo "Invalid option: $1. Use -h for help";;
  87. esac
  88. shift;
  89. done
  90. case "$ColorMode" in
  91. c) NormalColor="\e[0m"
  92. MachineColor="\e[0;33m"
  93. ValueColor="\e[1;36m"
  94. AddrColor="\e[1;31m"
  95. RXTXColor="\e[2;32m"
  96. ErrorColor="\e[0;31m";;
  97. c2) NormalColor="\e[0m"
  98. MachineColor="\e[0;31m"
  99. ValueColor="\e[0;33m"
  100. AddrColor="\e[0;35m"
  101. RXTXColor="\e[0;36m"
  102. ErrorColor="\e[0;31m";;
  103. m) NormalColor="\e[0m"
  104. MachineColor="\e[7m"
  105. ValueColor="\e[1m"
  106. AddrColor="\e[4m"
  107. RXTXColor="\e[1m"
  108. ErrorColor="\e[4";;
  109. html) NormalColor="</font><font class=\"Normal\">"
  110. MachineColor="</font><font class=\"Machine\">"
  111. ValueColor="</font><font class=\"Value\">"
  112. AddrColor="</font><font class=\"Addr\">"
  113. RXTXColor="</font><font class=\"RXTX\">"
  114. ErrorColor="</font><font class=\"Error\">";;
  115. *) ;;
  116. esac
  117. ([ "$Width" == "" ] || [ "$Width" -lt 65 ]) && Width=65
  118. if [ "$HTML" == "1" ]; then
  119. echo "Content-type: text/html"
  120. echo ""
  121. cat << EOF
  122. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  123. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr">
  124. <head>
  125. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  126. </head>
  127. <style>
  128. body {font-family:Consolas,Terminal,"Lucida Console",Courier,Monaco,monospace;
  129. font-size:1em;white-space:pre-wrap;word-wrap:break-word;line-height:1.2em;
  130. color:#bbbbbb;background-color:#000000}
  131. .Normal {color:#bbbbbb}
  132. .Machine {color:#bbbb00}
  133. .Value {color:#00ffff}
  134. .Addr {color:#ee5555}
  135. .RXTX {color:#00bb00}
  136. </style>
  137. <body>
  138. EOF
  139. [ -e /etc/banner ] && cat /etc/banner
  140. fi
  141. }
  142. finalize() {
  143. if [ "$HTML" == "1" ]; then
  144. cat << EOF
  145. </body>
  146. </html>
  147. EOF
  148. fi
  149. }
  150. human_readable() { # <Number of bytes>
  151. if [ $1 -gt 0 ]; then
  152. 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]}' 2>/dev/null)"
  153. else
  154. printf "0B"
  155. fi
  156. }
  157. device_rx_tx() { # <Device>
  158. local RXTX=$(awk -v Device=$1 '$1==Device ":"{printf "%.0f\t%.0f",$2,$10}' /proc/net/dev 2>/dev/null)
  159. [ "$RXTX" != "" ] && printf ", rx/tx: $RXTXColor$(human_readable $(echo "$RXTX" | cut -f 1))$NormalColor/$RXTXColor$(human_readable $(echo "$RXTX" | cut -f 2))$NormalColor"
  160. }
  161. uptime_str() { # <Time in Seconds>
  162. local Uptime=$1
  163. if [ $Uptime -gt 0 ]; then
  164. local Days=$(expr $Uptime / 60 / 60 / 24)
  165. local Hours=$(expr $Uptime / 60 / 60 % 24)
  166. local Minutes=$(expr $Uptime / 60 % 60)
  167. local Seconds=$(expr $Uptime % 60)
  168. if [ $Days -gt 0 ]; then
  169. Days=$(printf "%dd " $Days)
  170. else
  171. Days=""
  172. fi 2>/dev/null
  173. printf "$Days%02d:%02d:%02d" $Hours $Minutes $Seconds
  174. fi
  175. }
  176. print_line() { # <String to Print>, [[<String to Print>] ...]
  177. local Line="$@"
  178. if [ "$HTML" == "1" ]; then
  179. printf " $Line\n" 2>/dev/null
  180. else
  181. printf " | %-$(expr $Width - 5)s |\r | $Line\n" 2>/dev/null
  182. fi
  183. }
  184. print_horizontal_ruler() {
  185. printf "/%$(expr $Width - 1)s\n" | tr ' /' '- ' 2>/dev/null
  186. }
  187. print_machine() {
  188. local Machine=""
  189. local HostName=$(uci -q get system.@system[0].hostname)
  190. if [ -e /tmp/sysinfo/model ]; then
  191. Machine=$(cat /tmp/sysinfo/model 2>/dev/null)
  192. elif [ -e /proc/cpuinfo ]; then
  193. Machine=$(awk 'BEGIN{FS="[ \t]+:[ \t]";OFS=""}/machine/{Machine=$2}/Hardware/{Hardware=$2}END{print Machine,(Machine!="" && Hardware!="")?" ":"",Hardware}' /proc/cpuinfo 2>/dev/null)
  194. fi
  195. print_line "${NormalColor}Machine: $MachineColor${Machine:-n/a}$NormalColor,"\
  196. "Name: $MachineColor${HostName:-n/a}$NormalColor"
  197. }
  198. print_times() {
  199. local SysUptime=$(cut -d. -f1 /proc/uptime)
  200. local Uptime=$(uptime_str $SysUptime)
  201. local Now=$(date +'%Y-%m-%d %H:%M:%S')
  202. print_line "System uptime: $ValueColor$Uptime$NormalColor,"\
  203. "Now: $ValueColor$Now$NormalColor"
  204. }
  205. print_loadavg() {
  206. print_line "System load:"\
  207. "$ValueColor"$(cat /proc/loadavg | cut -d " " -f 1 2>/dev/null)"$NormalColor,"\
  208. "$ValueColor"$(cat /proc/loadavg | cut -d " " -f 2 2>/dev/null)"$NormalColor,"\
  209. "$ValueColor"$(cat /proc/loadavg | cut -d " " -f 3 2>/dev/null)"$NormalColor"
  210. }
  211. print_fs_summary() { # <Mount point> <Label>
  212. local DeviceInfo=$(df -k $1 2>/dev/null| awk 'BEGIN{Total=0;Free=0} NR>1 && $6=="'$1'"{Total=$2;Free=$4}END{Used=Total-Free;printf"%.0f\t%.0f\t%.1f\t%.0f",Total*1024,Used*1024,(Total>0)?((Used/Total)*100):0,Free*1024}' 2>/dev/null)
  213. local Total=$(echo "$DeviceInfo" | cut -f 1)
  214. local Used=$(echo "$DeviceInfo" | cut -f 2)
  215. local UsedPercent=$(echo "$DeviceInfo" | cut -f 3)
  216. local Free=$(echo "$DeviceInfo" | cut -f 4)
  217. [ "$Total" -gt 0 ] && print_line "$2:"\
  218. "total: $ValueColor$(human_readable $Total)$NormalColor,"\
  219. "used: $ValueColor$(human_readable $Used)$NormalColor, $ValueColor$UsedPercent$NormalColor%%,"\
  220. "free: $ValueColor$(human_readable $Free)$NormalColor"
  221. }
  222. print_disk() {
  223. local Overlay=$(awk '$3=="overlayfs"{print $2}' /proc/mounts 2>/dev/null)
  224. if [ "$Overlay" != "" ]; then
  225. print_fs_summary /overlay "Flash"
  226. fi
  227. if [ "$Overlay" == "" ] || [ "$Overlay" != "/" ]; then
  228. print_fs_summary / "RootFS"
  229. fi
  230. }
  231. print_memory() {
  232. 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 2>/dev/null)
  233. local Total=$(echo "$Memory" | cut -f 1)
  234. local Used=$(echo "$Memory" | cut -f 2)
  235. local UsedPercent=$(echo "$Memory" | cut -f 3)
  236. local Free=$(echo "$Memory" | cut -f 4)
  237. print_line "Memory:"\
  238. "total: $ValueColor$(human_readable $Total)$NormalColor,"\
  239. "used: $ValueColor$(human_readable $Used)$NormalColor, $ValueColor$UsedPercent$NormalColor%%,"\
  240. "free: $ValueColor$(human_readable $Free)$NormalColor"
  241. }
  242. print_swap() {
  243. local Swap=$(awk 'BEGIN{Total=0;Free=0}$1~/^SwapTotal:/{Total=$2}$1~/^SwapFree:/{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 2>/dev/null)
  244. local Total=$(echo "$Swap" | cut -f 1)
  245. local Used=$(echo "$Swap" | cut -f 2)
  246. local UsedPercent=$(echo "$Swap" | cut -f 3)
  247. local Free=$(echo "$Swap" | cut -f 4)
  248. [ "$Total" -gt 0 ] && print_line "Swap:"\
  249. "total: $ValueColor$(human_readable $Total)$NormalColor,"\
  250. "used: $ValueColor$(human_readable $Used)$NormalColor, $ValueColor$UsedPercent$NormalColor%%,"\
  251. "free: $ValueColor$(human_readable $Free)$NormalColor"
  252. }
  253. print_wan() {
  254. local Zone
  255. local Device
  256. for Zone in $(uci -q show firewall | grep .masq= | cut -f2 -d.); do
  257. if [ "$(uci -q get firewall.$Zone.masq)" == "1" ]; then
  258. for Device in $(uci -q get firewall.$Zone.network); do
  259. local Status="$(ubus call network.interface.$Device status 2>/dev/null)"
  260. if [ "$Status" != "" ]; then
  261. local State=""
  262. local Iface=""
  263. local Uptime=""
  264. local IP4=""
  265. local IP6=""
  266. local Subnet4=""
  267. local Subnet6=""
  268. local Gateway4=""
  269. local Gateway6=""
  270. local DNS=""
  271. local Protocol=""
  272. json_load "${Status:-{}}"
  273. json_get_var State up
  274. json_get_var Uptime uptime
  275. json_get_var Iface l3_device
  276. json_get_var Protocol proto
  277. if json_get_type Status ipv4_address && [ "$Status" = array ]; then
  278. json_select ipv4_address
  279. json_get_type Status 1
  280. if [ "$Status" = object ]; then
  281. json_select 1
  282. json_get_var IP4 address
  283. json_get_var Subnet4 mask
  284. [ "$IP4" != "" ] && [ "$Subnet4" != "" ] && IP4="$IP4/$Subnet4"
  285. fi
  286. fi
  287. json_select
  288. if json_get_type Status ipv6_address && [ "$Status" = array ]; then
  289. json_select ipv6_address
  290. json_get_type Status 1
  291. if [ "$Status" = object ]; then
  292. json_select 1
  293. json_get_var IP6 address
  294. json_get_var Subnet6 mask
  295. [ "$IP6" != "" ] && [ "$Subnet6" != "" ] && IP6="$IP6/$Subnet6"
  296. fi
  297. fi
  298. json_select
  299. if json_get_type Status route && [ "$Status" = array ]; then
  300. json_select route
  301. local Index="1"
  302. while json_get_type Status $Index && [ "$Status" = object ]; do
  303. json_select "$((Index++))"
  304. json_get_var Status target
  305. case "$Status" in
  306. 0.0.0.0)
  307. json_get_var Gateway4 nexthop;;
  308. ::)
  309. json_get_var Gateway6 nexthop;;
  310. esac
  311. json_select ".."
  312. done
  313. fi
  314. json_select
  315. if json_get_type Status dns_server && [ "$Status" = array ]; then
  316. json_select dns_server
  317. local Index="1"
  318. while json_get_type Status $Index && [ "$Status" = string ]; do
  319. json_get_var Status "$((Index++))"
  320. DNS="${DNS:+$DNS }$Status"
  321. done
  322. fi
  323. if [ "$State" == "1" ]; then
  324. [ "$IP4" != "" ] && print_line "WAN: $AddrColor$IP4$NormalColor($Iface),"\
  325. "gateway: $AddrColor${Gateway4:-n/a}$NormalColor"
  326. [ "$IP6" != "" ] && print_line "WAN: $AddrColor$IP6$NormalColor($Iface),"\
  327. "gateway: $AddrColor${Gateway6:-n/a}$NormalColor"
  328. print_line "proto: $ValueColor${Protocol:-n/a}$NormalColor,"\
  329. "uptime: $ValueColor$(uptime_str $Uptime)$NormalColor$(device_rx_tx $Iface)"
  330. [ "$DNS" != "" ] && print_line "dns: $AddrColor$DNS$NormalColor"
  331. fi
  332. fi
  333. done
  334. fi
  335. done
  336. }
  337. print_lan() {
  338. local Zone
  339. local Device
  340. for Zone in $(uci -q show firewall | grep []]=zone | cut -f2 -d. | cut -f1 -d=); do
  341. if [ "$(uci -q get firewall.$Zone.masq)" != "1" ]; then
  342. for Device in $(uci -q get firewall.$Zone.network); do
  343. local Status="$(ubus call network.interface.$Device status 2>/dev/null)"
  344. if [ "$Status" != "" ]; then
  345. local State=""
  346. local Iface=""
  347. local IP4=""
  348. local IP6=""
  349. local Subnet4=""
  350. local Subnet6=""
  351. json_load "${Status:-{}}"
  352. json_get_var State up
  353. json_get_var Iface device
  354. if json_get_type Status ipv4_address && [ "$Status" = array ]; then
  355. json_select ipv4_address
  356. json_get_type Status 1
  357. if [ "$Status" = object ]; then
  358. json_select 1
  359. json_get_var IP4 address
  360. json_get_var Subnet4 mask
  361. [ "$IP4" != "" ] && [ "$Subnet4" != "" ] && IP4="$IP4/$Subnet4"
  362. fi
  363. fi
  364. json_select
  365. if json_get_type Status ipv6_address && [ "$Status" = array ]; then
  366. json_select ipv6_address
  367. json_get_type Status 1
  368. if [ "$Status" = object ]; then
  369. json_select 1
  370. json_get_var IP6 address
  371. json_get_var Subnet6 mask
  372. [ "$IP6" != "" ] && [ "$Subnet6" != "" ] && IP6="$IP6/$Subnet6"
  373. fi
  374. fi
  375. local DHCPConfig=$(uci -q show dhcp | grep .interface=$Device | cut -d. -f2)
  376. if [ "$DHCPConfig" != "" ] && [ "$(uci -q get dhcp.$DHCPConfig.ignore)" != "1" ]; then
  377. local DHCPStart=$(uci -q get dhcp.$DHCPConfig.start)
  378. local DHCPLimit=$(uci -q get dhcp.$DHCPConfig.limit)
  379. [ "$DHCPStart" != "" ] && [ "$DHCPLimit" != "" ] && DHCP="$(echo $IP4 | cut -d. -f1-3).$DHCPStart-$(expr $DHCPStart + $DHCPLimit - 1)"
  380. fi
  381. [ "$IP4" != "" ] && print_line "LAN: $AddrColor$IP4$NormalColor($Iface), dhcp: $AddrColor${DHCP:-n/a}$NormalColor"
  382. [ "$IP6" != "" ] && print_line "LAN: $AddrColor$IP6$NormalColor($Iface)"
  383. fi
  384. done
  385. fi
  386. done
  387. }
  388. print_wlan() {
  389. local Iface
  390. for Iface in $(uci -q show wireless | grep device=radio | cut -f2 -d.); do
  391. local Device=$(uci -q get wireless.$Iface.device)
  392. local SSID=$(uci -q get wireless.$Iface.ssid)
  393. local IfaceDisabled=$(uci -q get wireless.$Iface.disabled)
  394. local DeviceDisabled=$(uci -q get wireless.$Device.disabled)
  395. if [ -n "$SSID" ] && [ "$IfaceDisabled" != "1" ] && [ "$DeviceDisabled" != "1" ]; then
  396. local Mode=$(uci -q -P /var/state get wireless.$Iface.mode)
  397. local Channel=$(uci -q get wireless.$Device.channel)
  398. local RadioIface=$(uci -q -P /var/state get wireless.$Iface.ifname)
  399. local Connection="Down"
  400. if [ -n "$RadioIface" ]; then
  401. if [ "$Mode" == "ap" ]; then
  402. Connection="$(iw dev $RadioIface station dump | grep Station | wc -l 2>/dev/null)"
  403. else
  404. Connection="$(iw dev $RadioIface link | awk 'BEGIN{FS=": ";Signal="";Bitrate=""} $1~/signal/ {Signal=$2} $1~/tx bitrate/ {Bitrate=$2}END{print Signal" "Bitrate}' 2>/dev/null)"
  405. fi
  406. fi
  407. if [ "$Mode" == "ap" ]; then
  408. print_line "WLAN: $ValueColor$SSID$NormalColor($Mode),"\
  409. "ch: $ValueColor${Channel:-n/a}$NormalColor,"\
  410. "conn: $ValueColor$Connection$NormalColor$(device_rx_tx $RadioIface)"
  411. else
  412. print_line "WLAN: $ValueColor$SSID$NormalColor($Mode),"\
  413. "ch: $ValueColor${Channel:-n/a}$NormalColor"
  414. print_line "conn: $ValueColor$Connection$NormalColor$(device_rx_tx $RadioIface)"
  415. fi
  416. fi
  417. done
  418. }
  419. print_vpn() {
  420. local VPN
  421. for VPN in $(uci -q show openvpn | grep .ca= | cut -f2 -d.); do
  422. local Device=$(uci -q get openvpn.$VPN.dev)
  423. local Enabled=$(uci -q get openvpn.$VPN.enabled)
  424. if [ "$Enabled" == "1" ] || [ "$Enabled" == "" ]; then
  425. local Mode=$(uci -q get openvpn.$VPN.mode)
  426. local Connection="n/a"
  427. if [ "$Mode" == "server" ]; then
  428. Mode="$ValueColor$VPN$NormalColor(svr):$(uci -q get openvpn.$VPN.port)"
  429. Status=$(uci -q get openvpn.$VPN.status)
  430. 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 2>/dev/null)
  431. else
  432. Mode="$ValueColor$VPN$NormalColor(cli)"
  433. Connection="Down"
  434. ifconfig $Device &>/dev/null && Connection="Up"
  435. fi
  436. print_line "VPN: $Mode,"\
  437. "conn: $ValueColor$Connection$NormalColor$(device_rx_tx $Device)"
  438. fi
  439. done
  440. }
  441. print_extra() {
  442. ([ "$ExtraName" != "" ] || [ "$ExtraValue" != "" ]) && print_line "$ExtraName $ValueColor$ExtraValue$NormalColor"
  443. }
  444. print_error() {
  445. logread | awk '/\w{3}+\.(err|warn|alert|emerg|crit)/{err[++i]=$0}END{j=i-4;j=j>=1?j:1;while(j<=i)print" '$ErrorColor'"err[j++]"'$NormalColor'"}' 2>/dev/null
  446. }
  447. initialize $@
  448. [ "$StartRuler" == "1" ] && print_horizontal_ruler
  449. print_machine
  450. print_times
  451. print_loadavg
  452. print_disk
  453. print_memory
  454. print_swap
  455. print_wan
  456. print_lan
  457. print_wlan
  458. print_vpn
  459. print_extra
  460. [ "$EndRuler" == "1" ] && print_horizontal_ruler
  461. [ "$LastErrors" == "1" ] && print_error
  462. finalize
  463. exit 0
  464. # Done.