Merge pull request #484 from YunoHost/mysql_mariadb_get_status

Mysql mariadb get status
This commit is contained in:
Bram 2018-05-30 00:51:46 +02:00 committed by GitHub
commit 744f46098e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 19 deletions

View file

@ -18,6 +18,7 @@ redis-server:
log: /var/log/redis/redis-server.log log: /var/log/redis/redis-server.log
mysql: mysql:
log: [/var/log/mysql.log,/var/log/mysql.err] log: [/var/log/mysql.log,/var/log/mysql.err]
alternates: ['mariadb']
glances: {} glances: {}
ssh: ssh:
log: /var/log/auth.log log: /var/log/auth.log

View file

@ -227,26 +227,47 @@ def service_status(names=[]):
status = _get_service_information_from_systemd(name) status = _get_service_information_from_systemd(name)
translation_key = "service_description_%s" % name # try to get status using alternative version if they exists
description = m18n.n(translation_key) # this is for mariadb/mysql but is generic in case of
alternates = services[name].get("alternates", [])
while status is None and alternates:
status = _get_service_information_from_systemd(alternates.pop())
# that mean that we don't have a translation for this string if status is None:
# that's the only way to test for that for now logger.error("Failed to get status information via dbus for service %s, systemctl didn't recognize this service ('NoSuchUnit')." % name)
# if we don't have it, uses the one provided by systemd result[name] = {
if description == translation_key: 'status': "unknown",
description = str(status.get("Description", "")) 'loaded': "unknown",
'active': "unknown",
'active_at': {
"timestamp": "unknown",
"human": "unknown",
},
'description': "Error: failed to get information for this service, it doesn't exists for systemd",
'service_file_path': "unknown",
}
result[name] = { else:
'status': str(status.get("SubState", "unknown")), translation_key = "service_description_%s" % name
'loaded': "enabled" if str(status.get("LoadState", "unknown")) == "loaded" else str(status.get("LoadState", "unknown")), description = m18n.n(translation_key)
'active': str(status.get("ActiveState", "unknown")),
'active_at': { # that mean that we don't have a translation for this string
"timestamp": str(status.get("ActiveEnterTimestamp", "unknown")), # that's the only way to test for that for now
"human": datetime.fromtimestamp(status.get("ActiveEnterTimestamp") / 1000000).strftime("%F %X"), # if we don't have it, uses the one provided by systemd
}, if description == translation_key:
'description': description, description = str(status.get("Description", ""))
'service_file_path': str(status.get("FragmentPath", "unknown")),
} result[name] = {
'status': str(status.get("SubState", "unknown")),
'loaded': "enabled" if str(status.get("LoadState", "unknown")) == "loaded" else str(status.get("LoadState", "unknown")),
'active': str(status.get("ActiveState", "unknown")),
'active_at': {
"timestamp": str(status.get("ActiveEnterTimestamp", "unknown")),
"human": datetime.fromtimestamp(status["ActiveEnterTimestamp"] / 1000000).strftime("%F %X") if "ActiveEnterTimestamp" in status else "unknown",
},
'description': description,
'service_file_path': str(status.get("FragmentPath", "unknown")),
}
if len(names) == 1: if len(names) == 1:
return result[names[0]] return result[names[0]]
@ -256,13 +277,20 @@ def service_status(names=[]):
def _get_service_information_from_systemd(service): def _get_service_information_from_systemd(service):
"this is the equivalent of 'systemctl status $service'" "this is the equivalent of 'systemctl status $service'"
import dbus import dbus
from dbus.exceptions import DBusException
d = dbus.SystemBus() d = dbus.SystemBus()
systemd = d.get_object('org.freedesktop.systemd1','/org/freedesktop/systemd1') systemd = d.get_object('org.freedesktop.systemd1','/org/freedesktop/systemd1')
manager = dbus.Interface(systemd, 'org.freedesktop.systemd1.Manager') manager = dbus.Interface(systemd, 'org.freedesktop.systemd1.Manager')
service_path = manager.GetUnit(service + ".service") try:
service_path = manager.GetUnit(service + ".service")
except DBusException as exception:
if exception.get_dbus_name() == 'org.freedesktop.systemd1.NoSuchUnit':
return None
raise
service_proxy = d.get_object('org.freedesktop.systemd1', service_path) service_proxy = d.get_object('org.freedesktop.systemd1', service_path)
# unit_proxy = dbus.Interface(service_proxy, 'org.freedesktop.systemd1.Unit',) # unit_proxy = dbus.Interface(service_proxy, 'org.freedesktop.systemd1.Unit',)