forked from olajowon/loggrove
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.py
158 lines (139 loc) · 5.21 KB
/
build.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
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
# Created by zhouwang on 2018/6/9.
import os
from settings import MYSQL_DB
import subprocess
import random
import string
import hashlib
import time
NEW_SUPERADMIN = False
def tools():
print('Step1: Install Tools')
command = 'yum install -y openldap openldap-devel'
print('-->', command)
status = os.system(command)
if status != 0:
exit()
print('Step1: End.\n')
def python_packages():
print('Step2: Install Python Packages')
command = 'pip3 install -r requirements.txt'
print('-->', command)
status = os.system(command)
if status != 0:
exit()
print('Step2: End.\n')
def mysql_db():
print('Step3: Build Mysql Database')
command = 'mysql -h%s -P%d -u%s -p%s -e \'CREATE DATABASE IF NOT EXISTS %s DEFAULT CHARSET utf8 COLLATE utf8_general_ci;\'' \
% (MYSQL_DB['host'], MYSQL_DB['port'], MYSQL_DB['user'], MYSQL_DB['password'], MYSQL_DB['db'])
print('-->', command)
status = os.system(command)
if status != 0:
exit()
print('Step3: End.\n')
def mysql_tables():
print('Step4: Build MySQL Tables:')
command = 'mysql -h%s -P%d -u%s -p%s loggrove < tables.sql' % \
(MYSQL_DB['host'], MYSQL_DB['port'], MYSQL_DB['user'], MYSQL_DB['password'])
print('-->', command)
status = os.system(command)
if status != 0:
exit()
print('Step4: End.\n')
def render_monitor_py():
print('Step5: Render Monitor Script')
import jinja2
j2 = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates', 'monitor.py.jinja2')
py = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'scripts', 'monitor.py')
print('-->', py)
with open(py, 'w') as f:
f.write(jinja2.Template(open(j2).read()).render(mysqldb=MYSQL_DB,
render_date=time.strftime('%Y/%m/%d', time.localtime())))
print('Step5: End.\n')
def local_monitor_cron():
print('Step6: Create Localhost Monitoring Task On Crontab')
monitor_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'scripts', 'monitor.py')
command = 'cat /var/spool/cron/`whoami` | grep %s' % 'loggrove_monitor'
print('-->', command)
status = os.system(command)
if status == 0:
print('Existence of Monitoring Task On Crontab, Skip.\n')
else:
command = 'echo -e "\\n* * * * * `which python3` %s localhost >> ' \
'/tmp/loggrove_monitor.log # loggrove_monitor\\n" >> /var/spool/cron/`whoami`' % (monitor_path)
print('-->', command)
status = os.system(command)
if status != 0:
exit()
print('Step6: End.\n')
def loggrove_admin():
global NEW_SUPERADMIN
print('Step7: Create Loggrove Superadmin')
status, output = subprocess.getstatusoutput(
'mysql -h%s -P%d -u%s -p%s loggrove -e \'select "Existence of superadmin" from user where username="admin"\'' %
(MYSQL_DB['host'], MYSQL_DB['port'], MYSQL_DB['user'], MYSQL_DB['password']))
if status != 0:
print(output)
exit()
if output.find('Existence of superadmin') < 0:
salt = ''.join(random.sample(string.ascii_letters, 8))
password = '%s%s' % (salt, hashlib.md5((salt + 'loggrove').encode('UTF-8')).hexdigest())
sqltext = '''
INSERT INTO user (username, password, fullname, email, join_time, status, role)
VALUES ("admin", "%s", "Admin", "[email protected]", now(), "1", "1");
''' % password
command = 'mysql -h%s -P%d -u%s -p%s loggrove -e \'%s\'' % \
(MYSQL_DB['host'], MYSQL_DB['port'], MYSQL_DB['user'], MYSQL_DB['password'], sqltext)
print('-->', command)
status = os.system(command)
if status != 0:
exit()
NEW_SUPERADMIN = True
else:
print('Existence of Superadmin, Skip.\n')
print('Step7: End.\n')
def main():
print('### Loggrove Build ###')
print('''
要求:
1: 已安装 Python36、PIP3、MySQL57、Crond,并保证 python3、pip3、mysql、crontab、yum 命令可用;
2: 已完成 settings.py > MYSQL_DB host、port、user、password, SSH username、password、port 等配置;
步骤:
1: 安装依赖工具(yum)
2: 安装Python包 (pip3)
3: 创建MySQL db,若存在则不进行创建动作
4: 创建or更新 MySQL tables 结构
5: 渲染生成监控脚本,若存在则进行覆盖
6: 创建本地监控任务,若存在则不进行创建动作(注:远程主机需要手动添加监控任务,详情查看README)
7: 创建超级管理员,若存在则不进行创建动作
''')
while True:
put = input('开始(y/n):')
if put == 'y':
break
if put == 'n':
exit()
print('\n')
print('>>> Begin:\n')
tools()
python_packages()
mysql_db()
mysql_tables()
render_monitor_py()
local_monitor_cron()
loggrove_admin()
print('End and successful. <<< \n')
if NEW_SUPERADMIN:
print(
'''
# # # # Super Admin # # # #
# #
# username: admin #
# password: loggrove #
# #
# # # # # # # # # # # # # #
'''
)
if __name__ == '__main__':
main()