systemadmin.zsh 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. # ------------------------------------------------------------------------------
  2. # Description
  3. # -----------
  4. #
  5. # This is one for the system administrator, operation and maintenance.
  6. # Some of which come from http://justinlilly.com/dotfiles/zsh.html
  7. #
  8. # ------------------------------------------------------------------------------
  9. # Authors
  10. # -------
  11. #
  12. # * Dongweiming <ciici123@gmail.com>
  13. #
  14. # ------------------------------------------------------------------------------
  15. function retval() {
  16. if [[ -z $1 ]];then
  17. echo '.'
  18. else
  19. echo $1
  20. fi
  21. }
  22. function retlog() {
  23. if [[ -z $1 ]];then
  24. echo '/var/log/nginx/access.log'
  25. else
  26. echo $1
  27. fi
  28. }
  29. alias ping='ping -c 5'
  30. alias clr='clear;echo "Currently logged in on $(tty), as $(whoami) in directory $(pwd)."'
  31. alias path='echo -e ${PATH//:/\\n}'
  32. alias mkdir='mkdir -pv'
  33. # get top process eating memory
  34. alias psmem='ps -e -orss=,args= | sort -b -k1,1n'
  35. alias psmem10='ps -e -orss=,args= | sort -b -k1,1n| head -10'
  36. # get top process eating cpu if not work try excute : export LC_ALL='C'
  37. alias pscpu='ps -e -o pcpu,cpu,nice,state,cputime,args|sort -k1 -nr'
  38. alias pscpu10='ps -e -o pcpu,cpu,nice,state,cputime,args|sort -k1 -nr | head -10'
  39. # top10 of the history
  40. alias hist10='print -l ${(o)history%% *} | uniq -c | sort -nr | head -n 10'
  41. # directory LS
  42. dls () {
  43. ls -l | grep "^d" | awk '{ print $9 }' | tr -d "/"
  44. }
  45. psgrep() {
  46. ps aux | grep "$(retval $1)" | grep -v grep
  47. }
  48. # Kills any process that matches a regexp passed to it
  49. killit() {
  50. ps aux | grep -v "grep" | grep "$@" | awk '{print $2}' | xargs sudo kill
  51. }
  52. # list contents of directories in a tree-like format
  53. if [ -z "\${which tree}" ]; then
  54. tree () {
  55. find $@ -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'
  56. }
  57. fi
  58. # Sort connection state
  59. sortcons() {
  60. netstat -nat |awk '{print $6}'|sort|uniq -c|sort -rn
  61. }
  62. # View all 80 Port Connections
  63. con80() {
  64. netstat -nat|grep -i ":80"|wc -l
  65. }
  66. # On the connected IP sorted by the number of connections
  67. sortconip() {
  68. netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n
  69. }
  70. # top20 of Find the number of requests on 80 port
  71. req20() {
  72. netstat -anlp|grep 80|grep tcp|awk '{print $5}'|awk -F: '{print $1}'|sort|uniq -c|sort -nr|head -n20
  73. }
  74. # top20 of Using tcpdump port 80 access to view
  75. http20() {
  76. sudo tcpdump -i eth0 -tnn dst port 80 -c 1000 | awk -F"." '{print $1"."$2"."$3"."$4}' | sort | uniq -c | sort -nr |head -20
  77. }
  78. # top20 of Find time_wait connection
  79. timewait20() {
  80. netstat -n|grep TIME_WAIT|awk '{print $5}'|sort|uniq -c|sort -rn|head -n20
  81. }
  82. # top20 of Find SYN connection
  83. syn20() {
  84. netstat -an | grep SYN | awk '{print $5}' | awk -F: '{print $1}' | sort | uniq -c | sort -nr|head -n20
  85. }
  86. # Printing process according to the port number
  87. port_pro() {
  88. netstat -ntlp | grep "$(retval $1)" | awk '{print $7}' | cut -d/ -f1
  89. }
  90. # top10 of gain access to the ip address
  91. accessip10() {
  92. awk '{counts[$(11)]+=1}; END {for(url in counts) print counts[url], url}' "$(retlog)"
  93. }
  94. # top20 of Most Visited file or page
  95. visitpage20() {
  96. awk '{print $11}' "$(retlog)"|sort|uniq -c|sort -nr|head -20
  97. }
  98. # top100 of Page lists the most time-consuming (more than 60 seconds) as well as the corresponding page number of occurrences
  99. consume100() {
  100. awk '($NF > 60 && $7~/\.php/){print $7}' "$(retlog)" |sort -n|uniq -c|sort -nr|head -100
  101. # if django website or other webiste make by no suffix language
  102. # awk '{print $7}' "$(retlog)" |sort -n|uniq -c|sort -nr|head -100
  103. }
  104. # Website traffic statistics (G)
  105. webtraffic() {
  106. awk "{sum+=$10} END {print sum/1024/1024/1024}" "$(retlog)"
  107. }
  108. # Statistical connections 404
  109. c404() {
  110. awk '($9 ~/404/)' "$(retlog)" | awk '{print $9,$7}' | sort
  111. }
  112. # Statistical http status.
  113. httpstatus() {
  114. awk '{counts[$(9)]+=1}; END {for(code in counts) print code, counts[code]}' "$(retlog)"
  115. }
  116. # Delete 0 byte file
  117. d0() {
  118. find "$(retval $1)" -type f -size 0 -exec rm -rf {} \;
  119. }
  120. # gather external ip address
  121. geteip() {
  122. curl http://ifconfig.me
  123. }
  124. # determine local IP address
  125. getip() {
  126. ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'
  127. }
  128. # Clear zombie processes
  129. clrz() {
  130. ps -eal | awk '{ if ($2 == "Z") {print $4}}' | kill -9
  131. }
  132. # Second concurrent
  133. conssec() {
  134. awk '{if($9~/200|30|404/)COUNT[$4]++}END{for( a in COUNT) print a,COUNT[a]}' "$(retlog)"|sort -k 2 -nr|head -n10
  135. }