Files
WPIQ/wo/cli/plugins/info.py

242 lines
9.9 KiB
Python
Raw Normal View History

2019-09-04 20:36:15 +02:00
"""WOInfo Plugin for WordOps"""
import configparser
import os
from cement.core.controller import CementBaseController, expose
from wo.core.aptget import WOAptGet
from wo.core.logging import Log
2023-08-12 14:44:56 +02:00
from wo.core.variables import WOVar
from wo.core.mysql import WOMysql
2019-09-04 20:36:15 +02:00
def wo_info_hook(app):
pass
class WOInfoController(CementBaseController):
class Meta:
label = 'info'
stacked_on = 'base'
stacked_type = 'nested'
description = ('Display configuration information related to '
'OpenLiteSpeed, PHP and MySQL')
2019-09-04 20:36:15 +02:00
arguments = [
(['--mysql'],
dict(help='Get MySQL configuration information',
action='store_true')),
(['--php'],
2023-08-12 14:44:56 +02:00
dict(help='Get PHP configuration information',
2022-12-10 10:37:20 -03:00
action='store_true')),
2019-09-04 20:36:15 +02:00
(['--nginx'],
dict(help='Get OpenLiteSpeed configuration information',
2019-09-04 20:36:15 +02:00
action='store_true')),
]
usage = "wo info [options]"
2023-08-12 14:44:56 +02:00
for php_version, php_number in WOVar.wo_php_versions.items():
arguments.append(([f'--{php_version}'],
dict(help=f'Get PHP {php_number} configuration information',
action='store_true')))
2019-09-04 20:36:15 +02:00
@expose(hide=True)
def info_ols(self):
"""Display OpenLiteSpeed information"""
version = os.popen("{0} -v 2>&1 | head -1"
.format(WOVar.wo_ols_bin)).read().strip()
httpd_conf = '{0}/httpd_config.conf'.format(WOVar.wo_ols_conf_dir)
server_name = os.popen("hostname -f 2>/dev/null || hostname"
).read().strip()
# Parse OLS httpd_config.conf for key settings
max_connections = ''
max_ssl_connections = ''
keepalive_timeout = ''
gzip_compress = ''
brotli_compress = ''
quic_enabled = ''
if os.path.isfile(httpd_conf):
with open(httpd_conf, 'r', encoding='utf-8') as f:
for line in f:
stripped = line.strip()
parts = stripped.split(None, 1)
if len(parts) == 2:
key, val = parts
if key == 'maxConnections':
max_connections = val
elif key == 'maxSSLConnections':
max_ssl_connections = val
elif key == 'keepAliveTimeout':
keepalive_timeout = val
elif key == 'enableGzipCompress':
gzip_compress = 'On' if val == '1' else 'Off'
elif key == 'enableBr':
brotli_compress = 'On' if val == '1' else 'Off'
elif key == 'enableQuic':
quic_enabled = 'On' if val == '1' else 'Off'
data = dict(version=version, server_name=server_name,
max_connections=max_connections,
max_ssl_connections=max_ssl_connections,
2019-09-04 20:36:15 +02:00
keepalive_timeout=keepalive_timeout,
gzip_compress=gzip_compress,
brotli_compress=brotli_compress,
quic_enabled=quic_enabled)
self.app.render((data), 'info_ols.mustache')
2019-09-04 20:36:15 +02:00
@expose(hide=True)
def info_php(self):
"""Display PHP information"""
2024-06-01 12:00:22 +02:00
pargs = self.app.pargs
for parg_version, dot_ver in WOVar.wo_php_versions.items():
short_ver = dot_ver.replace('.', '')
if WOAptGet.is_installed(self, 'lsphp{0}'.format(short_ver)):
setattr(pargs, parg_version, True)
else:
Log.info(self, "PHP {0} is not installed".format(dot_ver))
2019-09-04 20:36:15 +02:00
for parg_version, dot_ver in WOVar.wo_php_versions.items():
if getattr(pargs, parg_version, False):
short_ver = dot_ver.replace('.', '')
self._info_lsphp(short_ver, dot_ver)
2019-09-04 20:36:15 +02:00
@expose(hide=True)
def _info_lsphp(self, short_ver, dot_ver):
"""Display LSPHP information for a given version"""
php_bin = '/usr/local/lsws/lsphp{0}/bin/php'.format(short_ver)
php_ini = ('/usr/local/lsws/lsphp{0}/etc/php/{1}'
'/litespeed/php.ini'.format(short_ver, dot_ver))
version = os.popen("{0} -v 2>/dev/null | "
2022-01-23 15:14:24 -03:00
"head -n1 | cut -d' ' -f2 |"
" cut -d'+' -f1 | tr -d '\\n'"
.format(php_bin)).read()
2022-01-23 15:14:24 -03:00
2022-12-10 10:37:20 -03:00
config = configparser.ConfigParser()
if os.path.isfile(php_ini):
config.read(php_ini)
2022-12-10 10:37:20 -03:00
else:
Log.info(self, "LSPHP {0} php.ini not found at {1}"
.format(dot_ver, php_ini))
return
2022-12-10 10:37:20 -03:00
try:
expose_php = config['PHP']['expose_php']
except KeyError:
expose_php = 'N/A'
2023-12-20 23:22:22 -03:00
try:
memory_limit = config['PHP']['memory_limit']
except KeyError:
memory_limit = 'N/A'
2024-12-03 14:08:19 +01:00
try:
post_max_size = config['PHP']['post_max_size']
except KeyError:
post_max_size = 'N/A'
2024-12-03 14:08:19 +01:00
try:
upload_max_filesize = config['PHP']['upload_max_filesize']
except KeyError:
upload_max_filesize = 'N/A'
2023-12-20 23:22:22 -03:00
try:
max_execution_time = config['PHP']['max_execution_time']
except KeyError:
max_execution_time = 'N/A'
2023-12-20 23:22:22 -03:00
data = dict(version=version, expose_php=expose_php,
memory_limit=memory_limit, post_max_size=post_max_size,
upload_max_filesize=upload_max_filesize,
max_execution_time=max_execution_time,
www_listen='LSAPI (managed by OLS)',
www_ping_path='N/A',
www_pm_status_path='N/A', www_pm='N/A',
www_pm_max_requests='N/A',
www_pm_max_children='N/A',
www_pm_start_servers='N/A',
www_pm_min_spare_servers='N/A',
www_pm_max_spare_servers='N/A',
www_request_terminate_timeout='N/A',
www_xdebug_profiler_enable_trigger='N/A',
debug_listen='N/A', debug_ping_path='N/A',
debug_pm_status_path='N/A',
debug_pm='N/A',
debug_pm_max_requests='N/A',
debug_pm_max_children='N/A',
debug_pm_start_servers='N/A',
debug_pm_min_spare_servers='N/A',
debug_pm_max_spare_servers='N/A',
debug_request_terminate_timeout='N/A',
debug_xdebug_profiler_enable_trigger='N/A')
2023-12-20 23:22:22 -03:00
self.app.render((data), 'info_php.mustache')
2019-09-04 20:36:15 +02:00
@expose(hide=True)
def info_mysql(self):
"""Display MySQL information"""
2024-06-07 16:28:33 +02:00
if os.path.exists('/usr/bin/mariadb'):
mariadb_exec = "/usr/bin/mariadb"
else:
mariadb_exec = "/usr/bin/mysql"
version = os.popen(f"{mariadb_exec} -V |"
"awk '{print($5)}' | "
2019-09-04 20:36:15 +02:00
"cut -d ',' "
"-f1 | tr -d '\n'").read()
host = "localhost"
2024-06-07 16:28:33 +02:00
port = os.popen(f"{mariadb_exec} -e \"show variables\" | "
2019-09-04 20:36:15 +02:00
"/bin/grep ^port | awk "
"'{print($2)}' | tr -d '\n'").read()
2024-06-07 16:28:33 +02:00
wait_timeout = os.popen(f"{mariadb_exec} -e \"show variables\" | grep "
2019-09-04 20:36:15 +02:00
"^wait_timeout | awk '{print($2)}' | "
"tr -d '\n'").read()
2024-06-07 16:28:33 +02:00
interactive_timeout = os.popen(f"{mariadb_exec} -e "
2019-09-04 20:36:15 +02:00
"\"show variables\" | grep "
"^interactive_timeout | awk "
"'{print($2)}' | tr -d '\n'").read()
2024-06-07 16:28:33 +02:00
max_used_connections = os.popen(f"{mariadb_exec} - e "
2019-09-04 20:36:15 +02:00
"\"show global status\" | "
"grep Max_used_connections | awk "
"'{print($2)}' | tr -d '\n'").read()
2024-06-07 16:28:33 +02:00
datadir = os.popen(f"{mariadb_exec} -e \"show variables\" | "
2019-09-04 20:36:15 +02:00
"/bin/grep datadir | awk"
" '{print($2)}' | tr -d '\n'").read()
2024-06-07 16:28:33 +02:00
socket = os.popen(f"{mariadb_exec} -e \"show variables\" | "
2019-09-04 20:36:15 +02:00
"/bin/grep \"^socket\" | "
"awk '{print($2)}' | tr -d '\n'").read()
data = dict(version=version, host=host, port=port,
wait_timeout=wait_timeout,
interactive_timeout=interactive_timeout,
max_used_connections=max_used_connections,
datadir=datadir, socket=socket)
self.app.render((data), 'info_mysql.mustache')
@expose(hide=True)
def default(self):
"""default function for info"""
pargs = self.app.pargs
if (not pargs.nginx and not pargs.php and not pargs.mysql):
pargs.nginx = True
pargs.mysql = True
2024-06-01 12:00:22 +02:00
pargs.php = True
2019-09-04 20:36:15 +02:00
if pargs.nginx:
if ((not WOAptGet.is_installed(self, 'openlitespeed')) and
(not os.path.exists(WOVar.wo_ols_bin))):
Log.info(self, "OpenLiteSpeed is not installed")
2019-10-30 04:51:56 +01:00
else:
self.info_ols()
2019-09-04 20:36:15 +02:00
2024-06-01 12:00:22 +02:00
if pargs.php:
self.info_php()
2023-12-20 23:22:22 -03:00
if pargs.mysql:
if WOMysql.mariadb_ping(self):
2019-09-04 20:36:15 +02:00
self.info_mysql()
else:
Log.info(self, "MySQL is not installed")
2019-09-04 20:36:15 +02:00
def load(app):
# register the plugin class.. this only happens if the plugin is enabled
2019-09-24 00:01:20 +02:00
app.handler.register(WOInfoController)
2019-09-04 20:36:15 +02:00
# register a hook (function) to run after arguments are parsed.
2019-09-24 00:04:32 +02:00
app.hook.register('post_argument_parsing', wo_info_hook)