ansible取得サーバcpu、メモリ、swap情報格納elasticsearch
7495 ワード
運営メンテナンスプラットフォームの開発は、最初はzabbixを使用してサーバー情報を監視し、zabbixは使いやすいが、複雑で膨大であるため、第三者監視ツールから独立してサーバー情報を監視したいと考えている.linuxの中で実は1つのtopコマンドは解決することができて、それでは他のサーバーを監視してansibleで完成することができて、具体的なステップ1.ansible 2.0インタフェースを使用してtopコマンドを呼び出す
2.分析データは、1番目のデータがずっと変わらないため、2番目のデータを取る
3.正規表現、数値のフィルタ
4.elasticsearchに保存
最後に、タスクスケジューリングツールを使用してelasticsearch保存結果のフォーマットをタイミング的に実行します.
完全なコード
ps:ansible2.0インタフェースは书いていないで欲しいのは私に闻くことができます
a=MyRunner('/etc/ansible/hosts')
a.run('all','shell','top -bi -n 2 -d 0.02')
b=a.get_result()
succ=b['success']
2.分析データは、1番目のデータがずっと変わらないため、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('
')
# top
c = results[2].split('
')
# 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)
})
ps:ansible2.0インタフェースは书いていないで欲しいのは私に闻くことができます