def open_last_VFile(router):
    hostname = router.get_hostname()
    # Only the files related to that particular roter are considered. Then is taken the most recent
    bash_command = 'ls VFiles/| egrep "' + hostname + '"|tail -1'
    name_file = subprocess.check_output(bash_command, shell=True)
    name_file = name_file[:-1]  # The \n character won't be considered
    return name_file
def open_last_VFile(router):
	"""Given in input a router object, this function return the most recent VFile name corresponding to that router"""
	hostname=router.get_hostname()
               #Only the files related to that particular roter are considered. Then is taken the most recent
	bash_command='ls VFiles/| egrep "'+hostname+'"|tail -1'
	name_file=subprocess.check_output(bash_command, shell=True)
	name_file=name_file[:-1]#The \n character won't be considered
	return name_file
def get_utilization_polling(routers_list, how_much_often, community_name):
	"""Given in input the routers objects list, the interval period and the community name, this function prints out the utilization of each interface for each router. The method used to retrive such information is through SNMP protocol"""
	n_rel=0;
	in_utilization=0
	out_utilization=0

	while True:
		n_rel=n_rel+1;
		time.sleep(how_much_often);
		for router in routers_list:
			ifs_list=router.get_interfaces();
			print '\n###',n_rel, '(Router ',router.get_hostname(),')'
		
			timeUp=get_timeUp_SNMP(router, community_name)
			timeUp_diff=router.get_timeUp_diff(int(timeUp))

			for interface in ifs_list:
				if_id=int(interface.get_id())
				if_name=interface.get_name()
				if_speed=float(interface.get_if_speed())

				ifInBytes, ifOutBytes = get_in_out_bytes_SNMP(router.get_address(), if_id, community_name)
				delta_ifInBytes=int(ifInBytes)- interface.get_old_in_byte()
				delta_ifOutBytes=int(ifOutBytes)-interface.get_old_out_byte()
				debug=0
				if(debug):
					
					print 'Interface: ',interface.get_name()
					print '[NEW]','IN_Bytes: ', ifInBytes, 'OUT_Bytes: ',ifOutBytes, 'Time_Up: ', timeUp
					print '[OlD]','IN_Bytes: ', interface.get_old_in_byte(), 'OUT_Bytes: ',interface.get_old_out_byte(), 'Time_Up: ', router.get_old_timeUp()
					print 'Delta values: ', delta_ifInBytes, delta_ifOutBytes, timeUp_diff

				interface.set_old_in_byte(ifInBytes)
				interface.set_old_out_byte(ifOutBytes)
				##The utilization takes into account also the 	packets get from SNMP
				if(n_rel>1):
					format(in_utilization,'.3f')
					format(out_utilization,'.3f')
					in_utilization=((delta_ifInBytes)*8*100)/(if_speed*(float(timeUp_diff)/100));
					out_utilization=((delta_ifOutBytes)*8*100)/(if_speed*(float(timeUp_diff)/100));
				
				
				##Some times the shown utilization is bigger than one. This it could be due to buffer o burst packets incoming. In order to avoid this kind of situation, it will be one if it is greater than one. 
				
				#if (in_utilization>100): in_utilization=100
				#if (out_utilization>100): out_utilization=100
				

					print '(#', n_rel, ')', ' The  in utilization ', if_name,'interface (ID:', if_id,') is ', in_utilization, '% (if speed: ',if_speed,' )';
					print '(#', n_rel, ')', ' The  out utilization ', if_name,'interface (ID:', if_id,') is ', out_utilization, '% (if speed: ',if_speed,' )\n';
def get_utilization_polling(routers_list, how_much_often, community_name):
    n_rel = 0
    in_utilization = 0
    out_utilization = 0

    while True:
        n_rel = n_rel + 1
        time.sleep(how_much_often)
        for router in routers_list:
            ifs_list = router.get_interfaces()
            print "\n###", n_rel, "(Router ", router.get_hostname(), ")"

            timeUp = get_timeUp_SNMP(router, community_name)
            timeUp_diff = router.get_timeUp_diff(int(timeUp))

            for interface in ifs_list:
                if_id = int(interface.get_id())
                if_name = interface.get_name()
                if_speed = float(interface.get_if_speed())

                ifInBytes, ifOutBytes = get_in_out_bytes_SNMP(router.get_address(), if_id, community_name)
                delta_ifInBytes = int(ifInBytes) - interface.get_old_in_byte()
                delta_ifOutBytes = int(ifOutBytes) - interface.get_old_out_byte()
                debug = 0
                if debug:

                    print "Interface: ", interface.get_name()
                    print "[NEW]", "IN_Bytes: ", ifInBytes, "OUT_Bytes: ", ifOutBytes, "Time_Up: ", timeUp
                    print "[OlD]", "IN_Bytes: ", interface.get_old_in_byte(), "OUT_Bytes: ", interface.get_old_out_byte(), "Time_Up: ", router.get_old_timeUp()
                    print "Delta values: ", delta_ifInBytes, delta_ifOutBytes, timeUp_diff

                interface.set_old_in_byte(ifInBytes)
                interface.set_old_out_byte(ifOutBytes)
                ##The utilization takes into account also the 	packets get from SNMP
                if n_rel > 1:
                    format(in_utilization, ".3f")
                    format(out_utilization, ".3f")
                    in_utilization = ((delta_ifInBytes) * 8 * 100) / (if_speed * (timeUp_diff / 100))
                    out_utilization = ((delta_ifOutBytes) * 8 * 100) / (if_speed * (timeUp_diff / 100))

                    ##Some times the shown utilization is bigger than one. This it could be due to buffer o burst packets incoming. In order to avoid this kind of situation, it will be one if it is greater than one. It is correct this line of thinking ?

                    # if (in_utilization>100): in_utilization=100
                    # if (out_utilization>100): out_utilization=100

                    print "(#", n_rel, ")", " The  in utilization ", if_name, "interface (ID:", if_id, ") is ", in_utilization, "% (if speed: ", if_speed, " )"
                    print "(#", n_rel, ")", " The  out utilization ", if_name, "interface (ID:", if_id, ") is ", out_utilization, "% (if speed: ", if_speed, " )\n"
