|< 1 2 >| | 16 Einträge, 2 Seiten |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use strict;
use warnings;
use Fcntl qw(:DEFAULT :flock);
# Das ist der Hash mit den Daten, die irgendwo generiert werden
my $stats = {
ProcStats => {
Total => 0,
New => 0,
System => 0,
IOWait => 8.00,
User => 0,
Idle => 92.00,
Nice => 0,
},
MemStats => {
Cached => 531976,
SwapUsed => 276600,
MemTotal => 1035296,
SwapUsedPer => 26.09,
Buffers => 188256,
MemUsed => 981812,
SwapTotal => 1060248,
MemFree => 53484,
SwapFree => 783648,
MemUsedPer => 94.83,
},
TimeStamp => {
Time => '15:05:08',
Date => '2006-05-05',
},
};
# Das ist der Hash mit den Spaltenpositionen der einzelnen
# Keys. Die Positionen werden aus einer Konfigurationsdatei
# geladen... zum Test habe ich beide Hashs einfach mal so
# hierhin geschrieben...
my $defines = {
ProcStats => {
User => 1,
Nice => 2,
System => 3,
Idle => 3,
IOWait => 4,
Total => 5,
New => 6,
},
MemStats => {
MemUsed => 1,
MemFree => 2,
MemUsedPer => 3,
MemTotal => 4,
Buffers => 5,
Cached => 6,
SwapUsed => 7,
SwapFree => 8,
SwapUsedPer => 9,
SwapTotal => 10,
},
};
# Zunächst erzeuge ich das Datenverzeichnis...
my $datadir = $stats->{TimeStamp}->{Date};
unless (-e $datadir) {
mkdir($datadir) or die $!;
}
# Nun durchlaufe ich den Datenhash und schreibe die Daten
# Spaltenweise weg.
foreach my $alias (keys %$stats) {
next if $alias eq 'TimeStamp';
my $file = "$datadir/$alias";
my $data = $stats->{$alias};
# ** und genau diese Stelle hier sagt mir überhaupt nicht zu...
my @tmp_arr = ($stats->{TimeStamp}->{Time});
$tmp_arr[$defines->{$alias}->{$_}] = $data->{$_} for keys %{$defines->{$alias}};
sysopen my $fh,$file,O_WRONLY | O_APPEND | O_CREAT,0644 or die $!;
flock($fh,LOCK_EX);
print $fh join ' ', @tmp_arr;
print $fh "\n";
close $fh;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[ProcStats]
User = 1
Nice = 2
System = 3
Idle = 3
IOWait = 4
Total = 5
New = 6
[MemStats]
MemUsed = 1
MemFree = 2
MemUsedPer = 3
MemTotal = 4
Buffers = 5
Cached = 6
SwapUsed = 7
SwapFree = 8
SwapUsedPer = 9
SwapTotal = 10
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# ganz simples Auslesen...
my $defines = {};
open DEF,'<','./defines.cfg' or die $!;
my $sect;
while (<DEF>) {
if (/^\[(\w+)\]/) {
$sect = $1;
} elsif (/^(\w+)\s=\s(\d+)$/) {
# ich erzeuge sofort ein Array mit der Reihenfolge der Keys
$defines->{$sect}->[$2] = $1;
}
}
close DEF;
my $datadir = "$stats->{TimeStamp}->{Date}";
unless (-e $datadir) {
mkdir($datadir) or die $!;
}
foreach my $alias (keys %$stats) {
next if $alias eq 'TimeStamp';
my $file = "$datadir/$alias";
my $data = $stats->{$alias};
sysopen my $fh,$file,O_WRONLY | O_APPEND | O_CREAT,0644 or die $!;
flock($fh,LOCK_EX);
print $fh $stats->{TimeStamp}->{Time};
foreach my $key (@{$defines->{$alias}}) {
# if $key, weil die ersten Elemente leer sein können
print $fh " $data->{$key}" if $key;
}
print $fh "\n";
close $fh;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
55
56
57
58
59
60
61
62
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
package Stat;
use Moose;
has 'val' => (isa => 'HashRef', is => 'rw');
has 'position' => (is => 'rw');
sub add_kv {
my $self = shift;
my %to_add = @_;
my ($k, $v);
$self->{val}->{$k} = $v while (($k, $v) = each %to_add);
}
sub to_s {
my $self = shift;
my ($k, $v, $s);
$s .= $k . "\t" . $v . "\n" while (($k, $v) = each %{$self->{val}});
return $s;
}
package StatSet;
use Moose;
has 'stats' => (isa => 'ArrayRef', is => 'rw');
sub add_stat {
my $self = shift;
my $stat = shift;
push @{$self->{stats}}, $stat if $stat;
}
sub report {
my $self = shift;
my $s;
$s .= $_->to_s . "\n" for (sort { $a->position <=> $b->position } @{$self->stats});
return $s;
}
package main;
my $set = new StatSet;
my $stat1 = new Stat;
my $stat2 = new Stat;
$stat1->add_kv( color => 'green', size => 'huge' );
$stat1->add_kv( amount => 'some' );
$stat1->position(1);
$stat2->add_kv( color => 'blue', size => 'large', amount => 'many' );
$stat2->position(2);
$set->add_stat($stat1);
$set->add_stat($stat2);
print $set->report;
|< 1 2 >| | 16 Einträge, 2 Seiten |