98_serviced: add additional argument and attribute to declare the ssh port if not the default is used, fix retrieve status on define

git-svn-id: https://svn.fhem.de/fhem/trunk@23752 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
DeeSPe
2021-02-16 21:55:14 +00:00
parent 63943ba232
commit e55ad95808

View File

@@ -16,7 +16,7 @@ use Blocking;
use Time::HiRes;
use vars qw{%defs};
my $servicedVersion = "1.2.5";
my $servicedVersion = "1.2.6";
sub serviced_shutdownwait($);
@@ -36,6 +36,7 @@ sub serviced_Initialize($)
"serviceGetStatusOnInit:0,1 ".
"serviceInitd:1,0 ".
"serviceLogin ".
"servicePort ".
"serviceRegexFailed ".
"serviceRegexStarted ".
"serviceRegexStarting ".
@@ -50,11 +51,11 @@ sub serviced_Define($$)
{
my ($hash,$def) = @_;
my @args = split " ",$def;
return "Usage: define <name> serviced <service name> [user\@ip-address]"
if (@args < 3 || @args > 4);
my ($name,$type,$service,$remote) = @args;
return "Remote host must be like 'pi\@192.168.2.22' or 'pi\@myserver'!"
if ($remote && $remote !~ /^\w{2,}@(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|[\w\.\-_]{2,})$/);
return "Usage: define <name> serviced <service name> [user\@ip-address] [port]"
if (@args < 3 || @args > 5);
my ($name,$type,$service,$remote,$port) = @args;
return "Remote host must be like 'pi\@192.168.2.22' or 'pi\@myserver' or 'pi\@192.168.2.22 222' if you need to declare a SSH port other than the default port!"
if (($remote && $remote !~ /^\w{2,}@(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|[\w\.\-_]{2,})$/ && !$port) || ($remote && $remote !~ /^\w{2,}@(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|[\w\.\-_]{2,})$/ && $port && $port !~ /^\d{2,5}$/));
RemoveInternalTimer($hash);
$hash->{NOTIFYDEV} = "global";
$hash->{SERVICENAME} = $service;
@@ -67,6 +68,7 @@ sub serviced_Define($$)
$attr{$name}{room} = "Services";
$attr{$name}{icon} = "hue_room_garage";
$attr{$name}{serviceLogin} = $remote if ($remote);
$attr{$name}{servicePort} = $port if ($port);
$attr{$name}{webCmd} = "start:restart:stop:status";
if (grep /^homebridgeMapping/,split(" ",AttrVal("global","userattr","")))
{
@@ -149,6 +151,7 @@ sub serviced_Set($@)
if ($cmd eq "start" && ReadingsVal($name,"state","") =~ /^running|starting|failed$/);
my $service = $hash->{SERVICENAME};
my $login = AttrVal($name,"serviceLogin","");
$login .= $login ? " -p ".AttrNum($name,"servicePort",22) : "";
my $sudo = AttrNum($name,"serviceSudo",1) && $login !~ /^root@/ ? "sudo " : "";
my $line = AttrVal($name,"serviceStatusLine",3);
my $com;
@@ -191,7 +194,6 @@ sub serviced_Attr(@)
if ($attr_name eq "disable")
{
BlockingKill($hash->{helper}{RUNNING_PID}) if ($hash->{helper}{RUNNING_PID});
serviced_GetUpdate($hash) if (AttrNum($name,"serviceStatusInterval",0));
}
}
elsif ($attr_name =~ /^serviceAutostart|serviceAutostop$/)
@@ -200,12 +202,18 @@ sub serviced_Attr(@)
$er = "$attr_value not valid for $attr_name, must be a timeout in seconds like 1 to automatically stop service while shutdown of fhem (min: 1, max: 300)!" if ($attr_name eq "serviceAutostop");
return $er
if ($attr_value !~ /^(\d{1,3})$/ || $1 > 300 || $1 < 1);
return;
}
elsif ($attr_name eq "serviceLogin")
{
return "$attr_value not valid for $attr_name, must be a ssh login string like 'pi\@192.168.2.22' or 'pi\@myserver'!"
if ($attr_value !~ /^\w{2,}@(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|[\w\.\-_]{2,})$/);
}
elsif ($attr_name eq "servicePort")
{
return "$attr_value not valid for $attr_name, must be a port number like 22 or 222!"
if ($attr_value !~ /^\d{2,5}$/);
}
elsif ($attr_name =~ /^serviceRegexFailed|serviceRegexStopped|serviceRegexStarted|serviceRegexStarting$/)
{
my $ex = "dead|failed|exited";
@@ -220,13 +228,13 @@ sub serviced_Attr(@)
return "$attr_value not valid for $attr_name, must be a number in seconds like 300 (min: 5, max: 999999)!"
if ($attr_value !~ /^(\d{1,6})$/ || $1 < 5);
$hash->{helper}{interval} = $attr_value;
serviced_GetUpdate($hash);
}
elsif ($attr_name eq "serviceStatusLine")
{
return "$attr_value not valid for $attr_name, must be a number like 2, for 2nd line of status output, or 'last' for last line of status output!"
if ($attr_value !~ /^(\d{1,2}|last)$/);
}
serviced_GetUpdate($hash);
}
else
{
@@ -383,10 +391,11 @@ sub serviced_GetUpdate(@)
my $sec = defined $hash->{helper}{interval} ? $hash->{helper}{interval} : AttrNum($name,"serviceStatusInterval",undef);
delete $hash->{helper}{interval} if (defined $hash->{helper}{interval});
RemoveInternalTimer($hash);
return if (IsDisabled($name) || !$sec);
InternalTimer(gettimeofday() + $sec,"serviced_GetUpdate",$hash);
return if (IsDisabled($name));
serviced_Set($hash,$name,"status");
return undef;
return if (!$sec);
InternalTimer(gettimeofday() + $sec,"serviced_GetUpdate",$hash);
return;
}
sub serviced_Shutdown($)
@@ -563,6 +572,11 @@ sub serviced_shutdownwait($)
passwordless ssh is mandatory<br>
default:
</li>
<li>
<i>servicePort</i><br>
ssh port to use if other than default port (22)<br>
default: 22
</li>
<li>
<i>serviceRegexFailed</i><br>
regex for failed status<br>
@@ -729,6 +743,11 @@ sub serviced_shutdownwait($)
passwortloser SSH Zugang ist Grundvoraussetzung<br>
Voreinstellung:
</li>
<li>
<i>servicePort</i><br>
SSH Port falls ein anderer als der Standard Port (22) verwendet wird<br>
Voreinstellung: 22
</li>
<li>
<i>serviceRegexFailed</i><br>
Regex f&uuml;r failed Status<br>