def get_timeUp_SNMP(router, community_name):
	errorIndication, errorStatus, errorIndex, varBinds = next(
		    	getCmd(SnmpEngine(),CommunityData(community_name, mpModel=0),
			   	UdpTransportTarget((router.get_address(), 161)),#Address and port
			   	ContextData(),ObjectType(ObjectIdentity("1.3.6.1.2.1.1.3.0"))))#SysUptime

	if errorIndication:
		print(errorIndication)

	elif errorStatus:
		print('%s at %s' % (errorStatus.prettyPrint(),errorIndex and varBinds[int(errorIndex)-1][0] or '?'))

	timeUp=varBinds[0].prettyPrint().split("= ",1)[1];
	return timeUp
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_timeUp_SNMP(router, community_name):
	"""Given the router object in input and the community name, this function returns, through the SNMP protocol, the router system time up"""
	errorIndication, errorStatus, errorIndex, varBinds = next(
		    	getCmd(SnmpEngine(),CommunityData(community_name, mpModel=0),
			   	UdpTransportTarget((router.get_address(), SNMP_DEFAULT_PORT)),
			   	ContextData(),ObjectType(ObjectIdentity(OID_SYSTEM_TIMEUP))))#SysUptime

	if errorIndication:
		print(errorIndication)

	elif errorStatus:
		print('%s at %s' % (errorStatus.prettyPrint(),errorIndex and varBinds[int(errorIndex)-1][0] or '?'))

	timeUp=varBinds[0].prettyPrint().split("= ",1)[1];
	return timeUp
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