def ttttt(community,ipTraffic,collectionsName,indexTraffic):
    index = 0
    coll = router.connectDatabase(collectionsName + "_traffic" + "_" + indexTraffic)
    filename = "traffic_" + indexTraffic + "_" + router.makeFile(collectionsName)
    newTraffic = []
    for ip in ipTraffic:
        status = checkStatus(community,ip)
        interface = checkInterface(community,ip)

        start_time = time.time()
        In_1_data = traffIn(community,ip)
        Out_1_data = traffOut(community,ip)
        time.sleep(60)
        In_2_data = traffIn(community,ip)
        Out_2_data = traffOut(community,ip)
        t = time.time() - start_time

        ### Speed of each interface
        ifSpeed = ifSpeed(community,ip)

        form = {"index":str(index)}
        coll.insert_one(form)
        temp = 0
        
        for i in range(0,len(In_1_data)):
            if status[i] == "up(1)" and interface[i] != "Null0":
                ### *8 for bit and /1024*1024 for Mega
                print "i : " + str(i)
                In = ((int(In_2_data[i]) - int(In_1_data[i]))*8)/(t*1024*1024)
                Out = ((int(Out_2_data[i]) - int(Out_1_data[i]))*8)/(t*1024*1024)
                print "In : " + str(In) + " Mb"
                print "Out : " + str(Out) + " Mb"
                ### find Bandwith Usage in %
                Speed_In = In*100/ifSpeed[i]
                Speed_Out = Out*100/ifSpeed[i]

                newTraffic.append("index : " + str(index) + " = in " + str(In)+" , "+str(interface[i]) + "," +str(Speed_In))
                newTraffic.append("index : " + str(index) + " = out " + str(Out)+" , "+str(interface[i])+ "," +str(Speed_Out))
                coll.update({"index":str(index)},{'$set':{"BW_in"+str(temp):str(In)+","+str(interface[i])+ "," +str(Speed_In)}})
                coll.update({"index":str(index)},{'$set':{"BW_out"+str(temp):str(Out)+","+str(interface[i])+ "," +str(Speed_Out)}})

                temp +=1
        index += 1
    router.writeFile(newTraffic,filename)
