Class: Jupiter::Nagios

Inherits:
Object
  • Object
show all
Defined in:
lib/jupiter/nagios.rb

Instance Method Summary collapse

Constructor Details

#initializeNagios

Returns a new instance of Nagios.



28
29
30
31
32
33
34
35
# File 'lib/jupiter/nagios.rb', line 28

def initialize
  @url        = Jupiter.nagios.fetch(:url)
  @location   = Jupiter.nagios.fetch(:location)
  @sshuser    = Jupiter.nagios.fetch(:sshuser)
  @sshpass    = Jupiter.nagios.fetch(:sshpass)
  @sshport    = Jupiter.nagios.fetch(:sshport)
  @host       = Jupiter.nagios.fetch(:host)
end

Instance Method Details

#action(choice, machines_name, machines_alias, machines_ip) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/jupiter/nagios.rb', line 122

def action(choice, machines_name, machines_alias, machines_ip)
  if (choice == 1)
    include_services = ask("\nWould you like to include default services? (yes / no): ")
    if (include_services == 'yes')
      puts "\n\n\n"
      puts generate_template(machines_name, machines_alias, machines_ip, 1)
      puts "\n\n\n"
    end
    if (include_services == 'no')
      puts "\n\n\n"
      puts generate_template(machines_name, machines_alias, machines_ip, 0)
      puts "\n\n\n"
    end
  elsif (choice == 2)
    include_services = ask("\nWould you like to include default services? (yes / no): ")
    if (include_services == 'yes')
      puts "\n\n\n"
      upload_nagios_config(machines_name, machines_alias, machines_ip, 1)
      puts "Completed adding nagios config for #{machines_name}. Please restart Nagios to see changes."
      puts "\n\n\n"
    end
    if (include_services == 'no')
      puts "\n\n\n"
      upload_nagios_config(machines_name, machines_alias, machines_ip, 0)
      puts "Added Nagios configuration for #{machines_name}. Please restart Nagios to see changes."
      puts "\n\n\n"
    end
  else
    raise InvalidSelectionError.new("Not a valid selection.")
  end
end

#ask(question) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/jupiter/nagios.rb', line 37

def ask(question)
  print question + " "
  answer = STDIN.gets.chomp
  if answer == "" or answer.nil?
    return nil
  else
    return answer
  end
end

#close_sshObject



176
177
178
179
180
# File 'lib/jupiter/nagios.rb', line 176

def close_ssh
  ssh.close
  @ssh = nil
  true
end

#generate_template(machines_name, machines_alias, machines_ip, services) ⇒ Object



154
155
156
157
158
159
160
161
162
163
# File 'lib/jupiter/nagios.rb', line 154

def generate_template(machines_name, machines_alias, machines_ip, services)
  host = host_template(machines_name, machines_alias, machines_ip)
  serv = services_template(machines_name)
  if (services == 1)
    template = "########## GENERATED BY JUPITER ##########\n#{host}#{serv}\n\n########## GENERATED BY JUPITER ##########"
  else
    template = "########## GENERATED BY JUPITER ##########\n#{host}\n\n########## GENERATED BY JUPITER ##########"
  end
  return template
end

#host_template(machines_name, machines_alias, machines_ip) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/jupiter/nagios.rb', line 47

def host_template(machines_name, machines_alias, machines_ip)
  template_host = Jupiter::TextTemplate.new(
    "\ndefine host {"\
    "\n  use                   linux-server"\
    "\n  host_name             :::HOSTNAME:::"\
    "\n  alias                 :::HOSTALIAS:::"\
    "\n  address               :::IPADDRESS:::"\
    "\n  statusmap_image       linux40.gd2"\
    "\n  parents               GATEWAY1"\
    "\n}\n"
  )
  template_host.set( 'HOSTNAME', machines_name)
  template_host.set( 'HOSTALIAS', machines_alias)
  template_host.set( 'IPADDRESS', machines_ip)

  return template_host
end

#services_template(machines_name) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/jupiter/nagios.rb', line 65

def services_template(machines_name)
  template_services = Jupiter::TextTemplate.new(
   "\ndefine service {"\
    "\n  use                   local-service"\
    "\n  host_name             :::HOSTNAME:::"\
    "\n  service_description   Root Partition"\
    "\n  check_command         check_local_disk!20%!10%!/"\
    "\n  notifications_enabled 1"\
    "\n}\n"\
    "\ndefine service{"\
    "\n  use                   local-service"\
    "\n  host_name             :::HOSTNAME:::"\
    "\n  service_description   Current Users"\
    "\n  check_command         check_local_users!20!50"\
    "\n  notifications_enabled 1"\
    "\n}\n"\
    "\ndefine service{"\
    "\n  use                   local-service"\
    "\n  host_name             :::HOSTNAME:::"\
    "\n  service_description   Total Processes"\
    "\n  check_command         check_local_procs!250!400!RSZDT"\
    "\n  notifications_enabled 1"\
    "\n}\n"\
    "\ndefine service{"\
    "\n  use                   local-service"\
    "\n  host_name             :::HOSTNAME:::"\
    "\n  service_description   Current Load"\
    "\n  check_command         check_local_load!5.0,4.0,3.0!10.0,6.0,4.0"\
    "\n  notifications_enabled 1"\
    "\n}\n"\
    "\ndefine service{"\
    "\n  use                   local-service"\
    "\n  host_name             :::HOSTNAME:::"\
    "\n  service_description   Swap Usage"\
    "\n  check_command         check_local_swap!20!10"\
    "\n  notifications_enabled 1"\
    "\n}\n"\
    "\ndefine service{"\
    "\n  use                   local-service"\
    "\n  host_name             :::HOSTNAME:::"\
    "\n  service_description   SSH"\
    "\n  check_command         check_ssh!-p 2500"\
    "\n  notifications_enabled 0"\
    "\n}\n"\
    "\ndefine service{"\
    "\n  use                   local-service"\
    "\n  host_name             :::HOSTNAME:::"\
    "\n  service_description   HTTP"\
    "\n  check_command         check_http"\
    "\n  notifications_enabled 0"\
    "\n}"\
  )
  template_services.set( 'HOSTNAME', machines_name)

  return template_services
end

#sshObject



172
173
174
# File 'lib/jupiter/nagios.rb', line 172

def ssh
  @ssh ||= Net::SSH.start(@host, @sshuser, password: @sshpass, port: @sshport)
end

#upload_nagios_config(machines_name, machines_alias, machines_ip, services) ⇒ Object



165
166
167
168
169
170
# File 'lib/jupiter/nagios.rb', line 165

def upload_nagios_config(machines_name, machines_alias, machines_ip, services)
  template = generate_template(machines_name, machines_alias, machines_ip, services)
  puts template
  puts ssh.exec!(%Q[echo "#{template}" > #{@location}/#{machines_name}.cfg])
  close_ssh
end