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 topology(your_ip,ip,community,username,password):
    #print "your ip : " + str(your_ip) +" ip device : " + str(ip) + " community : " + str(community)
    
    done_list = []
    notdone_list = []
    newData = []
    ipTraffic = []
    index = 0
    
    print "index : " + str(index)
    start_time = time.time()
    ## db topology
    collectionsName = router.name()
    coll = router.connectDatabase(collectionsName)
    filename = router.makeFile(collectionsName)
    ##

    ## db config
    config_name = collectionsName+"_config"
    coll_config = router.connectDatabase(config_name)
    coll_config.insert_one({"name":"none","date":collectionsName,"index":"0","traffic_index":"0","ip_traffic":"none","community":"none"})
    ##
    done_list.append(your_ip)
    ipTraffic.append(str(ip))

    print "done list : " + str(done_list)
    print "not done list : " + str(notdone_list)
    print "ip : " +str(ip)
    code = check.findCode(community,ip)
    if code == []:
        print "skip this ip"
        index -=1
    else:
        device = check.checkDevice(code)
        print "type : " + str(device)
        if device == "router":
            done_list,notdone_list,index = router.router(ip,done_list,notdone_list,filename,index,coll,ipTraffic,community)
        elif device == "switch":
            done_list,notdone_list,index = switch.switch(ip,done_list,notdone_list,filename,index,coll,ipTraffic,community,username,password)
        else:
            print "dont know the type of device"
    
        while(notdone_list):
            index += 1
            ip = notdone_list.pop()
            done_list.append(ip)
            
            print "index : " + str(index)
            print "ip " + str(ip)
            print "done list : " + str(done_list)
            print "not done list : " + str(notdone_list)
            code = check.findCode(community,ip)
            if code == []:
                print "skip this ip"
                index -=1
            else:
                device = check.checkDevice(code)
                print "type : " + str(device)
                if device == "router":
                    ipTraffic.append(ip)
                    done_list,notdone_list,index = router.router(ip,done_list,notdone_list,filename,index,coll,ipTraffic,community)
                elif device == "switch":
                    ipTraffic.append(ip)
                    done_list,notdone_list,index = switch.switch(ip,done_list,notdone_list,filename,index,coll,ipTraffic,community,username,password)
                else:
                    print "dont know the type of device"
            print index
    print("--- %s seconds ---" % (time.time() - start_time))
    return community,ipTraffic,collectionsName,config_name
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 topology(your_ip, ip, community, username, password):
    #print "your ip : " + str(your_ip) +" ip device : " + str(ip) + " community : " + str(community)

    done_list = []
    notdone_list = []
    newData = []
    ipTraffic = []
    index = 0

    print "index : " + str(index)
    start_time = time.time()
    ## db topology
    collectionsName = router.name()
    coll = router.connectDatabase(collectionsName)
    filename = router.makeFile(collectionsName)
    ##

    ## db config
    config_name = collectionsName + "_config"
    coll_config = router.connectDatabase(config_name)
    coll_config.insert_one({
        "name": "none",
        "date": collectionsName,
        "index": "0",
        "traffic_index": "0",
        "ip_traffic": "none",
        "community": "none"
    })
    ##
    done_list.append(your_ip)
    ipTraffic.append(str(ip))

    print "done list : " + str(done_list)
    print "not done list : " + str(notdone_list)
    print "ip : " + str(ip)
    code = check.findCode(community, ip)
    if code == []:
        print "skip this ip"
        index -= 1
    else:
        device = check.checkDevice(code)
        print "type : " + str(device)
        if device == "router":
            done_list, notdone_list, index = router.router(
                ip, done_list, notdone_list, filename, index, coll, ipTraffic,
                community)
        elif device == "switch":
            done_list, notdone_list, index = switch.switch(
                ip, done_list, notdone_list, filename, index, coll, ipTraffic,
                community, username, password)
        else:
            print "dont know the type of device"

        while (notdone_list):
            index += 1
            ip = notdone_list.pop()
            done_list.append(ip)

            print "index : " + str(index)
            print "ip " + str(ip)
            print "done list : " + str(done_list)
            print "not done list : " + str(notdone_list)
            code = check.findCode(community, ip)
            if code == []:
                print "skip this ip"
                index -= 1
            else:
                device = check.checkDevice(code)
                print "type : " + str(device)
                if device == "router":
                    ipTraffic.append(ip)
                    done_list, notdone_list, index = router.router(
                        ip, done_list, notdone_list, filename, index, coll,
                        ipTraffic, community)
                elif device == "switch":
                    ipTraffic.append(ip)
                    done_list, notdone_list, index = switch.switch(
                        ip, done_list, notdone_list, filename, index, coll,
                        ipTraffic, community, username, password)
                else:
                    print "dont know the type of device"
            print index
    print("--- %s seconds ---" % (time.time() - start_time))
    return community, ipTraffic, collectionsName, config_name
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)