Class: Deckard::Util

Inherits:
Object
  • Object
show all
Defined in:
lib/deckard/util.rb

Class Method Summary collapse

Class Method Details

.alert(priority, subject, body, log, schedule, url) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/deckard/util.rb', line 35

def self.alert(priority, subject, body, log, schedule, url)
  email_to = Deckard::Config.email_to
  on_call_contacts = on_call()

  # if scheduled maintenance set to logging only
  if schedule(schedule) == true
    priority = 0
  end

  if priority == 0
    Deckard::Log.info(log)
  elsif priority == 1
    Deckard::Log.info(log)
    Deckard::Log.info("sending email alert to #{email_to}")
    send_email(email_to, subject, body)
  elsif priority == 2
    Deckard::Log.info(log)
    Deckard::Log.info("sending email alert to #{email_to}")
    send_email(email_to, subject, body)
    begin
      if on_call_contacts.has_key?("notifo_usernames")
        Deckard::Log.info("sending notifo alert to #{on_call_contacts["notifo_usernames"]}")
        send_notifo(on_call_contacts["notifo_usernames"], subject, url)
      else
        Deckard::Log.info("sending email alert to #{email_to} and sms to #{on_call_contacts["sms_email"]}")
        send_email(email_to, subject, body)
        send_email("#{on_call_contacts["sms_email"]}", subject, body)
      end
    rescue
      Deckard::Log.info("sending email alert to #{email_to} and sms to #{on_call_contacts["sms_email"]}")
      send_email(email_to, subject, body)
      send_email("#{on_call_contacts["sms_email"]}", subject, body)
    end
  end
end

.flip_failover(node) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/deckard/util.rb', line 123

def self.flip_failover(node)
  db_user = Deckard::Config.db_user
  db_password = Deckard::Config.db_password
  db_host = Deckard::Config.db_host
  db_port = Deckard::Config.db_port
  db_name = Deckard::Config.fo_check_db

  if db_user == "" || db_password == ""
    db_url = "http://#{db_host}:#{db_port}/#{db_name}"
  else
    db_url = "http://#{db_user}:#{db_password}@#{db_host}:#{db_port}/#{db_name}"
  end

  doc_json = RestClient.get("#{db_url}/#{node["_id"]}")
  doc = JSON.parse(doc_json)
  doc.store("failover", false)

  RestClient.put("#{db_url}/#{node["_id"]}?rev=#{node["_rev"]}", doc.to_json)
end

.get_nodes(db_name) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/deckard/util.rb', line 3

def self.get_nodes(db_name)
  doc_list = []
  node_list = []

  db_user = Deckard::Config.db_user
  db_password = Deckard::Config.db_password
  db_host = Deckard::Config.db_host
  db_port = Deckard::Config.db_port

  if db_user && db_password
    db_url = "http://#{db_user}:#{db_password}@#{db_host}:#{db_port}/#{db_name}"
  else
    db_url = "http://#{db_host}:#{db_port}/#{db_name}"
  end

  all_docs = RestClient.get("#{db_url}/_all_docs")

  all_docs_hash = JSON.parse(all_docs)

  all_docs_hash["rows"].each do |doc|
    doc_list << doc["id"]
  end

  doc_list.each do |doc|
    escaped_doc = CGI.escape(doc)
    node_json = RestClient.get("#{db_url}/#{escaped_doc}")
    node = JSON.parse(node_json)
    node_list << node
  end
  node_list
end

.on_callObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/deckard/util.rb', line 71

def self.on_call
  db_user = Deckard::Config.db_user
  db_password = Deckard::Config.db_password
  db_host = Deckard::Config.db_host
  db_port = Deckard::Config.db_port
  db_name = Deckard::Config.on_call_db
  doc_name = Deckard::Config.on_call_doc

  doc_url = "http://#{db_user}:#{db_password}@#{db_host}:#{db_port}/#{db_name}/#{doc_name}"

  on_call_json = RestClient.get doc_url
  on_call = JSON.parse(on_call_json)
  on_call
end

.schedule(schedule) ⇒ Object



115
116
117
118
119
120
121
# File 'lib/deckard/util.rb', line 115

def self.schedule(schedule)
  if schedule.nil?
    false
  else
    schedule.include? Time.now.hour
  end
end

.send_email(email_addr, subject, body) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/deckard/util.rb', line 86

def self.send_email(email_addr, subject, body)
  to = email_addr
  from = Deckard::Config.email_from
  host = Deckard::Config.email_host
  mail = TMail::Mail.new
  mail.to = to
  mail.from = from
  mail.subject = subject
  mail.date = Time.now
  mail.mime_version = '1.0'
  mail.body = body

  Net::SMTP.start( host ) do |smtp|
    smtp.send_message(
      mail.to_s,
      from,
      to
    )
  end
end

.send_notifo(usernames, subject, url) ⇒ Object



107
108
109
110
111
112
113
# File 'lib/deckard/util.rb', line 107

def self.send_notifo(usernames, subject, url)
  notifo = Notifo.new(Deckard::Config.notifo_user, Deckard::Config.notifo_apikey)
  usernames.each do |username|
    response = notifo.post(username, subject, "deckard alert", url)
    Deckard::Log.info("Notifo response: #{response}")
  end
end