module Nexpose::Alert

Alert base behavior. The supported three alert types should have these properties and behaviors

Attributes

alert_type[RW]

Alert type and its configuration. One of SMTPAlert, SyslogAlert, SNMPAlert

enabled[RW]

Whether or not this alert is currently active.

id[RW]

ID for this alert.

max_alerts[RW]

Send at most this many alerts per scan.

name[RW]

Name for this alert.

server[RW]

Server target the alerts

server_port[RW]

Server port

Public Class Methods

create(hash) click to toggle source
# File lib/nexpose/alert.rb, line 133
def self.create(hash)
  alert_type = hash[:alert_type]
  raise 'An alert must have an alert type' if alert_type.nil?
  raise 'Alert name cannot be empty.' if !hash.key?(:name) || hash[:name].to_s == ''
  raise 'SNMP and Syslog alerts must have a server defined' if ['SNMP', 'Syslog'].include?(alert_type) && hash[:server].to_s == ''

  case alert_type
  when 'SMTP'
    alert = SMTPAlert.new(hash[:name],
                          hash[:sender],
                          hash[:server],
                          hash[:recipients],
                          hash[:enabled],
                          hash[:max_alerts],
                          hash[:verbose])
  when 'SNMP'
    alert = SNMPAlert.new(hash[:name],
                          hash[:community],
                          hash[:server],
                          hash[:enabled],
                          hash[:max_alerts])
  when 'Syslog'
    alert = SyslogAlert.new(hash[:name],
                            hash[:server],
                            hash[:enabled],
                            hash[:max_alerts])
  else
    fail "Unknown alert type: #{alert_type}"
  end

  alert.scan_filter = ScanFilter.new
  alert.vuln_filter = VulnFilter.new
  alert
end
json_initializer(hash) click to toggle source
# File lib/nexpose/alert.rb, line 101
def self.json_initializer(hash)
  create(hash)
end
list_alerts(nsc, site_id) click to toggle source

load a list of alerts for a given site

# File lib/nexpose/alert.rb, line 94
def self.list_alerts(nsc, site_id)
  uri  = "/api/2.1/site_configurations/#{site_id}/alerts"
  resp = AJAX.get(nsc, uri, AJAX::CONTENT_TYPE::JSON)
  data = JSON.parse(resp, symbolize_names: true)
  load_alerts(data) unless data.nil?
end
load(nsc, site_id, alert_id) click to toggle source

load a particular site alert

# File lib/nexpose/alert.rb, line 78
def self.load(nsc, site_id, alert_id)
  uri  = "/api/2.1/site_configurations/#{site_id}/alerts/#{alert_id}"
  resp = AJAX.get(nsc, uri, AJAX::CONTENT_TYPE::JSON)

  unless resp.to_s == ''
    data = JSON.parse(resp, symbolize_names: true)
    json_initializer(data).deserialize(data)
  end
end
load_alerts(alerts) click to toggle source

load alerts from an array of hashes

# File lib/nexpose/alert.rb, line 89
def self.load_alerts(alerts)
  alerts.map { |hash| json_initializer(hash).deserialize(hash) }
end

Public Instance Methods

delete(nsc, site_id) click to toggle source

delete an alert from the given site

# File lib/nexpose/alert.rb, line 114
def delete(nsc, site_id)
  uri = "/api/2.1/site_configurations/#{site_id}/alerts/#{id}"
  AJAX.delete(nsc, uri, AJAX::CONTENT_TYPE::JSON)
end
save(nsc, site_id) click to toggle source

save an alert for a given site

# File lib/nexpose/alert.rb, line 120
def save(nsc, site_id)
  validate
  uri = "/api/2.1/site_configurations/#{site_id}/alerts"
  id  = AJAX.put(nsc, uri, self.to_json, AJAX::CONTENT_TYPE::JSON)
  @id = id.to_i
end
to_h() click to toggle source
# File lib/nexpose/alert.rb, line 105
def to_h
  to_hash(Hash.new)
end
to_json() click to toggle source
# File lib/nexpose/alert.rb, line 109
def to_json
  serialize
end
validate() click to toggle source
# File lib/nexpose/alert.rb, line 127
def validate
  raise ArgumentError.new('Name is a required attribute.') unless @name
  raise ArgumentError.new('Scan filter is a required attribute.') unless @scan_filter
  raise ArgumentError.new('Vuln filter is a required attribute.') unless @vuln_filter
end