systemadmin.plugin.zsh 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. function ip() {
  34. if [ -t 1 ]; then
  35. command ip -color "$@"
  36. else
  37. command ip "$@"
  38. fi
  39. }
  40. # directory LS
  41. function dls() {
  42. print -l *(/)
  43. }
  44. function psgrep() {
  45. ps aux | grep "${1:-.}" | grep -v grep
  46. }
  47. # Kills any process that matches a regexp passed to it
  48. function 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 ! (( $+commands[tree] )); then
  53. function tree() {
  54. find $@ -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'
  55. }
  56. fi
  57. # Sort connection state
  58. function sortcons() {
  59. {
  60. LANG= ss -nat | awk 'NR > 1 {print $1}' \
  61. || LANG= netstat -nat | awk 'NR > 2 {print $6}'
  62. } | sort | uniq -c | sort -rn
  63. }
  64. # View all 80 Port Connections
  65. function con80() {
  66. {
  67. LANG= ss -nat || LANG= netstat -nat
  68. } | grep -E ":80[^0-9]" | wc -l
  69. }
  70. # On the connected IP sorted by the number of connections
  71. function sortconip() {
  72. {
  73. LANG= ss -ntu | awk 'NR > 1 {print $6}' \
  74. || LANG= netstat -ntu | awk 'NR > 2 {print $5}'
  75. } | cut -d: -f1 | sort | uniq -c | sort -n
  76. }
  77. # top20 of Find the number of requests on 80 port
  78. function req20() {
  79. {
  80. LANG= ss -tn | awk '$4 ~ /:80$/ {print $5}' \
  81. || LANG= netstat -tn | awk '$4 ~ /:80$/ {print $5}'
  82. } | awk -F: '{print $1}' | sort | uniq -c | sort -nr | head -n 20
  83. }
  84. # top20 of Using tcpdump port 80 access to view
  85. function http20() {
  86. 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
  87. }
  88. # top20 of Find time_wait connection
  89. function timewait20() {
  90. {
  91. LANG= ss -nat | awk 'NR > 1 && /TIME-WAIT/ {print $5}' \
  92. || LANG= netstat -nat | awk 'NR > 2 && /TIME_WAIT/ {print $5}'
  93. } | sort | uniq -c | sort -rn | head -n 20
  94. }
  95. # top20 of Find SYN connection
  96. function syn20() {
  97. {
  98. LANG= ss -an | awk '/SYN/ {print $5}' \
  99. || LANG= netstat -an | awk '/SYN/ {print $5}'
  100. } | awk -F: '{print $1}' | sort | uniq -c | sort -nr | head -n20
  101. }
  102. # Printing process according to the port number
  103. function port_pro() {
  104. LANG= ss -ntlp | awk "NR > 1 && /:${1:-}/ {print \$6}" | sed 's/.*pid=\([^,]*\).*/\1/' \
  105. || LANG= netstat -ntlp | awk "NR > 2 && /:${1:-}/ {print \$7}" | cut -d/ -f1
  106. }
  107. # top10 of gain access to the ip address
  108. function accessip10() {
  109. awk '{counts[$(11)]+=1}; END {for(url in counts) print counts[url], url}' "$(retlog)"
  110. }
  111. # top20 of Most Visited file or page
  112. function visitpage20() {
  113. awk '{print $11}' "$(retlog)" | sort | uniq -c | sort -nr | head -n 20
  114. }
  115. # top100 of Page lists the most time-consuming (more than 60 seconds) as well as the corresponding page number of occurrences
  116. function consume100() {
  117. awk '($NF > 60 && $7~/\.php/){print $7}' "$(retlog)" | sort -n | uniq -c | sort -nr | head -n 100
  118. # if django website or other website make by no suffix language
  119. # awk '{print $7}' "$(retlog)" | sort -n | uniq -c | sort -nr | head -n 100
  120. }
  121. # Website traffic statistics (G)
  122. function webtraffic() {
  123. awk "{sum+=$10} END {print sum/1024/1024/1024}" "$(retlog)"
  124. }
  125. # Statistical connections 404
  126. function c404() {
  127. awk '($9 ~ /404/)' "$(retlog)" | awk '{print $9,$7}' | sort
  128. }
  129. # Statistical http status.
  130. function httpstatus() {
  131. awk '{counts[$(9)]+=1}; END {for(code in counts) print code, counts[code]}' "$(retlog)"
  132. }
  133. # Delete 0 byte file
  134. function d0() {
  135. find "${1:-.}" -type f -size 0 -exec rm -rf {} \;
  136. }
  137. # gather external ip address
  138. function geteip() {
  139. curl -s -S -4 https://icanhazip.com
  140. # handle case when there is no IPv6 external IP, which shows error
  141. # curl: (7) Couldn't connect to server
  142. curl -s -S -6 https://icanhazip.com 2>/dev/null
  143. local ret=$?
  144. (( ret == 7 )) && print -P -u2 "%F{red}error: no IPv6 route to host%f"
  145. return $ret
  146. }
  147. # determine local IP address(es)
  148. function getip() {
  149. if (( ${+commands[ip]} )); then
  150. ip addr | awk '/inet /{print $2}' | command grep -v 127.0.0.1
  151. else
  152. ifconfig | awk '/inet /{print $2}' | command grep -v 127.0.0.1
  153. fi
  154. }
  155. # Clear zombie processes
  156. function clrz() {
  157. ps -eal | awk '{ if ($2 == "Z") {print $4}}' | kill -9
  158. }
  159. # Second concurrent
  160. function conssec() {
  161. awk '{if($9~/200|30|404/)COUNT[$4]++}END{for( a in COUNT) print a,COUNT[a]}' "$(retlog)" | sort -k 2 -nr | head -n10
  162. }