python檢測網路延遲

神棍先生發表於2017-10-25
#!/usr/bin/env python
# coding: utf-8
# coding: cp950

```
Create Date: 2012-11-06
Version: 1.0
Description: Detection host survival
Author: Victor
QQ: 1409175531
```

``` Please run the script with root ```

import ping
import sys

def help():
  print ```
Usage:
%s <Dest_addr> <percent loss packages> <max round trip time>
``` % (sys.argv[0])

try:
  result = ping.quiet_ping(sys.argv[1], timeout=2, count=10, psize=64)
  if int(result[0]) == 100:
    print `Critical - 當機, 丟包率:%s%% | 報警閥值: >= %s%% 或 >=%s ms` % (result[0], int(sys.argv[2]), int(sys.argv[3]))
    sys.exit(2)
  else:
    max_time = round(result[1], 2)
    if int(result[0]) < int(sys.argv[2]) and int(result[1]) < int(sys.argv[3]):
      print `OK - 丟包率:%s%%, 最大響應時間:%s ms | 報警閥值: >= %s%% 或 >=%s ms` % (result[0], max_time, int(sys.argv[2]), int(sys.argv[3]))
      sys.exit(0)
    elif int(result[0]) >= int(sys.argv[2]) or int(result[1]) >= int(sys.argv[3]):
      print `Warning - 丟包率:%s%%, 最大響應時間:%s ms | 報警閥值: >= %s%% 或 >=%s ms` % (result[0], max_time, int(sys.argv[2]), int(sys.argv[3]))
      sys.exit(1)
    else:
      print `Unknown`
      sys.exit(3)
except IndexError:
  help()
  sys.exit(3)


相關文章