systemadmin.plugin.zsh 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. netstat -nat |awk '{print $6}'|sort|uniq -c|sort -rn
  60. }
  61. # View all 80 Port Connections
  62. function con80() {
  63. netstat -nat|grep -i ":80"|wc -l
  64. }
  65. # On the connected IP sorted by the number of connections
  66. function 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. function 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. function 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 -n 20
  76. }
  77. # top20 of Find time_wait connection
  78. function 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. function 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. function port_pro() {
  87. netstat -ntlp | grep "${1:-.}" | awk '{print $7}' | cut -d/ -f1
  88. }
  89. # top10 of gain access to the ip address
  90. function 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. function visitpage20() {
  95. awk '{print $11}' "$(retlog)"|sort|uniq -c|sort -nr|head -n 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. function consume100() {
  99. awk '($NF > 60 && $7~/\.php/){print $7}' "$(retlog)" |sort -n|uniq -c|sort -nr|head -n 100
  100. # if django website or other website make by no suffix language
  101. # awk '{print $7}' "$(retlog)" |sort -n|uniq -c|sort -nr|head -n 100
  102. }
  103. # Website traffic statistics (G)
  104. function webtraffic() {
  105. awk "{sum+=$10} END {print sum/1024/1024/1024}" "$(retlog)"
  106. }
  107. # Statistical connections 404
  108. function c404() {
  109. awk '($9 ~/404/)' "$(retlog)" | awk '{print $9,$7}' | sort
  110. }
  111. # Statistical http status.
  112. function httpstatus() {
  113. awk '{counts[$(9)]+=1}; END {for(code in counts) print code, counts[code]}' "$(retlog)"
  114. }
  115. # Delete 0 byte file
  116. function d0() {
  117. find "${1:-.}" -type f -size 0 -exec rm -rf {} \;
  118. }
  119. # gather external ip address
  120. function geteip() {
  121. curl -s -S -4 https://icanhazip.com
  122. curl -s -S -6 https://icanhazip.com
  123. }
  124. # determine local IP address(es)
  125. function getip() {
  126. if (( ${+commands[ip]} )); then
  127. ip addr | awk '/inet /{print $2}' | command grep -v 127.0.0.1
  128. else
  129. ifconfig | awk '/inet /{print $2}' | command grep -v 127.0.0.1
  130. fi
  131. }
  132. # Clear zombie processes
  133. function clrz() {
  134. ps -eal | awk '{ if ($2 == "Z") {print $4}}' | kill -9
  135. }
  136. # Second concurrent
  137. function conssec() {
  138. awk '{if($9~/200|30|404/)COUNT[$4]++}END{for( a in COUNT) print a,COUNT[a]}' "$(retlog)"|sort -k 2 -nr|head -n10
  139. }