1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| ## Ping Logger
## The script ping an host and add an entry on the ping database (used with crontab each minutes)
##== MYSQL TABLE ==
## CREATE TABLE `ping` (
## `when` datetime NOT NULL,
## `full_result` text NOT NULL,
## `packets_transmitted` varchar(255) default NULL,
## `received` varchar(255) default NULL,
## `packet_loss` varchar(255) default NULL,
## `time` varchar(255) default NULL
## ) ENGINE=ARCHIVE DEFAULT CHARSET=utf8
##
import MySQLdb
import subprocess
HOME = "***"
PING = ["/bin/ping","-c5", "-v", "-W2", "%s" % (HOME)]
dbcon = MySQLdb.connect(
host = "127.0.0.1",
user = "kedare",
passwd = "****",
db = "kedare")
output = subprocess.Popen(PING, stdout=subprocess.PIPE).communicate()
result = output[0].split("\n")[-3].split(", ")
ptransmitted = result[0]
preceived = result[1]
ploss = result[2]
ptime = result[3]
dbcur = dbcon.cursor()
dbcur.execute("""
INSERT INTO ping VALUES
(
NOW(),
%s,
%s,
%s,
%s,
%s
);""", (output[0], ptransmitted, preceived, ploss, ptime))
dbcur.close() |
Partager