Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1416086
  • 博文数量: 264
  • 博客积分: 5810
  • 博客等级: 大校
  • 技术积分: 3528
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-13 17:15
文章分类

全部博文(264)

文章存档

2011年(264)

分类: Python/Ruby

2011-03-24 14:36:14

  1. #-*-coding:utf8-*-
  2. """
  3. @copyright: V2010-11-10
  4. @see: 一个定时器定时去检查当前全部受控机器的监听状态
  5. """
  6. import pickle,string
  7. from threading import Thread
  8. import MySQLdb
  9. import time,os,httplib,json,datetime,sys
  10. import os
  11. import socket, sys
  12. logpath = "/tmp/telnet.log"
  13. class ExtTimer(Thread):
  14.     def __init__(self, interval, maxtime, func):
  15.         self.__interval = interval
  16.         self.__maxtime = maxtime
  17.         self.__tottime = 0.0
  18.         self.__func = func
  19.         self.__flag = 0
  20.         Thread.__init__(self, None, "ExtTimer", None)
  21.     def run(self):
  22.         while self.__flag == 0:
  23.             time.sleep(self.__interval)
  24.             conn = MySQLdb.connect("127.0.0.1","xxx","xxx","xxx",port=3306,connect_timeout=10,compress=True)
  25.             cursor=conn.cursor()
  26.             self.__func(cursor)
  27.             cursor.close()
  28.             conn.close()
  29.             if self.__maxtime > 0.0:
  30.                 self.__tottime += self.__interval
  31.                 if self.__tottime >= self.__maxtime:
  32.                     self.end()
  33.     def end(self):
  34.         self.__flag = 1

  35. class ExtTimerDummy:
  36.     def __init__(self):
  37.         self.__interval = 60.0
  38.     def __timerFunc(self,cursor):
  39.         parsedata(cursor)
  40.     def createTimer(self, interval):
  41.         self.__interval = interval
  42.         self.__timer = ExtTimer(self.__interval, -20, self.__timerFunc)
  43.         self.__timer.start()
  44. def log(_msg):
  45.     now = time.time()
  46.     strtime = str(time.localtime(now)[0]) + '-' + str(time.localtime(now)[1]) \
  47.                    + '-' + str(time.localtime(now)[2]) \
  48.                    + ' ' + str(time.localtime(now)[3]) \
  49.                    + ':' + str(time.localtime(now)[4]) \
  50.                    + ':' + str(time.localtime(now)[5]);
  51.     msg = strtime + ' ' + _msg + '\n';
  52.     try:
  53.         logfilehandle = open(logpath,'a+');
  54.         logfilehandle.write(msg);#write file
  55.         logfilehandle.close();
  56.     except:pass;

  57.     

  58. def parsedata(cursor):
  59.     try:
  60.         #遍历hosts文件逐行检查其主机状态是否正常
  61.         myfile = open("/etc/hosts")
  62.         for line in myfile.readlines():
  63.             s = line.split()
  64.             host = s[1].strip()
  65.             try:
  66.                 s=socket.socket()
  67.                 s.settimeout(5.0)
  68.                 s.connect((host,51234))
  69.                 sql = "update xxx set isping='1' where hostname = '%s'" % host
  70.                 cursor.execute(sql)
  71.             except Exception,e:
  72.                 print host
  73.                 sql = "update xxx set isping='0' where hostname = '%s'" % host
  74.                 cursor.execute(sql)
  75.     except Exception,e:print str(e)
  76. time_object1 = ExtTimerDummy()
  77. time_object1.createTimer(5)
阅读(1545) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~