Class: Perus::Server::System

Inherits:
Sequel::Model
  • Object
show all
Defined in:
lib/perus/server/models/system.rb

Instance Method Summary collapse

Instance Method Details

#action_hashesObject



48
49
50
51
52
53
# File 'lib/perus/server/models/system.rb', line 48

def action_hashes
    # actions referring to a command_config return a hash when calling
    # config_hashes, actions referring to scripts return an array.
    # flatten is needed to merge the two response types together.
    pending_actions.collect(&:config_hashes).flatten
end

#after_destroyObject



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/perus/server/models/system.rb', line 22

def after_destroy
    super

    # remove dependent records
    metrics.each(&:destroy)
    values.each(&:destroy)
    actions.each(&:destroy)
    collection_errors.each(&:destroy)
    active_alerts.each(&:destroy)

    # remove any uploaded files
    FileUtils.rm_rf([uploads_dir], secure: true)
end

#alert_classObject



36
37
38
39
40
41
42
# File 'lib/perus/server/models/system.rb', line 36

def alert_class
    return '' if active_alerts.empty?
    severities = active_alerts.map(&:severity).uniq
    return 'error' if severities.include?('error')
    return 'warning' if severities.include?('warning')
    return 'notice' if severities.include?('notice')
end

#config_hashObject



55
56
57
58
59
60
61
# File 'lib/perus/server/models/system.rb', line 55

def config_hash
    {
        id: id,
        metrics: config.metric_hashes,
        actions: pending_actions.map(&:config_hash).flatten
    }
end

#latest(name) ⇒ Object



116
117
118
# File 'lib/perus/server/models/system.rb', line 116

def latest(name)
    values_dataset.where(metric: name.to_s).order_by(:timestamp).last
end

#metric(name) ⇒ Object



120
121
122
# File 'lib/perus/server/models/system.rb', line 120

def metric(name)
    metrics_dataset.where(name: name.to_s).first
end

#pending_actionsObject



44
45
46
# File 'lib/perus/server/models/system.rb', line 44

def pending_actions
    actions_dataset.where(timestamp: nil).all
end

#save_actions(params, timestamp) ⇒ Object


actions




128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/perus/server/models/system.rb', line 128

def save_actions(params, timestamp)
    actions = JSON.parse(params['actions'])

    actions.each do |id, result|
        # JSON.dump turns integer keys into strings
        action = Action.with_pk!(id.to_i)
        action.timestamp = timestamp

        if result == true
            action.success = true
        elsif result.is_a?(Hash)
            action.success = true
            action.file = save_file(params, result, action.id)
        else
            action.success = false
            action.response = result.to_s
        end

        action.save
    end
end

#save_file(params, entry, name) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/perus/server/models/system.rb', line 158

def save_file(params, entry, name)
    # entry should be a hash with a single key 'file', which is the
    # key name used to find the file in params
    raise 'Invalid file upload' unless entry.is_a?(Hash) && entry.include?('file')

    # ensure the uploads directory exists for this system
    FileUtils.mkdir_p(uploads_dir)

    # files are sent as top level parameters. metrics rename files
    # with their own name (i.e 'screenshot' saves a file called
    # 'screenshot'), while actions rename files to the action id
    upload = params[entry['file']]

    # store the file
    name = name.to_s + File.extname(upload[:filename])
    File.open(File.join(uploads_dir, name.to_s), 'wb') do |f|
        f.write(upload[:tempfile].read)
    end

    # return details of the file so it can be served with the correct
    # original filename and mimetype
    {filename: name, original_name: upload[:filename], mimetype: upload[:type]}
end

#save_metric_errors(params, timestamp) ⇒ Object


metrics




67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/perus/server/models/system.rb', line 67

def save_metric_errors(params, timestamp)
    # ignore errors during maintenance window
    return if config.in_maintenance?
    errors = JSON.parse(params['metric_errors'])

    errors.each do |module_name, module_errors|
        module_errors.each do |error|
            Error.create(
                system_id: id,
                timestamp: timestamp,
                module: module_name,
                description: error
            )
        end
    end
end

#save_values(params, timestamp) ⇒ Object



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
# File 'lib/perus/server/models/system.rb', line 84

def save_values(params, timestamp)
    # store the set of known metrics for this system. metrics have either
    # str or num values.
    values = JSON.parse(params['metrics'])

    values.each do |(metric_name, value)|
        # file uploads are treated specially and don't need a value
        # record created for them
        if value.kind_of?(Hash)
            file_data = save_file(params, value, metric_name)
            Metric.add_file(metric_name, id, file_data)
            next
        end

        attrs = {
            system_id: id,
            timestamp: timestamp,
            metric: metric_name
        }

        if value.kind_of?(Numeric)
            attrs[:num_value] = value
            Metric.add_numeric(metric_name, id)
        else
            attrs[:str_value] = value
            Metric.add_string(metric_name, id)
        end

        Value.create(attrs)
    end
end

#screenshot_urlObject



192
193
194
195
# File 'lib/perus/server/models/system.rb', line 192

def screenshot_url
    screenshot = metrics_dataset.where(name: 'screenshot').first
    screenshot ? screenshot.url : ''
end

#upload_urlsObject



182
183
184
185
186
187
188
189
190
# File 'lib/perus/server/models/system.rb', line 182

def upload_urls
    file_metrics = metrics_dataset.where(type: 'file').all
    pairs = file_metrics.collect do |metric|
        [metric.name, metric.url]
    end

    # convert to name => url hash
    Hash[pairs]
end

#uploads_dirObject


files




154
155
156
# File 'lib/perus/server/models/system.rb', line 154

def uploads_dir
    @uploads_dir ||= File.join(Server.options.uploads_dir, id.to_s)
end

#validateObject



16
17
18
19
20
# File 'lib/perus/server/models/system.rb', line 16

def validate
    super
    validates_presence  :name
    validates_unique    :name
end