def switch(ip,done_list,notdone_list,filename,index,coll,ipTraffic,community,username,password):
    
    name_data = router.reArrange(router.getData(router.command(community,ip,SNMPv2MIB_sysName)))
    detail_data = router.detail(router.getData(router.command(community,ip,SNMPv2MIB_sysDescr)))
    ip_data = router.reArrange(router.getData(router.command(community,ip,IPMIB_ipAdEntAddr)))
    subnet_data = router.reArrange(router.getData(router.command(community,ip,IPMIB_ipAdEntNetMask)))
    index_data = router.reArrange(router.getData(router.command(community,ip,IPMIB_ipAdEntIfIndex)))
    interface_data = router.reArrange(router.getData(router.command(community,ip,IFMIB_ifDescr)))


    ## new version
    #name_data_cdps = reArrange(getData(command(community,ip,SNMPv2MIB_sysName)))
    interface_data_cdps = cdps.New_reArrange_name(router.getData(router.command(community,ip,CISCOCDPMIB_cdpInterfaceName)))
    name_cdp_cdps = cdps.New_reArrange_cdpname(router.getData(router.command(community,ip,CISCOCDPMIB_cdpCacheDeviceId)))
    interface_cdps = cdps.New_reArrange_cdpinterface(router.getData(router.command(community,ip,CISCOCDPMIB_cdpCacheDevicePort)))
    ##

    name_cdp = router.reArrange(router.getData(router.command(community,ip,CISCOCDPMIB_cdpCacheDeviceId)))
    interface_cdp = router.reArrange(router.getData(router.command(community,ip,CISCOCDPMIB_cdpCacheDevicePort)))
    ip_cdp = router.reArrange(router.getData(router.command(community,ip,IPMIB_ipNetToMediaNetAddress)))

    ### forwarding blocking
    #stp_port = ps.reArrange(ps.getData(ps.command(community,ip,BRIDGEMIB_dot1dStpPort)))
    #stp_portstate = ps.New_reArrange(ps.getData(ps.command(community,ip,BRIDGEMIB_dot1dStpPortState)))
    #stp_portindex = ps.New_reArrange(ps.getData(ps.command(community,ip,BRIDGEMIB_dot1dBasePortIfIndex)))
    #ps_interface_data = ps.New_reArrange(router.getData(router.command(community,ip,IFMIB_ifDescr)))
    ###
    ### vlan vlan_name
    vtp_vlanname = vn.New_reArrange(router.getData(router.command(community,ip,CISCOVTPMIB_vtpVlanName)))
    ###
    ### interface vlan
    iv_interface_data = iv.New_reArrange(router.getData(router.command(community,ip,IFMIB_ifDescr)))
    trunk = iv.New_reArrange(router.getData(router.command(community,ip,CISCOVTPMIB_vlanTrunkPortDynamicStatus)))
    vlan_interface = iv.New_reArrange_vlan(router.getData(router.command(community,ip,CISCOSMI_ciscoMgmt)))
    ###

    #print "name : " + str(name_data)
    #print "detail : " + str(detail_data)
    #print "------------------------------------------------"
    #print "ip : " + str(ip_data)
    #print "subnet : " + str(subnet_data)
    #print "index : " + str(index_data)
    #print "interface_data : " + str(interface_data)
    #print "------------------------------------------------"
    #print "name of neighbors : " + str(name_cdp)
    #print "interface of neighbors : " + str(interface_cdp)
    #print "ip of neighbors : " + str(ip_cdp)
    #print "------------------------------------------------"
    #print "stp port : " + str(stp_port)
    #print "stp port state : " + str(stp_portstate)
    #print "stp port index : " + str(stp_portindex)
    #print "stp interface : " + str(ps_interface_data)
    #print "------------------------------------------------"
    #print "vlan name : " + str(vtp_vlanname)
    #print "------------------------------------------------"
    #print "interface : " + str(iv_interface_data)
    #print "trunk : " + str(trunk)
    #print "vlan : " + str(vlan_interface)
    #print "------------------------------------------------"

    #print stp_port #"1.3.6.1.2.1.17.2.15.1.1"
    #print stp_portstate #".1.3.6.1.2.1.17.2.15.1.3"
    #print stp_portindex #".1.3.6.1.2.1.17.1.4.1.2"
    #print vlan_index #.1.3.6.1.2.1.2.2.1.1
    #print vlan_interface #"1.3.6.1.4.1.9.9.46.1.3.1.1.4"
    #print interface_data #.1.3.6.1.2.1.2.2.1.2

    ## add to database : index,switch_name,detail
    for i in range(0,len(name_data)):
            form = {"index":str(index),"switch_name":str(name_data[i]),"detail":str(detail_data[i])}
            coll.insert_one(form)

    ## type
    type = []
    type.append("switch")
    coll.update({"index":str(index)},{'$set':{"type":"switch"}})

    ## vlan , vlan_name
    vlan_name = []
    vlan_name = vn.vlanname(vtp_vlanname)
    for i in range(0,len(vlan_name)):
        coll.update({"index":str(index)},{'$set':{"vlan_name"+str(i):str(vlan_name[i])}}) 
    
    ## vlan , ip , subnet
    newData = []
    for i in range(0,len(ip_data)):
        done_list.append(ip_data[i])
        newData.append("Vlan" + index_data[i] + "," + ip_data[i] + "," + subnet_data[i])
        coll.update({"index":str(index)},{'$set':{"vlan_ip"+str(i):str(newData[i])}}) 

    ## interface , vlan
    interface_vlan = []
    interface_vlan2 = []
    interface_vlan = iv.interfaceVlan(iv_interface_data,trunk,vlan_interface)
    for i in range(0,len(interface_vlan)):
        #print interface_vlan[i]
        interface_vlan2.append(interface_vlan[i])
        coll.update({"index":str(index)},{'$set':{"interface_vlan" + str(i):str(interface_vlan[i])}})
    #interface_vlan2 = []
    #interface_vlan2 = interface_vlan
 
    

    ### interface , state vlan1
    #interface_state = []
    #interface_state = ps.portState(stp_port,stp_portstate,stp_portindex,ps_interface_data)
    #print "state : " + str(interface_state)
    #for i in range(0,len(interface_state)):
    #    coll.update({"index":str(index)},{'$set':{"interface_state" + str(i):str(interface_state[i])}})

    ## interface , state vlan 2 up
    #interface_state = []
    interface_state_vlan = []
    interface_state_vlan = s.ssh_portstate(ip,username,password)
    #interface_state_vlan = t.port_state(interface_vlan,interface_state)
    for i in range(0,len(interface_state_vlan)):
        coll.update({"index":str(index)},{'$set':{"interface_state" + str(i):str(interface_state_vlan[i])}})



    ## donelist notdonelist
    done_list,notdone_list = fl.findlist(ip_data,ip_cdp,done_list,notdone_list)

    ## update cdp_interface
    newCDP = []
    for i in range(0,len(name_cdp)):
        newCDP.append(name_cdp[i] + "," + interface_cdp[i])
        coll.update({"index":str(index)},{'$set':{"cdp_interface"+str(i):str(newCDP[i])}}) 
    
    newCDPs = []
    newCDPs = cdps.New_CDPs(name_data,interface_data_cdps,name_cdp_cdps,interface_cdps)
    for i in range(0,len(newCDPs)):
        coll.update({"index":str(index)},{'$set':{"new_cdp"+str(i):str(newCDPs[i])}})

   #add X , Y for position
    #x = []
    #y = []
    #x.append("none")
    #y.append("none")
    coll.update({"index":str(index)},{'$set':{"location":"none"}})
    #coll.update({"index":str(index)},{'$set':{"y":"none"}})


    #print "done : " + str(done_list)
    #print "##############################"
    #print "Not donw : " + str(notdone_list)
    a = name_data + detail_data + type + vlan_name + newData + interface_vlan2 + interface_state_vlan + newCDP +newCDPs
    #a = name_data + detail_data + type + vlan_name + newData + interface_vlan  + newCDP
    router.writeFile(a,filename)
    return done_list,notdone_list,index
