以下数据取自redis数据库中client list的结果,
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
id=565554692 addr=192.168.100.157:29328 fd=30 name= age=62 idle=62 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=del id=565535836 addr=192.168.100.136:44510 fd=8 name= age=226 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=setex id=565520657 addr=192.168.100.139:40102 fd=10 name= age=359 idle=359 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=get id=565535200 addr=192.168.100.157:8581 fd=85 name= age=232 idle=232 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=del id=565556494 addr=192.168.100.159:53633 fd=76 name= age=48 idle=2 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=setex id=565553831 addr=192.168.100.142:63331 fd=37 name= age=69 idle=68 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=del id=565520648 addr=192.168.100.140:38530 fd=12 name= age=359 idle=359 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=get id=565520650 addr=192.168.100.139:47644 fd=14 name= age=359 idle=359 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=get id=565562312 addr=192.168.100.145:53307 fd=41 name= age=0 idle=0 flags=N db=5 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=get id=565555904 addr=192.168.100.159:53067 fd=63 name= age=52 idle=2 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=setex id=559066239 addr=192.168.100.159:36982 fd=50 name= age=87373 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=get id=564734722 addr=192.168.100.137:26982 fd=74 name= age=7498 idle=2 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=setex id=565547213 addr=192.168.100.157:21357 fd=39 name= age=129 idle=129 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=get id=565562301 addr=192.168.100.156:16376 fd=71 name= age=0 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=get id=565562306 addr=192.168.100.145:53285 fd=68 name= age=0 idle=0 flags=N db=5 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=get id=565557358 addr=192.168.100.157:32287 fd=86 name= age=41 idle=41 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=del id=565562285 addr=192.168.100.146:45069 fd=16 name= age=0 idle=0 flags=N db=5 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=ttl id=565562287 addr=192.168.100.142:9909 fd=56 name= age=0 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=get id=565562288 addr=192.168.100.156:16372 fd=60 name= age=0 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=get id=565562293 addr=192.168.100.146:45081 fd=69 name= age=0 idle=0 flags=N db=5 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=get id=565549233 addr=192.168.100.156:64058 fd=78 name= age=110 idle=110 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=del id=559066241 addr=192.168.100.159:36983 fd=51 name= age=87373 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=get id=565280380 addr=192.168.100.136:17898 fd=23 name= age=2442 idle=1 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=get id=559066219 addr=192.168.100.159:36977 fd=29 name= age=87373 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=get id=565529381 addr=127.0.0.1:55764 fd=62 name= age=284 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=client id=565562281 addr=192.168.100.141:9437 fd=34 name= age=1 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=get id=565559188 addr=192.168.100.156:13039 fd=57 name= age=27 idle=27 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=del id=565561993 addr=192.168.100.159:60763 fd=11 name= age=3 idle=3 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=setex id=559066227 addr=192.168.100.159:36978 fd=26 name= age=87373 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=get id=565560022 addr=192.168.100.142:7889 fd=20 name= age=19 idle=19 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=del id=565559176 addr=192.168.100.156:13009 fd=21 name= age=27 idle=27 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=del id=565562245 addr=192.168.100.145:53112 fd=65 name= age=1 idle=1 flags=N db=5 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=get id=559066208 addr=192.168.100.159:36976 fd=27 name= age=87373 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=get id=565562231 addr=192.168.100.146:45011 fd=55 name= age=1 idle=1 flags=N db=5 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=get id=565544691 addr=192.168.100.137:18036 fd=7 name= age=151 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=setex id=565562226 addr=192.168.100.146:45001 fd=13 name= age=1 idle=1 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=get id=565562227 addr=192.168.100.146:45003 fd=54 name= age=1 idle=1 flags=N db=5 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=get id=565550398 addr=192.168.100.142:59478 fd=43 name= age=100 idle=98 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=del id=565559164 addr=192.168.100.142:7181 fd=31 name= age=27 idle=27 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=del id=565550525 addr=192.168.100.142:59559 fd=46 name= age=98 idle=96 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=del id=565562148 addr=192.168.100.142:9745 fd=32 name= age=2 idle=1 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=del id=565562155 addr=192.168.100.146:44873 fd=59 name= age=2 idle=2 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=get id=565562160 addr=192.168.100.146:44877 fd=66 name= age=2 idle=1 flags=N db=5 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=get id=565490880 addr=192.168.100.157:24199 fd=64 name= age=614 idle=614 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=del id=559066202 addr=192.168.100.159:36972 fd=19 name= age=87374 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=get id=565260322 addr=192.168.100.40:64656 fd=38 name= age=2616 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=get id=565561749 addr=192.168.100.157:36894 fd=25 name= age=5 idle=5 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=del id=565562139 addr=192.168.100.156:16249 fd=35 name= age=2 idle=2 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=del id=559066205 addr=192.168.100.159:36973 fd=9 name= age=87374 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=get id=565562129 addr=192.168.100.157:37272 fd=53 name= age=2 idle=2 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=del id=565504860 addr=192.168.100.156:18560 fd=15 name= age=494 idle=494 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=del id=565466895 addr=192.168.100.156:37048 fd=28 name= age=833 idle=833 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=del id=565557951 addr=192.168.100.141:5242 fd=18 name= age=36 idle=36 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=del id=565562120 addr=192.168.100.142:9641 fd=33 name= age=2 idle=2 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=del id=559066206 addr=192.168.100.159:36974 fd=22 name= age=87374 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=get id=565549931 addr=192.168.100.157:24329 fd=83 name= age=104 idle=104 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=expireat id=559066207 addr=192.168.100.159:36975 fd=24 name= age=87374 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=get id=559066234 addr=192.168.100.159:36979 fd=47 name= age=87373 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=get id=559066235 addr=192.168.100.159:36980 fd=48 name= age=87373 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=get id=565520665 addr=192.168.100.140:49030 fd=17 name= age=359 idle=359 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=get id=565551768 addr=192.168.100.142:61158 fd=61 name= age=88 idle=88 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=del id=565560878 addr=192.168.100.141:8034 fd=36 name= age=13 idle=13 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=expireat id=565560879 addr=192.168.100.142:8543 fd=58 name= age=13 idle=13 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=del id=565560840 addr=192.168.100.141:8020 fd=52 name= age=13 idle=13 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=del id=559066236 addr=192.168.100.159:36981 fd=49 name= age=87373 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=get id=565562326 addr=192.168.100.145:53361 fd=40 name= age=0 idle=0 flags=N db=5 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=get
获取按照ip进行统计,每个客户端ip的连接数。
[redis@lxd-vm1 ~]$ cat a | awk -F[\ \=\:] '{print $4}' | awk '{array[$0]++}END{for(i in array){print i,array[i]}}' | sort -nrk2 |column -t 192.168.100.159 15 192.168.100.142 10 192.168.100.157 8 192.168.100.156 8 192.168.100.146 7 192.168.100.145 4 192.168.100.141 4 192.168.100.140 2 192.168.100.139 2 192.168.100.137 2 192.168.100.136 2 192.168.100.40 1 127.0.0.1 1
(1)根据空格,等于号,分号,3个分隔符的第4列就是客户端ip,当然也可以多个awk分别获取。
(2)将获取到的每一行存在一个数组array中,下标是获取第一步获取的客户端ip,
(3)循环最后输出数组的元素和出现的次数,排序,格式化输出。
当然也可以使用sort和uniq的方式获取,这里主要是学习awk数组的使用。
[redis@lxd-vm1 ~]$ cat a | awk -F[\ \=\:] '{print $4}'|sort | uniq -c | sort -nrk1 |column -t 15 192.168.100.159 10 192.168.100.142 8 192.168.100.157 8 192.168.100.156 7 192.168.100.146 4 192.168.100.145 4 192.168.100.141 2 192.168.100.140 2 192.168.100.139 2 192.168.100.137 2 192.168.100.136 1 192.168.100.40 1 127.0.0.1