Skip to content
Snippets Groups Projects
Commit e9a89f37 authored by edileuton's avatar edileuton
Browse files

windows-collect: Add a script to get the network traffic


Signed-off-by: default avataredileuton <edileuton@gmail.com>
parent 9dd8212c
No related branches found
No related tags found
No related merge requests found
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
#coding: utf-8
from distutils.core import setup
import py2exe
setup(console=['netmon.py'], options={"py2exe":{"dll_excludes":[ "mswsock.dll", "powrprof.dll" ]}})
# 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)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment