Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
Toggle navigation
This project
Loading...
Sign in
刘航
/
watchmen
Go to a project
Project
Repository
Issues
2
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 7e816d7c
authored
Jan 31, 2019
by
刘航
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
【ADD】增加了connection_vproc.py脚本,用来监控tcp长连接状态,并写入到vproc系统,衔接zabbix
1 parent
aa9d5016
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
173 additions
and
0 deletions
awesome_script/connection_vproc/README.md
awesome_script/connection_vproc/connection_vproc.py
awesome_script/connection_vproc/README.md
0 → 100644
View file @
7e816d7
# connection_vproc
用lsof命令收集指定端口的tcp长连接,将连接状态写入vproc系统。放在/usr/local/bin下。改脚本阻塞持续运行,利用supervisor守护运行。
使用说明:
```
useage: connection_vproc.py [options]
options:
[+]-s any, --serverports=any | 作为服务端,连接到该端口的ip
[+]-c any, --clientports=any | 作为客户端,连接带端口的目标ip
example:
./connection_vproc.py --serverports=8851 --serverports=9999 --clientports=20080
```
举例:
想监控连接到主节点的运维(listen 8851端口)、与上级运行连接情况(connect to 8851端口)DataExportService(listen 9999端口)的ip地址,300秒更新一次状态。运行
```
connection_vproc.py -s 9999 -s 8851 -c 8851 -i 300
```
awesome_script/connection_vproc/connection_vproc.py
0 → 100644
View file @
7e816d7
#!/usr/bin/python
# encoding:utf-8
#######################################################
# author: liuh 2018-12-10
# version: v1.0
# python version: 2
#######################################################
import
sys
import
getopt
import
os
import
shutil
import
time
import
json
import
subprocess
import
threading
import
time
import
pdb
class
conn
(
object
):
"""docstring for conn"""
def
__init__
(
self
,
port
,
cors
,
interval
):
super
(
conn
,
self
)
.
__init__
()
self
.
port
=
port
self
.
cors
=
cors
self
.
interval
=
interval
def
display
(
self
):
if
(
self
.
cors
==
0
):
print
(
"conn info: server port {}, interval {}"
.
format
(
self
.
port
,
self
.
interval
))
else
:
print
(
"conn info: client port {}, interval {}"
.
format
(
self
.
port
,
self
.
interval
))
def
usage
():
print
(
"""
useage: connection_vproc.py [options]
options:
[+]-s any, --serverports=any | the connection that host as server listen port
[+]-c any, --clientports=any | the connection that host as client connect port
example:
./connection_vproc.py --serverports=8851 --serverports=9999 --clientports=20080
"""
)
global_working
=
1
def
loop
(
connlist_
,
interval_
):
print
(
"enter loop, conn size:{}, interval:{}"
.
format
(
len
(
connlist_
),
interval_
))
last
=
0
while
global_working
:
now
=
time
.
time
()
interval
=
now
-
last
if
(
interval
>
interval_
):
for
conn_
in
connlist_
:
port_status
(
conn_
.
port
,
conn_
.
cors
)
last
=
now
time
.
sleep
(
1
)
def
main
():
argc
=
len
(
sys
.
argv
)
try
:
options
,
args
=
getopt
.
getopt
(
sys
.
argv
[
1
:],
"-h-s:-c:-i:"
,[
"help"
,
"serverports="
,
"clientports="
,
"invertal="
])
except
getopt
.
GetoptError
:
print
(
"error: invalid args"
)
usage
()
sys
.
exit
(
-
1
)
serverports
=
[]
clientports
=
[]
interval
=
600
for
name
,
value
in
options
:
if
name
in
(
"--help"
):
usage
()
if
name
in
(
"--serverports"
):
serverports
.
append
(
value
)
if
name
in
(
"--clientports"
):
clientports
.
append
(
value
)
if
name
in
(
"--invertal"
):
interval
=
int
(
value
)
connlist
=
[]
for
sport
in
serverports
:
connlist
.
append
(
conn
(
sport
,
0
,
interval
))
for
cport
in
clientports
:
connlist
.
append
(
conn
(
sport
,
1
,
interval
))
for
_conn
in
connlist
:
_conn
.
display
()
global_working
=
1
t
=
threading
.
Thread
(
target
=
loop
,
args
=
(
connlist
,
interval
))
t
.
setDaemon
(
True
)
t
.
start
()
# print("before thread join")
# t.join()
try
:
while
1
:
time
.
sleep
(
1
)
except
KeyboardInterrupt
as
e
:
print
(
'recv exit signal'
)
global_working
=
0
t
.
join
(
5
)
print
(
'exit'
)
sys
.
exit
(
0
)
for
_conn
in
connlist
:
port_status
(
_conn
.
port
,
_conn
.
cors
)
sys
.
exit
(
0
)
# cors: int 0-server 1-client
def
port_status
(
port
,
cors
):
ipss
=
""
ipcs
=
""
p
=
subprocess
.
Popen
([
'lsof -i:{}'
.
format
(
port
)],
shell
=
True
,
stdout
=
subprocess
.
PIPE
)
while
1
:
out
=
p
.
stdout
.
readline
()
.
decode
()
if
len
(
out
)
==
0
:
break
out
=
out
.
split
(
" "
)[
-
2
]
if
(
out
.
find
(
"->"
)
==
-
1
):
continue
ipport
=
out
.
split
(
"->"
)[
1
]
.
split
(
":"
)
ip
=
ipport
[
0
]
# print("cors:{} ipport[1]:{} port:{}".format(cors,ipport[1],port))
if
(
cors
==
0
):
if
ipport
[
1
]
!=
port
:
ipss
=
ipss
+
ip
+
"
\
|"
else
:
if
ipport
[
1
]
==
port
:
ipcs
=
ipcs
+
ip
+
"
\
|"
if
cors
==
0
:
command
=
"vproc write --key=connection.server_{} --value={}"
.
format
(
port
,
ipss
)
print
(
command
)
subprocess
.
Popen
([
command
],
shell
=
True
)
else
:
command
=
"vproc write --key=connection.client_{} --value={}"
.
format
(
port
,
ipcs
)
print
(
command
)
subprocess
.
Popen
([
command
],
shell
=
True
)
if
__name__
==
'__main__'
:
main
()
\ No newline at end of file
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment