docu2
Write a Perl script as SCR agent:
First we have to say that it is a perl script ;-)
Then we add our lib ycp.
#!/usr/bin/perl
use lib "/usr/lib/YaST2/agents_non_y2";
use ycp;
In the next step we build the main loop
Our new perlscript is loaded only once by the SCR, which controlls all
agents.
For every request it reads the input from stdin an writes it output
to stdout. The protocol for stdin and stdout is YCP, and for perl
you have some convenice functions.
Let have a look at the simplest working script:
while ( <STDIN> )
{
# ycpDoVerboseLog();
ycpInit( $_ );
# read the arguments ...
# do the work ...
ycpReturn<whatever>( );
}
2. Read the Arguments
For a useable script we have to pass some arguments to this script.
In the YCP world for every call, we pass three types of
arguments to the script.
Pratical approach: (for details see the arguments)
1.The most important argument type is the value. The value we
pass to the script, is what in other scripts usually is call
arguments. So if we nedd a list of integer for our script, we
choose a list as value, if we need a single string, we choose
string
2.The function type: If you have no typical Read/Write, like
reading, writing a configfile use Execute. This is not in any way
critical. A script can support more than one type!
3.The subpath: For a little agent don't use subpath. Call your agent
every time with the path you mount it on. (see example later on)
simple value: "10.10.0.1" or 127
complex value: $[ "device":"eth0", "netmask":"255.255.255.0", "gateway":"0.0.0.0" ]
In our example, for agent which should test for a given list of server,
if they are reachable via ping we have made the following decisions:
function type: Execute
path: .ping
subpath: .
value: a .list. of hosts, every host as string
Now we look at our example. In first step we check the incoming data, if they have the expected
values ;-). We use the suitable ycpCommandIs* and ycpArgIs*
#----------------------------------------------------------
# Check the Command
# We expect a `Execute( .ping, [ , ...]);
#----------------------------------------------------------
if ( ycpCommandIsExecute && ycpArgIsList )
{
y2debug( "Now we check with ping the hosts:", ycpGetArgList );
In the last line we have written debug line, with
y2debug. The debug lines are written to /var/log/YaST2/y2log
Note: Be sure never to write debug messages or anything else via
print to stdout. You will break the communication protokoll, and your
server will stop working.
Now we will start to process our data. First we have read the list of servers into a perl array.
We use the
suitable ycpGetArg* call:
##########################################
# OUR PERL MAIN
##########################################
# get the list of hosts
my @host_array = ycpGetArgList;
3. Do the work
Our main task is a typical perl script, which send a ping to
all servers in the list.
my %result;
my $host;
# send a ping to all hosts
my $p = Net::Ping->new("icmp");
foreach $host (@host_array)
{
if ( $p->ping($host, 1) )
{
$result{$host} = " alive";
}
else
{
$result{$host} = "NOT alive";
}
}
$p->close();
At the end we make a debug output of the result hash.
# for debug purpose: print a result list to /var/log/YaST2/y2log
while (( $host, my $alive) = each(%result))
{
y2debug( $alive, ": Host", $host);
}
4. Writing the output value to the SCR
As last step our script should return the server list to the SCR, and
for every server the list should contain if the server is reachable or
not. We have this data stored in a perl hash and send this as a YCP
map
to the SCR.
For this task, we only have to return our result hash via ycpReturnHashAsMap:
Note: It is very important, that for every call of the script
you have to pass one, exactly one return Value to the SCR.
Therefore you have to call exactly one ycpRetun* function of
our perl-ycp interface.
##########################################
# END of PERL MAIN
##########################################
ycpReturnHashAsMap( %result );
}
else
{
my $return_value = sprintf( "Unknown instruction %s or argument: %s", ycpGetCommand, ycpGetArgType);
ycpReturnSkalarAsString( $return_value );
}
}
# end
Now our script is ready to use.
In first step you should test your script
see Test your script
To test it within a YCP module we have to
copy it to /usr/lib/YaST2/servers_non_y2
a good name for our new file is ag_, so we had
choosen ag_ping.
In the last step we have to define the mount point for our script, i.e
the path: