def service_control(name, state, emailTo, srv): log = '' command = '/sbin/service' + ' ' + str(name) + ' ' + str(state) p = subprocess.Popen(command, shell=True, stderr=subprocess.PIPE) out, log = p.communicate() if log != '': send_mail_with_attach.send_mail('*****@*****.**',emailTo, '', 'error', srv, log)
new_file.close() os.remove(confPath) shutil.move(absPath, confPath) for root, dirs, files in os.walk(pathToPgData): for momo in dirs: os.chmod(os.path.join(root, momo),0700) os.chown(os.path.join(root, momo),pgUid,pgGid) for momo in files: os.chmod(os.path.join(root, momo),0700) os.chown(os.path.join(root, momo),pgUid,pgGid) linux_service.service_control('postgresql-9.4', 'start', emailTo, srv) sleep(60) try: conn = psycopg2.connect("dbname='postgres' user='******' host='localhost'") except: send_mail_with_attach.send_mail('*****@*****.**',emailTo, '', 'error', srv, 'can\'t connect to database postgres') cur=conn.cursor() cur.execute("""SELECT datname from pg_database""") bases = cur.fetchall() cur.close() try: conn = psycopg2.connect("dbname='unit_dev' user='******' host='localhost'") except: send_mail_with_attach.send_mail('*****@*****.**',emailTo, '', 'error', srv, 'can\'t connect to database unit_dev') cur=conn.cursor() try: cur.execute("""UPDATE app_settings SET value = '1' WHERE param = 'MAIL.INBOX.EMAIL' or param = 'MAIL.INBOX.LOGIN' or param = 'MAIL.INBOX.PWD'""") conn.commit() cur.close() except: send_mail_with_attach.send_mail('*****@*****.**',emailTo, '', 'error', srv, 'can\'t update table')
except (requests.exceptions.Timeout, requests.exceptions.ConnectionError) as e: if url[0] == 'url1': shutil.copy('/etc/nginx/nginx_1.conf', '/etc/nginx/nginx.conf') elif url[0] == 'url2': shutil.copy('/etc/nginx/nginx_2.conf', '/etc/nginx/nginx.conf') elif url[0] == 'url3': shutil.copy('/etc/nginx/nginx_3.conf', '/etc/nginx/nginx.conf') service_control('nginx', 'stop', 'user', 'srv') service_control('nginx', 'start', 'user', 'srv') if url[3] == '': os.kill(FindProcessUsingPort(url[1]), signal.SIGKILL) os.popen("/opt/glassfish4/bin/asadmin start-domain %s" % url[2]) else: os.popen("ssh -p 63138 root@%s 'kill -9 %s'" % (url[3], FindProcessUsingPort(url[1], url[3]))) os.popen("ssh -p 63138 root@%s '/opt/glassfish4/bin/asadmin start-domain %s'" % (url[3], url[2])) send_mail('from', 'user', '', 'inform problem', 'srv', str(url)) time.sleep(300) try: response = requests.post(url[0], data=json.dumps(playload), headers=headers, verify=False, timeout=5) except requests.exceptions.Timeout as e: pass if re.sub("\".*", "", re.sub(".*\,\"status\"\:\"", "", response.text)) == 'ok': shutil.copy('/etc/nginx/nginx_allup.conf', '/etc/nginx/nginx.conf') service_control('nginx', 'stop', 'user', 'srv') service_control('nginx', 'start', 'user', 'srv') answer = re.sub("\".*", "", re.sub(".*\,\"status\"\:\"", "", response.text)) if answer == 'ok': pass else: if url[0] == 'url1': shutil.copy('/etc/nginx/nginx_1.conf', '/etc/nginx/nginx.conf')
#!/usr/bin/env python import subprocess import send_mail_with_attach import new_parse st_num = subprocess.Popen(["cat /media/big/Dropbox/python_training/123"], shell=True, stdout=subprocess.PIPE) for i in st_num.stdout: try: try: er_count = int(float(i.split()[14])) if er_count == 0: text = new_parse.parser('/media/big/Dropbox/python_training/resault.jtl') print(text) send_mail_with_attach.send_mail('*****@*****.**', '*****@*****.**', '/media/big/Dropbox/python_training/resault.jtl', 'error report', 'mx.unit-org.ru', text) break except ValueError: pass except IndexError: pass