def switch(ip, done_list, notdone_list, filename, index, coll, ipTraffic,
           community, username, password):

    name_data = router.reArrange(
        router.getData(router.command(community, ip, SNMPv2MIB_sysName)))
    detail_data = router.detail(
        router.getData(router.command(community, ip, SNMPv2MIB_sysDescr)))
    ip_data = router.reArrange(
        router.getData(router.command(community, ip, IPMIB_ipAdEntAddr)))
    subnet_data = router.reArrange(
        router.getData(router.command(community, ip, IPMIB_ipAdEntNetMask)))
    index_data = router.reArrange(
        router.getData(router.command(community, ip, IPMIB_ipAdEntIfIndex)))
    interface_data = router.reArrange(
        router.getData(router.command(community, ip, IFMIB_ifDescr)))

    ## new version
    #name_data_cdps = reArrange(getData(command(community,ip,SNMPv2MIB_sysName)))
    interface_data_cdps = cdps.New_reArrange_name(
        router.getData(
            router.command(community, ip, CISCOCDPMIB_cdpInterfaceName)))
    name_cdp_cdps = cdps.New_reArrange_cdpname(
        router.getData(
            router.command(community, ip, CISCOCDPMIB_cdpCacheDeviceId)))
    interface_cdps = cdps.New_reArrange_cdpinterface(
        router.getData(
            router.command(community, ip, CISCOCDPMIB_cdpCacheDevicePort)))
    ##

    name_cdp = router.reArrange(
        router.getData(
            router.command(community, ip, CISCOCDPMIB_cdpCacheDeviceId)))
    interface_cdp = router.reArrange(
        router.getData(
            router.command(community, ip, CISCOCDPMIB_cdpCacheDevicePort)))
    ip_cdp = router.reArrange(
        router.getData(
            router.command(community, ip, IPMIB_ipNetToMediaNetAddress)))

    ### forwarding blocking
    #stp_port = ps.reArrange(ps.getData(ps.command(community,ip,BRIDGEMIB_dot1dStpPort)))
    #stp_portstate = ps.New_reArrange(ps.getData(ps.command(community,ip,BRIDGEMIB_dot1dStpPortState)))
    #stp_portindex = ps.New_reArrange(ps.getData(ps.command(community,ip,BRIDGEMIB_dot1dBasePortIfIndex)))
    #ps_interface_data = ps.New_reArrange(router.getData(router.command(community,ip,IFMIB_ifDescr)))
    ###
    ### vlan vlan_name
    vtp_vlanname = vn.New_reArrange(
        router.getData(router.command(community, ip, CISCOVTPMIB_vtpVlanName)))
    ###
    ### interface vlan
    iv_interface_data = iv.New_reArrange(
        router.getData(router.command(community, ip, IFMIB_ifDescr)))
    trunk = iv.New_reArrange(
        router.getData(
            router.command(community, ip,
                           CISCOVTPMIB_vlanTrunkPortDynamicStatus)))
    vlan_interface = iv.New_reArrange_vlan(
        router.getData(router.command(community, ip, CISCOSMI_ciscoMgmt)))
    ###

    #print "name : " + str(name_data)
    #print "detail : " + str(detail_data)
    #print "------------------------------------------------"
    #print "ip : " + str(ip_data)
    #print "subnet : " + str(subnet_data)
    #print "index : " + str(index_data)
    #print "interface_data : " + str(interface_data)
    #print "------------------------------------------------"
    #print "name of neighbors : " + str(name_cdp)
    #print "interface of neighbors : " + str(interface_cdp)
    #print "ip of neighbors : " + str(ip_cdp)
    #print "------------------------------------------------"
    #print "stp port : " + str(stp_port)
    #print "stp port state : " + str(stp_portstate)
    #print "stp port index : " + str(stp_portindex)
    #print "stp interface : " + str(ps_interface_data)
    #print "------------------------------------------------"
    #print "vlan name : " + str(vtp_vlanname)
    #print "------------------------------------------------"
    #print "interface : " + str(iv_interface_data)
    #print "trunk : " + str(trunk)
    #print "vlan : " + str(vlan_interface)
    #print "------------------------------------------------"

    #print stp_port #"1.3.6.1.2.1.17.2.15.1.1"
    #print stp_portstate #".1.3.6.1.2.1.17.2.15.1.3"
    #print stp_portindex #".1.3.6.1.2.1.17.1.4.1.2"
    #print vlan_index #.1.3.6.1.2.1.2.2.1.1
    #print vlan_interface #"1.3.6.1.4.1.9.9.46.1.3.1.1.4"
    #print interface_data #.1.3.6.1.2.1.2.2.1.2

    ## add to database : index,switch_name,detail
    for i in range(0, len(name_data)):
        form = {
            "index": str(index),
            "switch_name": str(name_data[i]),
            "detail": str(detail_data[i])
        }
        coll.insert_one(form)

    ## type
    type = []
    type.append("switch")
    coll.update({"index": str(index)}, {'$set': {"type": "switch"}})

    ## vlan , vlan_name
    vlan_name = []
    vlan_name = vn.vlanname(vtp_vlanname)
    for i in range(0, len(vlan_name)):
        coll.update({"index": str(index)},
                    {'$set': {
                        "vlan_name" + str(i): str(vlan_name[i])
                    }})

    ## vlan , ip , subnet
    newData = []
    for i in range(0, len(ip_data)):
        done_list.append(ip_data[i])
        newData.append("Vlan" + index_data[i] + "," + ip_data[i] + "," +
                       subnet_data[i])
        coll.update({"index": str(index)},
                    {'$set': {
                        "vlan_ip" + str(i): str(newData[i])
                    }})

    ## interface , vlan
    interface_vlan = []
    interface_vlan2 = []
    interface_vlan = iv.interfaceVlan(iv_interface_data, trunk, vlan_interface)
    for i in range(0, len(interface_vlan)):
        #print interface_vlan[i]
        interface_vlan2.append(interface_vlan[i])
        coll.update(
            {"index": str(index)},
            {'$set': {
                "interface_vlan" + str(i): str(interface_vlan[i])
            }})
    #interface_vlan2 = []
    #interface_vlan2 = interface_vlan

    ### interface , state vlan1
    #interface_state = []
    #interface_state = ps.portState(stp_port,stp_portstate,stp_portindex,ps_interface_data)
    #print "state : " + str(interface_state)
    #for i in range(0,len(interface_state)):
    #    coll.update({"index":str(index)},{'$set':{"interface_state" + str(i):str(interface_state[i])}})

    ## interface , state vlan 2 up
    #interface_state = []
    interface_state_vlan = []
    interface_state_vlan = s.ssh_portstate(ip, username, password)
    #interface_state_vlan = t.port_state(interface_vlan,interface_state)
    for i in range(0, len(interface_state_vlan)):
        coll.update({"index": str(index)}, {
            '$set': {
                "interface_state" + str(i): str(interface_state_vlan[i])
            }
        })

    ## donelist notdonelist
    done_list, notdone_list = fl.findlist(ip_data, ip_cdp, done_list,
                                          notdone_list)

    ## update cdp_interface
    newCDP = []
    for i in range(0, len(name_cdp)):
        newCDP.append(name_cdp[i] + "," + interface_cdp[i])
        coll.update({"index": str(index)},
                    {'$set': {
                        "cdp_interface" + str(i): str(newCDP[i])
                    }})

    newCDPs = []
    newCDPs = cdps.New_CDPs(name_data, interface_data_cdps, name_cdp_cdps,
                            interface_cdps)
    for i in range(0, len(newCDPs)):
        coll.update({"index": str(index)},
                    {'$set': {
                        "new_cdp" + str(i): str(newCDPs[i])
                    }})

