0871594f58301df6dc08a365b2582c850638af2f

Author
DBX12 <dbx12@web.de>
Committer
Marc Cornellà <marc.cornella@live.com>
Date

Message

systemadmin: refactor plugin and fix README (#7295)

Diff

  1diff --git a/plugins/systemadmin/README.md b/plugins/systemadmin/README.md
  2index 5b32470a002cebc22063f3546a7ea5d2a9026842..edca4d87de515ab6f736f9c76d2573e910378be2 100644
  3--- a/plugins/systemadmin/README.md
  4+++ b/plugins/systemadmin/README.md
  5@@ -10,52 +10,42 @@ plugins=(... systemadmin)
  6 
  7 ## Aliases
  8 
  9-| Alias  | Command                                                                           | Description                                                        |
 10-|--------|-----------------------------------------------------------------------------------|--------------------------------------------------------------------|
 11-| ping   | `ping -c 5`                                                                       | Sends only 5 ICMP Messages                                         |
 12-| clr    | `clear;echo "Currently logged in on $(tty), as $USER in directory $PWD."`         | Clears the screen and prings the current user, TTY, and directory  |
 13-| path   | `echo -e ${PATH//:/\\n}`                                                          | Displays PATH with each entry on a separate line                   |
 14-| mkdir  | `mkdir -pv`                                                                       | Automatically create parent directories and display verbose output |
 15-| psmem  | <code>ps -e -orss=,args= \| sort -b -k1,1n</code>                                 | Display the processes using the most memory                        |
 16-| psmem10| <code>ps -e -orss=,args= \| sort -b -k1,1n\| head -10</code>                      | Display the top 10 processes using the most memory                 |
 17-| pscpu  | <code>ps -e -o pcpu,cpu,nice,state,cputime,args\|sort -k1 -nr</code>              | Display the top processes using the most CPU                       |
 18-| pscpu10| <code>ps -e -o pcpu,cpu,nice,state,cputime,args\|sort -k1 -nr \| head -10</code>  | Display the top 10 processes using the most CPU                    |
 19-| hist10 | <code>print -l ${(o)history%% *} \| uniq -c \| sort -nr \| head -n 10</code>      | Display the top 10 most used commands in the history               |
 20-
 21-
 22-## Named Functions
 23-These are used by some of the other functions to provide flexibility
 24-
 25-| Function    |  Description                                                                                                                          |
 26-|-------------|---------------------------------------------------------------------------------------------------------------------------------------|
 27-| retval      | Returns the first argument or a '.' if no arguments are specified                                                                     |
 28-| retlog      | Returns the first argument or /var/log/nginx/access.log if no arguments are specified                                                 |
 29-
 30-## Unamed Functions
 31-These functions are closer to aliases with complex arguments simplified (in most cases) into one line
 32-
 33-| Function    |  Description                                                                                                                          |
 34-|-------------|---------------------------------------------------------------------------------------------------------------------------------------|
 35-| dls         | List only directories in the current directory                                                                                        |
 36-| psgrep      | List all processes that match the pattern input after the command                                                                     |
 37-| killit      | xargs sudo kill | Kills any process that matches a regulr expression passed to it                                                     |
 38-| tree        | List contents of directories in a tree-like format (if tree is installed)                                                             |
 39-| sortcons    | Sort connections by state                                                                                                             |
 40-| con80       | View all 80 Port Connections                                                                                                          |
 41-| sortconip   | On the connected IP sorted by the number of connections                                                                               |
 42-| req20       | List the top 20 requests on port 80                                                                                                   |
 43-| http20      | List the top 20 connections to port 80 based on tcpdump data                                                                          |
 44-| timewait20  | List the top 20 time_wait connections                                                                                                 |
 45-| syn20       | List the top 20 SYN connections                                                                                                       |
 46-| port_pro    | Output all processes according to the port number                                                                                     |
 47-| accessip10  | List the top 10 accesses to the ip address in the nginx/access.log file or another log file if specified as an argument               |
 48-| visitpage20 | List the top 20 most visited files or pages in the nginx/access.log file or another log file if specified as an argument              |
 49-| consume100  | List the top 100 of Page lists the most time-consuming (more than 60 seconds) as well as the corresponding page number of occurrences |
 50-| webtraffic  | List website traffic statistics in GB from tne nginx/access.log file or another log file if specified as an argument                  |
 51-| c404        | List statistics on 404 connections in the nginx/access.log file or another log file if specified as an argument                       |
 52-| httpstatus  | List statistics based on http status in the nginx/access.log file or another log file if specified as an argument                     |
 53-| d0          | Delete 0 byte files recursively in the directory specified (or current directory if none is specificied)                              |
 54-| geteip      | Gather information regarding an external IP address                                                                                   |
 55-| getip       | Determine the local IP Address with `ip addr` or `ifconfig`                                                                           |
 56-| clrz        | Clear zombie processes                                                                                                                |
 57-| conssec     | Display the number of concurrent connections per second in the nginix/access.log file or another log file if specified as an argument |
 58+| Alias   | Command                                                                | Description                                                        |
 59+|---------|------------------------------------------------------------------------|--------------------------------------------------------------------|
 60+| ping    | `ping -c 5`                                                            | Sends only 5 ICMP Messages                                         |
 61+| clr     | `clear; echo Currently logged in on $TTY, as $USER in directory $PWD.` | Clears the screen and prints the current user, TTY, and directory  |
 62+| path    | `print -l $path`                                                       | Displays PATH with each entry on a separate line                   |
 63+| mkdir   | `mkdir -pv`                                                            | Automatically create parent directories and display verbose output |
 64+| psmem   | `ps -e -orss=,args= \| sort -b -k1,1n`                                 | Display the processes using the most memory                        |
 65+| psmem10 | `ps -e -orss=,args= \| sort -b -k1,1n \| head -10`                     | Display the top 10 processes using the most memory                 |
 66+| pscpu   | `ps -e -o pcpu,cpu,nice,state,cputime,args \|sort -k1 -nr`             | Display the top processes using the most CPU                       |
 67+| pscpu10 | `ps -e -o pcpu,cpu,nice,state,cputime,args \|sort -k1 -nr \| head -10` | Display the top 10 processes using the most CPU                    |
 68+| hist10  | `print -l ${(o)history%% *} \| uniq -c \| sort -nr \| head -n 10`      | Display the top 10 most used commands in the history               |
 69+
 70+## Functions
 71+
 72+| Function    |  Description                                                                                                          |
 73+|-------------|-----------------------------------------------------------------------------------------------------------------------|
 74+| dls         | List only directories in the current directory                                                                        |
 75+| psgrep      | List all processes that match the pattern input after the command                                                     |
 76+| killit      | Kills any process that matches a regular expression passed to it                                                      |
 77+| tree        | List contents of directories in a tree-like format (if tree isn't installed)                                          |
 78+| sortcons    | Sort connections by state                                                                                             |
 79+| con80       | View all 80 Port Connections                                                                                          |
 80+| sortconip   | On the connected IP sorted by the number of connections                                                               |
 81+| req20       | List the top 20 requests on port 80                                                                                   |
 82+| http20      | List the top 20 connections to port 80 based on tcpdump data                                                          |
 83+| timewait20  | List the top 20 time_wait connections                                                                                 |
 84+| syn20       | List the top 20 SYN connections                                                                                       |
 85+| port_pro    | Output all processes according to the port number                                                                     |
 86+| accessip10  | List the top 10 accesses to the ip address in the nginx/access.log file or another log file if specified              |
 87+| visitpage20 | List the top 20 most visited files or pages in the nginx/access.log file or another log file if specified             |
 88+| consume100  | List the 100 most time-consuming Page lists (more than 60 seconds) as well as the corresponding number of occurrences |
 89+| webtraffic  | List website traffic statistics in GB from tne nginx/access.log file or another log file if specified                 |
 90+| c404        | List statistics on 404 connections in the nginx/access.log file or another log file if specified                      |
 91+| httpstatus  | List statistics based on http status in the nginx/access.log file or another log file if specified                    |
 92+| d0          | Delete 0 byte files recursively in the current directory or another if specified                                      |
 93+| geteip      | Gather information regarding an external IP address using [icanhazip.com](https://icanhazip.com)                      |
 94+| getip       | Determine the local IP Address with `ip addr` or `ifconfig`                                                           |
 95+| clrz        | Clear zombie processes                                                                                                |
 96+| conssec     | Show number of concurrent connections per second based on ngnix/access.log file or another log file if specified      |
 97diff --git a/plugins/systemadmin/systemadmin.plugin.zsh b/plugins/systemadmin/systemadmin.plugin.zsh
 98index a74f818dd490d8f8330432586af5ef60ae02920a..5cc7b7397012ca20c12e425fbbd37463427671c8 100644
 99--- a/plugins/systemadmin/systemadmin.plugin.zsh
100+++ b/plugins/systemadmin/systemadmin.plugin.zsh
101@@ -12,14 +12,6 @@
102 #
103 # ------------------------------------------------------------------------------
104 
105-function retval() {
106-    if [[ -z $1 ]];then
107-        echo '.'
108-    else
109-        echo $1
110-    fi
111-}
112-
113 function retlog() {
114     if [[ -z $1 ]];then
115         echo '/var/log/nginx/access.log'
116@@ -29,8 +21,8 @@ function retlog() {
117 }
118 
119 alias ping='ping -c 5'
120-alias clr='clear;echo "Currently logged in on $(tty), as $USER in directory $PWD."'
121-alias path='echo -e ${PATH//:/\\n}'
122+alias clr='clear; echo Currently logged in on $TTY, as $USER in directory $PWD.'
123+alias path='print -l $path'
124 alias mkdir='mkdir -pv'
125 # get top process eating memory
126 alias psmem='ps -e -orss=,args= | sort -b -k1,1n'
127@@ -43,10 +35,10 @@ alias hist10='print -l ${(o)history%% *} | uniq -c | sort -nr | head -n 10'
128 
129 # directory LS
130 dls () {
131-    ls -l | grep "^d" | awk '{ print $9 }' | tr -d "/"
132+    print -l *(/)
133 }
134 psgrep() {
135-    ps aux | grep "$(retval $1)" | grep -v grep
136+    ps aux | grep "${1:-.}" | grep -v grep
137 }
138 # Kills any process that matches a regexp passed to it
139 killit() {
140@@ -54,10 +46,10 @@ killit() {
141 }
142 
143 # list contents of directories in a tree-like format
144-if [ -z "\${which tree}" ]; then
145-  tree () {
146-      find $@ -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'
147-  }
148+if ! (( $+commands[tree] )); then
149+    tree () {
150+        find $@ -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'
151+    }
152 fi
153 
154 # Sort connection state
155@@ -97,7 +89,7 @@ syn20() {
156 
157 # Printing process according to the port number
158 port_pro() {
159-    netstat -ntlp | grep "$(retval $1)" | awk '{print $7}' | cut -d/ -f1
160+    netstat -ntlp | grep "${1:-.}" | awk '{print $7}' | cut -d/ -f1
161 }
162 
163 # top10 of gain access to the ip address
164@@ -134,7 +126,7 @@ httpstatus() {
165 
166 # Delete 0 byte file
167 d0() {
168-    find "$(retval $1)" -type f -size 0 -exec rm -rf {} \;
169+    find "${1:-.}" -type f -size 0 -exec rm -rf {} \;
170 }
171 
172 # gather external ip address