日期:2014-05-16  浏览次数:20750 次

python 实现多台UNIX服务器自动巡检

python 实现多台UNIX服务器自动巡检
2012年01月06日
  工作需要,每天要对系统内的多台LINUX服务器做巡检,主要检查的内容有CPU,内存,进程,硬盘空间等。
  想来想去觉得还是用PYTHON来实现比较容易。平台移植性比较好。
  查了很多关于PYTHON SSH联接的文档。需要用到pexpect 或paramiko  中的一个包,后来发现这两个包安装到WINDOW下比较麻烦。
  经过多次偿试未成功。(实在没办法,很好的东西,哪位高手用成功了,可以交流交流。)最终选定用TELENET来实现,还好平台内网都支持TELNET。
  进入正题:
  思路如下:每天自动联接到服务器上运行服务器的命令。然后把结果存储到文件中,最后把结果发邮件出来。
  主程序:autoMonitor.py
  import ConfigParser
  import base64,time,telnetlib,os,struct,smtplib
  from email.MIMEText import MIMEText
  from email.MIMEMultipart import MIMEMultipart
  def WriteLog(filename,tmpstr):
  time_str = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
  logstr = str(tmpstr) + '\n'
  f = open(filename,"a")
  f.write(logstr)
  f.close()
  def getConfigFile(configFile):
  iconfs=[]
  Config = ConfigParser.ConfigParser()
  Config.read(configFile)
  confFiles=Config.items("Config")
  for conf in confFiles:
  iconfs.append(conf[1])
  return iconfs
  def readConfig(configFile):
  coms=[]
  Config = ConfigParser.ConfigParser()
  Config.read(configFile)
  host = Config.get("BASEINFO","HOST")
  user = Config.get("BASEINFO","USERNAME")
  pwd = base64.b64decode(Config.get("BASEINFO","PASSWORD"))
  poi = Config.get("BASEINFO","POINTING")
  commands = Config.items("COMMANDS")
  for com in commands:
  coms.append(com[1])
  return host,user,pwd,poi,coms
  def TelNet(host,user,pwd,poi,coms,outprintFile):
  time_str = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
  WriteLog(outprintFile,host+"       \n" + time_str)
  tn=telnetlib.Telnet(host)
  tn.read_until("login:")
  tn.write(user+"\n")
  if pwd:
  tn.read_until("Password:")
  tn.write(pwd + "\n")
  #    print "tn.pwd\n"
  tn.write("vt100\n")
  tn.read_until(poi)
  for com in coms:
  #        if poi=="$":
  print poi
  tn.write(com + "\n")
  abc = tn.read_until(poi)
  print abc
  WriteLog(outprintFile,abc)
  #        time.sleep(1)
  tn.write("exit\n")
  #    print "exit\n"
  #    print "test\n"
  #print tn.read_all()
  #    tn.interact()
  WriteLog(outprintFile,tn.read_all())  
  tn.close
  WriteLog(outprintFile,"======================================================================================\n")
  print "host=" + host + "checked OK!"
  def getMailConfig(configFile):
  coms=[]
  Config = ConfigParser.ConfigParser()
  Config.read(configFile)
  host = Config.get("FROM","HOST")
  user = Config.get("FROM","USERNAME")
  subject = Config.get("FROM","SUBJECT")
  pwd = base64.b64decode(Config.get("FROM","PASSWORD"))
  commands = Config.items("TO")
  for com in commands:
  coms.append(com[1])
  return host,user,pwd,subject,coms
  def SendMail(configFile,sendfile):
  host,user,pwd,subject,coms=getMailConfig(configFile)
  _tos=""
  for _to in coms:
  _tos = _tos + _to + ";"
  msg = MIMEMultipart()
  # att = MIMEText("test mail")
  att = MIMEText(open(sendfile,'rb').read(),'base64','gb2312')
  att["Content-Type"] ='application/octet-stream'
  att["Content-Disposition"]='attachment;filename="'+sendfile+'"'
  #    msg.attach(att)
  #    msg['to']=coms
  msg['from']=user
  msg['to']=_tos
  msg['subject']=subject
  msg.attach(att)
  try:
  smtp_svr=smtplib.SMTP()
  smtp_svr.connect(host,"25")
  except (smtplib.SMTPConnectError,socket.error):
  print "connect error"
  try:
  smtp_svr.login(user,pwd)