python脚本监控磁盘空间

写了个python小程序,监控磁盘空间,前面部分网上也有很多,写博客的目的是记录下来,已供自己后面使用,思路就是用pexpect 这个模块,ssh到不同的机器上,查到磁盘空间,最后对查到的结果进行处理,然后存到mysql数据中。以下是代码:


#coding=utf8

import pexpect

import getpass,os,sys

import re,datetime, time

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

ssh_newkey = ‘Are you sure you want to continue connecting‘

child = pexpect.spawn(‘ssh -l %s %s %s‘%(user, host, command))

i = child.expect([pexpect.TIMEOUT, ssh_newkey, ‘password: ‘])

if i == 0: # Timeout

print child.before, child.after

return None

# 如果 ssh 没有 public key,接受它.

if i == 1: # SSH does not have 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

# 输入密码.

child.sendline(password)

return child

def main (date):

base=‘ip文件‘

wbase=‘保存文件名‘

file=open(base)

f=open(wbase,"w")

#raw_command = raw_input("输入命令: ")

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‘)

# 匹配 pexpect.EOF

child.expect(pexpect.EOF)

# 输出命令结果.

#print serverM[1]+" 磁盘空间如下:"

#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[len(line)-13:len(line)-10])

a.append(line[len(line)-19:len(line)-14])

a.append(line[len(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用户名-p密码 -D数据库 -e "insert into 表名 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)

时间: 2025-01-07 03:05:06

python脚本监控磁盘空间的相关文章

Linux/Unix shell 脚本监控磁盘可用空间

Linux下监控磁盘的空闲空间的shell脚本,对于系统管理员或DBA来说,必不可少.下面是给出的一个监控磁盘空间空间shell脚本的样本,供大家参考. 1.监控磁盘的空闲空间shell脚本 [python] view plain copy print? [email protected]:~/dba_scripts/custom/bin> more ck_fs_space.sh #!/bin/bash # --------------------------------------------

[Linux监控]磁盘空间大小

echo 192.168.10.69>>ip.list ------------------------------------------- #!/bin/bash #注意if和[]之间的空格 rm -f ~/fanr/shell/DiskUsageAlert/out.print out=$(cat ~/fanr/shell/DiskUsageAlert/ip.list) echo $out for _IP in $out do ssh [email protected]$_IP df |

MS SQL 监控磁盘空间告警

这几天突然有个想法:希望能够自动监控.收集数据库服务器的磁盘容量信息,当达到一个阀值后,自动发送告警邮件给DBA,将数据库磁盘详细信息告知DBA,提醒DBA做好存储规划计划,初步的想法是通过作业调用存储过程来实现(每天调用一次),这样避免了我每天每台数据库服务器都上去检查一下,尤其是手头的数据库服务器N多的情况,这样可以避免我每天浪费无谓的时间.如果大家有更好的建议和方法,欢迎指点一二,我整理.修改了三个存储过程如下: 存储过程1:SP_DiskCapacityAlert1.prc 说明:需要通

shell vs python脚本监控http请求

各写一个shell和python脚本来监控http请求,并在服务不可用的时候重启服务. 监控的连接为: http://192.168.1.101:5022/product http://192.168.1.101:5024/module shell脚本如下,配合crontab计划任务每一分钟执行一次检查: #!/bin/bash # This shell is used to moniter 192.168.1.101 port 5022 & 5024 date  #在crontab里用来记录l

Redis之使用python脚本监控队列长度

编辑python脚本redis_conn.py #!/usr/bin/env python #ending:utf-8 import redis def redis_conn(): pool = redis.ConnectionPool(host="192.168.56.11",port=6379,db=3,password=123456) conn = redis.Redis(connection_pool=pool) data = conn.llen("system-lo

使用python脚本监控指定域名解析

python脚本:                                                                                                                  21,0-1        All #!/usr/bin/python # -*- coding: utf-8 -*- #此脚本用于检测域名解析是否正常 import os namelist = {'www.51cto.com':'218.11.0.91

使用python脚本监控weblogic

1.python的脚本如下: 1 ############################################################################### 2 #created on 2013-07-09 3 #author : zhaolijun 4 #used to get weblogic server runtime infomation 5 #wls_ver:weblogic 10.3.5.0 6 #########################

python脚本监控docker容器

脚本功能: 监控CPU使用率 监控内存使用状况 监控网络流量 #!/usr/bin/env  python # --*-- coding:UTF-8 --*-- import  sys import  tab import  re import  os import  time from docker  import Client import  commands keys_container_stats_list = ['blkio_stats', 'precpu_stats', 'netwo

Python脚本 - 查询磁盘的读写次数信息

测试系统为:Centos 6.7 Python版本为: 3.6.4 脚本功能:查看指定磁盘的读写及时间等相关信息 #!/usr/bin/env python3 from collections import namedtuple Disk = namedtuple('Disk','major_number minor_number device_name read_count read_merged_count read_sections time_spent_reading write_cou