def __query(self, ip): if not os.path.isfile(hosts_state_fname): # 防止主动创建 xxx.db 文件 print 'WARNING: db fname:', hosts_state_fname, ' NOT exist!!' log('chkalive: db %s NOT exist!' % hosts_state_tabname, project='reght', level=3) return [] try: s0 = 'select isLive from %s where ip="%s"' % (hosts_state_tabname, ip) db = sqlite3.connect(hosts_state_fname) c = db.cursor() rc = c.execute(s0) result = [] for r in rc: result.append(r[0]) db.close() return result except Exception as e: log('chkalive: query except: %s' % str(e), project='reght', level=2) return []
def ping_all(fname): ''' fname 为 tokens.json ''' sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) import platform if platform.uname()[0] == 'Windows': try: if not hasattr(socket, 'SIO_UDP_CONNRESET'): SIO_UDP_CONNRESET = 0x9800000c else: SIO_UDP_CONNRESET = socket.SIO_UDP_CONNRESET sock.ioctl(SIO_UDP_CONNRESET, False) except: print 'needn\'t UDP_CONNRESET Fase' ips = db_init_from_tokens(fname) print ips log('pping: load ips from tokens, include %s' % str(ips), project = 'dm', level = 3) last_send_ping = 0 while True: rc = select.select([sock], [], [], 10) if len(rc[0]) == 0: # timeout, just _send_pings(sock, ips) db_check_timeout(time.time()) last_send_ping = time.time() elif len(rc[0]) > 0: # 能收到 pong 了 try: pong, remote = sock.recvfrom(16) except: pass else: if pong == 'pong': remote_ip = remote[0] log('pping: update %s' % remote_ip, project='dm', level=4) db_update(remote_ip) t = time.time() if t - last_send_ping > 10: _send_pings(sock, ips) db_check_timeout(t) last_send_ping = t
def logme(info, level=5): log(info, project='reght', level=level)
def _send_pings(fd, ips): ''' TODO: 可以考虑在线的,60秒发一次,不在线的 10秒一次 ''' for ip in ips: log('pping: ping %s' % ip, project='dm', level=4) fd.sendto('ping', (ip, TARGET_PORT))
def logme(info, level = 5): log(info, project='reght', level = level)