python - 使用 Python 根据文件内容编写 splunk 查询

标签 python python-3.x file splunk

我正在尝试通过从文本文件内容中获取值来编写 Splunk 查询。在这里我不想使用任何 Splunk 模块/库。

这是我的简单代码 -

import pandas as pd
from pandas import ExcelWriter
from pandas import ExcelFile
import sys

df = pd.read_excel("I:\\splunk_dashboards\\FID_list.xlsx", sheetname='FID_lastweek')
sys.stdout = open("I:\\splunk_dashboards\\FID.txt", "w")


v = df['FID']
#print(df['FID'])

print(v)

这是一个简单的代码,它检索特定的列值并将其存储在文本文件中。

下一步是使用存储在文本文件中的结果形成 splunk 查询。

例如,下面是文本文件的结果 -

0                            CollectionLimitsValidation
1                               PaymentLimitsValidation
2                              AccountDetailsFacadeBean
3                              AccountDetailsFacadeBean

我在另一个文本文件中确实有一个如下所示的 splunk 查询 -

index=hfc_new_98764 host=QA FID=$(Value1_from_text_file) OR FID=$(value2_from _text_file) OR.... it goes on upto the final values

从上面的模板中,我需要一个像下面这样的 splunk 查询 -

index=hfc_new_98764 host=QA FID=CollectionLimitsValidation OR FID=PaymentLimitsValidation OR FID=.... it goes on upto the final values

我需要帮助来迭代文本文件中的值并将其存储在模板文件中。

最佳答案

我能够通过文件操作实现上述场景,这是我的完整代码 -

# -*- coding: utf-8 -*-
"""
Created on Wed May 30 18:24:04 2018

@author: Harish
"""

import pandas as pd
from pandas import ExcelWriter
from pandas import ExcelFile
import sys
import fileinput
#import os


#Getting the values from Excel sheet

df = pd.read_excel("I:\\splunk_dashboards\\FID_list.xlsx", sheetname='FID_lastweek')
sys.stdout = open("I:\\splunk_dashboards\\new.txt", "w")
df.FID.unique()
v = df['FID'].to_string(index=False)
pd.options.display.max_colwidth = 200
#print(df['FID'])
#print('"{}"'.format(v))
print(v)


#os.system("script_to_create_FID.py")

#left alignment script
sys.stdout = open("I:\\splunk_dashboards\\aligned_file.txt", "w")
with open("I:\\splunk_dashboards\\new.txt") as f:
    for line in f:
        s = line.lstrip()
        m = s.strip()
        print('"{}"'.format(m))
        #print(m)

#FID and OR values 
prefix = 'FID='
suffix = '  OR'

with open('I:\\splunk_dashboards\\aligned_file.txt', 'r') as src:
    with open('I:\\splunk_dashboards\\final_FID.txt', 'w') as dest:
       for line in src:
           dest.write('%s%s%s\n' % (prefix, line.rstrip('\n'), suffix))


#Added Splunk index here      
for linenum,line in enumerate( fileinput.FileInput("I:\\splunk_dashboards\\final_FID.txt",inplace=1) ):
    if linenum==0 :
        print 'index=hfc_new_98764 host=QA" NOT(WARN=yes)'
        print line.rstrip()
    else:
        print line.rstrip()

#Add sort function at the end
a = '| stats count As NumberOfCalls, count(eval(ERCD=0)) AS "Success" ,count(eval(ERCD!=0)) AS "Failures" by FID | sort – Failures'
with open("I:\\splunk_dashboards\\final_FID.txt","a") as text:    
    text.writelines(a)

第 1 步 - 创建一个新的文本文件,其中包含从 Excel 中获取的 FID 列表 第 2 步 - 设置文本文件格式 步骤 3 - 在查询的前面和最后附加“FID”和“OR” 第 4 步 - 生成查询

关于python - 使用 Python 根据文件内容编写 splunk 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50583261/

相关文章:

Python 3.3 + Django 1.6 + 连接器/Python Django 后端 + South

file - D 文件 I/O 函数

python - 用python分割文件名

python - Pandas 修剪数据的更好方法

Python 逻辑帮助 :

python - 访问字典值中的嵌套元组

python-3.x - 我想计算数据帧中列中重复值的出现次数并更新 python 中新列中的计数

python - 将数据框的列追加到列表中

java - AES 文件加密/解密

python - 如何使用线程在文本文件中每行发布请求