对与服务端我们可以自己写些API供监控使用。
以下Code只是简单的给监控机吐出固定的mem值:80,不过,可以根据后期需要来开发相应的模块。此次我采用的是select来作为server端的socket监控,不过对与他的1024限制来说,我觉得仅作为监控服务端来说足够了,如果作为更大并发的server端话,可以使用poll,epoll,或者多线程并且结合队列来做到一定的限制,别让过多的连接把服务器干挂。
Server端
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
#!/usr/bin/env python
#Author:Badboy
#CreatBy:2014/5/1
import
socket,select
host
=
"192.168.1.61"
port
=
1001
sock
=
socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sock.setblocking(
0
)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,
1
)
sock.bind((host,port))
sock.listen(
1024
)
inputs
=
[sock]
outputs
=
[]
addrs
=
{}
debug
=
0
def
get_free_mem():
return
"80"
def
send_data(sock,data):
if
data.strip()
=
=
'mem'
:
sock.send(get_free_mem())
else
:
sock.send(
"Error key words!"
)
while
inputs:
rlist,wlist,elist
=
select.select(inputs,outputs,[],
0
)
for
s
in
rlist:
if
s
=
=
inputs[
0
]:
clientsock,clientaddr
=
s.accept()
inputs.append(clientsock)
addrs[clientsock]
=
clientaddr[
0
]
print
"ClientAddr:"
,clientaddr[
0
]
if
debug:
clientsock.send(
"Welcome here!\r\n"
)
else
:
words
=
s.recv(
1024
)
if
not
words:
print
"Byebye"
,addrs[s]
s.close()
inputs.remove(s)
del
addrs[s]
if
s
in
outputs:
outputs.remove(s)
continue
if
s
not
in
outputs:
outputs.append(s)
for
s
in
wlist:
send_data(s,words)
outputs.remove(s)
|
Client
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
#!/usr/bin/env python
#Author Badboy
#CreatBy:2014/5/1
import
socket,sys
mem_check
=
"mem"
host
=
"192.168.1.61"
port
=
1001
sock
=
socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sock.connect((host,port))
sock.send(mem_check)
mem_warning
=
40
mem_critical
=
20
mem_size
=
sock.recv(
1024
)
if
mem_size
=
=
"Error key words!"
:
print
"Please check client!"
sys.exit(
1
)
mem_size
=
int
(mem_size)
if
mem_size > mem_warning:
print
"Mem Size Ok!"
elif
mem_size < mem_critical:
print
"Mem Size Critical!"
else
:
print
"Mem Size Warning!"
|
以上仅是案例,大家可以在此基础上扩展修改的。
本文转自hahazhu0634 51CTO博客,原文链接:http://blog.51cto.com/5ydycm/1405310,如需转载请自行联系原作者