patternrubyMinor
Logging the load balance
Viewed 0 times
loggingbalancetheload
Problem
I created a load balancing server at work and have to keep track of which server it adds the user to, so I wrote a little Ruby script that will do just that.
I'm looking for any critique of my work, can I do anything better at all.
Source(user information, directory information, and server information changed for security):
Example of log:
I'm looking for any critique of my work, can I do anything better at all.
Source(user information, directory information, and server information changed for security):
#!/local/usr/bin/ruby
require 'net/ssh'
@username = 'user'
@password = nil
@host = 'server_to_run_command_on'
def loadbalance_server_data(user)
check = Net::SSH.start(@host, @username, :password => @password)
cmd = "finduser #{user}" #<= Custom command that finds user info
res = check.exec!(cmd)
data = res
write_data(data)
end
def write_data(data)
File.open("path/to/log.txt", "a+"){ |s| s.puts(data) }
end
user_list = %w(user user user).each do |user|
loadbalance_server_data(user)
endExample of log:
Querying: servers..
Server usage report for user
server_name:
server_name:
server_name:
server_name:
server_name:
server_name:
server_name: dbus-daemon gvfsd thunderbird #<= These programs always run on this server no matter what server they are logged in on
server_name:
server_name:
server_name: dbus-daemon dbus-launch do_4000_clone_u execrulz gam_server gnome-screensav gnome-settings- gnome-vfs-daemo newphp openbox openbox-session qccm soffice soffice.bin tint2 umessage #<= The server the user is logged in on
server_name:Solution
Constants in UPPERCASE
should be constants, as well as:
Marking a variable as constant in Ruby just needs upper-casing its name.
Return value simplification
Is unnecessarily long just write:
Other then this minor remarks, the code looks as straightforward and readable as it can get.
@username = 'user'
@password = nil
@host = 'server_to_run_command_on'should be constants, as well as:
LOGFILE = "path/to/log.txt"Marking a variable as constant in Ruby just needs upper-casing its name.
Return value simplification
res = check.exec!(cmd)
data = res
write_data(data)Is unnecessarily long just write:
write_data( check.exec!(cmd) )Other then this minor remarks, the code looks as straightforward and readable as it can get.
Code Snippets
@username = 'user'
@password = nil
@host = 'server_to_run_command_on'LOGFILE = "path/to/log.txt"res = check.exec!(cmd)
data = res
write_data(data)write_data( check.exec!(cmd) )Context
StackExchange Code Review Q#115505, answer score: 3
Revisions (0)
No revisions yet.