Projet

Général

Profil

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

root / plugins / system / irq @ 09b88141

Historique | Voir | Annoter | Télécharger (14,5 ko)

1
#!/usr/bin/perl -w
2
# -*- perl -*-
3

    
4
=head1 NAME
5

    
6
irq - Plugin to monitor interrupts.
7

    
8
=head1 APPLICABLE SYSTEMS
9

    
10
All Linux systems
11

    
12
=head1 CONFIGURATION
13

    
14
None need
15

    
16
=head2 WARNING AND CRITICAL SETTINGS
17

    
18
You can set warning and critical levels for each of the data
19
series the plugin reports.
20
'General' graph support cpu-irqtype limits and irqtype limits
21

    
22
Examples:
23

    
24
 [irq]
25
 env.warning_cpu1_sirq_total 550
26
 env.critical_cpu0_irq_total 600
27
 env.warning_irq_total 700
28
 env.critical_sirq_total 700
29

    
30
'Child' graphs support cpu-irqtype-irqname and irqtype-irqname limits
31

    
32
Examples:
33

    
34
 [irq]
35
 env.warning_cpu0_irq_7 100
36
 env.critical_cpu1_sirq_HI 100
37
 env.warning_irq_LOC 100
38
 env.critical_irq_12 200
39
 env.warning_sirq_BLOCK 1000
40

    
41
Note: irqtype: sirq, irq; sirq - Software IRQ; irq name you can see in [] on graph
42

    
43
=head1 INTERPRETATION
44

    
45
The plugin shows each cpu interrupts: summary and IRQ/Software IRQ per CPU
46

    
47
=head1 MAGIC MARKERS
48

    
49
  #%# family=auto
50
  #%# capabilities=autoconf
51

    
52
=head1 VERSION
53

    
54
  1.0
