Python script Django---mysql-record host performance data to database

Source: Internet
Author: User
Tags null null stdin python script

[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 $} ')

List.append (Stdout.read (). Strip ())

Stdin,stdout,stderr=s.exec_command ("Free|grep ' buffers/' |awk ' {print $} ')

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 '



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 '

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

This article is from the "Galaxy | Computer network" blog, please be sure to keep this source http://qqran.blog.51cto.com/10014850/1965638

Python script Django---mysql-record host performance data to database

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.