From c83e002fda2d6a720595a0a520dc76eaa7c69f12 Mon Sep 17 00:00:00 2001 From: justme-1968 Date: Wed, 6 Feb 2013 21:06:04 +0000 Subject: [PATCH] new Module 32_SYSSTAT to monitor system load and disk usage on linux FHEM hosts git-svn-id: https://svn.fhem.de/fhem/trunk@2656 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/CHANGED | 2 + fhem/FHEM/32_SYSSTAT.pm | 192 +++++++++++++++++++++++++++++++++++ fhem/www/gplot/sysstat.gplot | 24 +++++ 3 files changed, 218 insertions(+) create mode 100644 fhem/FHEM/32_SYSSTAT.pm create mode 100644 fhem/www/gplot/sysstat.gplot diff --git a/fhem/CHANGED b/fhem/CHANGED index c5402b9cc..1571d8ae0 100644 --- a/fhem/CHANGED +++ b/fhem/CHANGED @@ -1,4 +1,6 @@ - SVN + - feature: new Module 32_SYSSTAT to monitor system load and disk usage + on linux FHEM hosts (by justme1968) - feature: new Module 73_PRESENCE to make automatic presence detection of mobile phones or other mobile devices (like tablets) via ping or bluetooth checks (by M. Bloch) diff --git a/fhem/FHEM/32_SYSSTAT.pm b/fhem/FHEM/32_SYSSTAT.pm new file mode 100644 index 000000000..62caae85c --- /dev/null +++ b/fhem/FHEM/32_SYSSTAT.pm @@ -0,0 +1,192 @@ + +package main; + +use strict; +use warnings; +use Sys::Statistics::Linux; + +sub +SYSSTAT_Initialize($) +{ + my ($hash) = @_; + + $hash->{DefFn} = "SYSSTAT_Define"; + $hash->{UndefFn} = "SYSSTAT_Undefine"; + $hash->{AttrFn} = "SYSSTAT_Attr"; + $hash->{AttrList} = "filesystems showpercent useregex loglevel:0,1,2,3,4,5,6 ". + $readingFnAttributes; +} + +##################################### + +sub +SYSSTAT_Define($$) +{ + my ($hash, $def) = @_; + + my @a = split("[ \t][ \t]*", $def); + + return "Usage: define SYSSTAT [interval]" if(@a < 2); + + my $interval = 60; + if(int(@a)>=3) { $interval = $a[2]; } + if( $interval < 60 ) { $interval = 60; } + + $hash->{STATE} = "Initialized"; + $hash->{INTERVAL} = $interval; + + $hash->{xls} = Sys::Statistics::Linux->new( loadavg => 1 ); + + InternalTimer(gettimeofday()+$hash->{INTERVAL}, "SYSSTAT_GetUpdate", $hash, 0); + + return undef; +} + +sub +SYSSTAT_Undefine($$) +{ + my ($hash, $arg) = @_; + + RemoveInternalTimer($hash); + return undef; +} + +sub +SYSSTAT_Attr($$$) +{ + my ($cmd, $name, $attrName, $attrVal) = @_; + my $hash = $defs{$name}; + + $attrVal= "" unless defined($attrVal); + $attrVal= "" if($cmd eq "useregex"); + $attrVal= "" if($cmd eq "showpercent"); + + if( $attrName eq "filesystems") { + my @filesystems = split(",",$attrVal); + @{$hash->{filesystems}} = @filesystems; + + if( $#filesystems >= 0 ) { + $hash->{xls}->set( loadavg => 1, + diskusage => 1 ); + } else { + $hash->{xls}->set( loadavg => 1, + diskusage => 0 ); + } + } + + return; +} + +sub +SYSSTAT_GetUpdate($) +{ + my ($hash) = @_; + + if(!$hash->{LOCAL}) { + InternalTimer(gettimeofday()+$hash->{INTERVAL}, "SYSSTAT_GetUpdate", $hash, 1); + } + + my $stat = $hash->{xls}->get; + + my $load = $stat->{loadavg}; + + $hash->{STATE} = $load->{avg_1} . " " . $load->{avg_5} . " " . $load->{avg_15}; + + readingsSingleUpdate($hash,"load",$load->{avg_1},defined($hash->{LOCAL} ? 0 : 1)); + + if( defined(my $usage = $stat->{diskusage}) ){ + + my $type = 'free'; + if( AttrVal($hash->{NAME}, "showpercent", "") ne "" ) { + $type = 'usageper'; + } + + if( AttrVal($hash->{NAME}, "useregex", "") eq "" ) { + for my $filesystem (@{$hash->{filesystems}}) { + my $fs = $usage->{$filesystem}; + readingsSingleUpdate($hash,$fs->{mountpoint},$fs->{$type},defined($hash->{LOCAL} ? 0 : 1)); + } + } else { + for my $filesystem (@{$hash->{filesystems}}) { + foreach my $key (keys %$usage) { + if( $key =~ /$filesystem/ ) { + my $fs = $usage->{$key}; + readingsSingleUpdate($hash,$fs->{mountpoint},$fs->{$type},defined($hash->{LOCAL} ? 0 : 1)); + } + } + } + } + } +} + +1; + +=pod +=begin html + + +

SYSSTAT

+ + +=end html +=cut diff --git a/fhem/www/gplot/sysstat.gplot b/fhem/www/gplot/sysstat.gplot new file mode 100644 index 000000000..2f82c5968 --- /dev/null +++ b/fhem/www/gplot/sysstat.gplot @@ -0,0 +1,24 @@ +############################ +# Display the system load values +# +# Corresponding FileLog definition: +# define sysstatlog FileLog /usr/local/FHEM/var/log/sysstat-%Y-%m.log sysstat +# attr sysstatlog nrarchive 1 +# +# Corresponding FileLog definition: +# define wl_sysstat weblink fileplot sysstatlog:sysstat:CURRENT +# attr wl_sysstat label "Load Min: $data{min1}, Max: $data{max1}, Aktuell: $data{currval1}" +# attr wl_sysstat room System + +set terminal png transparent size crop +set output '.png' +set xdata time +set timefmt "%Y-%m-%d_%H:%M:%S" +set xlabel " " +set title '' + +set ylabel "Load" + +#FileLog 4:load:0: + +plot "< awk '/load:/ {print $1, $4}' " using 1:2 ls l0 axes x1y1 title 'Load' with lines