1
1
import os
2
2
import time
3
- import pandas as pd
3
+
4
4
import ipywidgets as widgets
5
- from IPython .display import display
6
- import splunklib .results as splunk_results
5
+ import pandas as pd
7
6
import splunklib .client as splunk_client
7
+ import splunklib .results as splunk_results
8
+ from IPython .display import display
9
+
8
10
9
11
class SplunkSearch (object ):
10
12
def __init__ (self ,
@@ -21,7 +23,7 @@ def __init__(self,
21
23
self .host = host
22
24
self .port = port
23
25
self .token = token
24
-
26
+
25
27
if "splunk_access_enabled" in os .environ :
26
28
access_enabled = os .environ ["splunk_access_enabled" ]
27
29
if access_enabled == "1" :
@@ -70,22 +72,22 @@ def __init__(self,
70
72
layout = widgets .Layout (width = '100%' ),
71
73
disabled = True
72
74
)
73
-
75
+
74
76
# generate the widgets in a box layout
75
77
self .widgets = widgets .VBox ([
76
78
widgets .HBox ([ui ['spl' ], widgets .VBox ([ui ['button' ], ui ['earliest' ], ui ['latest' ]], layout = widgets .Layout (width = '20%' ))]),
77
- ui ['progress1' ],
79
+ ui ['progress1' ],
78
80
ui ['progress2' ],
79
81
ui ['resultinfo' ]
80
82
])
81
83
82
- # keep the reference to the UI widgets
84
+ # keep the reference to the UI widgets
83
85
self .ui = ui
84
86
85
87
# display the UI
86
88
display (self .widgets )
87
-
88
-
89
+
90
+
89
91
def search_button_clicked (self , button_event ):
90
92
self .ui ['button' ].disabled = True
91
93
self .ui ['progress1' ].description = "search"
@@ -99,10 +101,10 @@ def search_button_clicked(self, button_event):
99
101
)
100
102
self .ui ['button' ].disabled = False
101
103
self ._results = resultset
102
-
104
+
103
105
@property
104
106
def service (self ):
105
- if self ._service != None :
107
+ if self ._service is not None :
106
108
return self ._service
107
109
self ._service = splunk_client .connect (host = self .host , port = self .port , token = self .token )
108
110
return self ._service
@@ -116,7 +118,7 @@ def search(self, query, earliest="-15m@m", latest="now"):
116
118
elif query_cleaned [0 ]== '|' :
117
119
# assume a generating search command and do nothing
118
120
pass
119
- elif query_cleaned .startswith ("search " ):
121
+ elif query_cleaned .startswith ("search " ) or query_cleaned . startswith ( "search \n " ) :
120
122
# assume the search keyword is already there
121
123
pass
122
124
else :
@@ -128,9 +130,9 @@ def search(self, query, earliest="-15m@m", latest="now"):
128
130
try :
129
131
# create a search job in splunk
130
132
job = self .service .jobs .create (
131
- query_cleaned ,
132
- earliest_time = earliest ,
133
- latest_time = latest ,
133
+ query_cleaned ,
134
+ earliest_time = earliest ,
135
+ latest_time = latest ,
134
136
adhoc_search_level = "smart" ,
135
137
search_mode = "normal" )
136
138
self ._job = job
@@ -162,7 +164,7 @@ def search(self, query, earliest="-15m@m", latest="now"):
162
164
#print(result)
163
165
processed += 1
164
166
self .ui ['progress2' ].value = float (processed )
165
- offset = processed
167
+ offset = processed
166
168
self .ui ['progress2' ].value = self .ui ['progress2' ].max
167
169
self .ui ['progress2' ].description = "loading done"
168
170
@@ -179,7 +181,7 @@ def search(self, query, earliest="-15m@m", latest="now"):
179
181
return resultset
180
182
181
183
def search_logs (self ):
182
- if self ._job != None :
184
+ if self ._job is not None :
183
185
for line in self ._job .searchlog ():
184
186
print (str (line ))
185
187
0 commit comments