#add X , Y for position
#x = []
#y = []
#x.append("none")
#y.append("none")
    coll.update({"index": str(index)}, {'$set': {"location": "none"}})
    #coll.update({"index":str(index)},{'$set':{"y":"none"}})

    #print "done : " + str(done_list)
    #print "##############################"
    #print "Not donw : " + str(notdone_list)
    a = name_data + detail_data + type + vlan_name + newData + interface_vlan2 + interface_state_vlan + newCDP + newCDPs
    #a = name_data + detail_data + type + vlan_name + newData + interface_vlan  + newCDP
    router.writeFile(a, filename)
    return done_list, notdone_list, index
def ttttt(community, ipTraffic, collectionsName, indexTraffic):
    index = 0
    coll = router.connectDatabase(collectionsName + "_traffic" + "_" +
                                  indexTraffic)
    filename = "traffic_" + indexTraffic + "_" + router.makeFile(
        collectionsName)
    newTraffic = []
    for ip in ipTraffic:
        status = checkStatus(community, ip)
        interface = checkInterface(community, ip)

        start_time = time.time()
        In_1_data = traffIn(community, ip)
        Out_1_data = traffOut(community, ip)
        time.sleep(60)
        In_2_data = traffIn(community, ip)
        Out_2_data = traffOut(community, ip)
        t = time.time() - start_time

        ### Speed of each interface
        ifSpeed = ifSpeed(community, ip)

        form = {"index": str(index)}
        coll.insert_one(form)
        temp = 0

        for i in range(0, len(In_1_data)):
            if status[i] == "up(1)" and interface[i] != "Null0":
                ### *8 for bit and /1024*1024 for Mega
                print "i : " + str(i)
                In = ((int(In_2_data[i]) - int(In_1_data[i])) *
                      8) / (t * 1024 * 1024)
                Out = ((int(Out_2_data[i]) - int(Out_1_data[i])) *
                       8) / (t * 1024 * 1024)
                print "In : " + str(In) + " Mb"
                print "Out : " + str(Out) + " Mb"
                ### find Bandwith Usage in %
                Speed_In = In * 100 / ifSpeed[i]
                Speed_Out = Out * 100 / ifSpeed[i]

                newTraffic.append("index : " + str(index) + " = in " +
                                  str(In) + " , " + str(interface[i]) + "," +
                                  str(Speed_In))
                newTraffic.append("index : " + str(index) + " = out " +
                                  str(Out) + " , " + str(interface[i]) + "," +
                                  str(Speed_Out))
                coll.update({"index": str(index)}, {
                    '$set': {
                        "BW_in" + str(temp):
                        str(In) + "," + str(interface[i]) + "," + str(Speed_In)
                    }
                })
                coll.update({"index": str(index)}, {
                    '$set': {
                        "BW_out" + str(temp):
                        str(Out) + "," + str(interface[i]) + "," +
                        str(Speed_Out)
                    }
                })

                temp += 1
        index += 1
    router.writeFile(newTraffic, filename)
