Advertisement
Guest User

Untitled

a guest
Sep 10th, 2013
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.17 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. ################################################################################
  4. #
  5. # This script captures basic information for when a problem occurs. It can
  6. # be used any time a problem occurs, as root or as a mortal user. There are a
  7. # few modes of operation:
  8. #
  9. ################################################################################
  10.  
  11. usage="Usage: vacuum [ -thorough | -perf | -hang <pid> | -trap | -error <cmd> ]"
  12. mode_thorough=0
  13. mode_perf=0
  14. mode_hang=0
  15. mode_trap=0
  16. mode_error=0
  17. topdir="" # The data collection directory
  18.  
  19. if [ -n $HOME ]
  20. then
  21. topdir=$HOME/investigations
  22. else
  23. topdir=~/investigations/$i
  24. fi
  25.  
  26. if [ $# -gt 0 ]
  27. then
  28. while true ; do
  29. case $1 in
  30. "-thorough" )
  31. mode_thorough=1
  32. shift
  33. ;;
  34. "-perf" )
  35. mode_perf=1
  36. mode="PERF"
  37. shift
  38. ;;
  39. "-hang" )
  40. mode_hang=1
  41. shift
  42. if [ $# -le 0 ]
  43. then
  44. echo $usage
  45. exit 2
  46. fi
  47. pid=$1
  48. shift
  49. ;;
  50. "-trap" )
  51. mode_trap=1
  52. shift
  53. ;;
  54. "-error" )
  55. mode_error=1
  56. shift
  57. if [ $# -le 0 ]
  58. then
  59. echo $usage
  60. exit 2
  61. fi
  62. cmd=$1
  63. shift
  64. ;;
  65. * )
  66. echo $usage
  67. exit 2
  68. ;;
  69. esac
  70.  
  71. if [ $# -le 0 ]
  72. then
  73. break ;
  74. fi
  75. done
  76. fi
  77.  
  78. if [ ! -n $USER ]
  79. then
  80. USER=`whoami`
  81. fi
  82.  
  83. ################################################################################
  84. ## Create the appropriate directory for this problem
  85. ################################################################################
  86.  
  87. i=0
  88. invdir=$topdir/$i
  89.  
  90. while [ -d $invdir ]
  91. do
  92. let "i = i + 1"
  93. invdir=$topdir/$i
  94. done
  95.  
  96. echo Investigation directory: $invdir
  97.  
  98. ################################################################################
  99. ## Create data directory, src directory and the investigation log
  100. ################################################################################
  101.  
  102. mkdir $invdir
  103. mkdir $invdir/src
  104. datadir=$invdir/data
  105. invlog=$invdir/inv.txt
  106. mkdir $datadir
  107. touch $invlog
  108. echo "################################################################################" >> $invlog
  109. echo "## Header ##" >> $invlog
  110. echo "################################################################################" >> $invlog
  111. echo "Problem number : $i" >> $invlog
  112. echo -n "Time of data collector run : " >> $invlog
  113. date >> $invlog
  114. echo "Data collector run as : \"$0 $1 $2\" " >> $invlog
  115.  
  116. ################################################################################
  117. ## Ready to go...
  118. ################################################################################
  119.  
  120. function collectFile
  121. {
  122. local comment=$1
  123. local fileName=$2
  124. local output=""
  125.  
  126. echo -n "COLLECT: $fileName ($comment) ... " >> $invlog
  127.  
  128. output=`cp $fileName $datadir 2>&1`
  129.  
  130. if [ $? -ne 0 ]
  131. then
  132. echo "failed." >> $invlog
  133. echo "output from copy:" >> $invlog
  134. echo '{' >> $invlog
  135. echo $output >> $invlog
  136. echo '}' >> $invlog
  137.  
  138. else
  139. echo "success." >> $invlog
  140. fi
  141.  
  142. echo >> $invlog
  143.  
  144. }
  145.  
  146. function runCommand
  147. {
  148. local comment=$1
  149. local cmd=$2
  150.  
  151. echo "RUNCMD: $cmd ($comment) ... " >> $invlog
  152.  
  153. echo '{' >> $invlog
  154. $cmd 2>&1 >> $invlog 2>&1
  155. echo '}' >> $invlog
  156. echo >> $invlog
  157.  
  158. }
  159.  
  160. function doQuickCollect
  161. {
  162. echo >> $invlog
  163. echo "################################################################################" >> $invlog
  164. echo "## Quick Collect ##" >> $invlog
  165. echo "################################################################################" >> $invlog
  166.  
  167. #Environmental information
  168. runCommand "Environment variables" "/usr/bin/env"
  169.  
  170. #Network information
  171. collectFile "DNS resolution configuration file" "/etc/resolv.conf"
  172. collectFile "Name service switch configuration file" "/etc/nsswitch.conf"
  173. collectFile "Static table lookup file" "/etc/hosts"
  174. collectFile "TCP/IP services file" "/etc/services"
  175. runCommand "Interface information" "ifconfig -a"
  176. runCommand "Interface information (no DNS)" "/bin/netstat -i -n"
  177. runCommand "Socket information" "/bin/netstat -an"
  178. runCommand "Extended socket information" "/bin/netstat -avn"
  179. runCommand "Socket owner information" "/bin/netstat -p"
  180. runCommand "Network routing table" "/bin/netstat -rn"
  181. runCommand "Network statistics" "/bin/netstat -s"
  182. runCommand "Extended routing information" "/bin/netstat -rvn"
  183. ## the grep commands below look odd but it is a simple trick to get the contents of
  184. ## everything under specific directories
  185. runCommand "Network information from /proc" "/usr/bin/find /proc/net -type f -exec /bin/grep -Hv '^$' {} "
  186. runCommand "System information from /proc" "/usr/bin/find /proc/sys -type f -exec /bin/grep -Hv '^$' {} "
  187. runCommand "SYSV IPC info from /proc" '/usr/bin/find /proc/sysvipc -type f -exec /bin/grep -Hv ^$ {} ;'
  188.  
  189. #File system information
  190. runCommand "Type information" "/bin/df -lT"
  191. runCommand "Usage information" "/bin/df -lk"
  192. runCommand "Inode information" "/bin/df -li"
  193. runCommand "Share information" "/usr/sbin/showmount -e"
  194. runCommand "SCSI and IDE disk partition tables" "/sbin/fdisk -l /dev/sd* /dev/hd*"
  195. runCommand "NFS statistic" "/usr/sbin/nfsstat -cnrs"
  196. collectFile "Filesystems supported by the kernel" "/proc/filesystems"
  197. collectFile "Export file" "/etc/exports"
  198. collectFile "Mount file" "/etc/fstab"
  199. collectFile "Partition information" "/proc/partitions"
  200.  
  201. #Kernel information
  202. runCommand "User (resource) limits" "ulimit -a"
  203. runCommand "IPC information" "/usr/bin/ipcs -a"
  204. runCommand "Loaded module info" "/sbin/lsmod"
  205. runCommand "IPC resource limits" "/usr/bin/ipcs -l"
  206. runCommand "Kernel information" "/sbin/sysctl -a"
  207. runCommand "Memory usage" "/usr/bin/free"
  208. runCommand "Uptime" "/usr/bin/uptime"
  209. runCommand "System name, etc" "/bin/uname -a"
  210. runCommand "Current users" "/usr/bin/w"
  211. runCommand "Process listing" "/bin/ps auwx"
  212. runCommand "Recent users" "/usr/bin/last|/usr/bin/head -100"
  213. runCommand "Contents of home directory" "/bin/ls -lda $HOME"
  214. runCommand "Host ID" "/usr/bin/hostid"
  215. collectFile "Kernel limits specified by the user" "/etc/sysctl.conf"
  216. collectFile "Load average" "/proc/loadavg"
  217. collectFile "I/O memory map" "/proc/iomap"
  218. collectFile "I/O port regions" "/proc/ioports"
  219. collectFile "Interrupts per each IRQ" "/proc/interupts"
  220. collectFile "CPU status" "/proc/cpuinfo"
  221. collectFile "Memory usage" "/proc/meminfo"
  222. collectFile "Swap partition information" "/proc/swaps"
  223. collectFile "Slab information" "/proc/slabinfo"
  224. collectFile "Lock information" "/proc/locks"
  225. collectFile "Module information" "/proc/modules"
  226. collectFile "Version information" "/proc/version"
  227. collectFile "System status information" "/proc/stat"
  228. collectFile "PCI information" "/proc/pci"
  229.  
  230. #Version information
  231. runCommand "Package information" "/bin/rpm -qa"
  232.  
  233. #Misc
  234. collectFile "Main syslog file" "/var/log/messages"
  235. collectFile "Syslog configuration file" "/etc/syslog.conf"
  236.  
  237.  
  238. }
  239.  
  240. function doThoroughCollect
  241. {
  242. echo >> $invlog
  243. echo "################################################################################" >> $invlog
  244. echo "## Thorough Collect ##" >> $invlog
  245. echo "################################################################################" >> $invlog
  246.  
  247. runCommand "Virtual memory statistics" "/usr/bin/vmstat 2 5"
  248. runCommand "I/O statistics" "/usr/bin/iostat 2 5"
  249. runCommand "Extended I/O statistics" "/usr/bin/iostat -x 2 5"
  250. runCommand "CPU statistics" "/usr/bin/mpstat -P ALL 2 5"
  251. runCommand "System activity" "/usr/bin/sar -A 2 5"
  252.  
  253. }
  254.  
  255. function doPerfCollect
  256. {
  257. echo >> $invlog
  258. echo "################################################################################" >> $invlog
  259. echo "## Performance Collect ##" >> $invlog
  260. echo "################################################################################" >> $invlog
  261.  
  262. # Add specific commands here
  263.  
  264. }
  265.  
  266. function doHangCollect
  267. {
  268. echo >> $invlog
  269. echo "################################################################################" >> $invlog
  270. echo "## Hang Collect ##" >> $invlog
  271. echo "################################################################################" >> $invlog
  272.  
  273. # NOTE: $pid contains the process ID of the process that is hanging
  274.  
  275. ## check whether the process actually exists
  276. kill -0 $pid 2>/dev/null 1>/dev/null
  277. if [ ! $? -eq 0 ]
  278. then
  279. echo "Process ID \"$pid\" not found."
  280. exit 3
  281. fi
  282.  
  283. # Add specific commands here
  284.  
  285. }
  286.  
  287. function doErrorCollect
  288. {
  289. echo >> $invlog
  290. echo "################################################################################" >> $invlog
  291. echo "## Error Collect ##" >> $invlog
  292. echo "################################################################################" >> $invlog
  293.  
  294. # NOTE: $cmd contains the name of the command line that apparently produces an error
  295.  
  296. # Add specific commands here
  297. }
  298.  
  299. function doTrapCollect
  300. {
  301. echo >> $invlog
  302. echo "################################################################################" >> $invlog
  303. echo "## Trap Collect ##" >> $invlog
  304. echo "################################################################################" >> $invlog
  305.  
  306. # Add specific commands here
  307. }
  308.  
  309. ################################## MAIN SCRIPT BODY ######################################
  310.  
  311. ## Do the basics first, then anything else that might be needed
  312. ##
  313. doQuickCollect
  314.  
  315. if [ $mode_thorough -eq 1 ]
  316. then
  317. echo "Collecting thorough information"
  318. doThoroughCollect
  319. fi
  320.  
  321. if [ $mode_perf -eq 1 ]
  322. then
  323. echo "Collecting perf information"
  324. doPerfCollect
  325. fi
  326.  
  327. if [ $mode_hang -eq 1 ]
  328. then
  329. echo "Collecting hang information"
  330. doHangCollect
  331. fi
  332.  
  333. if [ $mode_trap -eq 1 ]
  334. then
  335. echo "Collecting trap information"
  336. doTrapCollect
  337. fi
  338.  
  339. if [ $mode_error -eq 1 ]
  340. then
  341. echo "Collecting error information"
  342. doErrorCollect
  343. fi
  344.  
  345. echo >> $invlog
  346. echo "################################################################################" >> $invlog
  347. echo "## End of Data Collection (the rest is for user investigation) ##" >> $invlog
  348. echo "################################################################################" >> $invlog
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement