ansible獲取服務器cpu、內存、swap信息存入elasticsearch

運維平臺開發(fā),最初使用zabbix監(jiān)控服務器信息,zabbix好用是好用,但也比較復雜、龐大,因此想擺脫第三方監(jiān)控工具獨立對服務器信息進行監(jiān)控。
在linux中其實一個top命令可以解決,那么監(jiān)控別的服務器就可以用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.分析數(shù)據(jù),由于第一條數(shù)據(jù)一直不變,所以取第二條數(shù)據(jù)

for i in succ:
     ....

3.正則表達式,篩選出數(shù)值

# 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)
                           })

最后使用任務調度工具定時執(zhí)行
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)
                           })

ps:ansible2.0接口沒寫出來想要的可以問我要

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容