Python Script monitors disk space

Source: Internet
Author: User
Tags save file

Wrote a Python applet, monitoring disk space, the front part of the network there are many, the purpose of blogging is to record down, has been used for their own back, the idea is to use pexpect this module, SSH to different machines, to find disk space, and finally to the results of processing, And then save it to the MySQL data. Here's the code:


#coding =utf8

Import Pexpect

Import Getpass,os,sys

Import Re,datetime, Time


def ssh_command (user, host, password, command):

Ssh_newkey = ' Sure you want to continue connecting '

Child = Pexpect.spawn (' ssh-l%s%s '% (user, host, command))

i = Child.expect ([Pexpect. TIMEOUT, Ssh_newkey, ' Password: '])

If i = = 0: # Timeout

Print Child.before, Child.after

Return None

# If SSH does not have public key, accept it.

if i = = 1: # SSH does not has the public key. Just accept it.

Child.sendline (' yes ')

Child.expect (' Password: ')

i = Child.expect ([Pexpect. TIMEOUT, ' password: '])

If i = = 0: # Timeout

Print Child.before, Child.after

Return None

# Enter your password.

Child.sendline (password)

return child


def main (date):

Base= ' IP file '

Wbase= ' Save file name '

File=open (Base)

F=open (Wbase, "w")

#raw_command = raw_input ("Input command:")

While 1:

Lines = File.readlines (10000)

If not lines:

Break

For line in lines:

Serverm=line.split (', ')

child = Ssh_command (serverm[0],serverm[1],serverm[2], ' df-h ')

# match Pexpect. Eof

Child.expect (pexpect. EOF)

# output command result.

#print serverm[1]+ "Disk space as follows:"

#print Child.before

F.write (Serverm[1])

F.write (Child.before)

F.close ()

File.close ()

ReadLine (Wbase,date)

def readLine (base,date):

File = open (Base)

A=[]

While 1:

Lines = File.readlines (10000)

If not lines:

Break

For line in lines:

Ob=re.search (' \d*[.] \d*[.] \d*[.] \d* ', line)

Oe=re.search ('/export ', line)

If OB:

A.append (Ob.group (0))

If OE:

A.append (line) -13:len (line)-10])

A.append (line) -19:len (line)-14])

A.append (line) -25:len (line)-20])



File.close ()

Mysql= "" "" "" "%date

Os.system (MySQL)

I= Len (a)/4

While i>0:

I=i-1

Mysql1= "" "Mysql-hip-p3358-u User name-p password-d database-e" INSERT into table name values (%s, '%s ', '%s ', '%s ', '%s ') "" ""% (date,a[0+4*i],a[3+4 *i],a[2+4*i],a[1+4*i])

#print A[0+4*i]

#print A[1+4*i]

#print A[2+4*i]

#print MySQL


Os.system (MYSQL1)



if __name__ = = ' __main__ ':

if (len (SYS.ARGV) > 1):

Y_date = sys.argv[1]

Else

Y_date = (Datetime.datetime.now ()-Datetime.timedelta (Days=1)). Strftime ("%y%m%d")

Try

Main (Y_date)

Except Exception, E:

Print str (e)

Traceback.print_exc ()

Os._exit (1)


Python Script monitors disk space

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.