98_ping.pm 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. # $Id: 98_ping.pm 10939 2016-02-25 22:30:46Z mattwire $
  2. ##############################################
  3. #
  4. # 98_ping.pm
  5. # FHEM module to check remote network device using ping.
  6. #
  7. # Author: Matthew Wire (mattwire)
  8. #
  9. # This file is part of fhem.
  10. #
  11. # Fhem is free software: you can redistribute it and/or modify
  12. # it under the terms of the GNU General Public License as published by
  13. # the Free Software Foundation, either version 2 of the License, or
  14. # (at your option) any later version.
  15. #
  16. # Fhem is distributed in the hope that it will be useful,
  17. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. # GNU General Public License for more details.
  20. #
  21. # You should have received a copy of the GNU General Public License
  22. # along with fhem. If not, see <http://www.gnu.org/licenses/>.
  23. #
  24. ##############################################################################
  25. package main;
  26. use strict;
  27. use warnings;
  28. use Blocking;
  29. use Net::Ping;
  30. sub ping_Initialize($)
  31. {
  32. my ($hash) = @_;
  33. $hash->{DefFn} = "ping_Define";
  34. $hash->{UndefFn} = "ping_Undefine";
  35. $hash->{AttrFn} = "ping_Attr";
  36. $hash->{AttrList} = "disable:1 checkInterval minFailCount ".$readingFnAttributes;
  37. return undef;
  38. }
  39. #####################################
  40. # Define ping device
  41. sub ping_Define($$)
  42. {
  43. my ($hash, $def) = @_;
  44. my @args = split("[ \t][ \t]*", $def);
  45. return "Usage: define <name> ping <host/ip> <mode> <timeout>" if(@args < 5);
  46. my ($name, $type, $host, $mode, $timeout) = @args;
  47. # Parameters
  48. $hash->{HOST} = $host;
  49. $hash->{MODE} = lc($mode);
  50. $hash->{TIMEOUT} = $timeout;
  51. $hash->{FAILCOUNT} = 0;
  52. delete $hash->{helper}{RUNNING_PID};
  53. readingsSingleUpdate($hash, "state", "Initialized", 1);
  54. return "ERROR: mode must be one of tcp,udp,icmp" if ($hash->{MODE} !~ "tcp|udp|icmp");
  55. return "ERROR: timeout must be 0 or higher." if (($hash->{TIMEOUT} !~ /^\d*$/) || ($hash->{TIMEOUT} < 0));
  56. $attr{$name}{"checkInterval"} = 10 if (!defined($attr{$name}{"checkInterval"}));
  57. $attr{$name}{"event-on-change-reading"} = "state" if (!defined($attr{$name}{"event-on-change-reading"}));
  58. ping_SetNextTimer($hash);
  59. return undef;
  60. }
  61. #####################################
  62. # Undefine ping device
  63. sub ping_Undefine($$)
  64. {
  65. my ($hash,$arg) = @_;
  66. RemoveInternalTimer($hash);
  67. BlockingKill($hash->{helper}{RUNNING_PID}) if(defined($hash->{helper}{RUNNING_PID}));
  68. return undef;
  69. }
  70. #####################################
  71. # Manage attribute changes
  72. sub ping_Attr($$$$) {
  73. my ($command,$name,$attribute,$value) = @_;
  74. my $hash = $defs{$name};
  75. Log3 ($hash, 5, "$hash->{NAME}_Attr: Attr $attribute; Value $value");
  76. if ($command eq "set") {
  77. if ($attribute eq "checkInterval")
  78. {
  79. if (($value !~ /^\d*$/) || ($value < 5))
  80. {
  81. $attr{$name}{"checkInterval"} = 10;
  82. return "checkInterval is required in s (default: 10, min: 5)";
  83. }
  84. }
  85. # Handle "disable" attribute by opening/closing connection to device
  86. elsif ($attribute eq "disable")
  87. {
  88. # Disable on 1, enable on anything else.
  89. if ($value eq "1")
  90. {
  91. readingsSingleUpdate($hash, "state", "disabled", 1);
  92. }
  93. else
  94. {
  95. readingsSingleUpdate($hash, "state", "Initialized", 1);
  96. }
  97. }
  98. }
  99. return undef;
  100. }
  101. #####################################
  102. # Set next timer for ping check
  103. sub ping_SetNextTimer($)
  104. {
  105. my ($hash) = @_;
  106. # Check state every X seconds
  107. RemoveInternalTimer($hash);
  108. InternalTimer(gettimeofday() + AttrVal($hash->{NAME}, "checkInterval", "10"), "ping_Start", $hash, 0);
  109. }
  110. #####################################
  111. # Prepare and start the blocking call in new thread
  112. sub ping_Start($)
  113. {
  114. my ($hash) = @_;
  115. return undef if (IsDisabled($hash->{NAME}));
  116. my $timeout = $hash->{TIMEOUT};
  117. my $arg = $hash->{NAME}."|".$hash->{HOST}."|".$hash->{MODE}."|".$hash->{TIMEOUT};
  118. my $blockingFn = "ping_DoPing";
  119. my $finishFn = "ping_DoPingDone";
  120. my $abortFn = "ping_DoPingAbort";
  121. if (!(exists($hash->{helper}{RUNNING_PID}))) {
  122. $hash->{helper}{RUNNING_PID} =
  123. BlockingCall($blockingFn, $arg, $finishFn, $timeout, $abortFn, $hash);
  124. } else {
  125. Log3 $hash, 3, "$hash->{NAME} Blocking Call running no new started";
  126. ping_SetNextTimer($hash);
  127. }
  128. }
  129. #####################################
  130. # BlockingCall DoPing in separate thread
  131. sub ping_DoPing(@)
  132. {
  133. my ($string) = @_;
  134. my ($name, $host, $mode, $timeout) = split("\\|", $string);
  135. Log3 ($name, 5, $name."_DoPing: Executing ping");
  136. # check via ping
  137. my $p;
  138. $p = Net::Ping->new($mode);
  139. my $result = $p->ping($host, $timeout);
  140. $p->close();
  141. $result="" if !(defined($result));
  142. return "$name|$result";
  143. }
  144. #####################################
  145. # Ping thread completed
  146. sub ping_DoPingDone($)
  147. {
  148. my ($string) = @_;
  149. my ($name, $result) = split("\\|", $string);
  150. my $hash = $defs{$name};
  151. if ($result) {
  152. # State is ok
  153. $hash->{FAILCOUNT} = 0;
  154. readingsSingleUpdate($hash, "state", "ok", 1);
  155. } else {
  156. # Increment failcount and report unreachable if over limit
  157. $hash->{FAILCOUNT} += 1;
  158. if ($hash->{FAILCOUNT} >= AttrVal($hash->{NAME}, "minFailCount", 1)) {
  159. readingsSingleUpdate($hash, "state", "unreachable", 1);
  160. }
  161. }
  162. delete($hash->{helper}{RUNNING_PID});
  163. ping_SetNextTimer($hash);
  164. }
  165. #####################################
  166. # Ping thread timeout
  167. sub ping_DoPingAbort($)
  168. {
  169. my ($hash) = @_;
  170. delete($hash->{helper}{RUNNING_PID});
  171. Log3 $hash->{NAME}, 3, "BlockingCall for ".$hash->{NAME}." was aborted";
  172. ping_SetNextTimer($hash);
  173. }
  174. 1;
  175. =pod
  176. =begin html
  177. <a name="ping"></a>
  178. <h3>ping</h3>
  179. <ul>
  180. <p>This module provides a simple "ping" function for testing the state of a remote network device.</p>
  181. <p>It allows for alerts to be triggered when devices cannot be reached using a notify function.</p>
  182. <a name="ping_define"></a>
  183. <p><b>Define</b></p>
  184. <ul>
  185. <p><code>define &lt;name&gt; ping &lt;host/ip&gt; &lt;mode&gt; &lt;timeout&gt;</code></p>
  186. <p>Specifies the ping device.<br/>
  187. &lt;host/ip&gt; is the hostname or IP address of the Bridge.</p>
  188. <p>Specifies ping mode.<br/>
  189. &lt;mode&gt; One of: tcp|udp|icmp. Read the perl docs for more detail: http://perldoc.perl.org/Net/Ping.html</p>
  190. <p>Timeout.<br/>
  191. &lt;timeout&gt; is the maximum time to wait for each ping.</p>
  192. </ul>
  193. <a name="ping_readings"></a>
  194. <p><b>Readings</b></p>
  195. <ul>
  196. <li>
  197. <b>state</b><br/>
  198. [Initialized|ok|unreachable]: Shows reachable status check every 10 (checkInterval) seconds.
  199. </li>
  200. </ul>
  201. <a name="ping_attr"></a>
  202. <p><b>Attributes</b></p>
  203. <ul>
  204. <li>
  205. <b>checkInterval</b><br/>
  206. Default: 10s. Time after the bridge connection is re-checked.
  207. </li>
  208. <li>
  209. <b>minFailCount</b><br/>
  210. Default: 1. Number of failures before reporting "unreachable".
  211. </li>
  212. </ul>
  213. </ul>
  214. =end html
  215. =cut