55

    
56
=head1 BUGS
57

    
58
I can not understand, how set limit to mirrored fields, so they may not display correctly
59

    
60
=head1 AUTHOR
61

    
62
Gorlow Maxim aka Sheridan <sheridan@sheridan-home.ru> (email and jabber)
63

    
64
=head1 LICENSE
65

    
66
GPLv2
67

    
68
=cut
69

    
70
use strict;
71
use warnings;
72
use Munin::Plugin;
73
use Data::Dumper;
74
my $IRQi = {};
75

    
76

    
77
my $graphs =
78
{
79
  'irq_total' =>
80
  {
81
    'title'    => 'Interrupts per cpu',
82
    'args'     => '--base 1000',
83
    'vlabel'   => 'count of IRQ (+) / sIRQ (-) per second',
84
    'info'     => 'This graph shows interrupts per second from last update',
85
    'category' => 'system'
86
  },
87
  'irq_cpu' =>
88
  {
89
    'title'    => 'CPU:cpu: :irq_type:',
90
    'args'     => '--base 1000',
91
    'vlabel'   => 'count per second',
92
    'info'     => 'This graph shows :irq_type: for CPU:cpu: per second from last update',
93
    'category' => 'cpu :cpu:'
94
  }
95
};
96

    
97
my $fields =
98
{
99
  'irq' =>
100
  {
101
    'label' => '[:irq:] :irqinfo:',
102
    'info'  => ':irq:: :irqinfo:',
103
    'type'  => 'GAUGE',
104
    'draw'  => 'LINE1'
105
  },
106
  'irq_sirq' =>
107
  {
108
    'label' => 'CPU:cpu: sIRQ/IRQ',
109
    'info'  => 'Total sIRQ/IRQ for CPU:cpu:',
110
    'type'  => 'GAUGE',
111
    'draw'  => 'LINE1'
112
  }
113
};
114

    
115
my $irq_types =
116
{
117
  'irq'  => 'Interrupts',
118
  'sirq' => 'Software interrupts'
119
};
120

    
121
my $irq_descriptions =
122
{
123
  'HI'      => 'High priority tasklets',
124
  'TIMER'   => 'Timer bottom half',
125
  'NET_TX'  => 'Transmit network packets',
126
  'NET_RX'  => 'Receive network packets',
127
  'SCSI'    => 'SCSI bottom half',
128
  'TASKLET' => 'Handles regular tasklets'
129
};
130

    
131
# ----------------- main ----------------
132
need_multigraph();
133
# -- autoconf --
134
if (defined($ARGV[0]) and ($ARGV[0] eq 'autoconf'))
135
{
136
  printf("%s\n", (-e "/proc/interrupts" and -e "/proc/softirqs") ? "yes" : "no (stats not exists)");
137
  exit (0);
138
}
139
# -- config --
140
load_irq_info();
141
if (defined($ARGV[0]) and ($ARGV[0] eq 'config')) { print_config(); exit (0); }
142
# -- values --
143
print_values(); exit(0);
144

    
145
# ----------------- sub's ----------------
146

    
147
# ----------------------- trim whitespace at begin and end of string ------------
148
sub trim
149
{
150
  my    ($string) = @_;
151
  for   ($string) { s/^\s+//; s/\s+$//; }
152
  return $string;
153
}
154

    
155

    
156
# -------------------------- loading irq stats ---------------------------------
157
sub load_irq_info_file
158
{
159
  my $file      = $_[0];
160
  my $info      = {};
161
  my $cpu_count = 0;
162
  my $summ      = 0;
163
  open (FH, '<', $file) or die "$! $file \n";
164
  for my $line (<FH>)
165
  {
166
    chomp $line;
167
    if($line =~ m/:/)
168
    {
169
      my ($name, $stat) = split(/:/, trim($line));
170
      my @data = split(/\s+/, trim($stat));
171
      for (my $i=0; $i<$cpu_count; $i++)
172
      {
173
        if (defined $data[$i])
174
        {
175
          $info->{'info'}{$i}{$name} = $data[$i];
176
          $info->{'summ'}{$i}       += $data[$i];
177
        }
178
      }
179
      if(scalar(@data) > $cpu_count)
180
      {
181
        my $iname = '';
182
        ($iname = $line) =~ s/^.*:(\s+\d+)+\s+(.*)$/$2/;
183
        if ($iname ne '')
184
        {
185
          if ($iname =~ m/.*-.*-.*/)
186
          {
187
            my @parts = ($iname =~ /^((\w+-)*\w+)\s+(.*)\s*$/g);
188
            $iname = sprintf("%s (%s)", $parts[0], $parts[2]);
189
          }
190
          $info->{'description'}{$name} = $iname;
191
        }
192
      }
193
      elsif(exists ($irq_descriptions->{$name}))
194
      {
195
        $info->{'description'}{$name} = $irq_descriptions->{$name};
196
      }
197
    }
198
    else
199
    {
200
      my @cpus = split(/\s+/, trim($line));
201
      $cpu_count = scalar(@cpus);
202
      for (my $i=0; $i<$cpu_count; $i++)
203
      {
204
        $info->{'summ'}{$i} = 0;
205
      }
206
    }
207
  }
208
  close (FH);
209
  $info->{'cpu_count'} = $cpu_count;
210
  return $info;
211
}
212

    
213
# -------------- loading all IRQ statistics ---------------------------
214
sub load_irq_info
215
{
216
  my ($irq, $sirq) = (load_irq_info_file("/proc/interrupts"), load_irq_info_file("/proc/softirqs"));
217
  $IRQi->{'stat'}{'irq' } = $irq ->{'info'};
218
  $IRQi->{'stat'}{'sirq'} = $sirq->{'info'};
219
  $IRQi->{'summ'}{'irq' } = $irq ->{'summ'};
220
  $IRQi->{'summ'}{'sirq'} = $sirq->{'summ'};
221
  $IRQi->{'description'}{'irq' } = $irq ->{'description'} if exists($irq ->{'description'});
222
  $IRQi->{'description'}{'sirq'} = $sirq->{'description'} if exists($sirq->{'description'});
223
  $IRQi->{'cpu_count'} = $irq ->{'cpu_count'};
224
}
225

    
226
# ------------------ loading limits ---------------------
227
sub load_limits
228
{
229
  my $flags = {};
230
  my $limits = {};
231
  my $name = '';
232
  for my $irq_type (qw(irq sirq))
233
  {
234
    for my $t (qw(warning critical))
235
    {
236
      $name = sprintf("%s_%s_total", $t, $irq_type); # env.warning_irq_total 22
237
      $limits->{'irq_total'}{$irq_type}{$t} = $ENV{$name} || undef;
238
      for (my $i=0; $i < $IRQi->{'cpu_count'}; $i++)
239
      {
240
        $name = sprintf("%s_cpu%s_%s_total", $t, $i, $irq_type); # env.warning_cpu1_sirq_total 1112
241
        $limits->{'irq_total_percpu'}{$irq_type}{$t}{$i} = $ENV{$name} || undef;
242
        for my $irq_name (keys %{$IRQi->{'stat'}{$irq_type}{$i}})
243
        {
244
          $name = sprintf("%s_cpu%s_%s_%s", $t, $i, $irq_type, $irq_name); # env.warning_cpu0_irq_7 25
245
          $limits->{'percpu_perirq'}{$irq_type}{$t}{$i}{$irq_name} = $ENV{$name} || undef;
246
          $name = sprintf("%s_%s_%s", $t, $irq_type, $irq_name);
247
          unless (exists($flags->{$name}))
248
          {
249
            $limits->{'perirq'}{$irq_type}{$t}{$irq_name} = $ENV{$name} || undef; # env.critical_sirq_RCU 14
250
            $flags->{$name} = 1;
251
          }
252
        }
253
      }
254
    }
255
  }
256
  return $limits;
257
}
258

    
259
# -------------------------------- replacing strings ------------------------
260
sub replace
261
{
262
  my ($string, $needle, $replacement) = @_[0..2];
263
  $string =~ s/$needle/$replacement/g;
264
  return $string;
265
}
266

    
267
# ----------------- append limit values to general graph fields-----------------------------
268
sub append_total_limit
269
{
270
  my ($limits, $gr, $irq_type, $field_name, $cpu_num) = @_[0..4];
271
  for my $t (qw(warning critical))
272
  {
273
    my $limit = defined($limits->{'irq_total_percpu'}{$irq_type}{$t}{$cpu_num}) ? $limits->{'irq_total_percpu'}{$irq_type}{$t}{$cpu_num} :
274
                ($limits->{'irq_total'}{$irq_type}{$t} || undef);
275
    if (defined($limit))
276
    {
277
      $gr->{'irq'}{'fields'}{$field_name}{$t} = $limit;
278
    }
279
  }
280
}
281

    
282
# ----------------- append limit values to chields graphs fields-----------------------------
283
sub append_cpu_limit
284
{
285
  my ($limits, $gr, $irq_type, $field_name, $graph_name, $cpu_num, $irq_name) = @_[0..6];
286
  for my $t (qw(warning critical))
287
  {
288
    my $limit = defined($limits->{'percpu_perirq'}{$irq_type}{$t}{$cpu_num}{$irq_name}) ? $limits->{'percpu_perirq'}{$irq_type}{$t}{$cpu_num}{$irq_name} :
289
                ($limits->{'perirq'}{$irq_type}{$t}{$irq_name} || undef);
290
    if (defined($limit))
291
    {
292
      $gr->{$graph_name}{'fields'}{$field_name}{$t} = $limit;
293
    }
294
  }
295
}
296

    
297
# ------------------------------ preparing graphs configurations ------------------------------
298
sub prepare_graphs
299
{
300
  my $gr = {};
301
  my $limits = load_limits();
302
  # --- general graph ---
303
  $gr->{'irq'}{'graph'} = $graphs->{'irq_total'};
304
  $gr->{'irq'}{'graph'}{'order'} = "";
305
  for (my $i=0; $i < $IRQi->{'cpu_count'}; $i++)
306
  {
307
    # --- general fields ---
308
    my ($up_field_name, $down_field_name) = (sprintf("i%s", $i), sprintf("si%s", $i));
309
    append_total_limit($limits, $gr, 'irq',  $up_field_name,   $i);
310
    append_total_limit($limits, $gr, 'sirq', $down_field_name, $i);
311
    $gr->{'irq'}{'graph'}{'order'} .= sprintf(" %s %s", $down_field_name, $up_field_name);
312
    $gr->{'irq'}{'fields'}{$up_field_name}{'type'}   = $fields->{'irq_sirq'}{'type'};
313
    $gr->{'irq'}{'fields'}{$down_field_name}{'type'} = $fields->{'irq_sirq'}{'type'};
314
    $gr->{'irq'}{'fields'}{$up_field_name}{'draw'}   = $fields->{'irq_sirq'}{'draw'};
315
    $gr->{'irq'}{'fields'}{$down_field_name}{'draw'} = $fields->{'irq_sirq'}{'draw'};
316

    
317
    $gr->{'irq'}{'fields'}{$up_field_name}{'label'} = replace($fields->{'irq_sirq'}{'label'}, ':cpu:', $i);
318
    $gr->{'irq'}{'fields'}{$up_field_name}{'info'}  = replace($fields->{'irq_sirq'}{'info'} , ':cpu:', $i);
319
    $gr->{'irq'}{'fields'}{$down_field_name}{'label'} = 'NaN';
320
    $gr->{'irq'}{'fields'}{$down_field_name}{'info'}  = 'NaN';
321

    
322
    $gr->{'irq'}{'fields'}{$up_field_name}{'negative'} = $down_field_name;
323
    $gr->{'irq'}{'fields'}{$down_field_name}{'graph'}  = 'no';
324

    
325
    # --- child graphs ---
326
    for my $irq_type (qw(irq sirq))
327
    {
328
      my $graph_name = sprintf("irq.%s_cpu%s", $irq_type, $i);
329
      $gr->{$graph_name}{'graph'}{'order'} = "";
330
      $gr->{$graph_name}{'graph'}{'args'} = $graphs->{'irq_cpu'}{'args'};
331
      $gr->{$graph_name}{'graph'}{'vlabel'} = $graphs->{'irq_cpu'}{'vlabel'};
332
      for my $go (qw(title info))
333
      {
334
        $gr->{$graph_name}{'graph'}{$go} = replace($graphs->{'irq_cpu'}{$go}, ':irq_type:', $irq_types->{$irq_type});
335
        $gr->{$graph_name}{'graph'}{$go} = replace($gr->{$graph_name}{'graph'}{$go}, ':cpu:', $i);
336
      }
337
      $gr->{$graph_name}{'graph'}{'category'} = replace($graphs->{'irq_cpu'}{'category'}, ':cpu:', $i);
338
      # -- child fields --
339
      my @irq_names = keys %{$IRQi->{'stat'}{$irq_type}{$i}};
340
      # names split for better sorting
341
      for my $irq_name ((
342
                          (sort {int $a <=> int $b} grep{/^\d/}            @irq_names),
343
                          (sort                     grep{!/(^\d|ERR|MIS)/} @irq_names),
344
                          (sort                     grep{/(ERR|MIS)/     } @irq_names)
345
                        ))
346
      {
347
        my $field_name = clean_fieldname(sprintf("irq_%s", $irq_name));
348
        append_cpu_limit($limits, $gr, $irq_type, $field_name, $graph_name, $i, $irq_name);
349
        $gr->{$graph_name}{'graph'}{'order'} .= ' '.$field_name;
350
        for my $this_field (qw(label info))
351
        {
352
          $gr->{$graph_name}{'fields'}{$field_name}{$this_field} = replace($fields->{'irq'}{$this_field}, ':irq:', $irq_name);
353
          $gr->{$graph_name}{'fields'}{$field_name}{$this_field} = replace($gr->{$graph_name}{'fields'}{$field_name}{$this_field},
354
                                                                   ':irqinfo:',
355
                                                                   exists($IRQi->{'description'}{$irq_type}{$irq_name}) ?
356
                                                                    $IRQi->{'description'}{$irq_type}{$irq_name} :
357
                                                                    '');
358
        }
359
        $gr->{$graph_name}{'fields'}{$field_name}{'type'} = $fields->{'irq'}{'type'};
360
        $gr->{$graph_name}{'fields'}{$field_name}{'draw'} = $fields->{'irq'}{'draw'};
361
      }
362
    }
363
  }
364
  return $gr;
365
}
366

    
367
# --------------------------------- graph configs ----------------------------
368
sub print_config
369
{
370
  my $config = prepare_graphs();
371
  for my $g (sort keys %{$config})
372
  {
373
    printf("multigraph %s\n", $g);
374
    for my $go (sort keys %{$config->{$g}{'graph'}}) { printf("graph_%s %s\n", $go, $config->{$g}{'graph'}{$go}); }
375
    for my $f (sort keys %{$config->{$g}{'fields'}}) { for my $this_field (sort keys %{$config->{$g}{'fields'}{$f}}) { printf("%s.%s %s\n", $f, $this_field, $config->{$g}{'fields'}{$f}{$this_field}); } }
376
    print "\n";
377
  }
378
}
379

    
380
# ----------------------------------- saving state data using munin --------------------
381
sub save_state_data
382
{
383
  my $data = $_[0];
384
  my $d = Data::Dumper->new([$data]);
385
  $d->Indent(0);
386
  save_state($d->Dump);
387
}
388

    
389
# -------------------------------- loading previous state data using munin -------------------
390
sub restore_state_data
391
{
392
  my $VAR1;
393
  my $states = (restore_state())[0];
394
  eval $states if defined $states;
395
  return $VAR1;
396
}
397

    
398
# ----------------------------- loading statistic and save it for feature use--------------
399
sub load_stats
400
{
401
  delete ($IRQi->{'description'});
402
  $IRQi->{'timestamp'} = time();
403
  save_state_data($IRQi);
404
  return $IRQi;
405
}
406

    
407
# ----------- calculate current and previous values difference -----------------------
408
sub diff_value
409
{
410
  my ($pvalue, $cvalue, $timediff) = @_[0..2];
411
  return 'NaN' if $timediff <= 0 or $pvalue > $cvalue;
412
  return ($cvalue - $pvalue)/$timediff;
413
}
414

    
415
# -----------------  calculating values ---------------------
416
sub calculate
417
{
418
  my ($pstats, $cstats) = @_[0..1];
419
  my $data = {};
420
  my $timediff = $cstats->{'timestamp'} - $pstats->{'timestamp'};
421
  for my $irq_type (qw(irq sirq))
422
  {
423
    for (my $i=0; $i < $IRQi->{'cpu_count'}; $i++)
424
    {
425
      $data->{'summ'}{$irq_type}{$i} = diff_value($pstats->{'summ'}{$irq_type}{$i}, $cstats->{'summ'}{$irq_type}{$i}, $timediff);
426
      for my $irq_name (keys %{$cstats->{'stat'}{$irq_type}{$i}})
427
      {
428
        $data->{'stat'}{$irq_type}{$i}{$irq_name} = diff_value($pstats->{'stat'}{$irq_type}{$i}{$irq_name}, $cstats->{'stat'}{$irq_type}{$i}{$irq_name}, $timediff);
429
      }
430
    }
431
  }
432
  return $data;
433
}
434

    
435
# --------------------- preparing graphs values config ------------------------
436
sub prepare_graphs_values
437
{
438
  my $data = $_[0];
439
  my $values = {};
440
  for (my $i=0; $i < $IRQi->{'cpu_count'}; $i++)
441
  {
442
    $values->{'irq'}{sprintf("i%s",  $i)} = $data->{'summ'}{'irq'} {$i};
443
    $values->{'irq'}{sprintf("si%s", $i)} = $data->{'summ'}{'sirq'}{$i};
444
    for my $irq_type (qw(irq sirq))
445
    {
446
      my $graph_name = sprintf("irq.%s_cpu%s", $irq_type, $i);
447
      for my $irq_name (keys %{$data->{'stat'}{$irq_type}{$i}})
448
      {
449
        my $field_name = clean_fieldname(sprintf("irq_%s", $irq_name));
450
        $values->{$graph_name}{$field_name} = $data->{'stat'}{$irq_type}{$i}{$irq_name};
451
      }
452
    }
453
  }
454
  return $values;
455
}
456

    
457
# -------------------------------- printing values -----------------------------------
458
sub print_values
459
{
460
  my $pstats = restore_state_data();
461
  my $cstats = load_stats();
462
  if (exists ($pstats->{'timestamp'}))
463
  {
464
    my $values = prepare_graphs_values(calculate($pstats, $cstats));
465
    for my $g (sort keys %{$values})
466
    {
467
      printf("multigraph %s\n", $g);
468
      for my $f (sort keys %{$values->{$g}}) { printf("%s.value %s\n", $f, $values->{$g}{$f}); }
469
      print "\n";
470
    }
471
  }
472
}
473