1
0
Fork 0
myping/myping-server.py

50 lines
1.0 KiB
Python
Executable File

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import pymongo
import subprocess
import re
import datetime
client = pymongo.MongoClient("localhost", 27017)
db = client.myping
hosts = [
"DeadBSD.org",
"nausea.DeadBSD.org",
"helvete.DeadBSD.org",
"gamito.DeadBSD.org",
"www.nos.pt",
"192.168.5.1",
]
arguments = [
"fping",
"-C", "20",
"-q",
"-B1",
"-r1",
"-i10",
]
for h in hosts:
arguments.append(h)
dt = datetime.datetime.now()
p = subprocess.Popen(arguments, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
output = p.communicate()[0]
for h in hosts:
obj = { 'date': dt, 'host': h }
match = re.search("" + h + " +: (.*)?", output)
if match:
result = match.group(1)
times = map(float, result.split())
obj['min'] = min(times)
obj['avg'] = sum(times) / float(len(times))
obj['max'] = max(times)
print obj
db.my_collection.insert_one(obj)