-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnmpy.py
More file actions
338 lines (311 loc) · 16.6 KB
/
Copy pathsnmpy.py
File metadata and controls
338 lines (311 loc) · 16.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#!/usr/bin/python2.7
# -*- coding: utf-8 -*-
from ScvLib import *
from SplunkLib import SplunkFeedObject
from yamlLoader import load_yaml
import writtingLog
import argparse
import sys
import subprocess
import re
import calendar
import time
import csv
import logging
parser = argparse.ArgumentParser()
parser.add_argument("-env", "--env", help="path of repo with environment",
type=str, default=None)
parser.add_argument("-f", "--file", help="mib file",
type=str, default=None)
parser.add_argument("-oid", "--oid", help="respective oid to make the query",
type=str, default=None)
parser.add_argument("-host", "--host", help="ip or hostname are valid "
"arguments",
type=str, default=None)
parser.add_argument("-c", "--config", help="path of configuration file (YAML "
"file)",
type=str, default=None)
parsed_args = parser.parse_args()
default_env_path = "/ms/dist/nms/PROJ/operations_analytics_scripts/dev"
# default_env_path = "/v/global/user/a/ac/acostosc/repos/"\
# "operations_analytics_scripts/"
if parsed_args.env is None:
parsed_args.env = default_env_path
sys.path.insert(1, "{}/python/lib".format(parsed_args.env))
# from writtingLog import writtingLog
# --------------------------- main definition ----------------------------------
def main(margs=None):
"""
The following 'if' statement is in charge of defaulting to sys.argv when no
'margs' is passed.
"""
if margs is None:
margs = sys.argv[1:]
# Argument parsing in main scope (based on global's arguments)
parsed_margs = argparse.ArgumentParser(
parents=[parser], add_help=False).parse_args(margs)
# Validate which method will be use run as program or module
# this section will run if the cmd get the info through yaml file
if parsed_margs.host is None and parsed_margs.oid is None:
# this condition use yaml file to load the parameters
# argument validation
conf_file = ("{}".format(parsed_margs.config)
if parsed_margs.config is None else parsed_margs.config)
config_data = load_yaml(conf_file)
if conf_file is None:
print("invalid configuration file please validate")
# return 1
else:
# loading data from yaml file
logsdir = config_data[1].get("logsdir", None)
nas = config_data[1].get("NAS", None)
scvNamespace = config_data[1].get("scvNamespace", None)
scvCommunity = config_data[1].get("scvCommunity", None)
scvUrl = config_data[1].get("scvUrl", None)
version = config_data[1].get("version", None)
splunk = config_data[1].get("splunkUrlPost", None)
if version == "v3":
username = config_data[1].get("username", None)
securityLevel = config_data[1].get("securityLevel", None)
authProtocol = config_data[1].get("authProtocol", None)
passPhrase = config_data[1].get("passPhrase", None)
passPhraseProtocol = config_data[1].get("passPhraseProtocol", None)
privProtocol = config_data[1].get("privProtocol", None)
mibPath = config_data[1].get("mibPath", None)
hosts = config_data[1].get("hosts", None)
oids = config_data[1].get("oids", None)
tables = config_data[1].get("tables", None)
# splunk = config_data[1].get("splunkUrlPost", None)
if splunk is not None:
splunk_host = re.search('(?P<prot>^http\:\/\/)\w*?(?P<host>'
'\w+[^:/]+)', 'http://ivapp1132449.'
'devin1.ms.com:9090/en-US/app'
'/launcher/home')
splunk_host = splunk_host.group(2)
splunk_val = "ping -c 3 {}".format(splunk_host)
try:
subprocess.check_output(splunk_val, stderr=subprocess.PIPE,
shell=True)
except subprocess.CalledProcessError as error:
print error
# return 1
# --------------------------- snmpwalk version 3 ---------------
# execute snmpwalk cmd itererating for every host and every
# oid contained in
# yaml file
for host in hosts:
for oid in oids:
snmpV3cmd = "snmpwalk -{} -Os -u {} -l {} -a {} -A {}"\
" -X {} -x {} -OQ -Oa -M {} -m ALL " \
"{} {}".format(version, username,
securityLevel,
authProtocol, passPhrase,
passPhraseProtocol,
privProtocol, mibPath, host,
oid)
# those print are for validation pruposes print the cmd
# that is running
print(snmpV3cmd)
print("[getting snmpwalk data from {}]".format(host))
# this try validate the output of the cmd
try:
output = subprocess.check_output(snmpV3cmd,
stderr=subprocess.PIPE,
shell=True).strip()
except:
snmperror = "Timeout occur for {}".format(host)
print(snmperror)
writtingLog("info", "warning {}".format(snmperror),
nas + logsdir)
return 1
outputList = output.split("\n")
# print(outputList)
# this is the regular expression that cut the output string
# and pass at their
# respective label
rex = re.compile("(?P<Description>.*?)\s=\s(?P<Value>'\
'.*?)$")
dictList = [rex.search(x).groupdict()
for x in outputList if rex.search(x)]
# for statement ingest the labels datatype and principal
for out in dictList:
out["dataType"] = "snmpWalk"
out["principal_oid"] = oid
host = host.split(".")
host = host[0]
out["hostname"] = host
""" ingest the data in splunk """
# print(dictList)
splunk_feeder = SplunkFeedObject(config_data[1], "p")
# print(config_data[1])
finaldata = splunk_feeder.eventPush(dictList)
# -------------------------- snmptable version 3 ---------------
# execute snmpwalk cmd itererating for every host and every oid
# contained
# in yaml file
for host in hosts:
for table in tables:
snmpV3tab = "snmptable -{} -Os -u {} -l {} -a {} -A {}"\
" -X {} -x {} -OQ -Oa -M {} -m ALL -Ci "\
"-Oq -Cf '|' -L n" \
" -OU {} {}".format(version, username,
securityLevel,
authProtocol,
passPhrase,
passPhraseProtocol,
privProtocol, mibPath,
host, table)
# those print are for validation pruposes print the cmd
# that is rinning print(snmpV3tab)
# print("[getting snmptable data from {}]".format(host))
# this try validate the output of the cmd
try:
output_table = subprocess.check_output(snmpV3tab,
stderr=subprocess.PIPE,
shell=True).strip()
except:
snmperror = "Timeout occur for {}".format(host)
print(snmperror)
writtingLog("info", "warning {}".format(snmperror),
nas + logsdir)
return 1
# this split the output string by brea line and pipe
outputList2 = output_table.split("\n")
headerlist = outputList2[2].split("|")
output_sample = outputList2[3:]
# this section ingest the output to a csv delimited by | and stablish the table headers
reader = csv.DictReader(output_sample, delimiter='|',
fieldnames=headerlist)
eventlist = [x for x in reader]
# for statement ingest the labels datatype and principal
for x in eventlist:
x["dataType"] = "snmpTable"
x["principal_table"] = table
x["snmpIndex"] = x.pop("index")
splunk_feeder2 = SplunkFeedObject(config_data[1], "p")
finaldata = splunk_feeder2.eventPush(eventlist)
# --------------- snmp version 2 begins ----------------
elif version == "v2":
if splunk is not None:
splunk_host = re.search('(?P<prot>^http\:\/\/)\w*?(?P<host>'
'\w+[^:/]+)',
'http://ivapp1132449.devin1.ms.com:'
'9090/en-US/app/launcher/home')
splunk_host = splunk_host.group(2)
splunk_val = "ping -c 3 {}".format(splunk_host)
try:
subprocess.check_output(splunk_val, stderr=subprocess.PIPE,
shell=True)
print('ping success')
except subprocess.CalledProcessError as error:
print error
# return 1
mibPath = config_data[1].get("mibPath", None)
hosts = config_data[1].get("hosts", None)
oids = config_data[1].get("oids", None)
tables = config_data[1].get("tables", None)
splunk = config_data[1].get("splunkUrlPost", None)
community = config_data[1].get("v2Community", None)
# ------------------------ snmpwalk version 2 ----------------------
# execute snmpwalk cmd itererating for every host and every
# oid contained in
# yaml file
for host in hosts:
snmpV2cmd = "snmpwalk -{}c -c {} {} -M {} -m ALL -Os -OQ {}"\
" -Oa".format(version, community,
host, mibPath, oid)
print(snmpV2cmd)
print("[getting snmpwalk data from {}]".format(host))
try:
output = subprocess.check_output(snmpV2cmd,
stderr=subprocess.PIPE,
shell=True).strip()
except:
snmperror = "Timeout occur for {}".format(host)
print(snmperror)
writtingLog("info", "warning {}".format(snmperror), nas +
logsdir)
return 1
outputList = output.split("\n")
rex = re.compile("(?P<Description>.*?)\s=\s(?P<Value>'\
'.*?)$")
dictList = [rex.search(x).groupdict()
for x in outputList if rex.search(x)]
# for statement ingest the labels datatype and principal
for out in dictList:
out["dataType"] = "snmpWalk"
out["principal_oid"] = oid
host = host.split(".")
host = host[0]
out["hostname"] = host
""" ingest the data in splunk """
# print(dictList)
splunk_feeder = SplunkFeedObject(config_data[1], "p")
# print(config_data[1])
finaldata = splunk_feeder.eventPush(dictList)
# -------------------------- snmptable version 2 ---------------
# execute snmpwalk cmd itererating for every host and every oid
# contained
# in yaml file
for host in hosts:
for table in tables:
snmpV2tab = "snmptable -{}c -c {} {} -CI -CB -Ci -OX -Cb"\
" -Cc 16 -Cw 64 {}".format(version, community,
host,
table)
# those print are for validation pruposes print the cmd
print(snmpV2tab)
print("[getting snmptable data from {}]".format(host))
# this try validate the output of the cmd
try:
output_table = subprocess.check_output(snmpV2tab,
stderr=subprocess.PIPE,
shell=True).strip()
except:
snmperror = "Timeout occur for {}".format(host)
print(snmperror)
writtingLog("info", "warning {}".format(snmperror),
nas + logsdir)
return 1
# this split the output string by brea line and pipe
outputList2 = output_table.split("\n")
headerlist = outputList2[2].split("|")
output_sample = outputList2[3:]
# this section ingest the output to a csv delimited by
# | and stablish the table headers
reader = csv.DictReader(output_sample, delimiter='|',
fieldnames=headerlist)
eventlist = [x for x in reader]
# for statement ingest the labels datatype and principal
for x in eventlist:
x["dataType"] = "snmpTable"
x["principal_table"] = table
x["snmpIndex"] = x.pop("index")
splunk_feeder2 = SplunkFeedObject(config_data[1], "p")
finaldata = splunk_feeder2.eventPush(eventlist)
if __name__ == "__main__":
try:
sys.exit(main())
except KeyboardInterrupt as k:
print("Forced Exit")
# --------------------------- snmpwalk version 2 ------------------------------
# elif version == "v2":
# mibPath = config_data[1].get("mibPath", None)
# hosts = config_data[1].get("hosts", None)
# for host in hosts:
# snmpV2cmd = "snmpwalk -{} -c {} {} -M {} -m ALL".format(version,
# scvCommunity,
# host,
# mibPath)
# try:
# output = subprocess.check_output(snmpV2cmd,
# stderr=subprocess.PIPE,
# shell=True).strip()
# except:
# snmperror = "Timeout occur for {}".format(host)
# print(snmperror)
# writtingLog("warning {}".format(snmperror), logsdir)
# return 1
# outputList = output.split("\n")
# do something
# for closing after error use return 1 instead of sys.exit()