def traffic(community, ipTraffic, collectionsName, indexTraffic):
    index = 0

    coll = router.connectDatabase(collectionsName + "_traffic_" +
                                  str(indexTraffic))
    filename = "traffic_" + str(indexTraffic) + "_" + router.makeFile(
        collectionsName)
    newTraffic = []
    for ip in ipTraffic:
        if_Speed = ifSpeed(community, ip)
        check_Status = checkStatus(community, ip)
        check_Interface = checkInterface(community, ip)
        #packet_Size = packetSize(community,ip)
        r, c = getRowCol(check_Interface)
        ## data_time for traffic_config
        date_time = datetime.now()
        date_time = date_time.strftime('%a%d%b%Y_%H%M%S')
        ##
        start_time = time.time()
        trafficIn_1 = traffIn(community, ip)
        trafficOut_1 = traffOut(community, ip)
        #print "traffic in 1 " ,trafficIn_1
        #print "traffic out 1 " ,trafficOut_1
        time.sleep(1)
        trafficIn_2 = traffIn(community, ip)
        trafficOut_2 = traffOut(community, ip)
        #print "traffic in 2 " ,trafficIn_2
        #print "traffic out 2 " ,trafficOut_2
        delta_time = time.time() - start_time
        ## make information
        form = {"index": str(index)}
        coll.insert_one(form)
        ##
        temp = 0

        print "index : " + str(index)
        for i in range(0, r):
            if check_Status[i][
                    1] == "up(1)" and check_Interface[i][1] != "Null0":

                #print check_Interface[i][1]
                ### *8 for bit and /1024*1024 for Mega
                #print "-----------------------------------in2 - in1 :" , int(trafficIn_2[i][1]) - int(trafficIn_1[i][1])
                #print "-----------------------------------out2 - out1 :" , int(trafficOut_2[i][1]) - int(trafficOut_1[i][1])

                In = round(((float(trafficIn_2[i][1]) * 8) -
                            (float(trafficIn_1[i][1]) * 8)) /
                           (delta_time * 1024 * 1024), 2)
                Out = round(((float(trafficOut_2[i][1]) * 8) -
                             (float(trafficOut_1[i][1]) * 8)) /
                            (delta_time * 1024 * 1024), 2)
                #In = ((float(trafficIn_2[i][1]) - float(trafficIn_1[i][1])))/(delta_time)
                #Out = ((float(trafficOut_2[i][1]) - float(trafficOut_1[i][1])))/(delta_time)

                print "---------------------------------------------------"

                print "In b/s : " + str(In * 1024 * 1024)
                print "Out b/s : " + str(Out * 1024 * 1024)
                print "total : " + str((In + Out) * 1024 * 1024)
                print "---------------------------------------------------"
                print "---------------------------------------------------"
                print "In Mb/s : " + str(In)
                print "Out Mb/s : " + str(Out)
                print "total : " + str((In + Out))
                print "---------------------------------------------------"
                print "---------------------------------------------------"
                print "BW in : " + str(if_Speed[i][1])
                print "BW out : " + str(if_Speed[i][1])
                print "---------------------------------------------------"

                #print check_Interface[2][1]

                ## for test
                #In_a = ((float(trafficIn_2[2][1]) - float(trafficIn_1[2][1])))/(delta_time)
                #Out_a = ((float(trafficOut_2[2][1]) - float(trafficOut_1[2][1])))/(delta_time)

                ### find Bandwith Usage in %
                #print In,Out
                Speed_In = round(In * 100 * 1024 * 1024 / int(if_Speed[i][1]))
                Speed_Out = round(Out * 100 * 1024 * 1024 /
                                  int(if_Speed[i][1]))

                #Speed_In = round(Speed_In,2)
                #Speed_Out = round(Speed_Out,2)
                #print Speed_In,Speed_Out

                #newTraffic.append("index : " + str(index) + " = in " + str(In)+" , "+str(check_Interface[i][1]) + "," +str(Speed_In))
                #newTraffic.append("index : " + str(index) + " = out " + str(Out)+" , "+str(check_Interface[i][1])+ "," +str(Speed_Out))

                newTraffic.append("index : " + str(index) + "," +
                                  str(check_Interface[i][1]) + "," + str(In) +
                                  "," + str(Speed_In) + "%")
                newTraffic.append("index : " + str(index) + "," +
                                  str(check_Interface[i][1]) + "," + str(Out) +
                                  "," + str(Speed_Out) + "%")
                coll.update({"index": str(index)}, {
                    '$set': {
                        "BW_in" + str(temp):
                        str(check_Interface[i][1]) + "," + str(In) + "," +
                        str(Speed_In) + "%"
                    }
                })
                coll.update({"index": str(index)}, {
                    '$set': {
                        "BW_out" + str(temp):
                        str(check_Interface[i][1]) + "," + str(Out) + "," +
                        str(Speed_Out) + "%"
                    }
                })

                temp += 1
        index += 1
        print "\n"
        #print check_Interface[2][1]
        #In_a = ((float(trafficIn_2[2][1]) - float(trafficIn_1[2][1])))/(delta_time)
        #Out_a = ((float(trafficOut_2[2][1]) - float(trafficOut_1[2][1])))/(delta_time)
        #avg = avg + In_a + Out_a
        #avg = avg/n
        #print "Avg : " +str(avg)
    indexTraffic = int(indexTraffic) + 1  # +1 = 5 min
    router.writeFile(newTraffic, filename)
    return indexTraffic, date_time


