shutil 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. #!/bin/sh
  2. #
  3. # shutil: some shared utilities used by all status scripts
  4. #
  5. # Copyright (C) 2011-2014 Dustin Kirkland
  6. #
  7. # Authors: Dustin Kirkland <kirkland@byobu.org>
  8. # Scott Moser <smoser@ubuntu.com>
  9. #
  10. # This program is free software: you can redistribute it and/or modify
  11. # it under the terms of the GNU General Public License as published by
  12. # the Free Software Foundation, version 3 of the License.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. # Define colors
  22. color_screen() {
  23. ESC="\005"
  24. case "$1" in
  25. "") return 0 ;;
  26. -) printf "$ESC{-}" ;;
  27. --) printf "$ESC{-} " ;;
  28. esc) printf "$ESC" ;;
  29. bold1) printf "$ESC{=b }" || printf "$ESC{= }" ;;
  30. bold2) printf "$ESC{+b }" || printf "$ESC{= }" ;;
  31. none) printf "$ESC{= }" ;;
  32. invert) printf "$ESC{=r }" ;;
  33. *)
  34. local attr fg bg
  35. case $# in
  36. 2)
  37. attr= ; fg=$1 ; bg=$2
  38. ;;
  39. 3)
  40. attr=$1 ; fg=$2 ; bg=$3
  41. ;;
  42. esac
  43. if [ "$MONOCHROME" = "1" ]; then
  44. fg=
  45. bg=
  46. fi
  47. printf "$ESC{=$attr $fg$bg}"
  48. ;;
  49. esac
  50. }
  51. color_map() {
  52. case "$1" in
  53. "k") _RET="black" ;;
  54. "r") _RET="red" ;;
  55. "g") _RET="green" ;;
  56. "y") _RET="yellow" ;;
  57. "b") _RET="blue" ;;
  58. "m") _RET="magenta" ;;
  59. "c") _RET="cyan" ;;
  60. "w") _RET="white" ;;
  61. "d") _RET="black" ;;
  62. "K") _RET="brightblack" ;;
  63. "R") _RET="brightred" ;;
  64. "G") _RET="brightgreen" ;;
  65. "Y") _RET="brightyellow" ;;
  66. "B") _RET="brightblue" ;;
  67. "M") _RET="brightmagenta" ;;
  68. "C") _RET="brightcyan" ;;
  69. "W") _RET="brightwhite" ;;
  70. *) _RET="$1" ;;
  71. esac
  72. }
  73. attr_map() {
  74. case "$1" in
  75. "d") _RET=,dim ;;
  76. "u") _RET=,underscore ;;
  77. "b") _RET=,bold ;;
  78. "r") _RET=,reverse ;;
  79. "s") _RET=,standout ;;
  80. "B") _RET=,blinking ;;
  81. "h") _RET=,hidden ;;
  82. "i") _RET=,italics ;;
  83. *) _RET= ;;
  84. esac
  85. }
  86. color_tmux() {
  87. local back fore attr
  88. case "$1" in
  89. "") return 0 ;;
  90. -) printf "#[default]#[fg=$BYOBU_LIGHT,bg=$BYOBU_DARK]" ;;
  91. --) printf "#[default]#[fg=$BYOBU_LIGHT]#[bg=$BYOBU_DARK] " ;;
  92. esc) printf "" ;;
  93. bold*) printf "#[default]#[fg=bold]" ;;
  94. none) printf "#[default]#[fg=$BYOBU_LIGHT,bg=$BYOBU_DARK]" ;;
  95. invert) printf "#[default]#[reverse]" ;;
  96. *)
  97. if [ "$#" = "2" ]; then
  98. color_map "$1"; back="$_RET"
  99. color_map "$2"; fore="$_RET"
  100. else
  101. attr_map "$1"; attr="$_RET"
  102. color_map "$2"; back="$_RET"
  103. color_map "$3"; fore="$_RET"
  104. fi
  105. [ "$MONOCHROME" = "1" ] && printf "#[default]" || printf "#[default]#[fg=$fore$attr,bg=$back]"
  106. ;;
  107. esac
  108. }
  109. color() {
  110. case "$BYOBU_BACKEND" in
  111. tmux)
  112. color_tmux "$@"
  113. ;;
  114. screen)
  115. color_screen "$@"
  116. ;;
  117. esac
  118. }
  119. # uncommented_lines(char=#)
  120. # does the standard input have lines that do not start with 'char'?
  121. uncommented_lines() {
  122. local line chr="${1:-#}"
  123. while read line; do
  124. [ "${line#${chr}}" = "${line}" ] && return 0;
  125. done
  126. return 1
  127. }
  128. # newest(file,file,file..)
  129. # return the newest file in the list
  130. newest() {
  131. local c="$1" i
  132. for i in "$@"; do [ "$i" -nt "$c" ] && c="$i"; done
  133. [ -e "$c" ] && _RET="$c"
  134. }
  135. error() {
  136. printf "%s\n" "ERROR: " "$@" 1>&2
  137. }
  138. fail() {
  139. [ $# -eq 0 ] || error "$@"; exit 1;
  140. }
  141. find_script() {
  142. # Allow for local status scripts
  143. if [ -x "$BYOBU_CONFIG_DIR/bin/$1" ]; then
  144. _RET="$BYOBU_CONFIG_DIR/bin/$1"
  145. elif [ -x "$BYOBU_PREFIX/lib/$PKG/$1" ]; then
  146. _RET="$BYOBU_PREFIX/lib/$PKG/$1"
  147. elif [ -x "$BYOBU_PREFIX/libexec/$PKG/$1" ]; then
  148. _RET="$BYOBU_PREFIX/libexec/$PKG/$1"
  149. else
  150. _RET="/dev/null"
  151. fi
  152. }
  153. # divide 2 integers and return a floating point number
  154. # third argument indicates how many digits after the decimal
  155. fpdiv() {
  156. local a=$1 b=$2 pres=${3:-3}
  157. local i=0 mp="10" whole="" part="" chunk="" n=0
  158. while i=$(($i+1)) && [ $i -le $pres ]; do
  159. mp="${mp}0"
  160. chunk="${chunk}?"
  161. done
  162. n=$(((${mp}*${a})/${b}))
  163. # round up if necessary
  164. [ $((($n-5)/10)) = $(($n/10)) ] && n=$(($n+5))
  165. # drop the last digit, which was only there for rounding
  166. n=${n%?}
  167. whole=${n%${chunk}}
  168. part=${n#${whole}}
  169. _RET=${whole:-0}${part:+.${part}}
  170. return
  171. }
  172. # rtrim(string,chars)
  173. rtrim() {
  174. local tab=' ' cr="
  175. "
  176. local cur="${1}" set="[${2:- ${tab}${cr}}]" n=""
  177. while n=${cur%${set}} && [ "$n" != "$cur" ]; do cur=${n}; done
  178. _RET=${cur}
  179. }
  180. readfile() {
  181. local c="" r="" cr="
  182. "
  183. OIFS="$IFS"; IFS="";
  184. while read c; do
  185. r="$r${cr}$c"
  186. done
  187. IFS=$OIFS
  188. _RET=${r}
  189. return 0
  190. }
  191. metadata_available() {
  192. # This is really ugly. We need a reliable, fast way of determining
  193. # if a metadata service is available, that does NOT slow down non-ec2
  194. # machines.
  195. local x=0 cache="$BYOBU_CONFIG_DIR/.metadata_available"
  196. # First, check the cache
  197. if [ -s "$cache" ]; then
  198. # Metadata is non-empty, so we have metadata available
  199. x=1
  200. else
  201. # Must seed the cache
  202. if [ -e /etc/ec2_version ] || [ -e /usr/sbin/update-grub-legacy-ec2 ]; then
  203. # This *looks* like a machine with metadata, so background a potentially slow check
  204. timeout 1 wget -q -O- --tries=1 http://169.254.169.254 </dev/null >"$cache" 2>/dev/null &
  205. sleep 0.02
  206. [ -s "$cache" ] && x=1
  207. fi
  208. fi
  209. [ "$x" = "1" ]
  210. }
  211. status_freq() {
  212. # Define status frequencies
  213. # Use prime number intervals, to decrease collisions, which
  214. # yields some less expensive status updates.
  215. # ~86000 ~1 day
  216. # ~600 ~10 minutes
  217. # ~180 ~3 minutes
  218. # ~60 ~1 minute
  219. case "$1" in
  220. apport) _RET=67 ;;
  221. arch) _RET=9999991 ;;
  222. battery) _RET=61 ;;
  223. color) _RET=9999991 ;;
  224. cpu_count) _RET=5 ;;
  225. cpu_freq) _RET=2 ;;
  226. cpu_temp) _RET=19 ;;
  227. custom) _RET=5 ;;
  228. date) _RET=9999991 ;;
  229. disk) _RET=13 ;;
  230. disk_io) _RET=3 ;;
  231. distro) _RET=9999991 ;;
  232. entropy) _RET=5 ;;
  233. fan_speed) _RET=23 ;;
  234. hostname) _RET=607 ;;
  235. ip_address) _RET=127 ;;
  236. load_average) _RET=2 ;;
  237. logo) _RET=9999991 ;;
  238. mail) _RET=5 ;;
  239. memory) _RET=13 ;;
  240. menu) _RET=9999991 ;;
  241. network) _RET=3 ;;
  242. notify_osd) _RET=9999991 ;;
  243. processes) _RET=7 ;;
  244. raid) _RET=59 ;;
  245. reboot_required) _RET=5 ;;
  246. release) _RET=599 ;;
  247. services) _RET=53 ;;
  248. session) _RET=9999991 ;;
  249. swap) _RET=19 ;;
  250. time) _RET=9999991 ;;
  251. time_binary) _RET=23 ;;
  252. time_utc) _RET=11 ;;
  253. trash) _RET=9999991 ;;
  254. updates_available) _RET=7 ;;
  255. uptime) _RET=29 ;;
  256. users) _RET=11 ;;
  257. whoami) _RET=86029 ;;
  258. wifi_quality) _RET=17 ;;
  259. *) _RET=9999991 ;;
  260. esac
  261. }
  262. get_now() {
  263. if [ -r /proc/uptime ]; then
  264. # return the integer part of the first item in /proc/uptime
  265. local s c
  266. read s c < /proc/uptime
  267. _RET=${s%.*}
  268. else
  269. _RET=$(date +%s);
  270. fi
  271. }
  272. get_network_interface() {
  273. if [ -n "$MONITORED_NETWORK" ]; then
  274. # Manual override
  275. _RET="$MONITORED_NETWORK"
  276. elif [ -e /proc/net/route ]; then
  277. # Linux systems, read route and interface from procfs
  278. local Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT
  279. while read Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT; do
  280. [ "$Mask" = "00000000" ] && break
  281. done < /proc/net/route
  282. _RET="$Iface"
  283. elif eval $BYOBU_TEST route >/dev/null 2>&1; then
  284. # Route command on path
  285. _RET=$(route get default|grep interface:|awk '{print $2}')
  286. elif [ -x "/sbin/route" ]; then
  287. # Mac OSX, shell out to the route command
  288. _RET=$(/sbin/route get default|grep interface:|awk '{print $2}')
  289. fi
  290. }
  291. get_distro() {
  292. local distro="${DISTRO}"
  293. if [ -n "$DISTRO" ]; then
  294. # user defined
  295. true
  296. elif [ -r "/etc/os-release" ]; then
  297. distro=$(. /etc/os-release && echo "$NAME")
  298. case "$distro" in
  299. Debian*)
  300. [ -r /etc/lsb-release ] && distro=$(. /etc/lsb-release && [ -n "$GOOGLE_ID" ] && echo "gLinux" || echo "Debian")
  301. ;;
  302. esac
  303. elif [ -r "/etc/issue" ]; then
  304. # lsb_release is *really* slow; try to use /etc/issue first
  305. local issue
  306. IFS="" read issue < /etc/issue
  307. case "$issue" in
  308. Ubuntu*)
  309. distro="Ubuntu";
  310. ;;
  311. Debian*)
  312. distro="Debian"
  313. ;;
  314. Red\ Hat\ Enterprise*)
  315. distro="RHEL"
  316. ;;
  317. *)
  318. # assume first field is what we want
  319. distro="${issue%% *}";
  320. ;;
  321. esac
  322. elif eval $BYOBU_TEST lsb_release >/dev/null 2>&1; then
  323. # If lsb_release is available, use it
  324. local r=$(lsb_release -s -d)
  325. case "$r" in
  326. Ubuntu*)
  327. # Use the -d if an Ubuntu LTS
  328. distro="Ubuntu"
  329. ;;
  330. *)
  331. # But for other distros the description
  332. # is too long, so build from -i and -r
  333. distro=$(lsb_release -s -i)
  334. ;;
  335. esac
  336. elif eval $BYOBU_TEST sw_vers >/dev/null 2>&1; then
  337. distro="$(sw_vers -productName)"
  338. elif eval $BYOBU_TEST uname >/dev/null 2>&1; then
  339. distro="$(uname -s)"
  340. else
  341. distro="Byobu"
  342. fi
  343. _RET="$distro"
  344. }
  345. # vi: syntax=sh ts=4 noexpandtab