systemadmin.plugin.zsh 4.4 KB

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