sys-info 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. #
  2. # Exposes Linux Standard Base and uname informations via the $lsb_info associative array.
  3. #
  4. # Authors:
  5. # Emmanuel Bouthenot <kolter@openics.org>
  6. #
  7. # Gets the SYS informations.
  8. typeset -gA sys_info
  9. function sys-info {
  10. local lsb_id
  11. local lsb_id_format
  12. local lsb_id_formatted
  13. local lsb_description
  14. local lsb_description_format
  15. local lsb_description_formatted
  16. local lsb_release
  17. local lsb_release_format
  18. local lsb_release_formatted
  19. local lsb_codename
  20. local lsb_codename_format
  21. local lsb_codename_formatted
  22. local uname_all
  23. local uname_all_format
  24. local uname_all_formatted
  25. local uname_hw
  26. local uname_hw_format
  27. local uname_hw_formatted
  28. local uname_kernel_name
  29. local uname_kernel_name_format
  30. local uname_kernel_name_formatted
  31. local uname_kernel_release
  32. local uname_kernel_release_format
  33. local uname_kernel_release_formatted
  34. local uname_kernel_version
  35. local uname_kernel_version_format
  36. local uname_kernel_version_formatted
  37. local uname_machine
  38. local uname_machine_format
  39. local uname_machine_formatted
  40. local uname_nodename
  41. local uname_nodename_format
  42. local uname_nodename_formatted
  43. local uname_processor
  44. local uname_processor_format
  45. local uname_processor_formatted
  46. local uname_os
  47. local uname_os_format
  48. local uname_os_formatted
  49. local chroot
  50. local chroot_format
  51. local chroot_formatted
  52. local ssh_tty
  53. local ssh_tty_format
  54. local ssh_tty_formatted
  55. local ssh_server_address
  56. local ssh_server_address_format
  57. local ssh_server_address_formatted
  58. local ssh_server_port
  59. local ssh_server_port_format
  60. local ssh_server_port_formatted
  61. local ssh_client_address
  62. local ssh_client_address_format
  63. local ssh_client_address_formatted
  64. local ssh_client_port
  65. local ssh_client_port_format
  66. local ssh_client_port_formatted
  67. local -A info_formats
  68. local info_format
  69. if (( $+commands[lsb_release] )); then
  70. # FORMAT LSB_ID
  71. zstyle -s ':prezto:module:sys:info:lsb_id' format 'lsb_id_format'
  72. if [[ -n "$lsb_id_format" ]]; then
  73. lsb_id="$(lsb_release --short --id)"
  74. if [[ -n "$lsb_id" ]]; then
  75. zformat -f lsb_id_formatted "$lsb_id_format" "i:$lsb_id"
  76. fi
  77. fi
  78. # FORMAT LSB_DESCRIPTION
  79. zstyle -s ':prezto:module:sys:info:lsb_description' format 'lsb_description_format'
  80. if [[ -n "$lsb_description_format" ]]; then
  81. lsb_description="$(lsb_release --short --description)"
  82. if [[ -n "$lsb_description" ]]; then
  83. zformat -f lsb_description_formatted "$lsb_description_format" "d:$lsb_description"
  84. fi
  85. fi
  86. # FORMAT LSB_RELEASE
  87. zstyle -s ':prezto:module:sys:info:lsb_release' format 'lsb_release_format'
  88. if [[ -n "$lsb_release_format" ]]; then
  89. lsb_release="$(lsb_release --short --release)"
  90. if [[ -n "$lsb_release" ]]; then
  91. zformat -f lsb_release_formatted "$lsb_release_format" "r:$lsb_release"
  92. fi
  93. fi
  94. # FORMAT LSB_CODENAME
  95. zstyle -s ':prezto:module:sys:info:lsb_codename' format 'lsb_codename_format'
  96. if [[ -n "$lsb_codename_format" ]]; then
  97. lsb_codename="$(lsb_release --short --codename)"
  98. if [[ -n "$lsb_codename" ]]; then
  99. zformat -f lsb_codename_formatted "$lsb_codename_format" "c:$lsb_codename"
  100. fi
  101. fi
  102. fi
  103. if (( $+commands[uname] )); then
  104. # UNAME ALL
  105. zstyle -s ':prezto:module:sys:info:uname_all' format 'uname_all_format'
  106. if [[ -n "$uname_all_format" ]]; then
  107. uname_all="$(uname -a)"
  108. if [[ -n "$uname_all" ]]; then
  109. zformat -f uname_all_formatted "$uname_all_format" "A:$uname_all"
  110. fi
  111. fi
  112. # UNAME HW
  113. zstyle -s ':prezto:module:sys:info:uname_hw' format 'uname_hw_format'
  114. if [[ -n "$uname_hw_format" ]]; then
  115. uname_hw="$(uname -i)"
  116. if [[ -n "$uname_hw" ]]; then
  117. zformat -f uname_hw_formatted "$uname_hw_format" "H:$uname_hw"
  118. fi
  119. fi
  120. # UNAME KERNEL_NAME
  121. zstyle -s ':prezto:module:sys:info:uname_kernel_name' format 'uname_kernel_name_format'
  122. if [[ -n "$uname_kernel_name_format" ]]; then
  123. uname_kernel_name="$(uname -s)"
  124. if [[ -n "$uname_kernel_name" ]]; then
  125. zformat -f uname_kernel_name_formatted "$uname_kernel_name_format" "K:$uname_kernel_name"
  126. fi
  127. fi
  128. # UNAME KERNEL_RELEASE
  129. zstyle -s ':prezto:module:sys:info:uname_kernel_release' format 'uname_kernel_release_format'
  130. if [[ -n "$uname_kernel_release_format" ]]; then
  131. uname_kernel_release="$(uname -r)"
  132. if [[ -n "$uname_kernel_release" ]]; then
  133. zformat -f uname_kernel_release_formatted "$uname_kernel_release_format" "R:$uname_kernel_release"
  134. fi
  135. fi
  136. # UNAME KERNEL_VERSION
  137. zstyle -s ':prezto:module:sys:info:uname_kernel_version' format 'uname_kernel_version_format'
  138. if [[ -n "$uname_kernel_version_format" ]]; then
  139. uname_kernel_version="$(uname -v)"
  140. if [[ -n "$uname_kernel_version" ]]; then
  141. zformat -f uname_kernel_version_formatted "$uname_kernel_version_format" "V:$uname_kernel_version"
  142. fi
  143. fi
  144. # UNAME MACHINE
  145. zstyle -s ':prezto:module:sys:info:uname_machine' format 'uname_machine_format'
  146. if [[ -n "$uname_machine_format" ]]; then
  147. uname_machine="$(uname -m)"
  148. if [[ -n "$uname_machine" ]]; then
  149. zformat -f uname_machine_formatted "$uname_machine_format" "M:$uname_machine"
  150. fi
  151. fi
  152. # UNAME NODENAME
  153. zstyle -s ':prezto:module:sys:info:uname_nodename' format 'uname_nodename_format'
  154. if [[ -n "$uname_nodename_format" ]]; then
  155. uname_nodename="$(uname -n)"
  156. if [[ -n "$uname_nodename" ]]; then
  157. zformat -f uname_nodename_formatted "$uname_nodename_format" "N:$uname_nodename"
  158. fi
  159. fi
  160. # UNAME PROCESSOR
  161. zstyle -s ':prezto:module:sys:info:uname_processor' format 'uname_processor_format'
  162. if [[ -n "$uname_processor_format" ]]; then
  163. uname_processor="$(uname -p)"
  164. if [[ -n "$uname_processor" ]]; then
  165. zformat -f uname_processor_formatted "$uname_processor_format" "P:$uname_processor"
  166. fi
  167. fi
  168. # UNAME OS
  169. zstyle -s ':prezto:module:sys:info:uname_os' format 'uname_os_format'
  170. if [[ -n "$uname_os_format" ]]; then
  171. # -o option is not supported on all OS (at least Linux and FreeBSD)
  172. uname_os="$(uname -o 2>/dev/null)"
  173. if [[ -n "$uname_os" ]]; then
  174. zformat -f uname_os_formatted "$uname_os_format" "O:$uname_os"
  175. fi
  176. fi
  177. fi
  178. # CHROOT
  179. zstyle -s ':prezto:module:sys:info:chroot' format 'chroot_format'
  180. if [[ -n "$chroot_format" ]]; then
  181. if [[ -f /etc/debian_chroot ]]; then
  182. chroot="Debian"
  183. fi
  184. if [[ -n "$chroot" ]]; then
  185. zformat -f chroot_formatted "$chroot_format" "C:$chroot"
  186. fi
  187. fi
  188. # SSH CONNECTION
  189. if [[ -n "${SSH_CONNECTION}" ]]; then
  190. zstyle -s ':prezto:module:sys:info:ssh_tty' format 'ssh_tty_format'
  191. if [[ -n "$ssh_tty_format" ]]; then
  192. if [[ -n "${SSH_TTY}" ]]; then
  193. ssh_tty="${SSH_TTY}"
  194. fi
  195. if [[ -n "$ssh_tty" ]]; then
  196. zformat -f ssh_tty_formatted "$ssh_tty_format" "T:$ssh_tty"
  197. fi
  198. fi
  199. echo ${SSH_CONNECTION} | read ssh_client_address ssh_client_port ssh_server_address ssh_server_port
  200. zstyle -s ':prezto:module:sys:info:ssh_server_address' format 'ssh_server_address_format'
  201. if [[ -n "$ssh_server_address_format" ]]; then
  202. if [[ -n "$ssh_server_address" ]]; then
  203. zformat -f ssh_server_address_formatted "$ssh_server_address_format" "X:$ssh_server_address"
  204. fi
  205. fi
  206. zstyle -s ':prezto:module:sys:info:ssh_server_port' format 'ssh_server_port_format'
  207. if [[ -n "$ssh_server_port_format" ]]; then
  208. if [[ -n "$ssh_server_port" ]]; then
  209. zformat -f ssh_server_port_formatted "$ssh_server_port_format" "Y:$ssh_server_port"
  210. fi
  211. fi
  212. zstyle -s ':prezto:module:sys:info:ssh_client_address' format 'ssh_client_address_format'
  213. if [[ -n "$ssh_client_address_format" ]]; then
  214. if [[ -n "$ssh_client_address" ]]; then
  215. zformat -f ssh_client_address_formatted "$ssh_client_address_format" "x:$ssh_client_address"
  216. fi
  217. fi
  218. zstyle -s ':prezto:module:sys:info:ssh_client_port' format 'ssh_client_port_format'
  219. if [[ -n "$ssh_client_port_format" ]]; then
  220. if [[ -n "$ssh_client_port" ]]; then
  221. zformat -f ssh_client_port_formatted "$ssh_client_port_format" "y:$ssh_client_port"
  222. fi
  223. fi
  224. fi
  225. # Format info.
  226. zstyle -a ':prezto:module:sys:info:keys' format 'info_formats'
  227. for info_format in ${(k)info_formats}; do
  228. zformat -f REPLY "$info_formats[$info_format]" \
  229. "i:$lsb_id_formatted" \
  230. "d:$lsb_description_formatted" \
  231. "r:$lsb_release_formatted" \
  232. "c:$lsb_codename_formatted" \
  233. "c:$lsb_codename_formatted" \
  234. "A:$uname_all_formatted" \
  235. "H:$uname_hw_formatted" \
  236. "K:$uname_kernel_name_formatted" \
  237. "R:$uname_kernel_release_formatted" \
  238. "V:$uname_kernel_version_formatted" \
  239. "M:$uname_machine_formatted" \
  240. "N:$uname_nodename_formatted" \
  241. "P:$uname_processor_formatted" \
  242. "O:$uname_os_formatted" \
  243. "C:$chroot_formatted" \
  244. "T:$ssh_tty_formatted" \
  245. "X:$ssh_server_address_formatted" \
  246. "Y:$ssh_server_port_formatted" \
  247. "x:$ssh_client_address_formatted" \
  248. "y:$ssh_client_port_formatted"
  249. sys_info[$info_format]="$REPLY"
  250. done
  251. unset REPLY
  252. return 0
  253. }
  254. sys-info "$@"