如何使用py脚本根据kibana中的时间戳获取所有先前的数据?(每次都覆盖)

问题描述 投票:0回答:1

下面是我远程连接python脚本并获取输出的代码。

*执行查询的服务器存储在弹性搜索索引中:e2

无法执行查询的服务器存储在弹性搜索索引中:e1 *

现在每当我运行此脚本时,我得到

'SERVERNAME':SERVERNAME,'timestamp':####,'ONLINE':“ ONLINE”

并且只有1个匹配项,这是最近的匹配项。...并非在kibana发现选项卡中的所有匹配项(服务器名)

下面是图片

image showing kibana discovery mode for index having multiple servername but showing only latest one(overriding every time and update)

下面是python代码:

ES_HOST = {"host":  "localhost", "port": ####}

es = Elasticsearch(hosts=[ES_HOST])

filepath=r'c:\remote server\ServerList.csv'
#with open (filepath, 'r') as servers:
with open(filepath, 'r', encoding="ISO-8859-1") as servers:
   for line in servers:
       l = line.split()
       #print(l)
       SERVERNAME = line.rstrip()
       #print(SERVERNAME)

       cnxn = pyodbc.connect(Trusted_Connection='no',
       Driver='{SQL Server}',
       Server=SERVERNAME,
       PORT='####',
       Database='####',
       UID='####',
       PWD='####')
       cursor = cnxn.cursor()
       #print("hureeee")
       #print(SERVERNAME)
       s=cursor.execute("select servername AS gandhiji from ######")
       row = cursor.fetchone()
       if row == None:
            print("There are no results for this query", SERVERNAME)
            e1 = {
                'SERVERNAME': SERVERNAME,
                'timestamp': datetime.utcnow(),
                'OFFLINE': "OFFLINE"
            }
            res1=es.index(index='rep100', id=1, body=e1)
            res1=es.get(index="rep100", id=1)
            print(res1['_source'])

            es.indices.refresh(index="rep100")
            res1=es.search(index="rep100", body={"query": {"match_all": {}}})
       else:
           print(SERVERNAME)
           e2 = {
               'SERVERNAME': SERVERNAME,
               'timestamp': datetime.utcnow(),
               'ONLINE': "ONLINE"
           }
           res2=es.index(index='re100', id=1, body=e2)
           res2=es.get(index="re100", id=1)
           print(res2['_source'])

           es.indices.refresh(index="re100")
           res2=es.search(index="re100", body={"query": {"match_all": {}}})
python elasticsearch pycharm kibana
1个回答
0
投票

我找到了答案:

1 $$$需要修改

res1=es.index(index='UR_INDEXNAME', id=1, body=e1)

to

res1=es.index(index='UR_INDEXNAME', body=e1)

如果我们在es.index中的for循环中指定id=1,该循环将再次运行并将现有数据替换为先前的ID。

2 $$$

此外,如果要查找连接错误,请使用: ------>尝试,但在python ::

中除外

QUERY ---->“从****中选择服务器名称”

TRY BLOCK :::将使SERVERNAME具有有效的连接和有效的查询输出(非空输出)

[[附加 ::::::::如果要为成功连接的服务器使用空值服务器名,请使用此代码:

if row == None:
            print("There are no results for this query", SERVERNAME)
            e1 = {
                'SERVERNAME': SERVERNAME,
                'timestamp': datetime.utcnow(),
                'status': "ONLINE SERVER WITH NULL VALUES"
            }
            res1=es.index(index='rep100', id=1, body=e1)

       else:
           print(SERVERNAME)
           e2 = {
               'SERVERNAME': SERVERNAME,
               'timestamp': datetime.utcnow(),
               'STATUS': "ONLINE"
           }
           res2=es.index(index='re100', id=1, body=e2)

EXCEPT ERROR :::将使SERVERNAME无法连接

© www.soinside.com 2019 - 2024. All rights reserved.