88import configparser
99import subprocess
1010import re
11+ import argparse
1112
1213DingoFS_TOOL = "dingo"
1314HOSTNAME_PORT_REGEX = r"[^\"\ ]\S*:\d+"
1415IP_PORT_REGEX = r"[0-9]+(?:\.[0-9]+){3}:\d+"
1516
1617targetPath = None
17- etcdTargetPath = None
1818
1919def loadConf ():
2020 global targetPath
21- global etcdTargetPath
2221 conf = configparser .ConfigParser ()
2322 conf .read ("target.ini" )
2423 targetPath = conf .get ("path" , "target_path" )
25- etcdTargetPath = conf .get ("path" , "etcd_target_path" )
2624
2725def runDingofsToolCommand (command ):
2826 cmd = [DingoFS_TOOL ]+ command
@@ -41,63 +39,37 @@ def loadMdsServer():
4139 label = lablesValue (None , "mds" )
4240 if ret == 0 :
4341 data = json .loads (output )
44- for mdsInfo in data ["result" ]:
45- # hostname:port:path
46- dummyAddr = mdsInfo ["dummyAddr " ]
47- mdsServers .append (dummyAddr )
42+ result = data ["result" ]
43+ for mdsInfo in result [ "mdses" ]:
44+ location = mdsInfo ["location " ]
45+ mdsServers .append (ipPort2Addr ( location [ "host" ], location [ "port" ]) )
4846 return unitValue (label , mdsServers )
4947
50- def loadMetaServer ():
51- ret , data = runDingofsToolCommand (["list" , "topology" ,"--format=json" ])
52- if ret == 0 :
53- jsonData = json .loads (data )
54- jsonData = jsonData ["result" ]
55-
56- metaservers = []
57- if jsonData is not None :
58- for pool in jsonData ["poollist" ]:
59- for zone in pool ["zoneList" ]:
60- for server in zone ["serverList" ]:
61- for metaserver in server ["metaserverList" ]:
62- metaservers .append (metaserver )
63- targets = []
64- labels = lablesValue (None , "metaserver" )
65- for server in metaservers :
66- targets .append (ipPort2Addr (server ["externalIp" ], server ["externalPort" ]))
67- targets = list (set (targets ))
68- return unitValue (labels , targets )
69-
70- def loadEtcdServer ():
71- ret , output = runDingofsToolCommand (["status" ,"etcd" ,"--format=json" ])
72- etcdServers = []
73- label = lablesValue (None , "etcd" )
74- if ret == 0 :
75- data = json .loads (output )
76- for etcdInfo in data ["result" ]:
77- etcdAddr = etcdInfo ["addr" ]
78- etcdServers .append (etcdAddr )
79- return unitValue (label , etcdServers )
80-
8148def loadClient ():
8249 ret , output = runDingofsToolCommand (["list" ,"mountpoint" ,"--format=json" ])
8350 clients = []
8451 label = lablesValue (None , "client" )
8552 if ret == 0 :
8653 data = json .loads (output )
87- for fsinfo in data ["result" ]:
88- # hostname:port:path
89- mountpoint = str (fsinfo ["mountpoint" ])
90- muontListData = mountpoint .split (":" )
91- clients .append (muontListData [0 ] + ":" + muontListData [1 ])
54+ result = data ["result" ]
55+ for fsinfo in result ["fsInfos" ]:
56+ mountPoints = fsinfo .get ("mountPoints" )
57+ if mountPoints is None :
58+ continue
59+ for mountpoint in mountPoints :
60+ clients .append (ipPort2Addr (mountpoint ["hostname" ],mountpoint ["port" ]))
9261 return unitValue (label , clients )
9362
94- def loadType (hostType ):
95- ret , output = runDingofsToolCommand (["status-%s" % hostType ])
96- targets = []
97- if ret == 0 :
98- targets = re .findall (IP_PORT_REGEX , str (output ))
99- labels = lablesValue (None , hostType )
100- return unitValue (labels , targets )
63+ def loadRemoteCacheServer ():
64+ ret , output = runDingofsToolCommand (["list" ,"cachemember" ,"--format=json" ])
65+ cacheServers = []
66+ label = lablesValue (None , "remotecache" )
67+ if ret == 0 :
68+ data = json .loads (output )
69+ result = data ["result" ]
70+ for cacheMember in result ["members" ]:
71+ cacheServers .append (ipPort2Addr (cacheMember ["ip" ],cacheMember ["port" ]))
72+ return unitValue (label , cacheServers )
10173
10274def ipPort2Addr (ip , port ):
10375 return str (ip ) + ":" + str (port )
@@ -119,22 +91,18 @@ def unitValue(lables, targets):
11991 return unit
12092
12193
122- def refresh ():
94+ def refresh (isShow = False ):
12395 targets = []
124- etcd_targets = []
12596
12697 # load mds
12798 mdsServers = loadMdsServer ()
12899 targets .append (mdsServers )
129- # load metaserver
130- metaServers = loadMetaServer ()
131- targets .append (metaServers )
132100 # load client
133101 client = loadClient ()
134102 targets .append (client )
135- # load etcd
136- etcdServers = loadEtcdServer ()
137- etcd_targets .append (etcdServers )
103+ # load cachemember
104+ cachemember = loadRemoteCacheServer ()
105+ targets .append (cachemember )
138106
139107 with open (targetPath + '.new' , 'w' , 0o777 ) as fd :
140108 json .dump (targets , fd , indent = 4 )
@@ -144,17 +112,41 @@ def refresh():
144112 os .rename (targetPath + '.new' , targetPath )
145113 os .chmod (targetPath , 0o777 )
146114
147- with open (etcdTargetPath + '.new' , 'w' , 0o777 ) as etcd_fd :
148- json .dump (etcd_targets , etcd_fd , indent = 4 )
149- etcd_fd .flush ()
150- os .fsync (etcd_fd .fileno ())
151-
152- os .rename (etcdTargetPath + '.new' , etcdTargetPath )
153- os .chmod (etcdTargetPath , 0o777 )
115+ if isShow :
116+ print (json .dumps (targets , indent = 4 ))
154117
155118if __name__ == '__main__' :
119+ parser = argparse .ArgumentParser (
120+ description = 'generate target for dingofs monitor' ,
121+ formatter_class = argparse .RawDescriptionHelpFormatter
122+ )
123+ parser .add_argument ('--interval' ,
124+ type = int ,
125+ default = 60 ,
126+ help = 'execute internal(s), default 60s' )
127+
128+ parser .add_argument ('--count' ,
129+ type = int ,
130+ help = 'execute count, default infinite, always execute' )
131+
132+ parser .add_argument ('--show' ,
133+ type = bool ,
134+ default = False ,
135+ help = 'show target info, default False' )
136+
137+ args = parser .parse_args ()
138+
139+ interval = args .interval
140+ count = args .count
141+ isShow = args .show
142+
143+ print ("Realtime update target is running, " ,"interval:" , interval , "count:" , count , "show:" , isShow )
144+
145+ current_count = 0
156146 while True :
147+ current_count += 1
157148 loadConf ()
158- refresh ()
159- # refresh every 30s
160- time .sleep (30 )
149+ refresh (isShow )
150+ if count is not None and current_count >= count :
151+ break
152+ time .sleep (interval )
0 commit comments