PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/c

http://power-collector.googlecode.com/
Korn Shell | 601 lines | 398 code | 69 blank | 134 comment | 36 complexity | 652106fccd2151c1f5e4a5ac44faf9c8 MD5 | raw file
  1. #!/bin/ksh
  2. #set -x
  3. # V A R I A B L E S
  4. level=0
  5. myName=$(basename $0)
  6. dFlag=
  7. sFlag=
  8. tFlag=
  9. # F U N C T I O N S
  10. #
  11. # All functions with names ending in _f provide formatted output and are aware of the tex or notex mode
  12. # All other functions provide raw output
  13. #
  14. function print_usage
  15. {
  16. # needs param: none
  17. # returns: raw usage info
  18. # called by: many
  19. # calls: none
  20. printf "Usage:\t%s [-d] [-s|-t]\n" ${myName}
  21. printf "\t-d: debug mode\n"
  22. printf "\t-s: screen (notex) output mode, the default\n"
  23. printf "\t-t: .tex output mode\n"
  24. }
  25. function lowerCase
  26. {
  27. # needs param: string
  28. # returns: lower case string
  29. # called by: many
  30. # calls: none
  31. echo $1 | tr 'ABCDEFGHIJKLMNOPQRSTUVXYZ' 'abcdefghijklmnopqrstuvwxyz'
  32. }
  33. function check_root
  34. {
  35. # needs param: none
  36. # returns: none
  37. # called by: main
  38. # calls: none
  39. if [ $LOGNAME != root ]
  40. then
  41. echo This script must be run as root!
  42. exit
  43. fi
  44. }
  45. function levelSpace
  46. {
  47. # needs param: level
  48. # returns: string of level*2 spaces (0,2,4,6,...)
  49. # called by: many
  50. # calls: none
  51. myLevel=$1
  52. levelSpace=
  53. oneTab='\t'
  54. while [ ${myLevel} -gt 0 ]
  55. do
  56. levelSpace=${levelSpace}${oneTab}
  57. (( myLevel = myLevel -1 ))
  58. done
  59. printf "%s" ${levelSpace}
  60. }
  61. function print_header_f
  62. {
  63. # needs param: level
  64. # returns: formatted document header
  65. # called by: main
  66. # calls: get_hostname, get_mt, get_sn
  67. level=$1
  68. if [ ! -z "$tFlag" ]
  69. then
  70. echo "\\documentclass{article}"
  71. echo "\\\\title{$(get_hostname)}"
  72. echo "\\\\author{Collector v0.1}"
  73. echo "\\date{\\\\today}"
  74. echo "\\\\begin{document}"
  75. echo "\\maketitle"
  76. echo "\\\\tableofcontents"
  77. else
  78. #echo "$(get_hostname) (machine type: $(get_mt), serial number: $(get_sn))"
  79. printf "$(levelSpace ${level})%s (machine type: %s, serial number: %s)\n" $(get_hostname) $(get_mt) $(get_sn)
  80. fi
  81. }
  82. function print_trailer_f
  83. {
  84. # needs param: none
  85. # returns: formatted document trailer
  86. # called by: main
  87. # calls: none
  88. if [ ! -z "$tFlag" ]
  89. then
  90. echo '\end{document}'
  91. fi
  92. }
  93. function print_imageType_f
  94. {
  95. # needs param: none
  96. # returns: formatted OS type: LPAR or standalone
  97. # called by: main
  98. # calls: get_lparId
  99. if [ ! -z "$tFlag" ]
  100. then
  101. echo FIXME: TEX imageType
  102. else
  103. if [ $(get_lparId) -ne 0 ]
  104. then
  105. lparstat -i | grep ^Type | awk '{print $3}' | read lparType
  106. lparstat -i | grep ^Mode | awk '{print $3}' | read lparMode
  107. printf "$(levelSpace ${level})This system is a %s type and %s mode LPAR.\n" $(lowerCase ${lparType}) $(lowerCase ${lparMode})
  108. printf "$(levelSpace ${level})LPAR ID: %s\n" $(get_lparId)
  109. else
  110. printf "$(levelSpace ${level})This system is a standalone box.\n"
  111. fi
  112. fi
  113. }
  114. function print_general_f
  115. {
  116. # needs param: none
  117. # returns: formatted general information
  118. # called by: main
  119. # calls: none
  120. if [ ! -z "$tFlag" ]
  121. then
  122. echo FIXME: TEX general
  123. else
  124. oslevel -s | read curOS
  125. oslevel -qs 2>/dev/null | head -1 | read latestOS
  126. if [ ${curOS} = ${latestOS} ]
  127. then
  128. printf "$(levelSpace ${level})OS level: %s OK\n" ${curOS}
  129. else
  130. printf "$(levelSpace ${level})OS level: %s, latest known level %s\n" ${curOS} ${latestOS}
  131. fi
  132. lparstat -i | grep "Entitled Capacity" | read j1 j2 j3 entCPU j5
  133. lparstat -i | grep "Minimum Capacity" | read j1 j2 j3 minCPU j5
  134. lparstat -i | grep "Maximum Capacity" | read j1 j2 j3 maxCPU j5
  135. lparstat -i | grep "Variable Capacity Weight" | read j1 j2 j3 j4 weightCPU j6
  136. printf "$(levelSpace ${level})CPU: entitled %s, weight %s, minimum %s, maximum %s\n" ${entCPU} ${weightCPU} ${minCPU} ${maxCPU}
  137. lparstat -i | grep "Online Memory"| read j1 j2 j3 onlineMemory j5
  138. lparstat -i | grep "Minimum Memory"| read j1 j2 j3 minMemory j5
  139. lparstat -i | grep "Maximum Memory"| read j1 j2 j3 maxMemory j5
  140. printf "$(levelSpace ${level})LPAR memory: online %s MB, minimum %s MB, maximum %s MB\n" ${onlineMemory} ${minMemory} ${maxMemory}
  141. fi
  142. }
  143. function get_lparId
  144. {
  145. # needs param: none
  146. # returns: raw LPAR id, a single number. if 0, the system is standalone
  147. # called by: ????
  148. # calls: none
  149. uname -L | read lparId hostName
  150. echo ${lparId}
  151. }
  152. function get_mt
  153. {
  154. # needs param: none
  155. # returns: raw machine type, in the form of TTTT-MMM, where T is type and M is model
  156. # called by: print_header_f
  157. # calls: none
  158. lsattr -El sys0 -a modelname -F value | cut -d',' -f2 | read mt
  159. echo ${mt}
  160. }
  161. function get_sn
  162. {
  163. # needs param: none
  164. # returns: raw serial number, in the form of OO-SSSSS, where O is origin and S is serial
  165. # called by: print_header_f
  166. # calls: none
  167. lsattr -El sys0 -a systemid -F value | cut -d',' -f2 | cut -c 3-4 | read origin
  168. lsattr -El sys0 -a systemid -F value | cut -d',' -f2 | cut -c 5-9 | read serial
  169. echo ${origin}-${serial}
  170. }
  171. function get_hostname
  172. {
  173. # needs param: none
  174. # returns: raw hostname
  175. # called by: print_header_f,get_tcpip_f
  176. # calls: none
  177. hostname
  178. }
  179. function get_network_adapters_f
  180. {
  181. # needs param: none
  182. # returns: formatted list of network adapters
  183. # called by: get_network
  184. # calls: get_virtual_network_adapter_macs,get_network_interfaces_f
  185. if [ ! -z "$tFlag" ]
  186. then
  187. echo FIXME: TEX network adapters
  188. else
  189. lsdev -Cc adapter -s vdevice -t IBM,l-lan -F name | wc -l | read number
  190. if [ ${number} -eq 0 ]
  191. then
  192. printf "$(levelSpace ${level})No virtual ethernet adapters on this system.\n"
  193. else
  194. printf "$(levelSpace ${level})Virtual network adapters:\n"
  195. (( level = level + 1 ))
  196. lsdev -Cc adapter -s vdevice -t IBM,l-lan -F name | while read device
  197. do
  198. printf "$(levelSpace ${level})Adapter %s, PVID: %s, VIDs: %s, MAC address %s, location %s\n" ${device} $(get_virtual_adapter_pvid ${device}) $(get_virtual_adapter_vids ${device}) $(get_virtual_network_adapter_macs ${device} | cut -f2) $(get_device_location ${device})
  199. get_network_interfaces_f ${device}
  200. done
  201. fi
  202. (( level = level - 1 ))
  203. printf "$(levelSpace ${level})No physical ethernet adapters on this system.\n"
  204. fi
  205. }
  206. function get_virtual_network_adapter_macs
  207. {
  208. # needs param: adapter device name
  209. # returns: space separated list of two forms of the same MAC address (32284F48026A 32:28:4f:48:02:6a)
  210. # called by: get_network_adapters
  211. # calls: none
  212. device=$1
  213. #lscfg -vl ${device} | grep "Network Address" | cut -c 37-49 | read mac1
  214. entstat -d ent0 | grep "Hardware Address:" | cut -c 19-35 | read mac2
  215. #printf "${mac1} ${mac2}"
  216. printf "${mac2}\n"
  217. }
  218. function get_virtual_adapter_pvid
  219. {
  220. # needs param: adapter device name
  221. # returns: PVID
  222. # called by: get_network_adapters
  223. # calls: none
  224. device=$1
  225. entstat -d ${device} | grep PVID | grep VIDs > /dev/null 2>&1
  226. case $? in
  227. 0)
  228. entstat -d ${device} | grep PVID | grep VIDs | awk '{print $2}'
  229. ;;
  230. 1)
  231. entstat -d ${device} | grep "Port VLAN ID:" | awk '{print $4}'
  232. ;;
  233. *)
  234. printf "???\n"
  235. ;;
  236. esac
  237. }
  238. function get_virtual_adapter_vids
  239. {
  240. # needs param: adapter device name
  241. # returns: VIDs
  242. # called by: get_network_adapters
  243. # calls: none
  244. device=$1
  245. # lowerCase $(entstat -d ${device} | grep PVID | grep VIDs | awk '{print $4}')
  246. entstat -d ${device} | grep PVID | grep VIDs > /dev/null 2>&1
  247. case $? in
  248. 0)
  249. lowerCase $(entstat -d ${device} | grep PVID | grep VIDs | awk '{print $4}')
  250. ;;
  251. 1)
  252. lowerCase $(entstat -d ${device} | grep "VLAN Tag IDs:" | awk '{print $4}')
  253. ;;
  254. *)
  255. printf "???\n"
  256. ;;
  257. esac
  258. }
  259. function get_device_location
  260. {
  261. # needs param: adapter device name
  262. # returns: device location string
  263. # called by: get_network_adapters
  264. # calls: none
  265. device=$1
  266. lscfg -vl ${device} | head -1 | awk '{print $2}'
  267. }
  268. function get_network_interfaces_f
  269. {
  270. # needs param: adapter device name (ent0)
  271. # returns: formatted list of all network interfaces on a given adapter
  272. # called by: get_network
  273. # calls: none
  274. device=$1
  275. if [ ! -z "$tFlag" ]
  276. then
  277. echo FIXME: TEX get_network_interfaces
  278. else
  279. echo ${device} | cut -c 1-3 | read ifType
  280. if [ ${ifType} != ent ]
  281. then
  282. printf "$(levelSpace ${level})Unknown adapter type: %s" ${device}
  283. else
  284. echo ${device} | cut -c 4 | read ifNumber
  285. lsattr -El en${ifNumber} -a netaddr -F value | read ifNetaddr
  286. if [ ! -z "$ifNetaddr" ]
  287. then
  288. lsattr -El en${ifNumber} -a state -F value | read ifState
  289. lsattr -El en${ifNumber} -a netmask -F value | read ifMask
  290. printf "$(levelSpace ${level})Inteface: %s, state: %s, address: %s, netmask: %s\n" en${ifNumber} ${ifState} ${ifNetaddr} ${ifMask}
  291. else
  292. printf "$(levelSpace ${level})Interface %s not configured." en${ifNumber}
  293. fi
  294. fi
  295. fi
  296. }
  297. function get_tcpip_f
  298. {
  299. # needs param: none
  300. # returns: formatted TCP/IP information
  301. # called by: get_network
  302. # calls:
  303. if [ ! -z "$tFlag" ]
  304. then
  305. echo FIXME: TEX tcpip
  306. else
  307. printf "$(levelSpace ${level})Hostname: $(get_hostname)\n"
  308. numGws=0
  309. netstat -rn | grep default | while read j1 gw rest
  310. do
  311. printf "$(levelSpace ${level})Default gateway: %s\n" ${gw}
  312. (( numGws = numGws + 1 ))
  313. done
  314. case ${numGws} in
  315. 0) printf "No active gateways.";;
  316. 1) ;;
  317. *) printf "More than one gateways active!";;
  318. esac
  319. fi
  320. }
  321. function get_namerslv_f
  322. {
  323. # needs param: none
  324. # returns: formatted name resolving information
  325. # called by: get_network
  326. # calls:
  327. if [ ! -z "$tFlag" ]
  328. then
  329. echo FIXME: TEX namerslv
  330. else
  331. counter=0
  332. namerslv -s -I | while read j1 dnsserver j2
  333. do
  334. (( counter = counter + 1 ))
  335. printf "$(levelSpace ${level})Domain name server (%s): %s\n" ${counter} ${dnsserver}
  336. done
  337. counter=0
  338. namerslv -s -n | while read j1 domain j2
  339. do
  340. (( counter = counter + 1 ))
  341. printf "$(levelSpace ${level})Domain name: %s\n" ${domain}
  342. done
  343. if [ ${counter} -ne 1 ]
  344. then
  345. printf "$(levelSpace ${level})Multiple domain names defined!\n"
  346. fi
  347. fi
  348. }
  349. function get_network
  350. {
  351. # needs param: none
  352. # returns: formatted network information
  353. # called by: main
  354. # calls: get_network_adapters, get_network_interfacesm, get_tcpip_f
  355. get_tcpip_f
  356. get_namerslv_f
  357. printf "$(levelSpace ${level})Network adapters:\n"
  358. (( level = level + 1 ))
  359. get_network_adapters_f
  360. }
  361. function get_storage
  362. {
  363. # needs param: none
  364. # returns: n/a
  365. # called by: main
  366. # calls : get_vgs_f, get_disks_f
  367. get_vgs_f
  368. get_disks_f
  369. }
  370. function check_mirr_status
  371. {
  372. # needs param: vgname
  373. # returns: mirror status
  374. # called by: get_disks_f
  375. # calls: none
  376. vg=$1
  377. result=OK
  378. lsvg ${vg} | grep "TOTAL PVs" | read j1 j2 numDisks rest
  379. if [ ${numDisks} -gt 1 ]
  380. then
  381. lsvg -l ${vg} | tail -n +3 | grep -v sysdump | while read lv type lps pps pvs state mp
  382. do
  383. (( lps = 2*lps ))
  384. state=$(echo ${state} | cut -d/ -f2)
  385. if [ ${lps} -ne ${pps} -o ${pvs} -lt 2 -o ${state} != syncd ]
  386. then
  387. result=ERROR
  388. fi
  389. done
  390. fi
  391. echo ${result}
  392. }
  393. function get_vgs_f
  394. {
  395. # needs param: none
  396. # returns: formatted vg information
  397. # called by: get_storage
  398. # calls: check_mirr_status
  399. if [ ! -z "$tFlag" ]
  400. then
  401. echo FIXME: TEX vgs
  402. else
  403. printf "$(levelSpace ${level})Volume groups:\n"
  404. (( level = level + 1 ))
  405. for vg in rootvg $(lsvg -o | grep -v rootvg)
  406. do
  407. printf "$(levelSpace ${level})%s: " ${vg}
  408. if [ ${vg} = rootvg ]
  409. then
  410. # if VG is rootvg
  411. lsvg rootvg | grep "TOTAL PVs" | read j1 j2 numDisks rest
  412. onlineStatus=ONLINE
  413. mirrStatus=$(check_mirr_status rootvg)
  414. lsvg -l rootvg | grep sysdump | wc -l | read numDump
  415. printf "VG has %s disk(s) and is %s, mirror status is %s, number of dump LVs is %s.\n" ${numDisks} ${onlineStatus} ${mirrStatus} ${numDump}
  416. else
  417. # if VG is NOT rootvg
  418. lsvg -o | grep -x ${vg} > /dev/null 2>&1
  419. if [ $? -eq 0 ]
  420. then
  421. # if VG is ONLINE
  422. onlineStatus=ONLINE
  423. lsvg ${vg} | grep "TOTAL PVs" | read j1 j2 numDisks rest
  424. mirrStatus=$(check_mirr_status ${vg})
  425. else
  426. # if VG is OFFLINE
  427. onlineStatus=OFFLINE
  428. lspv | grep -x ${vg} | wc -l | read numDisks
  429. mirrStatus=UNKNOWN
  430. fi
  431. printf "VG has %s disk(s) and is %s, mirror status is %s.\n" ${numDisks} ${onlineStatus} ${mirrStatus}
  432. fi
  433. done
  434. (( level = level - 1 ))
  435. fi
  436. }
  437. function get_disks_f
  438. {
  439. # needs param: none
  440. # returns: formatted disk information
  441. # called by: get_storage
  442. # calls: none
  443. if [ ! -z "$tFlag" ]
  444. then
  445. echo FIXME: TEX disks
  446. else
  447. printf "$(levelSpace ${level})Disks:\n"
  448. (( level = level + 1 ))
  449. lspv | sort | while read disk pvid vg status
  450. do
  451. if [ ${vg} = None ]
  452. then
  453. diskVg="not in VG"
  454. else
  455. diskVg="member of ${vg}"
  456. fi
  457. lsvg -o | grep -x ${vg} > /dev/null 2>&1
  458. if [ $? -eq 0 ]
  459. then
  460. # if VG is ONLINE
  461. lspv ${disk} | grep "TOTAL PPs" | read j1 j2 totPps rest
  462. lspv ${disk} | grep "USED PPs" | read j1 j2 usdPps rest
  463. diskFull=$(printf "%s\n" "scale=2; ${usdPps}/${totPps}*100" | bc)
  464. diskFull="${diskFull}% full,"
  465. else
  466. # if VG is OFFLINE
  467. diskFull=""
  468. fi
  469. diskSize=$(getconf DISK_SIZE /dev/${disk})
  470. diskDesc=$(lsdev -l ${disk} -F description)
  471. printf "$(levelSpace ${level})%s: %s, size %s MB, %s type %s.\n" ${disk} "${diskVg}" "${diskSize}" "${diskFull}" "${diskDesc}"
  472. done
  473. lsdev -Cc disk -S d | wc -l | read diskDefined
  474. case ${diskDefined} in
  475. 0)
  476. ;;
  477. 1)
  478. printf "$(levelSpace ${level})1 disk is in the DEFINED state!\n"
  479. ;;
  480. *)
  481. printf "$(levelSpace ${level})%s disks are in the DEFINED state!\n" ${diskDefined}
  482. ;;
  483. esac
  484. (( level = level - 1 ))
  485. fi
  486. }
  487. # M A I N
  488. # will be re-enabled when final
  489. #check_root
  490. while getopts dst opt
  491. do
  492. case ${opt} in
  493. d) dFlag=1
  494. set -x
  495. ;;
  496. s) sFlag=1
  497. ;;
  498. t) tFlag=1
  499. ;;
  500. ?) print_usage
  501. exit 1
  502. ;;
  503. esac
  504. done
  505. if [ "$sFlag" = "1" -a "$tFlag" = "1" ]
  506. then
  507. printf "Flags -s and -t are mutually exclusive.\n"
  508. print_usage
  509. exit 1
  510. fi
  511. #if [ ! -z "$dFlag" ]; then
  512. #printf "Option -d specified\n"
  513. #fi
  514. #if [ ! -z "$sFlag" ]; then
  515. # printf "Option -s specified\n"
  516. #fi
  517. #
  518. #if [ ! -z "$tFlag" ]; then
  519. # printf 'Option -t specified\n'
  520. #fi
  521. #shift $(($OPTIND -1))
  522. #printf "Remaining arguments are: %s\n" "$*"
  523. level=0
  524. print_header_f ${level}
  525. level=1
  526. print_imageType_f
  527. print_general_f
  528. #get_lparId
  529. get_network
  530. (( level = level - 1 ))
  531. get_storage
  532. print_trailer_f