systemadmin.plugin.zsh
5434 bytes
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
15function retlog() {
16 if [[ -z $1 ]];then
17 echo '/var/log/nginx/access.log'
18 else
19 echo $1
20 fi
21}
22
23alias ping='ping -c 5'
24alias ping6='ping6 -c 5'
25alias clr='clear; echo Currently logged in on $TTY, as $USERNAME in directory $PWD.'
26alias path='print -l $path'
27alias mkdir='mkdir -pv'
28# get top process eating memory
29alias psmem='ps -e -orss=,args= | sort -b -k1 -nr'
30alias psmem10='ps -e -orss=,args= | sort -b -k1 -nr | head -n 10'
31# get top process eating cpu if not work try execute : export LC_ALL='C'
32alias pscpu='ps -e -o pcpu,cpu,nice,state,cputime,args | sort -k1,1n -nr'
33alias pscpu10='ps -e -o pcpu,cpu,nice,state,cputime,args | sort -k1,1n -nr | head -n 10'
34# top10 of the history
35alias hist10='print -l ${(o)history%% *} | uniq -c | sort -nr | head -n 10'
36
37function ip() {
38 if [ -t 1 ]; then
39 command ip -color "$@"
40 else
41 command ip "$@"
42 fi
43}
44
45# directory LS
46function dls() {
47 print -l *(/)
48}
49function psgrep() {
50 ps aux | grep "${1:-.}" | grep -v grep
51}
52# Kills any process that matches a regexp passed to it
53function killit() {
54 ps aux | grep -v "grep" | grep "$@" | awk '{print $2}' | xargs sudo kill
55}
56
57# list contents of directories in a tree-like format
58if ! (( $+commands[tree] )); then
59 function tree() {
60 find $@ -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'
61 }
62fi
63
64# Sort connection state
65function sortcons() {
66 {
67 LANG= ss -nat | awk 'NR > 1 {print $1}' \
68 || LANG= netstat -nat | awk 'NR > 2 {print $6}'
69 } | sort | uniq -c | sort -rn
70}
71
72# View all 80 Port Connections
73function con80() {
74 {
75 LANG= ss -nat || LANG= netstat -nat
76 } | grep -E ":80[^0-9]" | wc -l
77}
78
79# On the connected IP sorted by the number of connections
80function sortconip() {
81 {
82 LANG= ss -ntu | awk 'NR > 1 {print $6}' \
83 || LANG= netstat -ntu | awk 'NR > 2 {print $5}'
84 } | cut -d: -f1 | sort | uniq -c | sort -n
85}
86
87# top20 of Find the number of requests on 80 port
88function req20() {
89 {
90 LANG= ss -tn | awk '$4 ~ /:80$/ {print $5}' \
91 || LANG= netstat -tn | awk '$4 ~ /:80$/ {print $5}'
92 } | awk -F: '{print $1}' | sort | uniq -c | sort -nr | head -n 20
93}
94
95# top20 of Using tcpdump port 80 access to view
96function http20() {
97 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
98}
99
100# top20 of Find time_wait connection
101function timewait20() {
102 {
103 LANG= ss -nat | awk 'NR > 1 && /TIME-WAIT/ {print $5}' \
104 || LANG= netstat -nat | awk 'NR > 2 && /TIME_WAIT/ {print $5}'
105 } | sort | uniq -c | sort -rn | head -n 20
106}
107
108# top20 of Find SYN connection
109function syn20() {
110 {
111 LANG= ss -an | awk '/SYN/ {print $5}' \
112 || LANG= netstat -an | awk '/SYN/ {print $5}'
113 } | awk -F: '{print $1}' | sort | uniq -c | sort -nr | head -n20
114}
115
116# Printing process according to the port number
117function port_pro() {
118 LANG= ss -ntlp | awk "NR > 1 && /:${1:-}/ {print \$6}" | sed 's/.*pid=\([^,]*\).*/\1/' \
119 || LANG= netstat -ntlp | awk "NR > 2 && /:${1:-}/ {print \$7}" | cut -d/ -f1
120}
121
122# top10 of gain access to the ip address
123function accessip10() {
124 awk '{counts[$(11)]+=1}; END {for(url in counts) print counts[url], url}' "$(retlog)"
125}
126
127# top20 of Most Visited file or page
128function visitpage20() {
129 awk '{print $11}' "$(retlog)" | sort | uniq -c | sort -nr | head -n 20
130}
131
132# top100 of Page lists the most time-consuming (more than 60 seconds) as well as the corresponding page number of occurrences
133function consume100() {
134 awk '($NF > 60 && $7~/\.php/){print $7}' "$(retlog)" | sort -n | uniq -c | sort -nr | head -n 100
135 # if django website or other website make by no suffix language
136 # awk '{print $7}' "$(retlog)" | sort -n | uniq -c | sort -nr | head -n 100
137}
138
139# Website traffic statistics (G)
140function webtraffic() {
141 awk "{sum+=$10} END {print sum/1024/1024/1024}" "$(retlog)"
142}
143
144# Statistical connections 404
145function c404() {
146 awk '($9 ~ /404/)' "$(retlog)" | awk '{print $9,$7}' | sort
147}
148
149# Statistical http status.
150function httpstatus() {
151 awk '{counts[$(9)]+=1}; END {for(code in counts) print code, counts[code]}' "$(retlog)"
152}
153
154# Delete 0 byte file
155function d0() {
156 find "${1:-.}" -type f -size 0 -exec rm -rf {} \;
157}
158
159# gather external ip address
160function geteip() {
161 curl -s -S -4 https://icanhazip.com
162
163 # handle case when there is no IPv6 external IP, which shows error
164 # curl: (7) Couldn't connect to server
165 curl -s -S -6 https://icanhazip.com 2>/dev/null
166 local ret=$?
167 (( ret == 7 )) && print -P -u2 "%F{red}error: no IPv6 route to host%f"
168 return $ret
169}
170
171# determine local IP address(es)
172function getip() {
173 if (( ${+commands[ip]} )); then
174 ip addr | awk '/inet /{print $2}' | command grep -v 127.0.0.1
175 else
176 ifconfig | awk '/inet /{print $2}' | command grep -v 127.0.0.1
177 fi
178}
179
180# Clear zombie processes
181function clrz() {
182 ps -eal | awk '{ if ($2 == "Z") {print $4}}' | kill -9
183}
184
185# Second concurrent
186function conssec() {
187 awk '{if($9~/200|30|404/)COUNT[$4]++}END{for( a in COUNT) print a,COUNT[a]}' "$(retlog)" | sort -k 2 -nr | head -n10
188}