#avg = 0
#x = 0
#avg_all = []
#while (True):

#    ipTraffic = []
#    ipTraffic.append(ip)
#    collectionsName = "testping"
#    indexTraffic = 0

#    in_a,out_a =traffic(community,ipTraffic,collectionsName,indexTraffic)
#    print in_a
#    print out_a
#    data = (in_a + out_a)
#    avg_all.append(data)

#    for i in range(0,len(avg_all)):
#        x += avg_all[i]
#    x = x/len(avg_all)
#    print "Avg : " +str(x)

#if_Speed = ifSpeed(community,ip)
#print if_Speed

#trafficIn = traffIn(community,ipTraffic)
#print trafficIn
#traffOut = traffOut(community,ipTraffic)
#print traffOut
#ifSpeed = ifSpeed(community,ipTraffic)
#print ifSpeed
#checkStatus = checkStatus(community,ipTraffic)
#print checkStatus
#checkInterface = checkInterface(community,ipTraffic)
#print checkInterface
#packetSize = packetSize(community,ipTraffic)
#print packetSize

#print getRowCol(trafficIn)
#print getRowCol(traffOut)
#print getRowCol(ifSpeed)
#print getRowCol(checkStatus)
#r,c = getRowCol(checkInterface)
#print getRowCol(packetSize)
def traffic(community,ipTraffic,collectionsName,indexTraffic):
    index = 0
    
    coll = router.connectDatabase(collectionsName + "_traffic_" + str(indexTraffic))
    filename = "traffic_" + str(indexTraffic) + "_" + router.makeFile(collectionsName)
    newTraffic = []
    for ip in ipTraffic:
        if_Speed = ifSpeed(community,ip)
        check_Status = checkStatus(community,ip)
        check_Interface = checkInterface(community,ip)
        #packet_Size = packetSize(community,ip)
        r,c = getRowCol(check_Interface)
        ## data_time for traffic_config
        date_time = datetime.now()
        date_time = date_time.strftime('%a%d%b%Y_%H%M%S')
        ##
        start_time = time.time()
        trafficIn_1 = traffIn(community,ip)
        trafficOut_1 = traffOut(community,ip)
        #print "traffic in 1 " ,trafficIn_1
        #print "traffic out 1 " ,trafficOut_1
        time.sleep(1)
        trafficIn_2 = traffIn(community,ip)
        trafficOut_2 = traffOut(community,ip)
        #print "traffic in 2 " ,trafficIn_2
        #print "traffic out 2 " ,trafficOut_2
        delta_time = time.time() - start_time
        ## make information
        form = {"index":str(index)}
        coll.insert_one(form)
        ##
        temp = 0
        
        print "index : " + str(index)
        for i in range(0,r):
            if check_Status[i][1] == "up(1)" and check_Interface[i][1] !="Null0":
                
                #print check_Interface[i][1]
                ### *8 for bit and /1024*1024 for Mega
                #print "-----------------------------------in2 - in1 :" , int(trafficIn_2[i][1]) - int(trafficIn_1[i][1])
                #print "-----------------------------------out2 - out1 :" , int(trafficOut_2[i][1]) - int(trafficOut_1[i][1])

                In = round(((float(trafficIn_2[i][1])*8) - (float(trafficIn_1[i][1])*8))/(delta_time*1024*1024),2)
                Out = round(((float(trafficOut_2[i][1])*8) - (float(trafficOut_1[i][1])*8))/(delta_time*1024*1024),2)
                #In = ((float(trafficIn_2[i][1]) - float(trafficIn_1[i][1])))/(delta_time)
                #Out = ((float(trafficOut_2[i][1]) - float(trafficOut_1[i][1])))/(delta_time)

                print "---------------------------------------------------"

                print "In b/s : " + str(In*1024*1024)
                print "Out b/s : " + str(Out*1024*1024)
                print "total : " +str((In+Out)*1024*1024)
                print "---------------------------------------------------"
                print "---------------------------------------------------"
                print "In Mb/s : " + str(In)
                print "Out Mb/s : " + str(Out)
                print "total : " +str((In+Out))
                print "---------------------------------------------------"
                print "---------------------------------------------------"
                print "BW in : " + str(if_Speed[i][1])
                print "BW out : " + str(if_Speed[i][1])
                print "---------------------------------------------------"

                #print check_Interface[2][1]

                ## for test
                #In_a = ((float(trafficIn_2[2][1]) - float(trafficIn_1[2][1])))/(delta_time)
                #Out_a = ((float(trafficOut_2[2][1]) - float(trafficOut_1[2][1])))/(delta_time)
                

                ### find Bandwith Usage in %
                #print In,Out
                Speed_In = round(In*100*1024*1024/int(if_Speed[i][1]))
                Speed_Out = round(Out*100*1024*1024/int(if_Speed[i][1]))


                #Speed_In = round(Speed_In,2)
                #Speed_Out = round(Speed_Out,2)
                #print Speed_In,Speed_Out

                #newTraffic.append("index : " + str(index) + " = in " + str(In)+" , "+str(check_Interface[i][1]) + "," +str(Speed_In))
                #newTraffic.append("index : " + str(index) + " = out " + str(Out)+" , "+str(check_Interface[i][1])+ "," +str(Speed_Out))


                newTraffic.append("index : " + str(index) + "," +str(check_Interface[i][1]) +"," + str(In)+","+ str(Speed_In)+"%")
                newTraffic.append("index : " + str(index) + "," +str(check_Interface[i][1]) +"," + str(Out)+","+ str(Speed_Out)+"%")
                coll.update({"index":str(index)},{'$set':{"BW_in"+str(temp):str(check_Interface[i][1]) + "," + str(In) + "," + str(Speed_In)+"%"}})
                coll.update({"index":str(index)},{'$set':{"BW_out"+str(temp):str(check_Interface[i][1]) + "," + str(Out) + "," + str(Speed_Out)+"%"}})

                temp +=1
        index +=1
        print "\n"
        #print check_Interface[2][1]
        #In_a = ((float(trafficIn_2[2][1]) - float(trafficIn_1[2][1])))/(delta_time)
        #Out_a = ((float(trafficOut_2[2][1]) - float(trafficOut_1[2][1])))/(delta_time)
        #avg = avg + In_a + Out_a
        #avg = avg/n
        #print "Avg : " +str(avg)
    indexTraffic = int(indexTraffic) + 1 # +1 = 5 min
    router.writeFile(newTraffic,filename)
    return indexTraffic,date_time


