(name, host, interval, type)
| 366 | |
| 367 | |
| 368 | def _monitor_thread(name, host, interval, type): |
| 369 | while True: |
| 370 | if name not in monitorServer.keys(): |
| 371 | break |
| 372 | try: |
| 373 | # 1) 解析目标 host 与端口 |
| 374 | if type == 'http': |
| 375 | addr = str(host).replace('http://','') |
| 376 | addr = addr.split('/',1)[0] |
| 377 | port = 80 |
| 378 | if ':' in addr and not addr.startswith('['): |
| 379 | a, p = addr.rsplit(':',1) |
| 380 | if p.isdigit(): |
| 381 | addr, port = a, int(p) |
| 382 | elif type == 'https': |
| 383 | addr = str(host).replace('https://','') |
| 384 | addr = addr.split('/',1)[0] |
| 385 | port = 443 |
| 386 | if ':' in addr and not addr.startswith('['): |
| 387 | a, p = addr.rsplit(':',1) |
| 388 | if p.isdigit(): |
| 389 | addr, port = a, int(p) |
| 390 | elif type == 'tcp': |
| 391 | addr = str(host) |
| 392 | if addr.startswith('[') and ']' in addr: |
| 393 | a = addr[1:addr.index(']')] |
| 394 | rest = addr[addr.index(']')+1:] |
| 395 | if rest.startswith(':') and rest[1:].isdigit(): |
| 396 | addr, port = a, int(rest[1:]) |
| 397 | else: |
| 398 | raise Exception('bad tcp target') |
| 399 | else: |
| 400 | a, p = addr.rsplit(':',1) |
| 401 | addr, port = a, int(p) |
| 402 | else: |
| 403 | time.sleep(interval) |
| 404 | continue |
| 405 | |
| 406 | # 2) 解析 IP(按偏好族) |
| 407 | IP = addr |
| 408 | if addr.count(':') < 1: # 非纯 IPv6 |
| 409 | try: |
| 410 | if PROBE_PROTOCOL_PREFER == 'ipv4': |
| 411 | IP = socket.getaddrinfo(addr, None, socket.AF_INET)[0][4][0] |
| 412 | else: |
| 413 | IP = socket.getaddrinfo(addr, None, socket.AF_INET6)[0][4][0] |
| 414 | except Exception: |
| 415 | pass |
| 416 | |
| 417 | # 3) 建连耗时(timeout=1s),ECONNREFUSED 也计入 |
| 418 | try: |
| 419 | b = timeit.default_timer() |
| 420 | socket.create_connection((IP, port), timeout=1).close() |
| 421 | monitorServer[name]["latency"] = int((timeit.default_timer() - b) * 1000) |
| 422 | except socket.error as error: |
| 423 | if getattr(error, 'errno', None) == errno.ECONNREFUSED: |
| 424 | monitorServer[name]["latency"] = int((timeit.default_timer() - b) * 1000) |
| 425 | else: |
nothing calls this directly
no outgoing calls
no test coverage detected