def get_utilization_single_router_polling(single_router, community_name):
	"""##Giving in input the router object and the community name, this funcion returns the utilization from a single router through SNMP polling"""
	router=single_router[0]
	n_gets=0
	in_utilization=0
	out_utilization=0
	print 'Router: ',router.get_hostname()

	while(n_gets<2):
		ifs_list=router.get_interfaces()
		timeUp=get_timeUp_SNMP(router, community_name)
		timeUp_diff=router.get_timeUp_diff(int(timeUp))
		
		for interface in ifs_list:##For each interface in the router will be get the number of input and output byes
			if_id=int(interface.get_id())
			if_name=interface.get_name()
			if_speed=int(interface.get_if_speed())
			ifInBytes, ifOutBytes = get_in_out_bytes_SNMP(router.get_address(), if_id, community_name)
			delta_ifInBytes=int(ifInBytes)- interface.get_old_in_byte()
			delta_ifOutBytes=int(ifOutBytes)-interface.get_old_out_byte()
			
			debug=0
			if(debug):
				print 'Interface: ',interface.get_name()
				print '[NEW]','IN_Bytes: ', ifInBytes, 'OUT_Bytes: ',ifOutBytes, 'Time_Up: ', timeUp
				print '[OlD]','IN_Bytes: ', interface.get_old_in_byte(), 'OUT_Bytes: ',interface.get_old_out_byte(), 'Time_Up: ', router.get_old_timeUp()
				print 'Delta values: ', delta_ifInBytes, delta_ifOutBytes, timeUp_diff

			interface.set_old_in_byte(ifInBytes)
			interface.set_old_out_byte(ifOutBytes)
		##The utilization takes into account also the packets get from SNMP
			format(in_utilization,'.3f')
			format(out_utilization,'.3f')
			in_utilization=(float(delta_ifInBytes)*8*100)/(if_speed*float(timeUp_diff)/100);
			out_utilization=(float(delta_ifOutBytes)*8*100)/(if_speed*float(timeUp_diff)/100);
			interface.set_in_out_utilization(in_utilization, out_utilization)
		n_gets=n_gets+1
		print '#####',n_gets
	router.set_interfaces(ifs_list)
	return router
def get_utilization_single_router_polling(single_router, community_name):
    router = single_router[0]
    n_gets = 0
    in_utilization = 0
    out_utilization = 0
    print "Router: ", router.get_hostname()

    while n_gets < 2:
        ifs_list = router.get_interfaces()
        timeUp = get_timeUp_SNMP(router, community_name)
        timeUp_diff = router.get_timeUp_diff(int(timeUp))

        for interface in ifs_list:  ##For each interface in the router will be get the number of input and output byes
            if_id = int(interface.get_id())
            if_name = interface.get_name()
            if_speed = int(interface.get_if_speed())
            ifInBytes, ifOutBytes = get_in_out_bytes_SNMP(router.get_address(), if_id, community_name)
            delta_ifInBytes = int(ifInBytes) - interface.get_old_in_byte()
            delta_ifOutBytes = int(ifOutBytes) - interface.get_old_out_byte()

            debug = 0
            if debug:
                print "Interface: ", interface.get_name()
                print "[NEW]", "IN_Bytes: ", ifInBytes, "OUT_Bytes: ", ifOutBytes, "Time_Up: ", timeUp
                print "[OlD]", "IN_Bytes: ", interface.get_old_in_byte(), "OUT_Bytes: ", interface.get_old_out_byte(), "Time_Up: ", router.get_old_timeUp()
                print "Delta values: ", delta_ifInBytes, delta_ifOutBytes, timeUp_diff

            interface.set_old_in_byte(ifInBytes)
            interface.set_old_out_byte(ifOutBytes)
            ##The utilization takes into account also the packets get from SNMP
            format(in_utilization, ".3f")
            format(out_utilization, ".3f")
            in_utilization = (float(delta_ifInBytes) * 8 * 100) / (if_speed * (timeUp_diff / 100))
            out_utilization = (float(delta_ifOutBytes) * 8 * 100) / (if_speed * (timeUp_diff / 100))
            interface.set_in_out_utilization(in_utilization, out_utilization)
        n_gets = n_gets + 1
        print "#####", n_gets
    router.set_interfaces(ifs_list)
    return router
def clear_VFiles(router):
    bash_command = "rm -f VFiles/bulktatistics_R" + router.get_hostname() + "*"
    print bash_command
    print subprocess.check_output(bash_command, shell=True)
def clear_VFiles(router):
	"""Given the object router in input, this function delete all corresponding VFiles """
	bash_command='rm -f VFiles/bulktatistics_R'+router.get_hostname()+'*'
	print bash_command
	print subprocess.check_output(bash_command, shell=True)