Class: Riemann::Tools::Bacula

Inherits:
Object
  • Object
show all
Includes:
Riemann::Tools
Defined in:
lib/riemann/tools/bacula.rb,
lib/riemann/tools/bacula/version.rb

Constant Summary collapse

VERSION =
'2.0.2'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.process_stdinObject



21
22
23
# File 'lib/riemann/tools/bacula.rb', line 21

def self.process_stdin
  new.process_stdin
end

Instance Method Details

#bacula_backup_stateObject



54
55
56
57
58
59
60
61
# File 'lib/riemann/tools/bacula.rb', line 54

def bacula_backup_state
  case opts[:status]
  when 'OK' then 'ok'
  when 'OK -- with warnings' then 'warning'
  else
    'critical'
  end
end

#enhance(data) ⇒ Object



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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/riemann/tools/bacula.rb', line 88

def enhance(data)
  # If the message on stdin was trucated, the last item might not make
  # sense.
  data.delete(data.keys.last) if data.keys.last != 'Termination'

  {
    parse_size: [
      'FD Bytes Written',
      'SD Bytes Written',
      'Last Volume Bytes',
      'Bytes Restored',
    ],
    parse_integer: [
      'JobId',
      'Priority',
      'Non-fatal FD errors',
      'FD Files Written',
      'FD Errors',
      'SD Files Written',
      'SD Errors',
      'Volume Session Id',
      'Volume Session Time',
      'Files Expected',
      'Files Restored',
    ],
    parse_duration: [
      'Elapsed time',
    ],
    parse_volumes: [
      'Volume name(s)',
    ],
    parse_rate: [
      'Rate',
    ],
    parse_ratio: [
      'Software Compression',
      'Comm Line Compression',
    ],
  }.each do |parser, keys|
    keys.each do |key|
      data[key] = send(parser, data[key]) if data[key]
    end
  end

  extract_source('Pool', data)
  extract_source('Catalog', data)
  extract_source('Storage', data)

  extract_time('FileSet', data)

  extract_client_info(data)
  extract_backup_level_info(data)

  data
end

#extract_backup_level_info(data) ⇒ Object



144
145
146
147
148
149
150
151
152
153
# File 'lib/riemann/tools/bacula.rb', line 144

def extract_backup_level_info(data)
  case data['Backup Level']
  when /\A(Differential|Incremental), since=(.*)\z/
    data['Backup Level'] = Regexp.last_match(1)
    data['Backup Level Since'] = Regexp.last_match(2)
  when /\A(Full) \(upgraded from (Differential|Incremental)\)\z/
    data['Backup Level'] = Regexp.last_match(1)
    data['Backup Level upgraded from'] = Regexp.last_match(2)
  end
end

#extract_client_info(data) ⇒ Object



155
156
157
158
159
160
# File 'lib/riemann/tools/bacula.rb', line 155

def extract_client_info(data)
  return unless /\A"([^"]+)" ([^ ]+)/.match(data['Client'])

  data['Client'] = Regexp.last_match(1)
  data['Client Version'] = Regexp.last_match(2)
end

#extract_source(item, data) ⇒ Object



162
163
164
165
166
167
# File 'lib/riemann/tools/bacula.rb', line 162

def extract_source(item, data)
  return unless /\A"([^"]+)" \(From (Client|Job|Pool) resource\)\z/.match(data[item])

  data[item] = Regexp.last_match(1)
  data["#{item} Source"] = Regexp.last_match(2)
end

#extract_time(item, data) ⇒ Object



169
170
171
172
173
174
# File 'lib/riemann/tools/bacula.rb', line 169

def extract_time(item, data)
  return unless /\A"([^"]+)" (.*)\z/.match(data[item])

  data[item] = Regexp.last_match(1)
  data["#{item} time"] = Regexp.last_match(2)
end

#parse(text) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/riemann/tools/bacula.rb', line 63

