-
Notifications
You must be signed in to change notification settings - Fork 2
/
bananainstall.py
121 lines (101 loc) · 2.4 KB
/
bananainstall.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
# coding=utf-8
# 生成配置文件
import time
import sys
import os
import sqlite3
server_config=\
'''[general]
type=server
pidfile=banana.pid
# 服务端默认配置
[service]
# 内存CPU报警阀值
alarm=60
# 数据接收端口
data_port=5000
# web服务端口
web_port=5001'''
client_config=\
'''[general]
type=client
pidfile=banana.pid
# 客户端默认配置
[client]
# 客户端ID
client_id=%s
# 服务端IP
server_ip=127.0.0.1
# 数据发送端口
server_port=5000
# 发送间隔
second=1'''
def write_server_cfg():
with open('banana.conf','w',encoding='utf-8') as f:
f.write(server_config)
def write_client_cfg():
id = str(int(round(time.time(),3)*1000))
with open('banana.conf','w',encoding='utf-8') as f:
f.write(client_config%id)
def find_db(db_name):
db='db/db'+db_name+'.db'
if os.path.exists(db):
return True
else:
return False
def rm_db(db_name):
db='db/db'+db_name+'.db'
if os.path.exists(db):
os.remove(db)
return True
else:
return False
def get_dblist():
path = os.getcwd()
dbls=os.listdir(path+'/db')
for i in dbls:
if 'journal' in i:
continue
if i.startswith('db'):
yield 'db/'+i
def create_database(db_name):
db='db/db'+db_name+'.db'
conn = sqlite3.connect(db)
c = conn.cursor()
# 实时状态表
c.execute('''CREATE TABLE realtime
(id TEXT primary key,
platform TEXT,
ip TEXT,
updatetime TEXT,
cpu REAL,
mem TEXT,
disk TEXT,
label TEXT,
message TEXT)''')
# 所有主机共享一张历史表,在服务端进程中创建
c.execute('''CREATE TABLE history
(id TEXT primary key,
updatetime TEXT,
cpu TEXT,
mem TEXT,
disk TEXT,
net TEXT)''')
conn.commit()
conn.close()
if __name__=='__main__':
if len(sys.argv) ==1:
print('[option]\n server \n client')
sys.exit()
else:
type=sys.argv[1]
if type=='server':
write_server_cfg()
elif type=='client':
write_client_cfg()
else:
print('[option]\n server \n client')
sys.exit()
# todo
# 如何生成系统服务文件,开机自启
# 如何生成系统服务 无需python启动