java平台端口监控 每天凌晨从数据库中得到一份java平台所有用户端口 ip地址列表,把这个列表rsync传输到rsync服务端,java平台服务器从rsync服务端下载这个文件
#!/usr/bin/python
import MySQLdb
import os
def Mysql_work(ip,port,javaip):
print javaip
conn=MySQLdb.connect(host=str(ip),user=‘***‘,passwd=‘***‘,port=int(port),db=‘***‘)
cur=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor)
cur.execute("select appname,ip,port from App2WS where status=‘up‘;")
result=cur.fetchall()
return result
def rsync_work(ip,pwd):
os.popen(‘rsync -avzp /etc/sae/jvm_port.txt %s::%s‘%(ip,pwd))
if __name__ == ‘__main__‘:
ip=‘****‘
port=****
b=os.popen(‘ifconfig eth1‘).read().split(‘\n‘)[1].split(‘ ‘)[11].split(‘:‘)[1]
a=Mysql_work(ip,port,b)
c=open(‘/etc/sae/jvm_port.txt‘,‘w‘)
c.truncate()
for i in a:
d=‘‘.join([‘%s:%s,‘%(k,i[k]) for k in [‘ip‘, ‘port‘, ‘appname‘]])
字典转成字符串
c.writelines(d+‘\n‘)
c.close()
rsync_ip=‘10.75.7.239‘
pwd=‘sae‘
rsync_work(rsync_ip,pwd)
java平台服务器脚本
#!/usr/bin/python
import os
import re
import pycurl
import cStringIO
import urllib
def curl_tyt(ip,port):
buf=cStringIO.StringIO()
cc=pycurl.Curl()
cc.setopt(cc.URL,‘http://%s‘%ip)
cc.setopt(cc.WRITEFUNCTION,buf.write)
cc.setopt(cc.CONNECTTIMEOUT,5)
cc.setopt(cc.TIMEOUT,8)
cc.setopt(cc.PROXY,‘http://%s:%s‘%(ip,port))
cc.perform()
def monitor_work_1(content):
dict_1={‘service‘:‘javaruntime_monitor‘,‘checkpoint‘:‘javaruntime_monitor_log‘,‘title‘:content,‘content‘:content,‘cluster‘:‘public‘,‘grade‘:‘2‘}
params = urllib.urlencode(dict_1)
urllib.urlopen(‘http://**********‘,params)
def rsync_work(ip,pwd):
os.popen(‘rsync -avzp %s::%s/jvm_port.txt /etc/sae/‘%(ip,pwd))
def buffer_line():
buf = open("/etc/sae/buffer.txt").read()
bytes=os.popen(‘wc -c /usr/local/sae/jkdaemon/log/jkdaemon.log‘).read().split(" ")[0]
if int(bytes) < int(buf):
return 0
else:
return int(re.findall("^\d*", buf)[0])
def set_last_pos(pos):
open("/etc/sae/buffer.txt", "w").write(str(pos))
def monitor_work():
fh=open(‘/usr/local/sae/jkdaemon/log/jkdaemon.log‘,‘r‘)
fh.seek(buffer_line())
content=fh.read()
new_total_lines=len(content)+buffer_line()
set_last_pos(new_total_lines)
new_lines=content.split("\n")
return new_lines
if __name__ == ‘__main__‘:
b=os.popen(‘ifconfig eth1‘).read().split(‘\n‘)[1].split(‘ ‘)[11].split(‘:‘)[1]
ip=‘****‘
pwd=‘****‘
rsync_work(ip,pwd)
a=open(‘/etc/sae/jvm_port.txt‘,‘r‘).readlines()
add_jkdaemon_log=monitor_work()
for i in a:
ii=i.split(‘,‘)
if b == ii[0].split(‘:‘)[1]:
port=ii[1].split(‘:‘)[1]
try:
print ‘+++++++++++++++‘
curl_tyt(b,port)
except pycurl.error, e:
print ‘error‘,port
appname=ii[2].split(‘:‘)[1]
for kk in add_jkdaemon_log:
if kk.find(port) >= 0:
if kk.find(‘start‘) >= 0:
if kk:
monitor_work_1(‘javaruntime_appname_‘+appname+‘_ip_‘+b+‘_port_‘+port+‘down‘)