def parse(text)
  data = {}
  line_continuation = nil

  text.each_line do |line|
    line.chomp!

    if line =~ /\A  ([^:]+):[[:blank:]]+(.*)/
      key = Regexp.last_match(1)
      raw_value = Regexp.last_match(2)

      data[key] = raw_value
    elsif line_continuation
      key = line_continuation
      data[key] += ".#{line}"
    else
      next
    end

    line_continuation = (key if line.length == 998)
  end

  enhance(data)
end

#parse_duration(duration) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/riemann/tools/bacula.rb', line 176

def parse_duration(duration)
  s = StringScanner.new(duration)
  res = 0

  until s.eos?
    case
    when s.scan(/\s+/)
      # ignore spaces
    when s.scan(/(\d+) hours?/) then res += s[0].to_i * 3600
    when s.scan(/(\d+) mins?/)  then res += s[0].to_i * 60
    when s.scan(/(\d+) secs?/)  then res += s[0].to_i
    else
      return -1
    end
  end

  res
end

#parse_integer(value) ⇒ Object



195
196
197
# File 'lib/riemann/tools/bacula.rb', line 195

def parse_integer(value)
  value.gsub(',', '').to_i
end

#parse_rate(value) ⇒ Object



199
200
201
202
# File 'lib/riemann/tools/bacula.rb', line 199

def parse_rate(value)
  %r{\A(\d+\.\d+) KB/s\z}.match(value)
  Regexp.last_match(1).to_f
end

#parse_ratio(value) ⇒ Object



204
205
206
207
208
209
# File 'lib/riemann/tools/bacula.rb', line 204

def parse_ratio(value)
  return 0.0 if value == 'None'

  /\A(\d+\.\d+)% \d+\.\d+:\d+\z/.match(value)
  Regexp.last_match(1).to_f / 100
end

#parse_size(value) ⇒ Object

Raises:

  • (ArgumentError)


211
212
213
214
215
# File 'lib/riemann/tools/bacula.rb', line 211

def parse_size(value)
  raise ArgumentError, %(Cannot parse size "#{value}") unless /\A([\d,]+) \([\d.]+ [KMGT]?B\)\z/.match(value)

  parse_integer(Regexp.last_match(1))
end

#parse_volumes(value) ⇒ Object



217
218
219
# File 'lib/riemann/tools/bacula.rb', line 217

def parse_volumes(value)
  value.split('|')
end

#runObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/riemann/tools/bacula.rb', line 25

def run
  %i[job_name backup_level status].each do |name|
    raise("Parameter #{name} is required") unless opts[name]
  end

  data = parse($stdin.read)

  report({
           service: "bacula backup #{opts[:job_name]}",
           state: bacula_backup_state,
           job_name: opts[:job_name],
           backup_level: opts[:backup_level],
           description: "#{opts[:status]} (#{data['Termination']})",
         })

  %i[bytes files].each do |metric|
    next unless opts[metric]

    report({
             service: "bacula backup #{opts[:job_name]} #{opts[:backup_level].downcase} #{metric}",
             metric: opts[metric],
             job_name: opts[:job_name],
             backup_level: opts[:backup_level],
           })
  end

  send_details(data) if options[:details]
end

#send_details(data) ⇒ Object



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/riemann/tools/bacula.rb', line 221

def send_details(data)
  [
    'Elapsed time',
    'FD Files Written',
    'SD Files Written',
    'FD Bytes Written',
    'SD Bytes Written',
    'SD Errors',
    'Rate',
    'Software Compression',
    'Comm Line Compression',
    'Non-fatal FD errors',
  ].each do |metric|
    next unless data[metric]

    report({
             service: "bacula backup #{opts[:job_name]} #{opts[:backup_level].downcase} #{metric.downcase}",
             metric: data[metric],
             job_name: opts[:job_name],
             backup_level: opts[:backup_level],
           })
  end
end