From 5352990f300f9ad14afcb66f7163f2f0835069a7 Mon Sep 17 00:00:00 2001 From: mattwire <> Date: Sat, 14 Nov 2015 17:50:31 +0000 Subject: [PATCH] 98_ping: added new module to allow easy ping checks for devices git-svn-id: https://svn.fhem.de/fhem/trunk@9891 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/FHEM/98_ping.pm | 188 +++++++++++++++++++++++++++++++++++++++++++ fhem/HISTORY | 5 +- fhem/MAINTAINER.txt | 1 + 3 files changed, 193 insertions(+), 1 deletion(-) create mode 100644 fhem/FHEM/98_ping.pm diff --git a/fhem/FHEM/98_ping.pm b/fhem/FHEM/98_ping.pm new file mode 100644 index 000000000..60b2fb0da --- /dev/null +++ b/fhem/FHEM/98_ping.pm @@ -0,0 +1,188 @@ +# $Id$ +############################################## +# +# 98_ping.pm +# FHEM module to check remote network device using ping. +# +# Author: Matthew Wire (mattwire) +# +# This file is part of fhem. +# +# Fhem is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. +# +# Fhem is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with fhem. If not, see . +# +############################################################################## + +package main; + +use strict; +use warnings; + +#use IO::Handle; +#use IO::Socket; +#use IO::Select; +#use Time::HiRes; +use Net::Ping; + +sub ping_Initialize($) +{ + my ($hash) = @_; + + $hash->{DefFn} = "ping_Define"; + $hash->{UndefFn} = "ping_Undefine"; + $hash->{AttrFn} = "ping_Attr"; + $hash->{AttrList} = "disable:0,1 checkInterval ".$readingFnAttributes; + + return undef; +} + +##################################### +# Define ping device +sub ping_Define($$) +{ + my ($hash, $def) = @_; + my @args = split("[ \t][ \t]*", $def); + + return "Usage: define ping " if(@args < 5); + + my ($name, $type, $host, $mode, $timeout) = @args; + + # Parameters + $hash->{HOST} = $host; + $hash->{MODE} = lc($mode); + $hash->{TIMEOUT} = $timeout; + + return "ERROR: mode must be one of tcp,udp,icmp" if ($hash->{MODE} !~ "tcp|udp|icmp"); + return "ERROR: timeout must be 0 or higher." if (($hash->{timeout} !~ /^\d*$/) || ($hash->{timeout} < 0)); + + $attr{$name}{"checkInterval"} = 10 if (!defined($attr{$name}{"checkInterval"})); + + ping_State($hash); + + return undef; +} + +##################################### +# Undefine ping device +sub ping_Undefine($$) +{ + my ($hash,$arg) = @_; + RemoveInternalTimer($hash); + + return undef; +} + +##################################### +# Manage attribute changes +sub ping_Attr($$$$) { + my ($command,$name,$attribute,$value) = @_; + my $hash = $defs{$name}; + + Log3 ($hash, 5, "$hash->{NAME}_Attr: Attr $attribute; Value $value"); + + if ($attribute eq "checkInterval") + { + if (($value !~ /^\d*$/) || ($value < 5)) + { + $attr{$name}{"checkInterval"} = 10; + return "checkInterval is required in s (default: 10, min: 5)"; + } + } + # Handle "disable" attribute by opening/closing connection to device + elsif ($attribute eq "disable") + { + # Disable on 1, enable on anything else. + if ($value eq "1") + { + readingsSingleUpdate($hash, "state", "disabled", 1); + } + else + { + readingsSingleUpdate($hash, "state", "Initialized", 1); + } + } + + return undef; +} + +##################################### +# Perform a ping and set state to result +sub ping_State(@) +{ + # Update Bridge state + my ($hash) = @_; + + return undef if (IsDisabled($hash->{NAME})); + + Log3 ( $hash, 5, "$hash->{NAME}_State: Executing ping"); + + # check via ping + my $pingstatus = "unreachable"; + my $p; + $p = Net::Ping->new($hash->{MODE}); + + my $alive = $p->ping($hash->{HOST}, $hash->{TIMEOUT}); + $p->close(); + $pingstatus = "ok" if $alive; + + # And update state + readingsSingleUpdate($hash, "state", $pingstatus, 1); + + # Check state every X seconds + InternalTimer(gettimeofday() + AttrVal($hash->{NAME}, "checkInterval", "10"), "ping_State", $hash, 0); + + return undef; +} + +1; + +=pod +=begin html + + +

ping

+
    +

    This module provides a simple "ping" function for testing the state of a remote network device.

    +

    It allows for alerts to be triggered when devices cannot be reached using a notify function.

    + + +

    Define

    +
      +

      define <name> ping <host/ip> <mode> <timeout>

      +

      Specifies the ping device.
      + <host/ip> is the hostname or IP address of the Bridge.

      +

      Specifies ping mode.
      + <mode> One of: tcp|udp|icmp. Read the perl docs for more detail: http://perldoc.perl.org/Net/Ping.html

      +

      Timeout.
      + <timeout> is the maximum time to wait for each ping.

      +
    + +

    Readings

    +
      +
    • + state
      + [Initialized|ok|unreachable]: Shows reachable status check every 10 (checkInterval) seconds. +
    • +
    + +

    Attributes

    +
      +
    • + checkInterval
      + Default: 10s. Time after the bridge connection is re-checked. +
    • +
    +
+ +=end html +=cut diff --git a/fhem/HISTORY b/fhem/HISTORY index 4902d5ef3..88a7f5096 100644 --- a/fhem/HISTORY +++ b/fhem/HISTORY @@ -645,4 +645,7 @@ - added new FHEM command 'msg' to introduce integrated message routing functionality - Fri Nov 13 2015 (loredo) - - added new module 50_HP1000 \ No newline at end of file + - added new module 50_HP1000 + +- Sat Nov 14 2015 (mattwire) + - added new module 98_ping to allow easy ping checks for devices diff --git a/fhem/MAINTAINER.txt b/fhem/MAINTAINER.txt index d1f7fb089..64f211c19 100644 --- a/fhem/MAINTAINER.txt +++ b/fhem/MAINTAINER.txt @@ -316,6 +316,7 @@ FHEM/98_HourCounter.pm john http://forum.fhem.de MAX FHEM/98_logProxy.pm justme1968 http://forum.fhem.de Frontends FHEM/98_notice.pm mfr69bs http://forum.fhem.de Sonstiges FHEM/98_pilight.pm andreas-fey http://forum.fhem.de Unterstuetzende Dienste +FHEM/98_ping mattwire http://forum.fhem.de Sonstiges FHEM/98_rain.pm baumrasen http://forum.fhem.de Sonstiges FHEM/98_restore.pm rudolfkoenig http://forum.fhem.de Sonstiges FHEM/98_statistics.pm tupol http://forum.fhem.de Unterstuetzende Dienste (Link als PM an tupol)