#avg = 0
#x = 0
#avg_all = []
#while (True):

#    ipTraffic = []
#    ipTraffic.append(ip)
#    collectionsName = "testping"
#    indexTraffic = 0
    
#    in_a,out_a =traffic(community,ipTraffic,collectionsName,indexTraffic)
#    print in_a
#    print out_a
#    data = (in_a + out_a)
#    avg_all.append(data)

#    for i in range(0,len(avg_all)):
#        x += avg_all[i]
#    x = x/len(avg_all)
#    print "Avg : " +str(x)
    

#if_Speed = ifSpeed(community,ip)
#print if_Speed

#trafficIn = traffIn(community,ipTraffic)
#print trafficIn
#traffOut = traffOut(community,ipTraffic)
#print traffOut
#ifSpeed = ifSpeed(community,ipTraffic)
#print ifSpeed
#checkStatus = checkStatus(community,ipTraffic)
#print checkStatus
#checkInterface = checkInterface(community,ipTraffic)
#print checkInterface
#packetSize = packetSize(community,ipTraffic)
#print packetSize


#print getRowCol(trafficIn)
#print getRowCol(traffOut)
#print getRowCol(ifSpeed)
#print getRowCol(checkStatus)
#r,c = getRowCol(checkInterface)
#print getRowCol(packetSize)