Skip to content

Instantly share code, notes, and snippets.

@pmbuko
Created August 8, 2014 16:16
Show Gist options
  • Save pmbuko/c7ca70f2ffc5378047ef to your computer and use it in GitHub Desktop.
Save pmbuko/c7ca70f2ffc5378047ef to your computer and use it in GitHub Desktop.
Custom facter fact to report the primary user, determined by greatest total login time. The paths are for linux. Adjust as necessary.
# primary_user.rb
Facter.add("primary_user") do
setcode do
%x{if [ -f /usr/bin/ac ]; then /usr/bin/ac -p | /bin/sort -nk 2 | /bin/awk '/total/{print x};{x=$1}';fi}.chomp
end
end
@dayglojesus
Copy link

I love this idea. Thanks, Peter.

# Obfuscated with Ruby
# needs to split on 2 or more whitespace to handle user names with spaces
Facter.add("primary_user") do
  setcode do
    (`ac -p`.split(/\n/).map { |l| l.strip!.split(/\s{2,}/) }.sort_by { |a| a.last.to_f })[-2][0]
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment