def initialize(self): self.hostname = get_hostname() self.cycle = None self.dests = DestList() self.startCycle = 0 self.RAW_DIR = self.shared.parameters.RAW_DIR self.STATS_DIR = self.shared.parameters.STATS_DIR self.SAVE_RAW_TRACES = self.shared.parameters.SAVE_RW
def preload_destinations(self,_increment=False): try: log = logging.getLogger("agent.ping_cycle.preload") self.dests = DestList() #file_dest = open(self.iplist_file,'r') with open(self.shared.parameters.filePath +'/iplist.dat') as f: self.destination=[line.rstrip() for line in f] try: for dest_ip in self.destination: #if dest_id == self.hostname: self.shared.check_killed("Sender",self.shared.sender.event)# check Sendere received killing signal. if self.UPPER_LIMIT != -1 and self.nDests > self.UPPER_LIMIT-1: break index = dest_ip.find("/") try: if index != -1: # is it a subnet? socket.inet_aton(dest_ip[:index]) # check ip_validity net = self.networkMask(dest_ip[:index],int(dest_ip[(index+1):])) # get subnet address addr = net+1 # JUMP subnet address while self.addressInNetwork(addr,net,int(dest_ip[(index+1):])) and (self.UPPER_LIMIT == -1 or (self.UPPER_LIMIT != -1 and self.nDests < self.UPPER_LIMIT-1)): # is IP into subnet? if self.nDests >= self.LOWER_LIMIT-1: self.dests.insert(self.numToDottedQuad(addr)) self.nDests += 1 addr += 1 if self.addressInNetwork(addr,net,int(dest_ip[(index+1):]))==False: if self.nDests >= self.LOWER_LIMIT-1: self.dests.remove_last() # JUMP broadcast address self.nDests -= 1 # correct self.nDests else: if self.nDests >= self.LOWER_LIMIT-1: addr = socket.gethostbyname(dest_ip) # CHECK VALIDITY and (if necessary) convert NAME into IP self.dests.insert(addr) #-------------------> problem: takes a lot of time socket.gethostbyname(dest_ip) self.nDests += 1 except socket.error: if index != -1: log.error("Invalid IP: %s" % dest_ip[:index]) else: log.error("Invalid IP: %s" % dest_ip) #-------------------------------------------------for testing #b=time.time()-a # a time.time before try #if time.time()-a>1: # print dest_ip # print b #-------------------------------------------------for testing self.nDests = self.nDests - max(self.LOWER_LIMIT,1) + 1 log.info("Loaded targets: %d" % self.nDests) except why: #eliminare tutto i csv log.error("TracerouteCycle::preload_destinations(): file %s, %s" % (self.iplist_file, why)) except Exception, why: log.error("Exception preload destination:") log.error(why)
class PingCycleOPT: #------------------------------------------------------------------------ # ++ METHOD 01: INITIALIZATION #------------------------------------------------------------------------ def __init__(self,shared, deltaM, freq,_skt): self.countCycle = 0 # cycle ID NUMBER self.dM = deltaM # ping measurement interval self.f = freq # ping frequency self.dests = None # list of ping destinations self.nDests = 0 # number of destinations self.socket = _skt # socket reference self.seq = 0 # sequence number self.end_time = 0 # end of the current slot self.stop_cycle = 0 # end of the current cycle self.penalty = 0 # delay counter self.ANS_LIMIT = 10 # answer timeout [s] self.shared=shared # LOAD PARAMETERS self.parse_config() # load configuration self.preload_destinations() # load destinations list shared.destList=self.dests #update destination #------------------------------------------------------------------------ # ++ METHOD 02: PRELOAD DESTINATIONS #------------------------------------------------------------------------ def preload_destinations(self,_increment=False): try: log = logging.getLogger("agent.ping_cycle.preload") self.dests = DestList() #file_dest = open(self.iplist_file,'r') with open(self.shared.parameters.filePath +'/iplist.dat') as f: self.destination=[line.rstrip() for line in f] try: for dest_ip in self.destination: #if dest_id == self.hostname: self.shared.check_killed("Sender",self.shared.sender.event)# check Sendere received killing signal. if self.UPPER_LIMIT != -1 and self.nDests > self.UPPER_LIMIT-1: break index = dest_ip.find("/") try: if index != -1: # is it a subnet? socket.inet_aton(dest_ip[:index]) # check ip_validity net = self.networkMask(dest_ip[:index],int(dest_ip[(index+1):])) # get subnet address addr = net+1 # JUMP subnet address while self.addressInNetwork(addr,net,int(dest_ip[(index+1):])) and (self.UPPER_LIMIT == -1 or (self.UPPER_LIMIT != -1 and self.nDests < self.UPPER_LIMIT-1)): # is IP into subnet? if self.nDests >= self.LOWER_LIMIT-1: self.dests.insert(self.numToDottedQuad(addr)) self.nDests += 1 addr += 1 if self.addressInNetwork(addr,net,int(dest_ip[(index+1):]))==False: if self.nDests >= self.LOWER_LIMIT-1: self.dests.remove_last() # JUMP broadcast address self.nDests -= 1 # correct self.nDests else: if self.nDests >= self.LOWER_LIMIT-1: addr = socket.gethostbyname(dest_ip) # CHECK VALIDITY and (if necessary) convert NAME into IP self.dests.insert(addr) #-------------------> problem: takes a lot of time socket.gethostbyname(dest_ip) self.nDests += 1 except socket.error: if index != -1: log.error("Invalid IP: %s" % dest_ip[:index]) else: log.error("Invalid IP: %s" % dest_ip) #-------------------------------------------------for testing #b=time.time()-a # a time.time before try #if time.time()-a>1: # print dest_ip # print b #-------------------------------------------------for testing self.nDests = self.nDests - max(self.LOWER_LIMIT,1) + 1 log.info("Loaded targets: %d" % self.nDests) except why: #eliminare tutto i csv log.error("TracerouteCycle::preload_destinations(): file %s, %s" % (self.iplist_file, why)) except Exception, why: log.error("Exception preload destination:") log.error(why)
class PingManagerOPT(): def __init__(self, Shared): self.shared=Shared #------------------------------------------------------------------------ # STATE MACHINE #------------------------------------------------------------------------ def initialize(self): self.hostname = get_hostname() self.cycle = None self.dests = DestList() self.startCycle = 0 self.RAW_DIR = self.shared.parameters.RAW_DIR self.STATS_DIR = self.shared.parameters.STATS_DIR self.SAVE_RAW_TRACES = self.shared.parameters.SAVE_RW #----------------------------------------------------------------------------------------# #----> STATE 01 -> SOCKET_OPENING #----------------------------------------------------------------------------------------# def state_socket(self): self.dests.reset() # LOAD PARAMETERS self.parse_config() # SET HOSTNAME self.shared.hostname=self.hostname try: log = logging.getLogger("agent.ping_manager.socket") # SOCKET OPENING try: icmp = socket.getprotobyname('icmp') self.shared.socket = socket.socket(socket.AF_INET, socket.SOCK_RAW,icmp) log.info("Socket opened: %s" % self.shared.socket) except socket.error, (errno, msg): if errno == 1: log.error("%s -> ICMP messages can only be sent from processes running as root." % msg) else: log.error("Too many failures, aborting API calls.") print "Socket error, for more information check the log." #-------------------change with a function, we use the same in fastping.py------------------- self.shared.sender.event.set() #killing:exception open socket self.shared.uploaderCondition.acquire() self.shared.uploaderCondition.notifyAll() #wake up thread blocked on wait self.shared.uploaderCondition.release() # CREATE PING_CYCLE_OPT and LISTENER self.cycle = PingCycleOPT(self.shared, self.delta_M,self.ping_freq, self.shared.socket) #preload # START PING MEASUREMENT log.info("PingCycle created (agent_hostname: %s) - INTERVAL: %s" % (self.hostname, self.delta_M)) # SYNCHRONIZATION #wait_time = random.randint(1,int(self.delta_M.seconds/3)) # RANDOM WAIT #---------------------------AHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH wait_time=0 #manage wait time with the number of probe log.info("PingCycle synchronization - SLEEP: %f s" % wait_time) if(self.shared.sender.event.wait(wait_time)): #Reentrant lock: if there is a set, it will unlock and it will return true return # CREATE RAW_FILE r_file = None try: os.makedirs(self.RAW_DIR) log.info("Directory %s do not exists, created it." % self.RAW_DIR) except OSError, (errno, msg): if errno != 17: log.error("%s" % msg) sys.exit(1) self.startCycle = time.time() if self.SAVE_RAW_TRACES: filename = str(self.hostname)+"_"+str(int(self.startCycle))+".rw" r_file = open(os.path.join(self.RAW_DIR,filename),'w') r_file.write("#ID\tTARGET_IP\tSEQ_N\tCYCLE\tRTT\tTIME_RECEIVED\tTIME_SENT\n") try: os.makedirs(self.STATS_DIR) log.info("Directory %s do not exists, created it." % self.STATS_DIR) except OSError, (errno, msg): if errno != 17: log.error("%s" % msg) self.shared.sender.event.set() #killing:exception
class PingManagerOPT(): def __init__(self, Shared): self.shared = Shared #------------------------------------------------------------------------ # STATE MACHINE #------------------------------------------------------------------------ def initialize(self): self.hostname = get_hostname() self.cycle = None self.dests = DestList() self.startCycle = 0 self.RAW_DIR = self.shared.parameters.RAW_DIR self.STATS_DIR = self.shared.parameters.STATS_DIR self.SAVE_RAW_TRACES = self.shared.parameters.SAVE_RW #----------------------------------------------------------------------------------------# #----> STATE 01 -> SOCKET_OPENING #----------------------------------------------------------------------------------------# def state_socket(self): self.dests.reset() # LOAD PARAMETERS self.parse_config() # SET HOSTNAME self.shared.hostname = self.hostname try: log = logging.getLogger("agent.ping_manager.socket") # SOCKET OPENING try: icmp = socket.getprotobyname('icmp') self.shared.socket = socket.socket(socket.AF_INET, socket.SOCK_RAW, icmp) log.info("Socket opened: %s" % self.shared.socket) except socket.error, (errno, msg): if errno == 1: log.error( "%s -> ICMP messages can only be sent from processes running as root." % msg) else: log.error("Too many failures, aborting API calls.") print "Error: Socket error, for more information check the log." self.shared.sender.event.set() #killing:exception open socket # CREATE PING_CYCLE_OPT and LISTENER self.cycle = PingCycleOPT(self.shared, self.delta_M, self.ping_freq, self.shared.socket) #preload # START PING MEASUREMENT log.info("PingCycle created (agent_hostname: %s) - INTERVAL: %s" % (self.hostname, self.delta_M)) # SYNCHRONIZATION #wait_time = random.randint(1,int(self.delta_M.seconds/3)) # RANDOM WAIT #---------------------------AHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH wait_time = 1 log.info("PingCycle synchronization - SLEEP: %f s" % wait_time) if ( self.shared.sender.event.wait(wait_time) ): #Reentrant lock: if there is a set, it will unlock and it will return true return # CREATE RAW_FILE r_file = None try: os.makedirs(self.RAW_DIR) log.info("Directory %s do not exists, created it." % self.RAW_DIR) except OSError, (errno, msg): if errno != 17: log.error("%s" % msg) sys.exit(1) self.startCycle = time.time() if self.SAVE_RAW_TRACES: filename = str(self.hostname) + "_" + str(int( self.startCycle)) + ".rw" r_file = open(os.path.join(self.RAW_DIR, filename), 'w') r_file.write( "#ID\tTARGET_IP\tSEQ_N\tCYCLE\tRTT\tTIME_RECEIVED\tTIME_SENT\n" ) try: os.makedirs(self.STATS_DIR) log.info("Directory %s do not exists, created it." % self.STATS_DIR) except OSError, (errno, msg): if errno != 17: log.error("%s" % msg) self.shared.sender.event.set() #killing:exception
def preload_destinations(self, _increment=False): try: log = logging.getLogger("agent.ping_cycle.preload") self.dests = DestList() #file_dest = open(self.iplist_file,'r') with open('iplist.dat') as f: self.destination = [line.rstrip() for line in f] try: for dest_ip in self.destination: #if dest_id == self.hostname: self.shared.check_killed( "Sender", self.shared.sender.event ) # check Sendere received killing signal. if self.UPPER_LIMIT != -1 and self.nDests > self.UPPER_LIMIT - 1: break index = dest_ip.find("/") try: if index != -1: # is it a subnet? socket.inet_aton( dest_ip[:index]) # check ip_validity net = self.networkMask( dest_ip[:index], int(dest_ip[(index + 1):])) # get subnet address addr = net + 1 # JUMP subnet address while self.addressInNetwork( addr, net, int(dest_ip[(index + 1):]) ) and (self.UPPER_LIMIT == -1 or (self.UPPER_LIMIT != -1 and self.nDests < self.UPPER_LIMIT - 1) ): # is IP into subnet? if self.nDests >= self.LOWER_LIMIT - 1: self.dests.insert( self.numToDottedQuad(addr)) self.nDests += 1 addr += 1 if self.addressInNetwork( addr, net, int(dest_ip[(index + 1):])) == False: if self.nDests >= self.LOWER_LIMIT - 1: self.dests.remove_last( ) # JUMP broadcast address self.nDests -= 1 # correct self.nDests else: if self.nDests >= self.LOWER_LIMIT - 1: addr = socket.gethostbyname( dest_ip ) # CHECK VALIDITY and (if necessary) convert NAME into IP self.dests.insert( addr ) #-------------------> problem: takes a lot of time socket.gethostbyname(dest_ip) self.nDests += 1 except socket.error: if index != -1: log.error("Invalid IP: %s" % dest_ip[:index]) else: log.error("Invalid IP: %s" % dest_ip) #-------------------------------------------------for testing #b=time.time()-a # a time.time before try #if time.time()-a>1: # print dest_ip # print b #-------------------------------------------------for testing self.nDests = self.nDests - max(self.LOWER_LIMIT, 1) + 1 log.info("Loaded targets: %d" % self.nDests) except why: #eliminare tutto i csv log.error( "TracerouteCycle::preload_destinations(): file %s, %s" % (self.iplist_file, why)) except Exception, why: log.error("Exception preload destination:") log.error(why)
class PingCycleOPT: #------------------------------------------------------------------------ # ++ METHOD 01: INITIALIZATION #------------------------------------------------------------------------ def __init__(self, shared, deltaM, freq, _skt): self.countCycle = 0 # cycle ID NUMBER self.dM = deltaM # ping measurement interval self.f = freq # ping frequency self.dests = None # list of ping destinations self.nDests = 0 # number of destinations self.socket = _skt # socket reference self.seq = 0 # sequence number self.end_time = 0 # end of the current slot self.stop_cycle = 0 # end of the current cycle self.penalty = 0 # delay counter self.ANS_LIMIT = 10 # answer timeout [s] self.shared = shared # LOAD PARAMETERS self.parse_config() # load configuration self.preload_destinations() # load destinations list shared.destList = self.dests #update destination #------------------------------------------------------------------------ # ++ METHOD 02: PRELOAD DESTINATIONS #------------------------------------------------------------------------ def preload_destinations(self, _increment=False): try: log = logging.getLogger("agent.ping_cycle.preload") self.dests = DestList() #file_dest = open(self.iplist_file,'r') with open('iplist.dat') as f: self.destination = [line.rstrip() for line in f] try: for dest_ip in self.destination: #if dest_id == self.hostname: self.shared.check_killed( "Sender", self.shared.sender.event ) # check Sendere received killing signal. if self.UPPER_LIMIT != -1 and self.nDests > self.UPPER_LIMIT - 1: break index = dest_ip.find("/") try: if index != -1: # is it a subnet? socket.inet_aton( dest_ip[:index]) # check ip_validity net = self.networkMask( dest_ip[:index], int(dest_ip[(index + 1):])) # get subnet address addr = net + 1 # JUMP subnet address while self.addressInNetwork( addr, net, int(dest_ip[(index + 1):]) ) and (self.UPPER_LIMIT == -1 or (self.UPPER_LIMIT != -1 and self.nDests < self.UPPER_LIMIT - 1) ): # is IP into subnet? if self.nDests >= self.LOWER_LIMIT - 1: self.dests.insert( self.numToDottedQuad(addr)) self.nDests += 1 addr += 1 if self.addressInNetwork( addr, net, int(dest_ip[(index + 1):])) == False: if self.nDests >= self.LOWER_LIMIT - 1: self.dests.remove_last( ) # JUMP broadcast address self.nDests -= 1 # correct self.nDests else: if self.nDests >= self.LOWER_LIMIT - 1: addr = socket.gethostbyname( dest_ip ) # CHECK VALIDITY and (if necessary) convert NAME into IP self.dests.insert( addr ) #-------------------> problem: takes a lot of time socket.gethostbyname(dest_ip) self.nDests += 1 except socket.error: if index != -1: log.error("Invalid IP: %s" % dest_ip[:index]) else: log.error("Invalid IP: %s" % dest_ip) #-------------------------------------------------for testing #b=time.time()-a # a time.time before try #if time.time()-a>1: # print dest_ip # print b #-------------------------------------------------for testing self.nDests = self.nDests - max(self.LOWER_LIMIT, 1) + 1 log.info("Loaded targets: %d" % self.nDests) except why: #eliminare tutto i csv log.error( "TracerouteCycle::preload_destinations(): file %s, %s" % (self.iplist_file, why)) except Exception, why: log.error("Exception preload destination:") log.error(why)