2. Monitor upload traffic using iptables

Source: Internet
Author: User

We can monitor the upload or download traffic for a host by adding iptables rules to the gateway, here are the steps:

  1. To add a iptables rule:

    /sbin/iptables-i input-s 10.0.5.110

  2. Create a database for storage: just one column,

  3. Mysql> desc proxy;+-------+----------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+-------+----------+------+-----+---------+-------+| IP | char (20) |     YES | |       NULL | |+-------+----------+------+-----+---------+-------+1 row in Set (0.00 sec)
  4. Use Python to get traffic and make judgments:

  5. #!/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 =  "" "     
  6. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.