53cb50acb5c2c70f6ba9d26d3d2ffb37c8a19e8f

Author
Marc Cornellà <marc@mcornella.com>
Committer
GitHub <noreply@github.com>
Date

Message

refactor(systemadmin): use `ss` instead of `netstat` when available (#11957)

Diff

This diff is truncated to protect this page.

  1diff --git a/plugins/systemadmin/systemadmin.plugin.zsh b/plugins/systemadmin/systemadmin.plugin.zsh
  2index 7ce62bac134fa845ee39b8ecbf25b9edfeecd1b5..03dd995b6e22a74f1fa6171f6b5491d6b09e0a30 100644
  3--- a/plugins/systemadmin/systemadmin.plugin.zsh
  4+++ b/plugins/systemadmin/systemadmin.plugin.zsh
  5@@ -13,11 +13,11 @@
  6 # ------------------------------------------------------------------------------
  7 
  8 function retlog() {
  9-    if [[ -z $1 ]];then
 10-        echo '/var/log/nginx/access.log'
 11-    else
 12-        echo $1
 13-    fi
 14+  if [[ -z $1 ]];then
 15+    echo '/var/log/nginx/access.log'
 16+  else
 17+    echo $1
 18+  fi
 19 }
 20 
 21 alias ping='ping -c 5'
 22@@ -28,142 +28,160 @@ alias mkdir='mkdir -pv'
 23 alias psmem='ps -e -orss=,args= | sort -b -k1 -nr'
 24 alias psmem10='ps -e -orss=,args= | sort -b -k1 -nr | head -n 10'
 25 # get top process eating cpu if not work try execute : export LC_ALL='C'
 26-alias pscpu='ps -e -o pcpu,cpu,nice,state,cputime,args|sort -k1,1n -nr'
 27-alias pscpu10='ps -e -o pcpu,cpu,nice,state,cputime,args|sort -k1,1n -nr | head -n 10'
 28+alias pscpu='ps -e -o pcpu,cpu,nice,state,cputime,args | sort -k1,1n -nr'
 29+alias pscpu10='ps -e -o pcpu,cpu,nice,state,cputime,args | sort -k1,1n -nr | head -n 10'
 30 # top10 of the history
 31 alias hist10='print -l ${(o)history%% *} | uniq -c | sort -nr | head -n 10'
 32 
 33 function ip() {
 34-    if [ -t 1 ]; then
 35-        command ip -color "$@"
 36-    else
 37-        command ip "$@"
 38-    fi
 39+  if [ -t 1 ]; then
 40+    command ip -color "$@"
 41+  else
 42+    command ip "$@"
 43+  fi
 44 }
 45 
 46 # directory LS
 47 function dls() {
 48-    print -l *(/)
 49+  print -l *(/)
 50 }
 51 function psgrep() {
 52-    ps aux | grep "${1:-.}" | grep -v grep
 53+  ps aux | grep "${1:-.}" | grep -v grep
 54 }
 55 # Kills any process that matches a regexp passed to it
 56 function killit() {
 57-    ps aux | grep -v "grep" | grep "$@" | awk '{print $2}' | xargs sudo kill
 58+  ps aux | grep -v "grep" | grep "$@" | awk '{print $2}' | xargs sudo kill
 59 }
 60 
 61 # list contents of directories in a tree-like format
 62 if ! (( $+commands[tree] )); then
 63-    function tree() {
 64-        find $@ -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'
 65-    }
 66+  function tree() {
 67+    find $@ -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'
 68+  }
 69 fi
 70 
 71 # Sort connection state
 72 function sortcons() {
 73-    netstat -nat |awk '{print $6}'|sort|uniq -c|sort -rn
 74+  {
 75+    LANG= ss -nat | awk 'NR > 1 {print $1}' \
 76+    || LANG= netstat -nat | awk 'NR > 2 {print $6}'
 77+  } | sort | uniq -c | sort -rn
 78 }
 79 
 80 # View all 80 Port Connections
 81 function con80() {
 82-    netstat -nat|grep -i ":80"|wc -l
 83+  {
 84+    LANG= ss -nat || LANG= netstat -nat
 85+  } | grep -E ":80[^0-9]" | wc -l
 86 }
 87 
 88 # On the connected IP sorted by the number of connections
 89 function sortconip() {
 90-    netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n
 91+  {
 92+    LANG= ss -ntu | awk 'NR > 1 {print $6}' \
 93+    || LANG= netstat -ntu | awk 'NR > 2 {print $5}'
 94+  } | cut -d: -f1 | sort | uniq -c | sort -n
 95 }
 96 
 97 # top20 of Find the number of requests on 80 port
 98 function req20() {
 99-    netstat -anlp|grep 80|grep tcp|awk '{print $5}'|awk -F: '{print $1}'|sort|uniq -c|sort -nr|head -n20
100+  {
101+    LANG= ss -tn | awk '$4 ~ /:80$/ {print $5}' \
102+    || LANG= netstat -tn | awk '$4 ~ /:80$/ {print $5}'
103+  } | awk -F: '{print $1}' | sort | uniq -c | sort -nr | head -n 20
104 }