Python脚本之django---mysql-记录主机性能数据到数据库
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python脚本之django---mysql-记录主机性能数据到数据库相关的知识,希望对你有一定的参考价值。
[[email protected] ~]# tail -20000 /tmp/python/alldjango-mysql.py
#!/bin/usr/bin python
import os,datetime,paramiko
import tab,sys,multiprocessing,time
sys.path.append(‘/tmp/python/Django-1.5.1/django/bin/myweb‘)
os.environ[‘DJANGO_SETTINGS_MODULE‘] = ‘myweb.settings‘
from pyweb.models import Filesystem,Men_Cpu,Tablespace
#hosts=[‘192.168.1.10‘,‘192.168.1.11‘,‘192.168.1.13‘,‘192.168.1.200‘,‘192.168.1.11‘]
hosts=[‘192.168.1.10‘,‘192.168.1.11‘,‘192.168.1.13‘,‘192.168.1.10‘,‘192.168.1.200‘,‘192.168.1.10‘,‘192.168.1.11‘,‘192.168.1.13‘,‘192.168.1.200‘,‘192.168.1.11‘,‘192.168.1.10‘,‘192.168.1.11‘,‘192.168.1.13‘,‘192.168.1.200‘,‘192.168.1.11‘,‘192.168.1.10‘,‘192.168.1.11‘,‘192.168.1.13‘,‘192.168.1.10‘,‘192.168.1.11‘,‘192.168.1.10‘,‘192.168.1.200‘,‘192.168.1.13‘,‘192.168.1.10‘,‘192.168.1.11‘,‘192.168.1.10‘,‘192.168.1.11‘,‘192.168.1.13‘,‘192.168.1.200‘,‘192.168.1.11‘,‘192.168.1.10‘,‘192.168.1.11‘,‘192.168.1.13‘,‘192.168.1.10‘,‘192.168.1.11‘,‘192.168.1.200‘,‘192.168.1.11‘,‘192.168.1.13‘,‘192.168.1.10‘,‘192.168.1.11‘,‘192.168.1.10‘,‘192.168.1.200‘,‘192.168.1.13‘,‘192.168.1.10‘,‘192.168.1.11‘,‘192.168.1.200‘,‘192.168.1.11‘,‘192.168.1.13‘,‘192.168.1.10‘,‘192.168.1.11‘,‘192.168.1.10‘,‘192.168.1.11‘,‘192.168.1.13‘,‘192.168.1.10‘,‘192.168.1.200‘,‘192.168.1.10‘,‘192.168.1.11‘,‘192.168.1.13‘,‘192.168.1.200‘,‘192.168.1.11‘,‘192.168.1.10‘,‘192.168.1.200‘,‘192.168.1.13‘,‘192.168.1.10‘,‘192.168.1.200‘]
username=‘root‘
password=‘123456‘
port=22
time=datetime.datetime.now().strftime(‘%Y-%m-%d %H:%M:%S‘)
def run_cmd(ip):
list=[]
list0=[]
# if os.system(‘ping %s -c 1 &>/dev/null‘%ip)==0:
try:
paramiko.util.log_to_file(‘paramiko.log‘)
s = paramiko.SSHClient()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
s.connect(ip,port,username,password)
stdin,stdout,stderr=s.exec_command(‘df -kP‘)
df= stdout.read().split(‘\n‘)
stdin,stdout,stderr=s.exec_command("free|grep Mem|awk ‘{print $2}‘")
list.append(stdout.read().strip())
stdin,stdout,stderr=s.exec_command("free|grep ‘buffers/‘|awk ‘{print $3}‘")
list.append(stdout.read().strip())
stdin,stdout,stderr=s.exec_command("free|grep ‘buffers/‘|awk ‘{print $4}‘")
list.append(stdout.read().strip())
list.append(‘%s‘%(float(list[2])/float(list[0])))
stdin,stdout,stderr=s.exec_command("vmstat 1 2|sed -n ‘4p‘|awk ‘{print $(NF-2)}‘")
list.append(stdout.read().strip())
try:
stdin,stdout,stderr=s.exec_command(‘sh /tmp/tablespace.sh‘)
list0=stdout.read().split(‘\n‘)
list0.pop(0)
list0.pop(0)
list0.pop(0)
list0.pop(-1)
except:
list0=[‘null null null null null null‘]
s.close()
print ‘xxxx‘,ip
# else:
except:
list=[‘null‘,‘null‘,‘null‘,‘null‘,‘null‘]
df= ‘nul \n null null null null null null \n‘.split(‘\n‘)
list0=[‘null null null null null null‘]
print ‘butong‘,ip
#time=datetime.datetime.now().strftime(‘%Y-%m-%d %H:%M:%S‘)
# df=os.popen(‘df -kP‘).read().split(‘\n‘)
df.pop(0)
df.pop(-1)
for line in df:
list00=line.split()
p1 = Filesystem(ip=‘%s‘%ip,date_time=‘%s‘%time,Filesystem=‘%s‘%list00[0], sum_kb=‘%s‘%list00[1],Used=‘%s‘%list00[2], Available=‘%s‘%list00[3], Capacity=‘%s‘%list00[4],Mounted_on=‘%s‘%list00[5])
p1.save()
p2 = Men_Cpu(ip=‘%s‘%ip,date_time=‘%s‘%time,Men_sum_kb=‘%s‘%list[0], Men_used=‘%s‘%list[1],Men_free=‘%s‘%list[2], Men_idle=‘%s‘%list[3], Cpu_idle=‘%s‘%list[4])
p2.save()
for list in list0:
list=list.split()
p3 = Tablespace(ip=‘%s‘%ip,date_time=‘%s‘%time,TABLESPACE_NAME=‘%s‘%list[0], SUMMARY=‘%s‘%list[1],FREE=‘%s‘%list[2], MAX_FREE_EXTENT=‘%s‘%list[3], FREE_EXTENTS=‘%s‘%list[4],USED=‘%s‘%list[5])
p3.save()
p=multiprocessing.Pool(processes=10)
for hostname in hosts:
p.apply_async(run_cmd,(‘%s‘%hostname,))
print hostname
#time.sleep(240)
print len(hosts)
time2=datetime.datetime.now().strftime(‘%Y-%m-%d %H:%M:%S‘)
print time2
p.close()
p.join()
time3=datetime.datetime.now().strftime(‘%Y-%m-%d %H:%M:%S‘)
print time2
print time3
############################################################
[[email protected] myweb]# tail -200 /tmp/python/Django-1.5.1/django/bin/myweb/pyweb/models.py
from django.db import models
# Create your models here.
from django.db import models
class Filesystem(models.Model):
ip = models.CharField(max_length=30)
date_time = models.CharField(max_length=50)
Filesystem = models.CharField(max_length=120)
sum_kb = models.CharField(max_length=60)
Used = models.CharField(max_length=30)
Available = models.CharField(max_length=50)
Capacity = models.CharField(max_length=60)
Mounted_on = models.CharField(max_length=60)
def __unicode__(self):
return self.ip
class Men_Cpu(models.Model):
ip = models.CharField(max_length=30)
date_time = models.CharField(max_length=50)
Men_sum_kb = models.CharField(max_length=40)
Men_used = models.CharField(max_length=40)
Men_free = models.CharField(max_length=40)
Men_idle = models.CharField(max_length=40)
Cpu_idle = models.CharField(max_length=40)
def __unicode__(self):
return self.ip
class Tablespace(models.Model):
ip = models.CharField(max_length=30)
date_time = models.CharField(max_length=50)
TABLESPACE_NAME = models.CharField(max_length=120)
SUMMARY = models.CharField(max_length=60)
FREE = models.CharField(max_length=30)
MAX_FREE_EXTENT = models.CharField(max_length=50)
FREE_EXTENTS = models.CharField(max_length=60)
USED = models.CharField(max_length=60)
def __unicode__(self):
return self.ip
#class Book(models.Model):
# title = models.CharField(max_length=100)
# authors = models.ManyToManyField(Author)
# publisher = models.ForeignKey(Publisher)
# publication_date = models.DateField()
# def __unicode__(self):
# return self.title
###################################################################
[[email protected] myweb]# tail -200 /tmp/python/Django-1.5.1/django/bin/myweb/pyweb/admin.py
from django.contrib import admin
from pyweb.models import Filesystem,Men_Cpu,Tablespace
class Filesystem_adin(admin.ModelAdmin):
list_display=(‘ip‘,‘date_time‘,‘Filesystem‘,‘sum_kb‘,‘Used‘,‘Available‘,‘Capacity‘,‘Mounted_on‘)
list_filter=(‘ip‘,)
search_fields=(‘ip‘,‘date_time‘,‘Filesystem‘)
ordering=(‘-date_time‘,)
class Men_Cpu_admin(admin.ModelAdmin):
list_display=(‘ip‘,‘date_time‘,‘Men_sum_kb‘,‘Men_used‘,‘Men_free‘,‘Men_idle‘,‘Cpu_idle‘)
list_filter=(‘ip‘,)
search_fields=(‘ip‘,‘date_time‘)
ordering=(‘-date_time‘,)
class Tablespace_admin(admin.ModelAdmin):
list_display=(‘ip‘,‘date_time‘,‘TABLESPACE_NAME‘,‘SUMMARY‘,‘FREE‘,‘MAX_FREE_EXTENT‘,‘FREE_EXTENTS‘,‘USED‘)
list_filter=(‘ip‘,)
search_fields=(‘ip‘,‘date_time‘,‘TABLESPACE_NAME‘)
ordering=(‘-date_time‘,)
admin.site.register(Filesystem,Filesystem_adin)
admin.site.register(Men_Cpu,Men_Cpu_admin)
admin.site.register(Tablespace,Tablespace_admin)
#admin.site.register(Author)
#admin.site.register(Book)
#########################################################################3
本文出自 “银河系|计算机网络” 博客,请务必保留此出处http://qqran.blog.51cto.com/10014850/1965638
以上是关于Python脚本之django---mysql-记录主机性能数据到数据库的主要内容,如果未能解决你的问题,请参考以下文章
Python脚本之django---mysql-记录主机性能数据到数据库-web站点管理数据库及web显示命令执行结果
Python脚本之利用django---mysql将磁盘文件系统写入数据库保存