From e9a89f37f70eb552dccc45b88ff571e9753435eb Mon Sep 17 00:00:00 2001 From: edileuton <edileuton@gmail.com> Date: Wed, 22 Jan 2014 10:59:32 -0200 Subject: [PATCH] windows-collect: Add a script to get the network traffic Signed-off-by: edileuton <edileuton@gmail.com> --- windows-collect/Makefile | 3 + windows-collect/src/netmon/compileNetmon.py | 5 + windows-collect/src/netmon/netmon.py | 109 ++++++++++++++++++++ 3 files changed, 117 insertions(+) create mode 100644 windows-collect/src/netmon/compileNetmon.py create mode 100644 windows-collect/src/netmon/netmon.py diff --git a/windows-collect/Makefile b/windows-collect/Makefile index 46fa4d1..47b8cf0 100644 --- a/windows-collect/Makefile +++ b/windows-collect/Makefile @@ -1,2 +1,5 @@ all: src/compile.py src/datasidAgent.py src/collect.py @cd src/ && wine ~/.wine/drive_c/Python27/python compile.py py2exe + +netmon.py: + @cd src/netmon && wine ~/.wine/drive_c/Python27/python compileNetmon.py py2exe diff --git a/windows-collect/src/netmon/compileNetmon.py b/windows-collect/src/netmon/compileNetmon.py new file mode 100644 index 0000000..f556790 --- /dev/null +++ b/windows-collect/src/netmon/compileNetmon.py @@ -0,0 +1,5 @@ +#coding: utf-8 +from distutils.core import setup +import py2exe + +setup(console=['netmon.py'], options={"py2exe":{"dll_excludes":[ "mswsock.dll", "powrprof.dll" ]}}) diff --git a/windows-collect/src/netmon/netmon.py b/windows-collect/src/netmon/netmon.py new file mode 100644 index 0000000..c27b8de --- /dev/null +++ b/windows-collect/src/netmon/netmon.py @@ -0,0 +1,109 @@ +# Copyright (C) 2013 Centro de Computacao Cientifica e Software Livre +# Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR +# +# This file is part of datasid +# +# windows-coleta is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, +# USA. + +#coding: utf-8 +import os +import sys +import shutil +import subprocess +import re +from datetime import date, timedelta, datetime + +DATASIDPATH = os.environ["ProgramFiles"] + "\\DataSID" +PREVIOUSPATH = DATASIDPATH + "\\data\\previous" +TRAFFICPATH = DATASIDPATH + "\\data\\traffic.txt" + +# Get the network traffic from the current day +def getTraffic(): + # Uses netstat command to get the net usage + res = subprocess.Popen(["netstat", "-e"], shell=True, stdout=subprocess.PIPE).communicate()[0] + bytesLine = res.splitlines()[4] + unicastPackagesLine = res.splitlines()[5] + packagesLine = res.splitlines()[6] + + # Get the amount of bytes received and transmited + rxBytes = re.split(" +", bytesLine)[-2] + txBytes = re.split(" +", bytesLine)[-1] + + # Get the amount of packages received and transmited + rxPackages = int(re.split(" +", unicastPackagesLine)[-2]) + int(re.split(" +", packagesLine)[-2]) + txPackages = int(re.split(" +", unicastPackagesLine)[-1]) + int(re.split(" +", packagesLine)[-1]) + + return rxBytes, str(rxPackages), txBytes, str(txPackages) + +# Get a number identifying the current five minutes interval +def getIntervalId( ): + # Get a number identifying the current five minutes interval + intervalId = int(datetime.now().strftime("%H")) * 12 + (int(datetime.now().strftime("%M")) / 5 + 0.5) - 1 + # INTERVALID equal to -1 means midnight 00:00, but midnight needs + # to be the last id possible, so force it to 287 + if intervalId == -1: + intervalId = 287 + + return int(intervalId) + +# Get the traffic file +def getTrafficFile(currentDate): + trafficDate = "" + + # If there is no TRAFFIC (probably its the first run), then just create a + # new one. + try: + trafficFile = file(TRAFFICPATH, "r+") + trafficDate = trafficFile.readline() + trafficFile.seek(0, os.SEEK_END) + except IOError: + trafficFile = file(TRAFFICPATH, "w+") + trafficFile.write(currentDate + "\n") + return trafficFile + + # Finally, create a new TRAFFIC (keeping the previous one as + # PREVIOUS_TRAFFIC) if the dates doesnt match. + if trafficDate.find(currentDate) == -1: + if not os.path.exists(PREVIOUSPATH): + os.makedirs(PREVIOUSPATH) + trafficFile.close() + shutil.copy2(TRAFFICPATH, PREVIOUSPATH + "\\traffic-" + currentDate + ".txt") + trafficFile = file(TRAFFICPATH, "w+") + trafficFile.write(currentDate + "\n") + + return trafficFile + +# ========================================== +# Main program +# ========================================== + +try: + rxBytes, rxPackages, txBytes, txPackages = getTraffic() + intervalId = getIntervalId() + + # If it midnight (INTERVALID = 287), then consider current date as + # yesterday (subtract one day from current date). Thats because the + # data collect belongs to an interval beginning yesterday 23:55 and + # only ending today, at midnight. + if intervalId == 287: + currentDate = date.today() - timedelta(days=1) + else: + currentDate = date.today() + trafficFile = getTrafficFile(currentDate.strftime("%y-%m-%d")) + trafficFile.write(str(intervalId)+ " " + rxPackages + " " + rxBytes + " " + txPackages + " " + txBytes +"\n") + trafficFile.close() +except: + sys.exit(1) -- GitLab