We can monitor the upload or download traffic for a host by adding iptables rules to the gateway, here are the steps:
To add a iptables rule:
/sbin/iptables-i input-s 10.0.5.110
Create a database for storage: just one column,
Mysql> desc proxy;+-------+----------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+-------+----------+------+-----+---------+-------+| IP | char (20) | YES | | NULL | |+-------+----------+------+-----+---------+-------+1 row in Set (0.00 sec)
Use Python to get traffic and make judgments:
#!/usr/bin/env python# -*- coding: utf-8 -*-import datetimeimport Subprocessimport mysqldbimport smtplibfrom email.mime.text import mimetextimport sysreload (SYS) sys.setdefaultencoding (' Utf-8 ') def get_traffic (): j = subprocess. Popen ("/sbin/iptables -n -v -x -l input|grep all|grep 10.0.5", shell= True, stderr=subprocess. Pipe, stdout=subprocess. PIPE) u = j.communicate () n = u[0].split (' \ n ') return ndef html_construct (): con = MySQLdb.connect (host= ' localhost ', user= ' user ', passwd= ' passwd ', db= ' traffic_monitor ', charset= ' UTF8 ') cur = con.cursor () html = "" html_head = "" "
Create Crontab
This article is from the "11062687" blog, please be sure to keep this source http://11072687.blog.51cto.com/11062687/1768071
2. Monitor upload traffic using iptables