运维平台开发,最初使用zabbix监控服务器信息,zabbix好用是好用,但也比较复杂、庞大,因此想摆脱第三方监控工具独立对服务器信息进行监控。
在linux中其实一个top命令可以解决,那么监控别的服务器就可以用ansible来完成,具体步骤
1.使用ansible 2.0接口调用top命令
a=MyRunner('/etc/ansible/hosts')
a.run('all','shell','top -bi -n 2 -d 0.02')
b=a.get_result()
succ=b['success']
2.分析数据,由于第一条数据一直不变,所以取第二条数据
for i in succ:
....
3.正则表达式,筛选出数值
# cpu处理 格式
# %Cpu(s): 3.7 us, 1.3 sy, 0.0 ni, 91.3 id, 3.7 wa, 0.0 hi, 0.0 si, 0.0 st
cpu = patt.findall(c[2].split(':')[1])
# 内存处理
# KiB Mem : 1017184 total, 70824 free, 533504 used, 412856 buff/cache
memory = patt1.findall(c[3].split(':')[1])
# 交换空间
# KiB Swap: 2097148 total, 1257612 free, 839536 used. 270960 avail Mem
swap = patt1.findall(c[4].split(':')[1])
4.保存到elasticsearch
es.index(index="monitor", doc_type=i, id=None,
body={"cpu": {"us": cpu[0], "sy": cpu[1]
, "ni": cpu[2], "id": cpu[3], "wa": cpu[4]
, "hi": cpu[5], "si": cpu[6], "st": cpu[7]}
, "memory": {"totalMem": memory[0], "freeMem": memory[1]
, "usedMem": memory[2], "buffcacheMem": memory[3]}
, "swap": {"totalSwap": swap[0], "freeSwap": swap[1]
, "usedSwap": swap[2], "availSwap": swap[3]}
, "time": nstr
, "timespan": int(nspan)
})
最后使用任务调度工具定时执行
elasticsearch保存结果格式如下:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 1,
"hits": [
{
"_index": "monitor",
"_type": "yt_ops",
"_id": "AV8FSr0ENPmlBHBapUB-",
"_score": 1,
"_source": {
"swap": {
"availSwap": "269840",
"usedSwap": "839388",
"freeSwap": "1257760",
"totalSwap": "2097148"
},
"time": "2017-10-10 15:58:24",
"timespan": 1507622304,
"cpu": {
"ni": "0.0",
"sy": "33.3",
"hi": "0.0",
"wa": "0.0",
"si": "0.0",
"id": "66.7",
"us": "0.0",
"st": "0.0"
},
"memory": {
"buffcacheMem": "412048",
"totalMem": "1017184",
"freeMem": "70572",
"usedMem": "534564"
}
}
},
{
"_index": "monitor",
"_type": "yt_tools",
"_id": "AV8FSrx0NPmlBHBapUB8",
"_score": 1,
"_source": {
"swap": {
"availSwap": "6014056",
"usedSwap": "14772",
"freeSwap": "8242760",
"totalSwap": "8257532"
},
"time": "2017-10-10 15:58:24",
"timespan": 1507622304,
"cpu": {
"ni": "0.0",
"sy": "0.0",
"hi": "0.0",
"wa": "0.0",
"si": "0.0",
"id": "50.0",
"us": "50.0",
"st": "0.0"
},
"memory": {
"buffcacheMem": "5333340",
"totalMem": "7994372",
"freeMem": "1156972",
"usedMem": "1504060"
}
}
}
]
}
}
完整的代码
# -*- coding: utf-8 -*-
from ansible.run import MyRunner
from elasticsearch import Elasticsearch
import re,time
ISOTIMEFORMAT = '%Y-%m-%d %X'
es = Elasticsearch('http://127.0.0.1:9200/')
a=MyRunner('/etc/ansible/hosts')
a.run('all','shell','top -bi -n 2 -d 0.02')
b=a.get_result()
succ=b['success']
print succ['yt_ops']['stdout']
print '######################'
patt = re.compile(r"(\d+\.\d+)")
patt1 = re.compile(r"(\d+)")
nspan = time.time()
nstr = time.strftime(ISOTIMEFORMAT, time.localtime(nspan))
for i in succ:
results = succ[i]['stdout'].split('\n\n')
# 获取第二个top结果
c = results[2].split('\n')
# cpu处理 格式
# %Cpu(s): 3.7 us, 1.3 sy, 0.0 ni, 91.3 id, 3.7 wa, 0.0 hi, 0.0 si, 0.0 st
cpu = patt.findall(c[2].split(':')[1])
#print c[2].split(':')[1]
#print cpu
# 内存处理
# KiB Mem : 1017184 total, 70824 free, 533504 used, 412856 buff/cache
memory = patt1.findall(c[3].split(':')[1])
#print
#print memory
# 交换空间
# KiB Swap: 2097148 total, 1257612 free, 839536 used. 270960 avail Mem
swap = patt1.findall(c[4].split(':')[1])
es.index(index="monitor", doc_type=i, id=None,
body={"cpu": {"us": cpu[0], "sy": cpu[1]
, "ni": cpu[2], "id": cpu[3], "wa": cpu[4]
, "hi": cpu[5], "si": cpu[6], "st": cpu[7]}
, "memory": {"totalMem": memory[0], "freeMem": memory[1]
, "usedMem": memory[2], "buffcacheMem": memory[3]}
, "swap": {"totalSwap": swap[0], "freeSwap": swap[1]
, "usedSwap": swap[2], "availSwap": swap[3]}
, "time": nstr
, "timespan": int(nspan)
})
网友评论