Explorar o código

* add support for fhem modules traffic
* fix some perl warnings (client)
* "state" now allways shown by default (all devices)
* new defaults level for speedtest

erdoking %!s(int64=8) %!d(string=hai) anos
pai
achega
f5990d9ea7

+ 20 - 16
local/share/check_mk/agents/mk_fhem

@@ -2,22 +2,26 @@
 use strict;
 use warnings;
 
-#use lib qw(..);
 use JSON qw( );
-use Data::Dumper;               # Perl core module
-
-my %devices;
+use Data::Dumper;
 
+## change path / port here if necessary 
 my $fhem_outpout = `/opt/fhem/fhem.pl 7072 "jsonlist2 TYPE=.*:FILTER=state=..*:FILTER=model!=CCU-FHEM:FILTER=model!=ActionDetector"`;
+
+## special device types to be monitored
+## normaly filtered out
+my @allowed_types = ( 'speedtest', 'CUL', 'JeeLink', 'TRAFFIC' );
+
+my %devices;
 my $json = JSON->new;
 my $data = $json->decode($fhem_outpout);
-
 my $aref = $data->{Results};
 
 for my $device (@$aref) {
 
-        ## filter for physical devices (IODev is set) OR channel-devices OR type speedtest
-        if (defined $device->{Attributes}{IODev} || ( defined $device->{Internals}{device} && defined $device->{Internals}{chanNo} ) || $device->{Internals}{TYPE} eq "speedtest" ) {
+        ## filter for physical devices (IODev is set) OR channel-devices OR Type speedtest
+        if (defined $device->{Attributes}{IODev} || ( defined $device->{Internals}{device} && defined $device->{Internals}{chanNo} ) || $device->{Internals}{TYPE} ~~ @allowed_types ) {
+
                 my $devicename;
 
                 ## check if device is a channel
@@ -30,19 +34,19 @@ for my $device (@$aref) {
                 }
 
                 ## get device attributes
-                while (my ($attribute, $value) = (each($device->{Attributes}))) {
+                while (my ($attribute, $value) = (each(%{$device->{Attributes}}))) {
                         ## e.g: $devices{eg.wz.thermostat}{Attributes}{model} = HM-SEC-SCo;
                         $devices{$devicename}{$device->{Name}}{Attributes}{$attribute} = $value;
                 }
-                ## get device readings
-                foreach my $data (sort keys $device->{Readings}){
-                        while (my ($reading, $value) = (each($device->{Readings}{$data}))) {
+                ## get device readings  
+                foreach my $data (sort keys %{$device->{Readings}}){
+                        while (my ($reading, $value) = (each(%{$device->{Readings}{$data}}))) {
                                 ## e.g: $devices{eg.wz.thermostat}{Readings}{model} = HM-SEC-SCo;
                                 $devices{$devicename}{$device->{Name}}{Readings}{$data}{$reading} = $value;
                         }
                 }
                 ## get device internals
-                while (my ($internal, $value) = (each($device->{Internals}))) {
+                while (my ($internal, $value) = (each(%{$device->{Internals}}))) {
                         ## e.g: $devices{eg.wz.thermostat}{Internals}{LASTInputDev} = myJeeLink;
                         $devices{$devicename}{$device->{Name}}{Internals}{$internal} = $value;
                 }
@@ -67,9 +71,9 @@ foreach my $device (sort keys %devices ){
                 printf("%-41s %-32s %-20s %s\n", "", $device, 'model', $devices{$device}{$device}{Attributes}{model});
         }
 
-        foreach my $channel (sort keys $devices{$device}){
-                ## get device readings
-                foreach my $reading (sort keys $devices{$device}{$channel}{Readings}){
+        foreach my $channel (sort keys %{$devices{$device}}){   
+                ## get device readings  
+                foreach my $reading (sort keys %{$devices{$device}{$channel}{Readings}}){
                         if ( "$reading" !~ /RegL_\d+/) {
                                 ## e.g: $devices{eg.wz.thermostat}{Readings}{model} = HM-SEC-SCo
                                 printf("%-20s %-20s %-30s   %-20s %s\n","", $devices{$device}{$channel}{Readings}{$reading}{Time}, $channel, $reading, $devices{$device}{$channel}{Readings}{$reading}{Value});
@@ -78,6 +82,6 @@ foreach my $device (sort keys %devices ){
         }
 }
 
-
+## debug
 #print Dumper \%devices;
 

+ 15 - 3
local/share/check_mk/checks/fhem

@@ -92,6 +92,11 @@ def check_fhem(item, params, info):
                                 data[line[3]]['value']=line[4]
                                 data[line[3]]['time']=( "%s %s" % (line[0], line[1]))
                                 data[line[3]]['channel'] = line[2]
+                                ## needed for traffic
+                                if len(line) > 6:
+                                        if line[5] == 'hour':
+#                                                hour2min=
+                                                data[line[3]]['value']=(int(line[4])*60)+int(line[6])
         
         if ourstatus == 0:
                 return (3, "UNKNOWN - %s - %s " % (device, dewpoint))
@@ -141,9 +146,15 @@ def check_fhem(item, params, info):
                 ## Speedtest
                 ('download',            'download',             'Mbit/s',           '',                1,          1),
                 ('ping',                'ping',                 'ms',               '',                1,          1),
-                ('state',               'state',                '',                 '',                0,          0),
+                ('state',               'state',                '',                 '',                1,          0),
                 ('upload',              'upload',               'Mbit/s',           '',                1,          1),
 #               ('path',                'path',                 '',                 '',                0,          0),
+                ## TRAFFIC
+                ('delay',               'delay',                'min',              '',                1,          1),
+                ('distance',            'distance',             'km',               '',                1,          1),
+                ('duration',            'duration',             'mins',             '',                1,          1),
+                ('duration_in_traffic', 'duration_in_traffic',  'mins',             '',                1,          1),
+
 
 
 
@@ -225,6 +236,7 @@ def check_fhem(item, params, info):
                     if key == 'humidity' and params['var_dewpoint_override'] == 'true':
                         ignoreit = 'true'
 
+
                     ## there are sometime duplicate keys on channel like controlmode in '_Clima' and '_Climate' 
                     ## Just filter out some HomeMatic-channel
                     if channel == '' or data[key]['channel'] == "%s_%s" % (item, channel):
@@ -301,8 +313,8 @@ factory_settings["fhem_default_params"] = {
         "level_humidity_min"           : (50, 45),         # warn/crit for min. humidity
         "level_dewpoint_max"           : (3, 1),           # warn/crit for diff to temperatur (exp. 17°C (dewp) vs 20°C(temp))
         "level_batteryLevel_min"       : (2.1, 2.3),       # warn/crit for max. temperature
-        "level_download_min"           : (0, 0),           # warn/crit for min download (speedtest)
-        "level_upload_min"             : (0, 0),           # warn/crit for min upload (speedtest)
+        "level_download_min"           : (10, 8),           # warn/crit for min download (speedtest)
+        "level_upload_min"             : (1.5, 1),           # warn/crit for min upload (speedtest)
         "level_ping"                   : (100, 150),       # warn/crit for ping (speedtest)
         "var_activity"                 : ("alive"),        # default for alive
         "var_contact"                  : ("ignore"),       # default for contact

+ 11 - 2
local/share/check_mk/pnp-templates/check_mk-fhem.php

@@ -135,7 +135,7 @@ if (isset($fhem_defines['download']) ) {
         ;
 }
 
-# 7. Speedtest (ping)
+# 8. Speedtest (ping)
 if (isset($fhem_defines['ping']) ) {
    $ds_name[] = 'speedtest_ping';
    $opt[] = $defopt . "--title \"Speedtest Ping\"";
@@ -144,6 +144,15 @@ if (isset($fhem_defines['ping']) ) {
         ;
 }
 
-
+# 9. Traffic (duration)
+if (isset($fhem_defines['duration']) ) {
+   $ds_name[] = 'duration';
+   $opt[] = $defopt . "--title \"Traffic duration\"";
+   $def[] = ""
+        . fhem_area("duration", "00bfff", "Duration", "min", FALSE)
+        . fhem_area("delay", "f78181", "Delay", "min", TRUE)
+        . fhem_curve("LINE2", "duration_in_traffic", "B40404", "Duration in traffic", "min", FALSE)
+        ;
+}   
 
 ?>