本文共 1668 字,大约阅读时间需要 5 分钟。
python抓取jenkins slave信息写道mysql并展现到grafana:
注:
包括slave总数、online数、offline数、job排队数、空闲slave数create database jenkins default character set utf8;
create table slaves(time datetime,online float,offline float,total float,idle float,Qu float);grant all on ming.* to aa@'10.0.0.2' identified by 'xxxx';exit
#!/usr/bin/python
#coding:utf-8
import jenkins
import collectionsimport pymysqlimport timeserver = jenkins.Jenkins('', username="ming", password="xxxxx")
nodes = server.get_nodes()
Que = server.get_queue_info()To = len(nodes)
Qu = len(Que)on = collections.Counter(str(nodes))['F']
off = collections.Counter(str(nodes))['T']
ti = time.localtime()
e=[]
nodes.remove({'offline': False, 'name': 'master'})for i in range(len(nodes)-1):e.append(server.get_node_info(nodes[i]['name'])['idle'])Id = e.count(True)conn = pymysql.connect(host="10.0.0.3",port=3306,database='jenkins',user='ming',password='xxxxx',charset='utf8')
cur = conn.cursor()cur.execute("insert into slaves (time,online,offline,total,idle,queue) values (%s,%s,%s,%s,%s,%s)", [ti,on,off,To,Id,Qu])conn.commit()conn.close();print on
print offprint Toprint Idprint Quprint ('Data has been inserted')
:wq
grafa:
select UNIX_TIMESTAMP(time) as time_sec, total-1 as total from slaves group by time_sec;
select UNIX_TIMESTAMP(time) as time_sec, online-1 as online from slaves group by time_sec;
select UNIX_TIMESTAMP(time) as time_sec, offline as offline from slaves group by time_sec;
SELECT UNIX_TIMESTAMP(time) as time_sec,idle FROM slaves;
转载于:https://blog.51cto.com/yangzhiming/2316740