IT猫扑网:您身边最放心的安全下载站! 最新更新| 软件分类| 专题汇总| 手机版

您当前所在位置:IT猫扑网 > 操作系统 > LINUX > crond运行Python脚本实现多台linux服务器的监控

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

时间:2015-06-28 00:00 来源:IT猫扑网|http://www.itmop.com/ 作者:网管联盟 我要评论(0)

  一、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

#p#副标题#e#

  # 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('nn***')

  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 stoppedn')

  processChild = restartProcess(host, user, password)

  processChild.expect(pexpect.EOF)

  else:

  logOperation('processName on ' + host + ' is okn')

  serviceChild = logServiceInfo(host, user, password, 'serviceName')

  serviceChild.expect(pexpect.EOF)

  if hasServiceStart():

  logOperation('serviceName on ' + host + ' is okn')

  else:

  logOperation('serviceName on ' + host + ' has been stoppedn')

  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,Python脚本,lin

相关阅读 安装红帽子RedHat Linux9.0操作系统教程 Tomcat9.0如何安装_Tomcat9.0环境变量配置方法 多种操作系统NTP客户端配置 Linux操作系统修改IP Linux实现SCSI硬盘热插拔及在线识别 Linux下用CDMA modem拨号上网

文章评论
发表评论

热门文章 安装红帽子RedHat Linux9.0操作系统教程 安装红帽子RedHat Linux9.0操作系统教程 Linux服务器:设计高性能网站架构-LLMP Linux服务器:设计高性能网站架构-LLMP 使用Clonezilla迁移到虚拟Linux环境 使用Clonezilla迁移到虚拟Linux环境 Linux上的MRTG流量监控中心 Linux上的MRTG流量监控中心 Linux 双网卡绑定一个IP原理及实现 Linux 双网卡绑定一个IP原理及实现 linux和windows等系统远程控制ubuntu桌面 linux和windows等系统远程控制ubuntu桌面

相关下载

人气排行 Linux下获取CPUID、硬盘序列号与MAC地址 dmidecode命令查看内存型号 linux tc实现ip流量限制 安装红帽子RedHat Linux9.0操作系统教程 linux下解压rar文件 lcx.exe、nc.exe、sc.exe入侵中的使用方法 Ubuntu linux 关机、重启、注销 命令 查看linux服务器硬盘IO读写负载 linux命令行浏览器的使用方法 Linux NFS服务固定端口及防火墙配置 U盘安装Ubuntu 10.04 Linux清除用户登录记录和命令历史方法