Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / other / ipmitool_sensor_ @ 6685d69a

Historique | Voir | Annoter | Télécharger (15,8 ko)

1
#!/usr/bin/perl -w
2
#
3
# Wildcard plugin to monitor sensor by using ipmitool sensor program.
4
#
5
# Contributed by Jun Futagawa
6
# This script is based on sensors_ plugin.
7
#
8
# Usage:
9
#       ln -s /usr/share/munin/plugins/ipmitool_sensor_ /etc/munin/plugins/ipmitool_sensor_fan
10
#       ln -s /usr/share/munin/plugins/ipmitool_sensor_ /etc/munin/plugins/ipmitool_sensor_temp
11
#       ln -s /usr/share/munin/plugins/ipmitool_sensor_ /etc/munin/plugins/ipmitool_sensor_volt
12
#
13
# Requirements:
14
#       - OpenIPMI tool (ipmitool command)
15
#
16
# Note:
17
#       - Sensor names are read from the output of the ipmitool sensor program.
18
#
19
# Add the following to your /etc/munin/plugin-conf.d/munin-node:
20
#
21
#       [ipmitool_sensor*]
22
#       user root
23
#       timeout 20
24
#
25
# If you want to use "ipmitool sdr", add the following:
26
# Note: When you use this, the threshold provided by the sensor board is not used.
27
#
28
#       [ipmitool_sensor*]
29
#       user root
30
#       timeout 20
31
#       env.ipmitool_options sdr
32
#
33
# Parameters supported:
34
#
35
#       config
36
#       autoconf
37
#       suggest
38
#
39
# Configurable variables
40
#
41
#       ipmitool            - ipmitool command (default: ipmitool)
42
#       ipmitool_options    - ipmitool command options (default: sensor)
43
#                             sdr: you can use 'sdr' instead of sensor.
44
#       cache_file          - cache file
45
#                             (default: /var/lib/munin/plugin-state/plugin-ipmitool_sensor.cache)
46
#       cache_expires       - cache expires (default: 275)
47
#
48
#       fan_type_regex      - Regular expression for unit of fan (default: RPM)
49
#       temp_type_regex     - Regular expression for unit of temp (default: degrees C)
50
#       volt_type_regex     - Regular expression for unit of volt (default: (Volts|Watts|Amps))
51
#
52
#       fan_warn_percent    - Percentage over mininum for warning (default: 5)
53
#       fan_lower_critical  - Preferred lower critical value for fan
54
#       fan_upper_critical  - Preferred upper critical value for fan
55
#       temp_lower_critical - Preferred lower critical value for temp
56
#       temp_lower_warning  - Preferred lower warining value for temp
57
#       temp_upper_warning  - Preferred upper warning value for temp
58
#       temp_upper_critical - Preferred upper critical value for temp
59
#       volt_warn_percent   - Percentage over mininum/under maximum for warning
60
#                             Narrow the voltage bracket by this. (default: 20)
61
#
62
# $Log$
63
# Revision 1.4  2009/02/08 23:51:00  jfut
64
# Support "ipmitool sdr".
65
# Add Watts and Amp as voltage unit.
66
# Add fan_type_regex/temp_type_regex/volt_type_regex as option of sensor type.
67
#
68
# Revision 1.3  2008/11/11 13:55:00  jfut
69
# Add infinity value check for HP ProLiant DL160.
70
# Add preferred value option for fan and temp.
71
#
72
# Revision 1.2  2008/10/28 19:21:22  jfut
73
# Add file check.
74
#
75
# Revision 1.1  2008/10/27 18:52:31  jfut
76
# Add cache mechanism.
77
#
78
# Revision 1.0  2008/10/27 14:25:12  jfut
79
# Initial release.
80
#
81
# Magic markers:
82
#%# family=manual
83
#%# capabilities=autoconf suggest
84

    
85
use strict;
86

    
87
$ENV{'LANG'} = "C"; # Force parseable output from sensors.
88
$ENV{'LC_ALL'} = "C"; # Force parseable output from sensors.
89
my $IPMITOOL = $ENV{'ipmitool'} || 'ipmitool';
90
my @IPMITOOL_OPTS = exists $ENV{'ipmitool_options'} ? split(/\s+/, $ENV{'ipmitool_options'}) : ('sensor');
91

    
92
my $CACHE_DIR = "/var/lib/munin/plugin-state";
93
my $CACHE_FILE = $ENV{'cache_file'} || "$CACHE_DIR/plugin-ipmitool_sensor.cache";
94
my $CACHE_EXPIRES = $ENV{'cache_expires'} || 275;
95

    
96
my %config = (
97
  fan => {
98
    regex => exists $ENV{'fan_type_regex'} ? qr/$ENV{'fan_type_regex'}/im : qr/RPM/im,
99
    title => 'IPMITool Sensor: Fans',
100
    vtitle => 'RPM',
101
    print_threshold => \&fan_threshold,
102
    graph_args => '--base 1000 -l 0'
103
  },
104
  temp => {
105
    regex => exists $ENV{'temp_type_regex'} ? qr/$ENV{'temp_type_regex'}/im : qr/degrees C/im,
106
    title => 'IPMITool Sensor: Temperatures',
107
    vtitle => 'Celsius',
108
    print_threshold => \&temp_threshold,
109
    graph_args => '--base 1000 -l 0'
110
  },
111
  volt => {
112
    regex => exists $ENV{'volt_type_regex'} ? qr/$ENV{'volt_type_regex'}/im : qr/(Volts|Watts|Amps)/im,
113
    title => 'IPMITool Sensor: Voltages',
114
    vtitle => '_AUTO_DETECT_FAILED_',
115
    print_threshold => \&volt_threshold,
116
    graph_args => '--base 1000'
117
  },
118
);
119

    
120
if (defined $ARGV[0] and $ARGV[0] eq 'autoconf') {
121
  close(STDERR);
122
  my $ret = system($IPMITOOL);
123
  open (STDERR, ">&STDOUT");
124
  if ($ret == 0 || $ret == 256) {
125
    print "yes\n";
126
    exit 0;
127
  } else {
128
    print "no (program $IPMITOOL not found)\n";
129
  }
130
  exit 1;
131
}
132

    
133
if (defined $ARGV[0] and $ARGV[0] eq 'suggest') {
134
  my $text = get_sensor_data();
135
  my $alltext = join('\n', @{$text});
136
  foreach my $func (keys %config) {
137
    print $func, "\n" if $alltext =~ $config{$func}->{regex};
138
  }
139
  exit;
140
}
141

    
142
$0 =~ /ipmitool_sensor_(.+)*$/;
143
my $func = $1;
144
exit 2 unless defined $func;
145

    
146
my $text = get_sensor_data();
147
my $sensor = 1;
148

    
149
if (defined $ARGV[0] and $ARGV[0] eq 'config') {
150
  # detect the unit of volt
151
  if ($func eq 'volt') {
152
    foreach my $line (@{$text}) {
153
      if ($line =~ /$config{$func}->{regex}/) {
154
        my ($label, $value, $unit, $lcr, $lnc, $unc, $ucr) = &get_sensor_items($line, $config{$func}->{regex});
155
        $config{$func}->{vtitle} = $unit;
156
        last;
157
      }
158
    }
159
    $text = get_sensor_data();
160
  }
161

    
162
  # print header
163
  print "graph_title $config{$func}->{title}\n";
164
  print "graph_vtitle $config{$func}->{vtitle}\n";
165
  print "graph_args $config{$func}->{graph_args}\n";
166
  print "graph_category sensors\n";
167

    
168
  # print data
169
  foreach my $line (@{$text}) {
170
    if ($line =~ /$config{$func}->{regex}/) {
171
      my ($label, $value, $unit, $lcr, $lnc, $unc, $ucr) = &get_sensor_items($line, $config{$func}->{regex});
172
      if (&is_valid_value($value)) {
173
        print "$func$sensor.label $label\n";
174
        $config{$func}->{print_threshold}->($func.$sensor, $lcr, $lnc, $unc, $ucr);
175
        print "$func$sensor.graph no\n" if exists $ENV{"ignore_$func$sensor"};
176
        $sensor++;
177
      }
178
    }
179
  }
180
  exit 0;
181
}
182

    
183
foreach my $line (@{$text}) {
184
  if ($line =~ /$config{$func}->{regex}/) {
185
    my ($label, $value, $unit, $lcr, $lnc, $unc, $ucr) = &get_sensor_items($line, $config{$func}->{regex});
186
    # for debug
187
    # print "$func$sensor.value [$label] [$value] [$lcr] [$lnc] [$unc] [$ucr]\n";
188
    if (&is_valid_value($value)) {
189
      print "$func$sensor.value $value\n";
190
      $sensor++;
191
    }
192
  }
193
}
194

    
195
sub get_sensor_data {
196
  my $text = undef;
197
  if (-f $CACHE_FILE) {
198
    my $cache_timestamp = (stat($CACHE_FILE))[9];
199
    if ($CACHE_EXPIRES == -1 || time - $cache_timestamp <= $CACHE_EXPIRES) {
200
      open(IN, "<", $CACHE_FILE) or die "Could not open \"$CACHE_FILE\" for reading\n";
201
      while (<IN>) {
202
        push (@{$text}, $_);
203
      }
204
      close(IN);
205
    }
206
  }
207
  if (! defined $text) {
208
    my $pid = open(EXE, '-|');
209
    if ($pid == 0) {
210
      exec($IPMITOOL, @IPMITOOL_OPTS);
211
    } elsif (defined $pid) {
212
      while(<EXE>) {
213
        push (@{$text}, $_);
214
      }
215
      close(EXE);
216
    } else {
217
      die "fork failed: $!";
218
    }
219
    if (-w $CACHE_DIR) {
220
      open(OUT, ">", $CACHE_FILE) or die "Could not open \"$CACHE_FILE\" for writing\n";
221
      foreach my $line (@{$text}) {
222
        print OUT "$line";
223
      }
224
      close OUT;
225
    }
226
  }
227
  return $text;
228
}
229

    
230
sub get_sensor_items {
231
  my ($line, $regex) = @_;
232
  my @items = split(/\s*\|\s*/, $line);
233
  my ($label, $value, $unit, $lcr, $lnc, $unc, $ucr)
234
    = (trim($items[0]), trim($items[1]), trim($items[2]), trim($items[5]), trim($items[6]), trim($items[7]), trim($items[8]));
235
  if ($#items == 9) {
236
     # ipmitool sensor
237
  } elsif ($#items == 2) {
238
     # ipmitool sdr
239
     if ($value =~ /$regex/) {
240
        $value = trim($`);
241
        $unit = trim($1);
242
     }
243
  }
244

    
245
  # some boards show data in incorrect order.
246
  # - HP ProLiant ML110 G5
247
  # CPU FAN     | 1434.309 | RPM | ok | 5537.099 | 4960.317 | 4859.086 | na | 937.383 | na
248
  # SYSTEM FAN  | 1506.932 | RPM | ok | 5952.381 | 5668.934 | 5411.255 | na | 937.383 | na
249
  # - HP ProLiant DL160
250
  # FAN1 ROTOR1 | 7680.492 | RPM | ok | na | inf | na | na | 1000.400 | na
251
  if (&is_valid_value($lcr) && &is_valid_value($ucr) && $lcr > $ucr || $lcr eq 'inf') {
252
    ($lcr, $lnc, $unc, $ucr) = ($ucr, $unc, $lnc, $lcr);
253
  }
254
  if (&is_valid_value($lnc) && &is_valid_value($unc) && $lnc > $unc || $lnc eq 'inf') {
255
    ($lcr, $lnc, $unc, $ucr) = ($ucr, $unc, $lnc, $lcr);
256
  }
257
  return ($label, $value, $unit, $lcr, $lnc, $unc, $ucr);
258
}
259

    
260
sub fan_threshold {
261
  my ($name, $lcr, $lnc, $unc, $ucr) = @_;
262
  my $warn_percent = exists $ENV{fan_warn_percent} ? $ENV{fan_warn_percent} : 5;
263

    
264
  # lcr: lower critical
265
  if (exists $ENV{fan_lower_critical}) {
266
    $lcr = $ENV{fan_lower_critical};
267
  } elsif (! &is_valid_value($lcr)) {
268
    if ($lcr eq 'inf') { $lcr = ''; }
269
    else { $lcr = '50'; }
270
  }
271
  # lnc: lower warning
272
  if (! &is_valid_value($lnc)) {
273
    if ($lnc eq 'inf') { $lnc = ''; }
274
    else { $lnc = ($lcr eq '') ? '' : $lcr * (100 + $warn_percent) / 100; }
275
  }
276
  # ucr: upper critical
277
  if (exists $ENV{fan_upper_critical}) {
278
    $ucr = $ENV{fan_upper_critical};
279
  } elsif (! &is_valid_value($ucr)) {
280
    if ($ucr eq 'inf') { $ucr = ''; }
281
    else { $ucr = '6000'; }
282
  }
283
  # unc: upper warning
284
  if (! &is_valid_value($unc)) {
285
    if ($unc eq 'inf') { $unc = ''; }
286
    else { $unc = ($ucr eq '') ? '' : $ucr * (100 - $warn_percent) / 100; }
287
  }
288

    
289
  return unless ($lcr ne '' || $lnc ne '' || $unc ne '' || $ucr ne '');
290

    
291
  printf "$name.warning $lnc:$unc\n";
292
  printf "$name.critical $lcr:$ucr\n";
293
}
294

    
295
sub temp_threshold {
296
  my ($name, $lcr, $lnc, $unc, $ucr) = @_;
297

    
298
  # lcr: lower critical
299
  if (exists $ENV{temp_lower_critical}) {
300
    $lcr = $ENV{temp_lower_critical};
301
  } elsif (! &is_valid_value($lcr)) {
302
    if ($lcr eq 'inf') { $lcr = ''; }
303
    else { $lcr = 5; }
304
  }
305
  # lnc: lower warning
306
  if (exists $ENV{temp_lower_warningl}) {
307
    $lnc = $ENV{temp_lower_warning};
308
  } elsif (! &is_valid_value($lnc)) {
309
    if ($lnc eq 'inf') { $lnc = ''; }
310
    else { $lnc = 10; }
311
  }
312
  # unc: upper critical
313
  if (exists $ENV{fan_upper_critical}) {
314
    $unc = $ENV{fan_upper_critical};
315
  } elsif (! &is_valid_value($unc)) {
316
    if ($unc eq 'inf') { $unc = ''; }
317
    else { $unc = '65'; }
318
  }
319
  # ucr: upper warning
320
  if (exists $ENV{upper_critical}) {
321
    $ucr = $ENV{upper_critical};
322
  } elsif (! &is_valid_value($ucr)) {
323
    if ($ucr eq 'inf') { $ucr = ''; }
324
    else { $ucr = '70'; }
325
  }
326

    
327
  return unless ($lcr ne '' || $lnc ne '' || $unc ne '' || $ucr ne '');
328

    
329
  printf "$name.warning $lnc:$unc\n";
330
  printf "$name.critical $lcr:$ucr\n";
331
}
332

    
333
sub volt_threshold {
334
  my ($name, $lcr, $lnc, $unc, $ucr) = @_;
335
  my $warn_percent = exists $ENV{volt_warn_percent} ? $ENV{volt_warn_percent} : 20;
336

    
337
  if (! &is_valid_value($lcr)) { $lcr = ''; }
338
  if (! &is_valid_value($lnc)) { $lnc = ($lcr eq '') ? '' : $lcr * (100 + $warn_percent) / 100; }
339
  if (! &is_valid_value($ucr)) { $ucr = ''; }
340
  if (! &is_valid_value($unc)) { $unc = ($ucr eq '') ? '' : $ucr * (100 - $warn_percent) / 100; }
341

    
342
  return unless ($lcr ne '' || $lnc ne '' || $unc ne '' || $ucr ne '');
343

    
344
  printf "$name.warning $lnc:$unc\n";
345
  printf "$name.critical $lcr:$ucr\n";
346
}
347

    
348
sub trim {
349
  my $value = shift;
350
  if (defined $value) {
351
    $value =~ s/^\s*(.*?)\s*$/$1/;
352
  } else {
353
    $value = 'na'
354
  }
355
  return $value;
356
}
357

    
358
sub is_valid_value() {
359
  my $value = shift;
360
  if ($value eq 'na' || $value eq 'inf' || $value eq '') {
361
    return 0;
362
  } else {
363
    return 1;
364
  }
365
}
366

    
367
########################################
368

    
369
=head1 How to test
370

    
371
  cache_file=ipmitool_sensor_ cache_expires=-1 ./ipmitool_sensor_volt
372
  cache_file=ipmitool_sensor_ cache_expires=-1 ./ipmitool_sensor_volt config
373
  cache_file=ipmitool_sensor_ cache_expires=-1 ./ipmitool_sensor_volt suggest
374
  cache_file=ipmitool_sensor_ cache_expires=-1 ./ipmitool_sensor_volt autoconf
375

    
376
=head1 Test Data
377

    
378
=head2 ipmitool sensor
379

    
380
  # HP ProLiant ML110 G5
381
  CPU FAN          | 1434.309   | RPM        | ok    | 5537.099  | 4960.317  | 4859.086  | na        | 937.383   | na
382
  SYSTEM FAN       | 1497.454   | RPM        | ok    | 5952.381  | 5668.934  | 5411.255  | na        | 937.383   | na
383
  System 12V       | 12.152     | Volts      | ok    | na        | na        | na        | na        | na        | na
384
  System 5V        | 5.078      | Volts      | ok    | na        | na        | na        | na        | na        | na
385
  System 3.3V      | 3.271      | Volts      | ok    | na        | na        | na        | na        | na        | na
386
  CPU0 Vcore       | 1.127      | Volts      | ok    | na        | na        | na        | na        | na        | na
387
  System 1.25V     | 1.254      | Volts      | ok    | na        | na        | na        | na        | na        | na
388
  System 1.8V      | 1.842      | Volts      | ok    | na        | na        | na        | na        | na        | na
389
  System 1.2V      | 1.107      | Volts      | ok    | na        | na        | na        | na        | na        | na
390
  CPU0 Diode       | na         | degrees C  | na    | na        | 20.000    | 25.000    | 85.000    | 90.000    | 95.000
391
  CPU0 Dmn 0 Temp  | 24.500     | degrees C  | ok    | na        | 0.000     | 0.000     | 97.000    | 100.000   | 100.500
392
  CPU0 Dmn 1 Temp  | 29.000     | degrees C  | ok    | na        | 0.000     | 0.000     | 97.000    | 100.000   | 100.500
393
  # HP ProLiant DL160
394
  FAN1 ROTOR1      | 7680.492   | RPM        | ok    | na        | inf       | na        | na        | 1000.400  | na
395
  # HP ProLiant DL360 G5
396
  Fan Block 1      | 34.888     | unspecified | nc    | na        | na        | 75.264    | na        | na        | na
397
  Fan Block 2      | 29.792     | unspecified | nc    | na        | na        | 75.264    | na        | na        | na
398
  Fan Block 3      | 37.240     | unspecified | nc    | na        | na        | 75.264    | na        | na        | na
399
  Fan Blocks       | 0.000      | unspecified | nc    | na        | na        | 0.000     | na        | na        | na
400
  Temp 1           | 40.000     | degrees C  | ok    | na        | na        | -64.000   | na        | na        | na
401
  Temp 2           | 21.000     | degrees C  | ok    | na        | na        | -64.000   | na        | na        | na
402
  Temp 3           | 30.000     | degrees C  | ok    | na        | na        | -64.000   | na        | na        | na
403
  Temp 4           | 30.000     | degrees C  | ok    | na        | na        | -64.000   | na        | na        | na
404
  Temp 5           | 28.000     | degrees C  | ok    | na        | na        | -64.000   | na        | na        | na
405
  Temp 6           | na         | degrees C  | na    | na        | na        | 32.000    | na        | na        | na
406
  Temp 7           | na         | degrees C  | na    | na        | na        | 32.000    | na        | na        | na
407
  Power Meter      | 214.000    | Watts      | cr    | na        | na        | 384.000   | na        | na        | na
408
  Power Meter 2    | 220.000    | watts      | cr    | na        | na        | 384.000   | na        | na        | na
409

    
410
=head2 ipmitool sdr
411

    
412
  # HP ProLiant ML110 G5
413
  CPU FAN          | 1434.31 RPM       | ok
414
  SYSTEM FAN       | 1497.45 RPM       | ok
415
  System 12V       | 12.10 Volts       | ok
416
  System 5V        | 5.08 Volts        | ok
417
  System 3.3V      | 3.27 Volts        | ok
418
  CPU0 Vcore       | 1.14 Volts        | ok
419
  System 1.25V     | 1.25 Volts        | ok
420
  System 1.8V      | 1.84 Volts        | ok
421
  System 1.2V      | 1.11 Volts        | ok
422
  CPU0 Diode       | disabled          | ns
423
  CPU0 Dmn 0 Temp  | 23.50 degrees C   | ok
424
  CPU0 Dmn 1 Temp  | 29 degrees C      | ok
425
  # HP ProLiant DL360 G5
426
  Fan Block 1      | 34.89 unspecifi | nc
427
  Fan Block 2      | 29.79 unspecifi | nc
428
  Fan Block 3      | 37.24 unspecifi | nc
429
  Fan Blocks       | 0 unspecified     | nc
430
  Temp 1           | 41 degrees C      | ok
431
  Temp 2           | 19 degrees C      | ok
432
  Temp 3           | 30 degrees C      | ok
433
  Temp 4           | 30 degrees C      | ok
434
  Temp 5           | 26 degrees C      | ok
435
  Temp 6           | disabled          | ns
436
  Temp 7           | disabled          | ns
437
  Power Meter      | 208 Watts         | cr
438
  Power Meter 2    | 210 watts         | cr
439

    
440
=cut
441

    
442
# vim:syntax=perl