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 =
'4.0.0'

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



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

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

#enhance(data) ⇒ Object



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
143
144
# File 'lib/riemann/tools/bacula.rb', line 90

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



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

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



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

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



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

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



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

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



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

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



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

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



197
198
199
# File 'lib/riemann/tools/bacula.rb', line 197

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

#parse_rate(value) ⇒ Object



201
202
203
204
# File 'lib/riemann/tools/bacula.rb', line 201

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

#parse_ratio(value) ⇒ Object



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

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)


213
214
215
216
217
# File 'lib/riemann/tools/bacula.rb', line 213

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



219
220
221
# File 'lib/riemann/tools/bacula.rb', line 219

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
53
54
# 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)

  if opts[:status] != 'Canceled'
    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']})",
           })
  end

  %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



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

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