refactor the mess into subs
This commit is contained in:
parent
ff1d80a7c2
commit
a532814e5e
1 changed files with 123 additions and 116 deletions
239
conky-disks
239
conky-disks
|
@ -39,137 +39,144 @@ sub magic_level {
|
||||||
return $new_level;
|
return $new_level;
|
||||||
}
|
}
|
||||||
|
|
||||||
my $fs = Sys::Filesystem->new();
|
sub conky_fs {
|
||||||
my @filesystems = $fs->filesystems();
|
my $fs = Sys::Filesystem->new();
|
||||||
|
my @filesystems = $fs->filesystems();
|
||||||
|
|
||||||
# determine longest mount point path
|
# determine longest mount point path
|
||||||
my $mlength = 5;
|
my $mlength = 5;
|
||||||
for my $f (@filesystems) {
|
for my $f (@filesystems) {
|
||||||
my $mount_point = $fs->mount_point($f);
|
my $mount_point = $fs->mount_point($f);
|
||||||
|
|
||||||
if (length($mount_point) > $mlength) {
|
if (length($mount_point) > $mlength) {
|
||||||
$mlength = length($mount_point);
|
$mlength = length($mount_point);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
for my $f (@filesystems) {
|
|
||||||
my $mount_point = $fs->mount_point($f);
|
|
||||||
my $format = $fs->format($f);
|
|
||||||
|
|
||||||
next if grep { /^$format$/xsm } @ignoreformats;
|
|
||||||
next if grep { $mount_point =~ $_ } @ignorepaths;
|
|
||||||
|
|
||||||
my $df = df($mount_point, 1024); # 1k blocks
|
|
||||||
my $blocks = $df->{blocks};
|
|
||||||
if (!defined($blocks)) { $blocks = 1 };
|
|
||||||
my $size = $blocks/(1024*1024);
|
|
||||||
|
|
||||||
my $level_warn = magic_level(0.80, $size) * 100;
|
|
||||||
my $level_crit = magic_level(0.90, $size) * 100;
|
|
||||||
|
|
||||||
# color depends on used percent
|
|
||||||
my $color = $color_ok;
|
|
||||||
my $per = $df->{per};
|
|
||||||
if (!defined($per)) { $per = 100 };
|
|
||||||
if ($per >= $level_warn) {
|
|
||||||
$color = $color_warn;
|
|
||||||
}
|
|
||||||
if ($per >= $level_crit) {
|
|
||||||
$color = $color_crit;
|
|
||||||
}
|
|
||||||
my $bfree = $df->{bfree};
|
|
||||||
if (!defined($bfree)) { $bfree = 0 };
|
|
||||||
|
|
||||||
printf(" \${color #98c2c7}%-${mlength}s\$color %2.f%% %6.1fGB \${color $color}\${fs_bar 6 %s}\$color\n",
|
|
||||||
$mount_point,
|
|
||||||
100 - $per,
|
|
||||||
$bfree/(1024*1024),
|
|
||||||
$mount_point
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
# hddtemp
|
|
||||||
|
|
||||||
my $color_toocold = "#0000ff";
|
|
||||||
my $color_toohot = "#ff0000";
|
|
||||||
|
|
||||||
my $socket=IO::Socket::INET->new(
|
|
||||||
PeerAddr => 'localhost', PeerPort=> 7634,
|
|
||||||
Proto => 'tcp', Type => $IO::Socket::SOCK_STREAM)
|
|
||||||
or croak "Can't talk to hddtemp";
|
|
||||||
my $hddtemp_output = <$socket>;
|
|
||||||
close $socket;
|
|
||||||
|
|
||||||
my @hddtemp_output = split /[|]/x, $hddtemp_output;
|
|
||||||
my $diskcount = $#hddtemp_output/5;
|
|
||||||
for (my $i = 0; $i < $diskcount; $i++) {
|
|
||||||
my $dev = $hddtemp_output[$i*5+1];
|
|
||||||
my $temp = $hddtemp_output[$i*5+3];
|
|
||||||
my $unit = $hddtemp_output[$i*5+4];
|
|
||||||
|
|
||||||
my $color = $color_ok;
|
|
||||||
if ($temp > 40) {
|
|
||||||
$color = $color_toohot;
|
|
||||||
} elsif ($temp < 25) {
|
|
||||||
$color = $color_toocold;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
printf " \${color #98c2c7}%s\$color ", $dev;
|
for my $f (@filesystems) {
|
||||||
printf "\${color $color}%02.f°%s\$color\n", $temp, $unit;
|
my $mount_point = $fs->mount_point($f);
|
||||||
}
|
my $format = $fs->format($f);
|
||||||
|
|
||||||
# mdstat
|
next if grep { /^$format$/xsm } @ignoreformats;
|
||||||
open my $fh, "<", "/proc/mdstat";
|
next if grep { $mount_point =~ $_ } @ignorepaths;
|
||||||
my $md_current;
|
|
||||||
my $md_current_state;
|
|
||||||
my $md_current_mdstat;
|
|
||||||
|
|
||||||
sub print_md_current_state {
|
my $df = df($mount_point, 1024); # 1k blocks
|
||||||
if (defined $md_current) {
|
my $blocks = $df->{blocks};
|
||||||
|
if (!defined($blocks)) { $blocks = 1 };
|
||||||
|
my $size = $blocks/(1024*1024);
|
||||||
|
|
||||||
|
my $level_warn = magic_level(0.80, $size) * 100;
|
||||||
|
my $level_crit = magic_level(0.90, $size) * 100;
|
||||||
|
|
||||||
|
# color depends on used percent
|
||||||
my $color = $color_ok;
|
my $color = $color_ok;
|
||||||
my $print_mdstat = 0;
|
my $per = $df->{per};
|
||||||
|
if (!defined($per)) { $per = 100 };
|
||||||
|
if ($per >= $level_warn) {
|
||||||
|
$color = $color_warn;
|
||||||
|
}
|
||||||
|
if ($per >= $level_crit) {
|
||||||
|
$color = $color_crit;
|
||||||
|
}
|
||||||
|
my $bfree = $df->{bfree};
|
||||||
|
if (!defined($bfree)) { $bfree = 0 };
|
||||||
|
|
||||||
if ($md_current_state =~ /^\[(\d+)\/(\d+)\]\s+\[([U_]+)\]$/xsm) {
|
printf(" \${color #98c2c7}%-${mlength}s\$color %2.f%% %6.1fGB \${color $color}\${fs_bar 6 %s}\$color\n",
|
||||||
my $should = $1;
|
$mount_point,
|
||||||
my $are = $2;
|
100 - $per,
|
||||||
my $states = $3;
|
$bfree/(1024*1024),
|
||||||
|
$mount_point
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if ($are < $should) {
|
sub conky_hddtemp {
|
||||||
$color = $color_crit;
|
my $color_toocold = "#0000ff";
|
||||||
$print_mdstat = 1;
|
my $color_toohot = "#ff0000";
|
||||||
|
|
||||||
|
my $socket=IO::Socket::INET->new(
|
||||||
|
PeerAddr => 'localhost', PeerPort=> 7634,
|
||||||
|
Proto => 'tcp', Type => $IO::Socket::SOCK_STREAM)
|
||||||
|
or croak "Can't talk to hddtemp";
|
||||||
|
my $hddtemp_output = <$socket>;
|
||||||
|
close $socket;
|
||||||
|
|
||||||
|
my @hddtemp_output = split /[|]/x, $hddtemp_output;
|
||||||
|
my $diskcount = $#hddtemp_output/5;
|
||||||
|
for (my $i = 0; $i < $diskcount; $i++) {
|
||||||
|
my $dev = $hddtemp_output[$i*5+1];
|
||||||
|
my $temp = $hddtemp_output[$i*5+3];
|
||||||
|
my $unit = $hddtemp_output[$i*5+4];
|
||||||
|
|
||||||
|
my $color = $color_ok;
|
||||||
|
if ($temp > 40) {
|
||||||
|
$color = $color_toohot;
|
||||||
|
} elsif ($temp < 25) {
|
||||||
|
$color = $color_toocold;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf " \${color #98c2c7}%s\$color ", $dev;
|
||||||
|
printf "\${color $color}%02.f°%s\$color\n", $temp, $unit;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sub conky_mdstat {
|
||||||
|
open my $fh, "<", "/proc/mdstat";
|
||||||
|
my $md_current;
|
||||||
|
my $md_current_state;
|
||||||
|
my $md_current_mdstat;
|
||||||
|
|
||||||
|
my $print_md_current_state = sub {
|
||||||
|
if (defined $md_current) {
|
||||||
|
my $color = $color_ok;
|
||||||
|
my $print_mdstat = 0;
|
||||||
|
|
||||||
|
if ($md_current_state =~ /^\[(\d+)\/(\d+)\]\s+\[([U_]+)\]$/xsm) {
|
||||||
|
my $should = $1;
|
||||||
|
my $are = $2;
|
||||||
|
my $states = $3;
|
||||||
|
|
||||||
|
if ($are < $should) {
|
||||||
|
$color = $color_crit;
|
||||||
|
$print_mdstat = 1;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$color = $color_unknown;
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
$color = $color_unknown;
|
printf " \${color #98c2c7}%s\$color \${color $color}%s\$color\n",
|
||||||
|
"/dev/$md_current", $md_current_state;
|
||||||
|
if ($print_mdstat) {
|
||||||
|
printf "\n\${color $color}%s\$color",
|
||||||
|
$md_current_mdstat;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
while (my $line = <$fh>) {
|
||||||
|
next if $line =~ /^Personalities\s*:/xsmi;
|
||||||
|
next if $line =~ /^unused\sdevices\s*:/xsmi;
|
||||||
|
|
||||||
|
if ($line =~ /^(.*) : /) {
|
||||||
|
&$print_md_current_state();
|
||||||
|
|
||||||
|
$md_current = $1;
|
||||||
|
$md_current_state = undef;
|
||||||
|
$md_current_mdstat = undef;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf " \${color #98c2c7}%s\$color \${color $color}%s\$color\n",
|
if ($line =~ /^\s*\d+\s+blocks(?: super [0-9.]+)? (.*)$/) {
|
||||||
"/dev/$md_current", $md_current_state;
|
$md_current_state = $1;
|
||||||
if ($print_mdstat) {
|
|
||||||
printf "\n\${color $color}%s\$color",
|
|
||||||
$md_current_mdstat;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$md_current_mdstat .= $line;
|
||||||
}
|
}
|
||||||
|
close $fh;
|
||||||
|
&$print_md_current_state();
|
||||||
}
|
}
|
||||||
|
|
||||||
while (my $line = <$fh>) {
|
conky_fs();
|
||||||
next if $line =~ /^Personalities\s*:/xsmi;
|
conky_hddtemp();
|
||||||
next if $line =~ /^unused\sdevices\s*:/xsmi;
|
conky_mdstat();
|
||||||
|
|
||||||
if ($line =~ /^(.*) : /) {
|
|
||||||
print_md_current_state();
|
|
||||||
|
|
||||||
$md_current = $1;
|
|
||||||
$md_current_state = undef;
|
|
||||||
$md_current_mdstat = undef;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($line =~ /^\s*\d+\s+blocks(?: super [0-9.]+)? (.*)$/) {
|
|
||||||
$md_current_state = $1;
|
|
||||||
}
|
|
||||||
|
|
||||||
$md_current_mdstat .= $line;
|
|
||||||
}
|
|
||||||
close $fh;
|
|
||||||
print_md_current_state();
|
|
||||||
|
|
||||||
# vim:textwidth=120:
|
# vim:textwidth=120:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue