将字符串保存为弹性

时间:2016-01-18 10:21:06

标签: regex shell elasticsearch

保存这些mysql参数(如uptime,线程到elasticsearch以及当前时间戳)的最佳方法是什么?

In [16]: import subprocess

In [17]: Command=subprocess.Popen(["mysqladmin status"],stdout=subprocess.PIPE,shell=True)

In [18]: (Out1,err)=Command.communicate() 

In [19]: Out1
Out[19]: 'Uptime: 5148929  Threads: 2  Questions: 6684  Slow queries: 0  Opens: 65  Flush tables: 1  Open tables: 4  Queries per second avg: 0.001\n'

In [20]: type(Out1)
Out[20]: str

(python或shell脚本)

1 个答案:

答案 0 :(得分:0)

import subprocess
Command=subprocess.Popen(["mysqladmin status"],stdout=subprocess.PIPE,shell=True)
Out1, err=Command.communicate() 

import re
values=re.findall(r"[-+]?\d*\.\d+|\d+", Out1)

heading='uptime threads questions slow_queries, opens, flush_tables, open_tables, queries_avg'.split()

myrow=dict(zip(heading, values))

from datetime import datetime
naive_dt = datetime.now()
myrow['current_time'] = naive_dt

import elasticsearch
es = elasticsearch.Elasticsearch('https://some_site.com')
index_name='mysql_status'
doc_type_name='prod_server'
res = es.index(index=index_name, doc_type=doc_type_name, body=myrow)