サーバーにドメインを割り当てるスクリプトの作成

サーバーにドメインを割り当てるスクリプトの作成

2つのテキストファイルがあります。

ファイル1:ドメイン.txt

cocacola.com
airtel.com
pepsi.com

文書#2:サーバー.txt

192.0.53.42 , 4    # 4 domains already allocated on server 192.0.53.42
192.53.1.2 , 1     # 1 domains already allocated on server 192.53.1.2
192.36.155.21 , 2  # 2 domains already allocated on server  192.36.155.21

特定の瞬間に最小負荷を持つサーバー(〜)に各ドメイン(上から下に1つ)を割り当てるdomains.txtスクリプトを作成する必要がありますserver.txt(最小負荷が同点の場合、サーバーはFCFSとして割り当てられます)。 )。

最終的に上記のタスクを実行するスクリプトを作成し、次の名前の新しい修正ファイルを作成したいと思いますallocation.txt

上記の例では、allocation.txt出力は次のようになります(スクリプトの実行後)。

192.0.53.42 , 4
192.53.1.2 , 3 , cocacola.com , airtel.com
192.36.155.21 , 3 , pepsi.com

助けやご案内をいただきありがとうございます!

この問題を解決するための基本的なアプローチは何ですか?これはすべてスクリプトで実行できますか?

ベストアンサー1

次のPythonスクリプトは必要なタスクを実行する必要があります。

#!/usr/bin/python
serv=[]
for l in open("servers.txt","r").xreadlines():  # for each server
    s,n = l.split(",") # extract server name and load
    n=int(n.split("#")[0].strip()) # ignore comments
    serv.append([s.strip(),n]) # store server and its load
for l in open("domain.txt","r").xreadlines(): # for each domain
    m = serv.index(min(serv,key=lambda i:i[1])) # find server with lowest load
    serv[m].append(l.strip()) # add the domain
    serv[m][1]=serv[m][1]+1 # increase the load
alloc=open("allocation.txt","w")
for l in serv:
    print>>alloc, " , ".join([l[0],str(l[1])] + l[2:]) # write output file

おすすめ記事