lib/cloudstack-nagios/commands/snmpd_config.rb in cloudstack-nagios-0.1.3 vs lib/cloudstack-nagios/commands/snmpd_config.rb in cloudstack-nagios-0.2.0
- old
+ new
@@ -1,37 +1,70 @@
require 'sshkit/dsl'
-require 'highline/import'
+require 'socket'
+require 'timeout'
class SnmpdConfig < CloudstackNagios::Base
- desc "check", "check if snpd is configured on virtual routers"
- def check
-
+ desc "snmpd_config check [vms]", "check if snmpd is configured on virtual routers"
+ def check(*vms)
+ if vms.size == 0
+ say 'Get a list of all routers from cloudstack..', :yellow
+ vms = router_ips(routers)
+ end
+ vms.each do |host|
+ begin
+ Timeout::timeout(1) do
+ socket = TCPSocket.new(host, "161")
+ socket.close
+ puts "port is open on host #{host}"
+ end
+ rescue => e
+ puts "port is closed on host #{host}"
+ end
+ end
end
- desc "enable", "enable snmpd configuration on virtual routers"
- def enable
- hosts = routers.map {|router| router['linklocalip']}
- on hosts, in: :sequence, wait: 5 do
- puts
- puts "On host #{host}"
- puts "____" * 20
- puts
- puts output = capture(:free, '-m')
- puts output =~ /Mem:\s+(\d+)\s+(\d+)/
- puts $1
- puts $2
- end
+ desc "snmpd_config enable [vms]", "enable snmpd configuration on virtual routers"
+ option :apt,
+ desc: 'use apt-get to install snmpd packages',
+ type: :boolean
+ def enable(*vms)
+ apt = options[:apt]
+ if vms.size == 0
+ say 'Get a list of all routers from cloudstack..', :yellow
+ vms = router_ips
+ end
+ hosts = vms.map do |router|
+ host = SSHKit::Host.new("root@#{router}")
+ host.ssh_options = sshoptions
+ host.port = 3922
+ host
+ end
+ say 'Connect to router and enable snmpd...', :yellow
+ on hosts, in: :sequence, wait: 10 do
+ begin
+ execute 'apt-get', 'update'
+ execute 'apt-get', '-y', 'install', 'snmpd'
+ upload! File.join(File.dirname(__FILE__), '..', 'files', 'snmpd.conf'), '/etc/snmp/snmpd.conf'
+ execute 'service', 'snmpd', 'restart'
+ execute 'iptables', '-A INPUT -p tcp -m tcp --dport 161 -j ACCEPT'
+ rescue StandardError => e
+ puts 'configuration failed!'
+ puts e.message
+ puts e.backtrace
+ end
+ end
end
no_commands do
- def snmp_hosts(host_names)
- hosts = host_names.map do |host_name|
- host = SSHKit::Host.new("root@#{host_name}")
- host
- end
- end
+ def router_ips(vrs = routers)
+ ips = []
+ vrs.each do |router|
+ ips << router['linklocalip'] unless router['linklocalip'] == nil
+ end
+ ips
+ end
- end
+ end
-end
\ No newline at end of file
+end