Class: Mutx::Alert

Inherits:
Object
  • Object
show all
Defined in:
lib/mutx/models/alert.rb

Defined Under Namespace

Modules: Script

Constant Summary collapse

ALERT_FOLDER =
File.join("#{Dir.pwd}", 'alerts')
STATUS =
{
  ok: 0,
  unknown: 1,
  skip: 2,
  warning: 3,
  critical: 4
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Alert

Returns a new instance of Alert.



64
65
66
67
68
69
# File 'lib/mutx/models/alert.rb', line 64

def initialize(filename)
  @filename = filename
  extract_info
  @enqueue = true
  @job = Sidekiq::Cron::Job.find(name) || ((@enqueue = false) || create_job )
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



62
63
64
# File 'lib/mutx/models/alert.rb', line 62

def attributes
  @attributes
end

#filenameObject (readonly)

Returns the value of attribute filename.



62
63
64
# File 'lib/mutx/models/alert.rb', line 62

def filename
  @filename
end

#jobObject (readonly)

Returns the value of attribute job.



62
63
64
# File 'lib/mutx/models/alert.rb', line 62

def job
  @job
end

#notificationObject (readonly)

Returns the value of attribute notification.



62
63
64
# File 'lib/mutx/models/alert.rb', line 62

def notification
  @notification
end

Class Method Details

.allObject



43
44
45
46
47
# File 'lib/mutx/models/alert.rb', line 43

def self.all
  alerts =  Dir.entries(ALERT_FOLDER).select { |file|
              file.match(/_alert.rb$/)        
            }.map {|file| self.new(file) }
end

.find(args = {}) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/mutx/models/alert.rb', line 49

def self.find(args={})
  if args.is_a? Hash
    filename = "#{args[:name]}_alert.rb"
    if File.exists?(File.join(ALERT_FOLDER, filename))
      return self.new(filename)
    else
      return nil
    end
  else
    return nil    
  end
end

Instance Method Details

#clearObject



135
136
137
138
139
# File 'lib/mutx/models/alert.rb', line 135

def clear
  Sidekiq.redis do |redis|
    redis.del("alerts:#{name}")
  end
end

#cronObject



87
88
89
# File 'lib/mutx/models/alert.rb', line 87

def cron
  attributes['cron']
end

#emailObject



75
76
77
# File 'lib/mutx/models/alert.rb', line 75

def email
  attributes['email']
end

#enqueue?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/mutx/models/alert.rb', line 95

def enqueue?
  @enqueue
end

#last_enqueueObject



91
92
93
# File 'lib/mutx/models/alert.rb', line 91

def last_enqueue
  @job.last_enqueue_time
end

#nameObject



71
72
73
# File 'lib/mutx/models/alert.rb', line 71

def name
  filename.gsub('_alert.rb', '')
end

#off!Object



107
108
109
110
# File 'lib/mutx/models/alert.rb', line 107

def off!
  @job.destroy
  @enqueue = false
end

#on!Object



99
100
101
102
103
104
105
# File 'lib/mutx/models/alert.rb', line 99

def on!
  if @job.valid?
    @job.save
    @enqueue = true
  end
  return self.enqueue?
end

#resultObject



123
124
125
126
127
# File 'lib/mutx/models/alert.rb', line 123

def result
  Sidekiq.redis do |redis|
    redis.get("alerts:#{name}")
  end
end

#result=(value) ⇒ Object



129
130
131
132
133
# File 'lib/mutx/models/alert.rb', line 129

def result=(value)
  Sidekiq.redis do |redis|
    redis.set("alerts:#{name}", value.to_s)
  end
end

#slackObject



83
84
85
# File 'lib/mutx/models/alert.rb', line 83

def slack
  attributes['slack']
end

#statusObject



112
113
114
# File 'lib/mutx/models/alert.rb', line 112

def status
  STATUS.invert[self.result.to_i] || :unknown
end

#telegramObject



79
80
81
# File 'lib/mutx/models/alert.rb', line 79

def telegram
  attributes['telegram']
end

#update_status(a_new_status, info: nil) ⇒ Object



116
117
118
119
120
121
# File 'lib/mutx/models/alert.rb', line 116

def update_status(a_new_status, info: nil)
  last_result = self.result
  return if ((last_result.to_s == a_new_status.to_s) or (a_new_status.to_i == STATUS[:skip]))
  self.result = a_new_status
  notify(info) if last_result || a_new_status.to_i == STATUS[:unknown]
end