forked from opencurve/curve
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtarget_json.py
129 lines (111 loc) · 3.36 KB
/
target_json.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env python
# coding=utf-8
import os
import time
import json
import ConfigParser
import MySQLdb
import commands
from itertools import groupby
from MySQLdb import connect
from MySQLdb import cursors
host=None
port=None
username=None
password=None
database='curve_mds'
targetPath=None
etcd_exporter=None
mds_exporter=None
mysql_exporter=None
snapclone_exporter=None
def loadConf():
global host,port,username,password,targetPath,etcd_exporter,mds_exporter,mysql_exporter,snapclone_exporter
conf=ConfigParser.ConfigParser()
conf.read("target.ini")
host=conf.get("mysql", "ip")
port=conf.getint("mysql", "port")
username=conf.get("mysql", "user")
password=conf.get("mysql", "pwd")
targetPath=conf.get("path", "target_path")
etcd_exporter=conf.get("exporter", "etcd")
mds_exporter=conf.get("exporter", "mds")
mysql_exporter=conf.get("exporter", "mysql")
snapclone_exporter=conf.get("exporter", "snapclone")
def refresh(cur):
targets = []
# 获取chunkserver的ip和port
cur.execute("SELECT internalHostIP,port FROM curve_chunkserver")
result=cur.fetchall()
cur.execute("SELECT internalHostIP,hostName FROM curve_server")
hostnames=cur.fetchall()
# 添加chunkserver targets
for t in result:
count = cur.execute("SELECT hostName FROM curve_server where internalHostIP ='%s'" % t[0])
if count > 0:
server=cur.fetchone()
hostname=server[0]
else:
hostname=t[0]
targets.append({
'labels': {'job': "chunkserver", 'hostname': hostname},
'targets': [t[0]+':'+str(t[1])],
})
# 添加node_exporter targets
chunkserverip=set([t[0] for t in result])
targets.append({
'labels': {'job': "node_exporter"},
'targets': [t+':9100' for t in chunkserverip],
})
# 获取client的ip和port
result = commands.getstatusoutput("curve_ops_tool client-list -listClientInRepo=true")
if result[0] != 0:
print "curve_ops_tool list client fail!"
return
# 添加client targets
targets.append({
'labels': {'job': "client"},
'targets': result[1].split("\n"),
})
# 添加mysql exporter targets
targets.append({
'labels': {'job': "mysqld"},
'targets': [mysql_exporter],
})
# 添加etcd targets
targets.append({
'labels': {'job': "etcd"},
'targets': etcd_exporter.split(','),
})
# 添加mds targets
targets.append({
'labels': {'job': "mds"},
'targets': mds_exporter.split(','),
})
# 添加snapclone targets
targets.append({
'labels': {'job': "snapclone"},
'targets': snapclone_exporter.split(','),
})
with open(targetPath+'.new', 'w', 0777) as fd:
json.dump(targets, fd)
fd.flush()
os.fsync(fd.fileno())
os.rename(targetPath+'.new', targetPath)
os.chmod(targetPath, 0777)
if __name__ == '__main__':
while True:
loadConf()
db = None
try:
db = connect(host=host, user=username, passwd=password, db=database, port=port)
cur = db.cursor()
refresh(cur)
except MySQLdb.Error, e:
print "MySQL Error:%s" % str(e)
finally:
if db:
cur.close()
db.close()
# 每隔30s刷新一次
time.sleep(30)