您的位置:首页 > 运维架构 > Linux

通过crond自动运行Python脚本实现多台linux服务器的监控

2010-03-10 14:59 961 查看
一、Pexpect简介

  Pexpect 是一个用来启动子程序并对其进行自动控制的 Python 模块,它可以用来和像 ssh、ftp、passwd、telnet 等命令行程序进行自动交互。本文介绍 Pexpect 的主要用法和在实际应用中的注意点。 Python 语言的爱好者,系统管理人员,部署及测试人员都能使用 Pexpect 在自己的工作中实现与命令行交互的自动化。

  具体可以参考http://www.noah.org/wiki/Pexpect#Download_and_Installation

  下载地址 http://pexpect.sourceforge.net/pexpect-2.3.tar.gz

  今天介绍的内容就是利用pexpect来实现的远程多服务器的管理。

二、Python脚本

  以下是python的源代码。程序的大概过程是使用pexpect的ssh命令循环登陆到远程服务器上,将服务和进程的运行情况纪录到临时文件中,然后再从文件中获取服务和进程的运行情况,如果服务或进程异常停止,程序则将其重新启动,所有服务器的运行状况都将纪录在日志文件中。

代码

# coding=utf-8
#!/usr/bin/env python

import pexpect
import getpass, os
import string
import time
from datetime import datetime, date

ssh_newkey = 'Are you sure you want to continue connecting (yes/no)?'
hosts = []

hosts.append('xx.xx.xx.xx')
user = 'user'
password = 'pwd'

logFile = '/tmp/pexpect-2.3/monitor.log'

def restartService(host, user, password, service):
child = pexpect.spawn('ssh %s@%s' %(user, host))
i = child.expect([pexpect.TIMEOUT, ssh_newkey, 'password: '])
if i == 0: # Timeout
return None
if i == 1:
child.sendline('yes')
child.sendline(password)
child.sendline('sudo service %s restart' %service)
j = child.expect(['Password: ', '$', '#'])
if j == 0:
child.sendline(password)
if j == 1:
child.sendline(password)
time.sleep(30)
child.sendline('exit')
fout = file(logFile, 'a')
child.logfile_read = fout
return child
# restart one process
def restartProcess(host, user, password):
child = pexpect.spawn('ssh %s@%s' %(user, host))
i = child.expect([pexpect.TIMEOUT, ssh_newkey, 'password: '])
if i == 0: # Timeout
return None
if i == 1:
child.sendline('yes')
child.sendline(password)
child.sendline('sudo su')
j = child.expect(['Password: ', '$', '#'])
if j == 0:
child.sendline(password)
if j == 1:
child.sendline(password)
child.sendline('cd /processlocation/bin')
child.sendline('./processName.sh &')
time.sleep(20)
child.sendline('exit')
child.sendline('exit')
fout = file(logFile, 'a')
child.logfile_read = fout
return child

# log process' status to file tmpProc.txt
def logProcessInfo(host, user, password, process):
child = pexpect.spawn('ssh %s@%s' %(user, host))
i = child.expect([pexpect.TIMEOUT, ssh_newkey, 'password: '])
if i == 0: # Timeout
return None
if i == 1:
child.sendline('yes')
child.sendline(password)
child.sendline('ps -ef|grep %s' %process)
child.sendline('exit')
fout = file('tmpProc.txt', 'w')
child.logfile_read = fout
return child
# get process' id from file tmpProc.txt
def getProcessId():
val = ''
f = file('tmpProc.txt', 'r')
f.seek(0)
while True:
line = f.readline()
if line.find('processName') > 0:
index = line.find(' ')
subline = line[index+1:]
while True:
if subline[0] == ' ':
index = subline.find(' ')
subline = subline[index+1:]
else:
break
index2 = subline.find(' ')
number = subline[:index2]
val = number
if len(line) == 0:
break
f.close()
return val

# log service's status to file tmpServ.txt
def logServiceInfo(host, user, password, serviceName):
child = pexpect.spawn('ssh %s@%s' %(user, host))
i = child.expect([pexpect.TIMEOUT, ssh_newkey, 'password: '])
if i == 0: # Timeout
return None
if i == 1:
child.sendline('yes')
child.sendline(password)
child.sendline('service %s status' %serviceName)
child.sendline('exit')
fout = file('tmpServ.txt', 'w')
child.logfile_read = fout
return child

# get servicee's status of start or stop from file tmpServ.txt
def hasServiceStart():
val = True
f = file('tmpServ.txt', 'r')
f.seek(0)
while True:
line = f.readline()
if line.find('stop') > 0:
val = False
if len(line) == 0:
break
f.close()
return val

def logOperation(content):
f = file(logFile, 'a')
f.write(content)
f.close()

current = datetime.now()
logOperation('\n\n***')
logOperation(current.strftime("%a %b %d %H:%M:%S %Y") + '\n')
for host in hosts:
processChild = logProcessInfo(host, user, password, 'processName')
processChild.expect(pexpect.EOF)
processId = getProcessId()
if processId == '':
logOperation('processName on ' + host + ' has been stopped\n')
processChild = restartProcess(host, user, password)
processChild.expect(pexpect.EOF)
else:
logOperation('processName on ' + host + ' is ok\n')

serviceChild = logServiceInfo(host, user, password, 'serviceName')
serviceChild.expect(pexpect.EOF)
if hasServiceStart():
logOperation('serviceName on ' + host + ' is ok\n')
else:
logOperation('serviceName on ' + host + ' has been stopped\n')
serviceChild = restartService(host, user, password, 'serviceName')
serviceChild.expect(pexpect.EOF)

print 'mission accomplished'

三、通过配置crond使脚本能够定时执行

1)登陆到脚本所在的linux服务器上

2)运行命令crontab -e

说明:系统默认的编辑器是VIM,如果不是请加上以下shell:

$EDITOR=vi

$export EDITOR

3)添加0 * * * * python /tmp/pexpect-2.3/autoMonitor.py
4)重起crond
cd /etc/init.d
./crond restart
这样每个小时0分的时候105上会自动运行autoMonitor.py脚本

具体crond的配置可以参考http://www.oklinux.cn/html/Basic/jyjq/20090107/66512.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: