diff --git a/windows-collect/src/collect.py b/windows-collect/src/collect.py index 4a45fa20073a1af64fe6994d6d124f4d9949de0b..a0f2bdff3ea9a8b62b6984a93acbc4156ad66ad4 100644 --- a/windows-collect/src/collect.py +++ b/windows-collect/src/collect.py @@ -25,9 +25,13 @@ import ctypes import wmi import socket import struct +import glob from xml.etree.ElementTree import Element, SubElement, Comment, tostring +from datetime import timedelta, date, datetime DATASIDPATH = os.environ["ProgramFiles"] + "\\DataSID" +PREVIOUSPATH = DATASIDPATH + "\\data\\previous" + # Return the value from key/subkey in windows registry def getRegistryValue(key, subkey, value): key = getattr(_winreg, key) @@ -39,10 +43,10 @@ def getTelecentroInfo(info): tlInfoFile = open(DATASIDPATH + "\\conf\\telecentroInfo", "r") tlInfo = value = " " while (tlInfo != info and tlInfo != ""): - try: - (tlInfo, value) = tlInfoFile.readline().split("=") - except: - tlInfo = value = "" + try: + (tlInfo, value) = tlInfoFile.readline().split("=") + except: + tlInfo = value = "" tlInfoFile.close() return value @@ -78,7 +82,7 @@ def getMac(host): addlen = ctypes.c_ulong(ctypes.sizeof(buffer)) if SendARP (inetaddr, 0, ctypes.byref(buffer), - ctypes.byref(addlen)) != 0: + ctypes.byref(addlen)) != 0: raise WindowsError('Retrieval of mac address(%s) - failed' % host) # Convert binary data into a string @@ -184,7 +188,7 @@ def collect(): # Collect Hard Disk c = wmi.WMI() tagInventoryDisks = SubElement (tagInventory, 'disks', {'type':'tree'}) - + i = -1 for disk in c.win32_DiskDrive(): if disk.MediaType != "Removable Media": @@ -202,7 +206,7 @@ def collect(): tagInventoryDiskValue2.text = str(hddSize) tagInventoryDiskValue3 = SubElement(tagInventoryDisk, 'used') tagInventoryDiskValue3.text = str(used) - + # Collect Memory memory = getRam() @@ -245,43 +249,64 @@ def collect(): # Main collect function. This function gets the net usage information and creates # the XML -def netCollect(): - mainTag = Element('collect') +def netCollect(log): + mainTag = Element('net-collected-data') - # Collect INEP - inep = getINEP() - - tagInep = SubElement(mainTag, 'inep') - tagInepValue = SubElement(tagInep, 'number', {'type':'int', 'value':inep}) - # Collect Version - tagAgent = SubElement(mainTag, 'agent') - tagVersion = SubElement(tagAgent, 'version', {'type':'string', 'value':getRegistryValue("HKEY_LOCAL_MACHINE", "SOFTWARE\\DataSID", "Version")}) - - # Collect Mac Address + tagAgent = SubElement(mainTag, 'agent-version') + tagAgent.text = getRegistryValue("HKEY_LOCAL_MACHINE", "SOFTWARE\\DataSID", "Version") + + # Collect Telecentro's id + superid = getTelecentroInfo("superid") + tagTelecentroId = SubElement(mainTag, 'telecentro-id') + tagTelecentroId.text = superid + + # Collect MAC Address macAddress = getMac('localhost') macAddress = macAddress[0:-1] - - tagMac = SubElement(mainTag, 'mac-address') - tagMacValue = SubElement(tagMac, 'mac', {'type':'string', 'value':macAddress}) - - tagProject = SubElement( mainTag, 'project') - tagProjectSource = SubElement(tagProject, 'source', {'type':'int', 'value':"0"}) - - tagUse = SubElement(mainTag, 'use') - tagUseBandUsage = SubElement(tagUse, 'bandwidth-usage', {'type':'tree'}) - - for i in range(1, 289): - tagNetUse = SubElement(tagUseBandUsage, 'netuse', {'id':str(i)}) - tagDate = SubElement(tagNetUse, 'date', {'value':'0001-01-01', 'type':'string'}) - tagTime = SubElement(tagNetUse, 'time', {'value':'0:0', 'type':'string'}) - - tagRx = SubElement(tagNetUse, 'rx') - tagPackets = SubElement(tagRx, 'packets', {'value':"0", 'type':'int'}) - tagBytes = SubElement(tagRx, 'bytes', {'value':"0", 'type':'int'}) - - tagTx = SubElement(tagNetUse, 'tx') - tagPackets = SubElement(tagTx, 'packets', {'value':"0", 'type':'int'}) - tagBytes = SubElement(tagTx, 'bytes', {'value':"0", 'type':'int'}) - + tagMac = SubElement(mainTag, 'interfaces') + tagMacValue = SubElement(tagMac, 'interface', {'name':'', 'mac-addres':macAddress}) + + # Collect network usage + tagUseBandUsage = SubElement(mainTag, 'bandwidth-usage') + for t in glob.glob(PREVIOUSPATH + "\\*"): + traffic = file(t, "r") + trafficDate = traffic.readline().split("\n")[0] + dateTmp = datetime.strptime(trafficDate, '%Y-%m-%d') + intervalIdOld, rxPackageOld, rxBytesOld, txPackageOld, txBytesOld = traffic.readline().split("\n")[0].split(" ") + if dateTmp >= datetime.now() - timedelta(days=15): + for line in traffic: + + intervalId, rxPackageAux, rxBytesAux, txPackageAux, txBytesAux = line.split(" ") + txBytesAux = txBytesAux.split("\n")[0] + rxPackage = int(rxPackageAux) - int(rxPackageOld) + rxBytes = int(rxBytesAux) - int(rxBytesOld) + txPackage = int(txPackageAux) - int(txPackageOld) + txBytes = int(txBytesAux) - int(txBytesOld) + + if rxBytes >= 0 and txBytes >= 0: + # Since we want the mean time between the five minutes intervals, + # the minute counter starts at 2 and gets 5 minutes increments. + # Also the "seconds counter" is fixed at 30. So we always get + # something like HH:2:30 or HH:7:30 (mean times). + # + # Example: if interval = 10:15~10:20 => mean = 10:17:30 + intervalId = int(intervalId) + 1 + trafficTime = timedelta(minutes=intervalId * 5 - 3) + timedelta(seconds=30) + tagNetUse = SubElement(tagUseBandUsage, 'netuse', {'id':str(intervalId)}) + tagDate = SubElement(tagNetUse, 'date', {'value':trafficDate, 'type':'string'}) + tagTime = SubElement(tagNetUse, 'time', {'value':str(trafficTime), 'type':'string'}) + + tagRx = SubElement(tagNetUse, 'rx') + tagPackets = SubElement(tagRx, 'packets', {'value':str(rxPackage), 'type':'int'}) + tagBytes = SubElement(tagRx, 'bytes', {'value':str(rxBytes), 'type':'int'}) + + tagTx = SubElement(tagNetUse, 'tx') + tagPackets = SubElement(tagTx, 'packets', {'value':str(txPackage), 'type':'int'}) + tagBytes = SubElement(tagTx, 'bytes', {'value':str(txBytes), 'type':'int'}) + intervalIdOld, rxPackageOld, rxBytesOld, txPackageOld, txBytesOld = intervalId, rxPackageAux, rxBytesAux, txPackageAux, txBytesAux + traffic.close() + else: + traffic.close() + os.remove(t) return mainTag diff --git a/windows-collect/src/datasidAgent.py b/windows-collect/src/datasidAgent.py index d306579b2dac08e75eaec4f0618b48d72a05a89c..97c47f655932823d1847ef8ad18fb16ec3522329 100644 --- a/windows-collect/src/datasidAgent.py +++ b/windows-collect/src/datasidAgent.py @@ -1,7 +1,7 @@ -# Copyright (C) 2009-2012 Centro de Computacao Cientifica e Software Livre +# Copyright (C) 2013 Centro de Computacao Cientifica e Software Livre # Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR # -# This file is part of windows-coleta +# 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 @@ -25,17 +25,18 @@ import _winreg import shutil import subprocess import urllib -from time import localtime, strftime -from xml.etree.ElementTree import tostring import collect import glob +from time import localtime, strftime +from xml.etree.ElementTree import tostring # ========================================== # Constants # ========================================== # TODO: Check compatibility with 64bit Windows (Program Files(x86) folder) DATASIDPATH = os.environ["ProgramFiles"] + "\\DataSID" -URL = "http://bisimmcdev.c3sl.ufpr.br:8080/axis2/services/DataSID" +PREVIOUSPATH = DATASIDPATH + "\\data\\previous" +URL = "http://bisimmcdev.c3sl.ufpr.br/axis2/services/DataSID" # Log lifetime in days LOGLIFETIME = 30 @@ -147,9 +148,9 @@ def deleteOldLogs(): if fileDate <= ageLimit: os.remove(infile) -# Send the collect-data.xml -def sendData(log): - args = " --send-inventory " + '"' + DATASIDPATH + "\\data\\collect-data.xml" +'"'+ " --url=" + URL +# Send the collected data +def sendData(option, xmlFile): + args = option + '"' + DATASIDPATH + "\\data\\" + xmlFile +'"'+ " --url=" + URL (phost, pport, puser, ppass) = getProxyInfo() if phost != "": @@ -165,8 +166,8 @@ def sendData(log): return 0 # Create collect-data.xml file -def writeCollectData(collectData): - collectDataFile = file(DATASIDPATH + "\\data\\collect-data.xml", "w") +def writeCollectData(collectData, xmlName): + collectDataFile = file(DATASIDPATH + "\\data\\" + xmlName, "w") collectDataFile.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n") collectDataFile.write(tostring(collectData)) collectDataFile.close() @@ -238,8 +239,8 @@ if(updatelink): log.write("UPDATE: Updating from " + version + " to " + newVersion.strip() + ".") try: downloadUpdate(updatelink) - except: - log.write("ERROR(6): Failed to dowload update package.") + except Exception, e: + log.write("ERROR(6): Failed to dowload update package: " + str(e)) log.close() sys.exit(6) @@ -250,8 +251,8 @@ if(updatelink): log.write("UPDATE: Stopping for update process.") log.flush() callUpdater() - except: - log.write("ERROR(7): Failed to call the updater.") + except Exception, e: + log.write("ERROR(7): Failed to call the updater: " + str(e)) log.close() sys.exit(7) else: @@ -260,14 +261,14 @@ else: # Collect data try: collectData = collect.collect() -except: - log.write("ERROR(8): Failed to collect data.") +except Exception, e: + log.write("ERROR(8): Failed to collect data: " + str(e)) log.close() sys.exit(8) - + # Write the collect-data.xml file try: - writeCollectData(collectData) + writeCollectData(collectData, "collect-data.xml") except: log.write("ERROR(9): Failed to write collect-data.xml.") log.close() @@ -275,7 +276,7 @@ except: # Send data try: - sendData(log) + sendData(" --send-inventory ", "collect-data.xml") except clientError, e: log.write("ERROR(10): Failed to send data. Client returned " + str(e.code) + ".") log.write("Client message: " + str(e.errorMsg)) @@ -286,6 +287,35 @@ except: log.close() sys.exit(11) +# Collect net usage +if os.path.isdir(PREVIOUSPATH): + try: + netCollectData = collect.netCollect(log) + except Exception, e: + log.write("ERROR(12): Failed to collect net usage: " + str(e)) + log.close() + sys.exit(12) + # Write the collect-data.xml file + try: + writeCollectData(netCollectData, "net-collect-data.xml") + except: + log.write("ERROR(13): Failed to write net-collect-data.xml.") + log.close() + sys.exit(13) + + try: + sendData(" --send-net-usage ", "net-collect-data.xml") + shutil.rmtree(PREVIOUSPATH) + except clientError, e: + log.write("ERROR(10): Failed to send net usage data. Client returned " + str(e.code) + ".") + log.write("Client message: " + str(e.errorMsg)) + log.close() + sys.exit(10) + except: + log.write("ERROR(11): Failed to get the proxy information from Windows registry.") + log.close() + sys.exit(11) + # If everything ran ok, exit with success status log.write("EXIT(0): Success.") log.close()