systemadmin.plugin.zsh 4.3 KB

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