# -*- coding: utf-8 -*-
#!/bin/env python
#AUTHOR:karl
#DATE:2018-1-19
#VERSION:V1.0
######################
import time
import os
import paramiko
import datetime
import sys
import MySQLdb
import threading
#######################################
#首次插入数据是REG=0,更新数据REG为其他
#######################################
REG=1
private_key = paramiko.RSAKey.from_private_key_file('/home/appdeploy/.ssh/id_rsa')
def Data_mysql(info):
try:
string=info
Pip=string.split(" ")[0]
Sip=string.split(" ")[1]
Dip=string.split(" ")[2]
Port=string.split(" ")[3]
Result=string.split(" ")[4].replace('\n','')
con = MySQLdb.connect(host='localhost',user="root",passwd="******",db="zabbix",port=3306,charset="utf8")
dt = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
cursor=con.cursor()
valuse=(dt,Result,Pip,Sip,Dip,Port)
if REG == 0:
sql="""INSERT INTO Balant_telnet (create_time,telnet_result,telnet_physicalip,telnet_sourceip,telnet_desip,telnet_port) VALUES (%s,%s,%s,%s,%s,%s) """
result=cursor.execute(sql,valuse)
else:
sql="""UPDATE Balant_telnet set create_time='%s',telnet_result=%s where telnet_physicalip='%s' and telnet_sourceip='%s' and telnet_desip='%s' and telnet_port='%s';""" %(valuse)
result=cursor.execute(sql)
cursor.close()
con.commit()
con.close()
except MySQLdb.Error, e:
print "Error %d: %s" % (e.args[0], e.args[1])
sys.exit(1)
def check_port(value,content):
ager={}
ager[value]=content
ssh=paramiko.SSHClient()
try:
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(value,port=22,username='appdeploy',pkey=private_key,timeout=5)
cmd="sh /home/appdeploy/monitor.sh {value}".format(value=ager[ager.keys()[index]])
stdin,stdout,stderr = ssh.exec_command(cmd)
for file_d in stdout.readlines():
Data_mysql(file_d)
except:
result=cmd+','+'failed'+'\n'
ssh.close()
###################################
# 对配置文件进行参数解析
###################################
def get_parameter():
ager={}
value=[]
with open("monitor_config") as context:
for line in context:
while not "," in list(line):
host=line.strip()[1:-1]
value=[]
break
line=line.strip().split(",")
value=value+line
ager[str(host)]=value
return ager
if __name__ == '__main__':
threads=[]
parm=get_parameter()
files=range(len(parm.keys()))
for index,content in parm.items():
t = threading.Thread(target=check_port,args=(index,content))
threads.append(t)
for index in files:
threads[index].start()
for index in files:
threads[index].join()
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com