import subprocess
import shutil
import os
def download(url):
print('----------------- downloading -------------')
command = "curl -L -O {0}".format(url)
p = subprocess.Popen(command,shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
print(out)
print(err)
def extract(gzfile):
print('------------------- extracting -----------')
command = ["tar", "xzvf", "{0}".format(gzfile)]
subprocess.call(command)
def replace_file(src_file, des_file):
print('--------------------- replacing-------------------')
file = src_file + ".yml"
src = os.path.join(os.getcwd(), file)
print(src)
permission = ["chmod 755 {0}".format(src)]
p = subprocess.Popen(permission ,shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
print(out)
print(err)
des = os.path.join(des_file, file)
permission = ["chmod 755 {0}".format(des)]
p = subprocess.Popen(permission ,shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
print(out)
print(err)
print(des)
if os.path.exists(src):
shutil.move(src, des)
def start(beat, beat_dir):
des = beat_dir +"/" +beat + ".yml"
cdir = os.getcwd()
permission = ["chmod 755 {0}".format(des)]
p = subprocess.Popen(permission ,shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
print(out)
print(err)
if beat == "metricbeat":
permission = ["chmod 755 {0}".format(os.path.join(beat_dir, "modules.d/system.yml"))]
p = subprocess.Popen(permission ,shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
print(out)
print(err)
os.chdir(beat_dir)
command = "./{0} &".format(beat)
os.system(command)
os.chdir(cdir)
#print('out is --------------------- {0}'.format(p))
if __name__ =="__main__":
permission = ["chmod 755 {0}".format("beat_config.txt")]
p = subprocess.Popen(permission ,shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
print(out)
print(err)
with open('beat_config.txt', 'r') as f:
beat_config = f.readlines()
version_number = ""
for line in beat_config:
if line.startswith('version'):
version_number = (line.split('=')[-1]).strip()
if version_number == "":
raise ValueError('version is not specified, please specify it in beat_config.txt file')
sys.exit()
beat_download = {"filebeat":"https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-{0}-linux-x86_64.tar.gz".format(version_number),
"auditbeat":"https://artifacts.elastic.co/downloads/beats/auditbeat/auditbeat-{0}-linux-x86_64.tar.gz".format(version_number),
"metricbeat":"https://artifacts.elastic.co/downloads/beats/metricbeat/metricbeat-{0}-linux-x86_64.tar.gz".format(version_number),
"heartbeat":"https://artifacts.elastic.co/downloads/beats/heartbeat/heartbeat-{0}-linux-x86_64.tar.gz".format(version_number)
}
beat_dir= {"auditbeat":"auditbeat-{0}-linux-x86_64".format(version_number),
"filebeat":"filebeat-{0}-linux-x86_64".format(version_number),
"metricbeat":"metricbeat-{0}-linux-x86_64".format(version_number),
"heartbeat":"heartbeat-{0}-linux-x86_64".format(version_number)
}
for beat in beat_download.keys():
download(beat_download[beat])
extract(beat_dir[beat] + ".tar.gz")
replace_file(beat, beat_dir[beat])
start(beat, beat_dir[beat])
This is the script which I am using to download,extract and start.