#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import subprocess
import paramiko
import MySQLdb
from datetime import datetime
def cathost():
hostmapip = os.popen("cat /etc/hosts | grep -v 127.0.0.1 | grep -v ::1 | awk -F '\t' '{print$1}'").read().strip().split('\n')
return hostmapip
def sshoperation():
datas = {}
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
for hostip in cathost():
ssh.connect(hostip,'22','root')
stdin, stdout, stderr = ssh.exec_command('df -h |grep /dev/sda1')
data = stdout.read().rstrip('\n').split()[3] #.split('/')[1] + stdout.read().strip().split()[1]
datas[hostip] = data
ssh.close()
return datas
def Connectdb(TableName,dic):
try:
conn = MySQLdb.connect(host='localhost',user='root',passwd='',db='test',port=3306) #3306 no ''
cursor = conn.cursor()
ColumnStyle = ' VARCHAR(255)' # yaoyoukongge
value = ''
now_time = datetime.now().strftime('%Y%m%d%H%M%S')
for key in dic.keys(): #jieguowei [a,c]
value = ('"%s"' + ',' + '"%s"'+ ','+'"%s""')%(key,now_time,dic[key]) #use ""
# print ("INSERT INTO %s VALUES (%s)"%(TableName,value[:-1]))
try:
cursor.execute("SELECT * FROM %s"%(TableName))
cursor.execute("INSERT INTO %s VALUES (%s)"%(TableName,value[:-1]))
except MySQLdb.Error,e:
cursor.execute("CREATE TABLE %s (ip VARCHAR(255),time VARCHAR(255),status VARCHAR(255))"%(TableName))
cursor.execute("INSERT INTO %s VALUES (%s)"%(TableName,value[:-1]))
conn.commit()
cursor.close()
conn.close()
except MySQLdb.Error,e:
print "MySQL Error %d:%s" % (e.args[0],e.args[1])
if __name__ == "__main__":
dic = {}
dic = sshoperation()
Connectdb('testtable3',dic)
# -*- coding: utf-8 -*-
import os
import subprocess
import paramiko
import MySQLdb
from datetime import datetime
def cathost():
hostmapip = os.popen("cat /etc/hosts | grep -v 127.0.0.1 | grep -v ::1 | awk -F '\t' '{print$1}'").read().strip().split('\n')
return hostmapip
def sshoperation():
datas = {}
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
for hostip in cathost():
ssh.connect(hostip,'22','root')
stdin, stdout, stderr = ssh.exec_command('df -h |grep /dev/sda1')
data = stdout.read().rstrip('\n').split()[3] #.split('/')[1] + stdout.read().strip().split()[1]
datas[hostip] = data
ssh.close()
return datas
def Connectdb(TableName,dic):
try:
conn = MySQLdb.connect(host='localhost',user='root',passwd='',db='test',port=3306) #3306 no ''
cursor = conn.cursor()
ColumnStyle = ' VARCHAR(255)' # yaoyoukongge
value = ''
now_time = datetime.now().strftime('%Y%m%d%H%M%S')
for key in dic.keys(): #jieguowei [a,c]
value = ('"%s"' + ',' + '"%s"'+ ','+'"%s""')%(key,now_time,dic[key]) #use ""
# print ("INSERT INTO %s VALUES (%s)"%(TableName,value[:-1]))
try:
cursor.execute("SELECT * FROM %s"%(TableName))
cursor.execute("INSERT INTO %s VALUES (%s)"%(TableName,value[:-1]))
except MySQLdb.Error,e:
cursor.execute("CREATE TABLE %s (ip VARCHAR(255),time VARCHAR(255),status VARCHAR(255))"%(TableName))
cursor.execute("INSERT INTO %s VALUES (%s)"%(TableName,value[:-1]))
conn.commit()
cursor.close()
conn.close()
except MySQLdb.Error,e:
print "MySQL Error %d:%s" % (e.args[0],e.args[1])
if __name__ == "__main__":
dic = {}
dic = sshoperation()
Connectdb('testtable3',dic)
通过hosts文件获取需要获取数值的主机,
然后通过ssh进入对应主机来执行shell命令获取需要的数据,
把数据存放到mysql数据库中。
还差一些前端的展现。
做了一些思考:添加时间列,存储到环形数据库,updata数据。等等都是可是考虑做的。准备先把流程跑通,这几天去看django。
发现了一个很好的python入门博客:
http://www.cnblogs.com/vamei/archive/2012/09/13/2682778.html