-
Notifications
You must be signed in to change notification settings - Fork 2
/
backupkvstore.py
executable file
·91 lines (68 loc) · 2.71 KB
/
backupkvstore.py
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
import requests
import json
import csv
from ConfigParser import SafeConfigParser
import sys, getopt
import os
__author__ = "[email protected] (George Starcher)"
def loadConfig(filename):
global splunk_server
global splunk_server_port
global splunk_server_verify
global backupFolder
parser = SafeConfigParser()
parser.read(filename)
splunk_server = parser.get('splunk', 'splunk_server')
splunk_server_port = parser.get('splunk', 'splunk_server_port')
splunk_server_verify = parser.getboolean('splunk', 'splunk_server_verify')
backupFolder = parser.get('splunk', 'backup_folder')
def getKVStoreData(url):
try:
url = url.replace('config','data')
splunk_url = ''.join([url,'/?output_mode=json'])
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
r = requests.get(splunk_url,auth=(splunk_user,splunk_password),verify=splunk_server_verify,headers=headers)
r.raise_for_status()
return json.loads(r.text)
except requests.exceptions.HTTPError as e:
print( "%s" % str(e))
sys.exit(1)
def getKVStoreCollections():
try:
splunk_url = ''.join(['https://',splunk_server,':',splunk_server_port,'/servicesNS/-/-//storage/collections/config/?output_mode=json'])
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
r = requests.get(splunk_url,auth=(splunk_user,splunk_password),verify=splunk_server_verify,headers=headers)
r.raise_for_status()
return json.loads(r.text)
except requests.exceptions.HTTPError as e:
print( "%s" % str(e))
sys.exit(1)
def outputCollectionDataToFile(name,data):
outFile = "data_"+name+".bak"
json.dump(data,open(os.path.join(backupFolder,outFile),"wt"))
def outputCollectionDefToFile(name,data):
outFile = "def_"+name+".bak"
json.dump(data,open(os.path.join(backupFolder,outFile),"wt"))
if __name__ == "__main__":
config_file = 'kvstore.conf'
template = 'template.csv'
app = ''
collection = ''
argv = sys.argv[1:]
global splunk_user
global splunk_password
try:
loadConfig(config_file)
except Exception, e:
print "Config File Error: %s" % e
sys.exit(2)
splunk_user = raw_input('enter Splunk user:')
splunk_password = raw_input('enter Splunk password:')
outputCollections = []
collections=getKVStoreCollections()
for collection in collections.get('entry'):
collectionName = collection.get('name')
url = collection.get('id')
collectionData = getKVStoreData(url)
outputCollectionDefToFile(collectionName, collection)
outputCollectionDataToFile(collectionName,collectionData)