Simple Linux Panel
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mdserver-web/plugins/zabbix/index.py

315 lines
7.8 KiB

10 months ago
# coding:utf-8
import sys
import io
import os
import time
import re
sys.path.append(os.getcwd() + "/class/core")
import mw
app_debug = False
if mw.isAppleSystem():
app_debug = True
def getPluginName():
return 'zabbix'
def getPluginDir():
return mw.getPluginDir() + '/' + getPluginName()
def getServerDir():
return mw.getServerDir() + '/' + getPluginName()
def getInitDFile():
current_os = mw.getOs()
if current_os == 'darwin':
return '/tmp/' + getPluginName()
if current_os.startswith('freebsd'):
return '/etc/rc.d/' + getPluginName()
return '/etc/init.d/' + getPluginName()
def getConf():
10 months ago
path = getServerDir() + "/web_conf/nginx/vhost/zabbix.conf"
10 months ago
return path
def getInitDTpl():
path = getPluginDir() + "/init.d/" + getPluginName() + ".tpl"
return path
def getArgs():
args = sys.argv[3:]
tmp = {}
args_len = len(args)
if args_len == 1:
t = args[0].strip('{').strip('}')
if t.strip() == '':
tmp = []
else:
t = t.split(':')
tmp[t[0]] = t[1]
tmp[t[0]] = t[1]
elif args_len > 1:
for i in range(len(args)):
t = args[i].split(':')
tmp[t[0]] = t[1]
return tmp
def checkArgs(data, ck=[]):
for i in range(len(ck)):
if not ck[i] in data:
return (False, mw.returnJson(False, '参数:(' + ck[i] + ')没有!'))
return (True, mw.returnJson(True, 'ok'))
def configTpl():
path = getPluginDir() + '/tpl'
pathFile = os.listdir(path)
tmp = []
for one in pathFile:
file = path + '/' + one
tmp.append(file)
return mw.getJson(tmp)
def readConfigTpl():
args = getArgs()
data = checkArgs(args, ['file'])
if not data[0]:
return data[1]
content = mw.readFile(args['file'])
content = contentReplace(content)
return mw.returnJson(True, 'ok', content)
def getPidFile():
file = getConf()
content = mw.readFile(file)
rep = 'pidfile\s*(.*)'
tmp = re.search(rep, content)
return tmp.groups()[0].strip()
def status():
10 months ago
cmd = "ps aux|grep zabbix-server |grep -v grep | grep -v python | grep -v mdserver-web | awk '{print $2}'"
data = mw.execShell(cmd)
if data[0] == '':
10 months ago
return 'stop'
return 'start'
def contentReplace(content):
service_path = mw.getServerDir()
content = content.replace('{$ROOT_PATH}', mw.getRootDir())
content = content.replace('{$SERVER_PATH}', service_path)
10 months ago
content = content.replace('{$ZABBIX_ROOT}', '/usr/share/zabbix')
content = content.replace('{$ZABBIX_PORT}', '18888')
10 months ago
return content
10 months ago
10 months ago
def getMySQLConf():
10 months ago
path = mw.getServerDir() + '/mysql/etc/my.cnf'
return path
10 months ago
def getMySQLPort():
file = getMySQLConf()
10 months ago
content = mw.readFile(file)
rep = 'port\s*=\s*(.*)'
tmp = re.search(rep, content)
return tmp.groups()[0].strip()
10 months ago
def getMySQLSocketFile():
file = getMySQLConf()
10 months ago
content = mw.readFile(file)
rep = 'socket\s*=\s*(.*)'
tmp = re.search(rep, content)
return tmp.groups()[0].strip()
def pSqliteDb(dbname='databases'):
mysql_dir = mw.getServerDir() + '/mysql'
conn = mw.M(dbname).dbPos(mysql_dir, 'mysql')
return conn
def pMysqlDb():
# pymysql
db = mw.getMyORM()
10 months ago
db.setDbName('zabbix')
10 months ago
db.setPort(getMySQLPort())
db.setSocket(getMySQLSocketFile())
10 months ago
db.setPwd(pSqliteDb('config').where('id=?', (1,)).getField('mysql_root'))
return db
10 months ago
def zabbixNginxConf():
return mw.getServerDir()+'/web_conf/nginx/vhost/zabbix.conf'
10 months ago
def zabbixPhpConf():
return '/etc/zabbix/web/zabbix.conf.php'
10 months ago
10 months ago
def zabbixImportMySQLData():
10 months ago
pmdb = pMysqlDb()
10 months ago
psdb = pSqliteDb('databases')
find_ps_zabbix = psdb.field('id').where('name = ?', ('zabbix',)).select()
10 months ago
if len(find_ps_zabbix) < 1:
db_pass = mw.getRandomString(16)
# 创建数据
cmd = 'python3 plugins/mysql/index.py add_db {"name":"zabbix","codeing":"utf8mb4","db_user":"zabbix","password":"'+db_pass+'","dataAccess":"127.0.0.1","ps":"zabbix","address":"127.0.0.1"}'
# print(cmd)
mw.execShell(cmd)
10 months ago
pmdb.query("ALTER DATABASE `zabbix` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_bin")
10 months ago
10 months ago
db_pass = psdb.where('name = ?', ('zabbix',)).getField('password')
10 months ago
find_zabbix_version = pmdb.query("show tables like 'dbversion'")
if len(find_zabbix_version) == 0:
10 months ago
# 初始化导入数据
import_data_cmd = 'zcat /usr/share/zabbix-sql-scripts/mysql/server.sql.gz | /www/server/mysql/bin/mysql --default-character-set=utf8mb4 -uzabbix -p"'+db_pass+'" zabbix'
mw.execShell(import_data_cmd)
10 months ago
10 months ago
php_src_tpl = getPluginDir()+'/conf/zabbix.conf.php'
php_dst_path = zabbixPhpConf()
# php配置
if not os.path.exists(php_dst_path):
content = mw.readFile(php_src_tpl)
10 months ago
content = content.replace('{$ZABBIX_PORT}', getMySQLPort())
10 months ago
content = content.replace('{$ZABBIX_PASS}', db_pass)
mw.writeFile(php_dst_path, content)
10 months ago
return True
10 months ago
def initDreplace():
10 months ago
nginx_src_tpl = getPluginDir()+'/conf/zabbix.nginx.conf'
10 months ago
nginx_dst_vhost = zabbixNginxConf()
10 months ago
10 months ago
# nginx配置
if not os.path.exists(nginx_dst_vhost):
10 months ago
content = mw.readFile(nginx_src_tpl)
content = contentReplace(content)
10 months ago
mw.writeFile(nginx_dst_vhost, content)
# 导入MySQL配置
10 months ago
zabbixImportMySQLData()
10 months ago
return True
10 months ago
10 months ago
def zOp(method):
10 months ago
10 months ago
initDreplace()
10 months ago
current_os = mw.getOs()
if current_os.startswith("freebsd"):
10 months ago
data = mw.execShell('service zabbix-server ' + method)
10 months ago
if data[1] == '':
return 'ok'
return data[1]
10 months ago
data = mw.execShell('systemctl ' + method + ' zabbix-server')
10 months ago
if data[1] == '':
return 'ok'
return data[1]
def start():
10 months ago
return zOp('start')
10 months ago
def stop():
10 months ago
val = zOp('stop')
return val
10 months ago
def restart():
10 months ago
status = zOp('restart')
10 months ago
return status
def reload():
10 months ago
return zOp('reload')
10 months ago
def initdStatus():
current_os = mw.getOs()
if current_os == 'darwin':
return "Apple Computer does not support"
10 months ago
shell_cmd = 'systemctl status zabbix-server | grep loaded | grep "enabled;"'
10 months ago
data = mw.execShell(shell_cmd)
if data[0] == '':
return 'fail'
return 'ok'
def initdInstall():
current_os = mw.getOs()
if current_os == 'darwin':
return "Apple Computer does not support"
10 months ago
mw.execShell('systemctl enable zabbix-server')
10 months ago
return 'ok'
def initdUinstall():
current_os = mw.getOs()
if current_os == 'darwin':
return "Apple Computer does not support"
10 months ago
mw.execShell('systemctl disable zabbix-server')
10 months ago
return 'ok'
10 months ago
def installPreInspection():
openresty_dir = mw.getServerDir() + "/openresty"
if not os.path.exists(openresty_dir):
return '需要安装Openresty插件'
10 months ago
10 months ago
mysql_dir = mw.getServerDir() + "/mysql"
if not os.path.exists(mysql_dir):
return '需要安装MySQL插件,至少8.0!'
10 months ago
10 months ago
return 'ok'
10 months ago
10 months ago
def uninstallPreInspection():
return 'ok'
10 months ago
if __name__ == "__main__":
func = sys.argv[1]
if func == 'status':
print(status())
elif func == 'start':
print(start())
elif func == 'stop':
print(stop())
elif func == 'restart':
print(restart())
elif func == 'reload':
print(reload())
elif func == 'initd_status':
print(initdStatus())
elif func == 'initd_install':
print(initdInstall())
elif func == 'initd_uninstall':
print(initdUinstall())
10 months ago
elif func == 'install_pre_inspection':
print(installPreInspection())
elif func == 'uninstall_pre_inspection':
print(uninstallPreInspection())
10 months ago
elif func == 'conf':
10 months ago
print(zabbixNginxConf())
elif func == 'php_conf':
print(zabbixPhpConf())
10 months ago
elif func == 'run_log':
print(runLog())
elif func == 'config_tpl':
print(configTpl())
else:
print('error')