Skip to content

Instantly share code, notes, and snippets.

@picatz
Created November 6, 2016 07:06
Show Gist options
  • Save picatz/fb960cf2454430956aea05827f3c737d to your computer and use it in GitHub Desktop.
Save picatz/fb960cf2454430956aea05827f3c737d to your computer and use it in GitHub Desktop.
Simple fosl statistics
require 'fosl/parser'
# -P : Do not resolve port names
# -n : Do not resolve hostnames
parser = FOSL::Parser.new
all_data = parser.lsof("-Pn")
# store statistics
stats = Hash.new(0)
stats[:users] = []
stats[:types] = Hash.new(0)
# iterate through all the data and generate basic stats
stats[:processes] = all_data.keys.count
all_data.each do |data|
pid = data[0]
data = data[1]
stats[:listeners] += data.listeners.count
stats[:users] << data.login unless stats[:users].include? data.login
stats[:files] += data.files.count
data.files.each do |file|
stats[:types][file[:type]] += 1
end
end
# => example output ...
# stats = {
# :users=>["root", "statd", "daemon", "messagebus", "Debian-exim", "vagrant"],
# :processes=>72,
# :listeners=>10,
# :files=>1098,
# :types=>{"DIR"=>151, "REG"=>637, "CHR"=>120, "0000"=>21, "netlink"=>11, "unix"=>67, "FIFO"=>27, "unknown"=>37, "sock"=>2, "IPv4"=>15, "IPv6"=>9, "pack"=>1}
# }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment