Last active
March 16, 2017 09:04
-
-
Save Pimentoso/4acf7b89ce903d8c2ffe6fe6c2652f5b to your computer and use it in GitHub Desktop.
Ruby systemctl show / systemctl status output parser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SystemdParser | |
SHOW_PROPERTIES = ['LoadState', 'ActiveState', 'SubState', 'MainPID', 'ExecMainStartTimestamp'] | |
# Calls systemctl show and returns a properties hash. | |
# @param user: adds the '--user' flag if true. | |
# @param properties: pass an empty array to get all of them (very verbose). | |
# Example output: | |
# => {"MainPID"=>"1477", "ExecMainStartTimestamp"=>"Wed 2017-03-15 11:01:59 CET", "LoadState"=>"loaded", "ActiveState"=>"active", "SubState"=>"running"} | |
def self.systemctl_show(service:, user: false, properties: SHOW_PROPERTIES) | |
user_param = user ? '--user' : '' | |
property_param = properties.any? ? "--property=#{properties.join(',')}" : '' | |
output = `systemctl #{user_param} show #{property_param} #{service}` | |
return output.split("\n").map{|l| l.split('=',2)}.to_h | |
end | |
STATUS_PROPERTIES = ['Loaded', 'Active', 'Main PID', 'Tasks', 'Memory', 'CPU'] | |
# Calls systemctl status and returns a structured hash. | |
# @param service: you can use an instance name or PID. Also accepts wildcards. Examples: | |
# SystemdParser.systemctl_status(service: 'ssh') | |
# SystemdParser.systemctl_status(service: 24722) | |
# SystemdParser.systemctl_status(service: 'test-*@*') | |
# @param user: adds the '--user' flag if true. | |
# Example output: | |
# => {"ssh.service"=>{"Loaded"=>"loaded", "Active"=>"active", "Main PID"=>"1477", "Tasks"=>"1", "Memory"=>"7.0M", "CPU"=>"49ms"}} | |
def self.systemctl_status(service:, user: false) | |
user_param = user ? '--user' : '' | |
service_param = "'#{service}'" | |
output = `systemctl #{user_param} status #{service_param}` | |
output = output.split("\n") | |
status = {} | |
current_service = '' | |
output.each do |line| | |
if line.start_with?('●') | |
current_service = line[2..-1].split(' ', 2)[0].strip | |
status[current_service] ||= {} | |
else | |
begin | |
attr = line.split(':', 2) | |
key = attr[0].strip | |
if STATUS_PROPERTIES.include? key | |
val = attr[1].split('(', 2)[0].strip | |
status[current_service][key] = val | |
end | |
rescue | |
end | |
end | |
end | |
return status | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment