Steve.org.ukSteve Kemp's Homepage |
|
|
Slaughter
Primitives
|
Slaughter : Primitives
AlertThe alert primitive is used to send an email. Sample usage is: Alert( Message => "Server on fire: $hostname",
To => 'steve[at]steve.org.uk',
Subject => "Alert: $fqdn" );
The following parameters are available:
AppendIfMissingThis primitive will open a local file, and append a line to it if it is not already present. AppendIfMissing( File => "/etc/hosts.allow",
Line => "All: 1.2.3.4" );
The following parameters are available:
CommentLinesMatchingThis primitive will open a local file, and comment out any line which matches the specified regular expression. if ( CommentLinesMatching( Pattern => "telnet|ftp",
File => "/etc/inetd.conf" ) )
{
RunCommand( Cmd => "/etc/init.d/inetd restart" );
}
The following parameters are available:
The return value of this function is the number of lines updated, or -1 if the file could not be opened. DeleteFilesMatchingThis primitive will delete files with names matching a particular pattern, recursively. #
# Delete *.dpkg-old - recursively
#
DeleteFilesMatching( Root => "/etc",
Pattern => "\\.dpkg-old\$" );
The following parameters are available:
DeleteOldFilesThis primitive will delete files older than the given number of days from the specified directory. Note unlike "DeleteFilesMatching" this function is not recursive. #
# Delete files older than ten days from /tmp.
#
DeleteFilesMatching( Root => "/tmp",
Age => 10 );
The following parameters are available:
The return value of this function is the number of files deleted. IdenticalContentsThe IdenticalContents primitive is used to compare whether two filenames have identical contents. The following is an example of usage:
#
# If the current contents don't match then move into place.
#
if (
1 != IdenticalContents( File1 => $tmp,
File2 => $dest ) )
{
system( "cp", $tmp, $dest );
}
else
{
unlink( $tmp );
}
The following parameters are available:
The return value will depend on the matching:
FetchFileThe FetchFile primitive is used to copy a file from the remote server to the local system. The file will have be moved into place if the local file is missing OR if it exists but contains different contents to the remote version. The following is an example of usage: if ( FetchFile( Source => "/etc/motd",
Dest => "/etc/motd",
Owner => "root",
Group => "root",
Mode => "644" ) )
{
# File was created/updated.
}
else
{
# File already existed locally with the same contents.
}
The following parameters are available:
When a file fetch is attempted several variations are attempted, not just the literal filename. The first file which exists and matches is returned, and the fetch is aborted: Template template expansion involves the use of the Text::Template module, of "Expand => true". This will convert the following text: # This is the config file for SSHD on {$fqdn}
To the following, assuming the local host is called "precious.my.flat": # This is the config file for SSHD on precious.my.flat The return value will depend on the result of the fetch:
FileMatchesThis allows you to test whether the contents of a given file match either a literal line of text, or a regular expression. if ( FileMatches( File => "/etc/sudoers",
Pattern => "steve" ) )
{
# OK "steve" is in sudoers. Somewhere.
}
The following parameters are available:
The return value of this function will be the number of matches found - regardless of whether a regular expression or literal match is in use. FindBinaryThis method allows you to search for an executable upon your system $PATH, or a supplied alternative string. if ( FindBinary( Binary => "ls" ) )
{
# we have ls!
}
The following parameters are available:
If the binary is found the full path will be returned, otherwise undef. InstallPackageThe InstallPackage primitive will allow you to install a system package. The precise mechanism used will depend upon your operating system:
foreach my $package ( qw! bash tcsh ! )
{
if ( PackageInstalled( Package => $package ) )
{
print "$package installed\n";
}
else
{
InstallPackage( Package => $package );
}
}
The following parameters are available:
LogMessageThis primitive is used to store a log-worthy message. When slaughter finishes executing it will output a summary of all log-messages which were encountered, sorted by priority. LogMessage( Message => "Server on fire: $hostname",
Level => "normal" );
The following parameters are available:
MountsReturn a list of all the mounted filesystems upon the current system. my @mounts = Mounts(); No parameters are required or supported in this method, and the return value is an array of all mounted filesystems upon this host. PackageInstalledTest whether a given system package is installed. if ( PackageInstalled( Package => "exim4-config" ) )
{
print "$package installed\n";
}
The following parameters are supported:
The return value will be a 0 if not installed, or 1 if it is. The precise mechanism used will depend upon your operating system:
PercentageUsedReturn the percentage of space used in in the given mounted-device. foreach my $point ( Mounts() )
{
if ( PercentageUsed( Path => $point ) > 80 )
{
Alert( To => "root",
From => "root",
Subject => "$server is running out of space on $point",
Message => "This is a friendly warning." );
}
}
The following parameters are supported:
The return value will be a percentage in the range 0-100. ReplaceRegexpThis primitive will open a local file, and replace any lines matching a given regular expression. ReplaceRegexp( File => "/etc/ssh/sshd_config",
Pattern => "^PermitRootLogin.*yes.*",
Replace => "PermitRootLogin no" );
The following parameters are available:
The return value of this function is the number of lines updated, 0 if none, or -1 if the file could not be opened. RemovePackageRemove the specified system package from the system. if ( PackageInstalled( Package => 'telnetd' ) )
{
RemovePackage( Package => 'telnetd' );
}
The following parameters are supported:
The precise mechanism used will depend upon your operating system:
RunCommandThis primitive will execute a system command. RunCommand( Cmd => "/usr/bin/id" ); The following parameters are available:
The return value of this function is the result of the perl system function. SetPermissionsThis method allows the file owner,group, and mode-bits of a local file to be changed. SetPermissions( File => "/etc/motd" ,
Owner => "root",
Group => "root",
Mode => "644" );
The following parameters are supported:
UserExistsThis primitive will test to see whether the given local user exists. if ( UserExists( User => "skx" ) )
{
# skx exists
}
The following parameters are available:
The return value of this function is 1 if the user exists, and 0 otherwise. UserDetailsThis primitive will return a hash of data about the local Unix user specified, if it exists. if ( UserExists( User => "skx" ) )
{
my %data = UserDetails( User => "skx" );
}
The following parameters are available:
The return value of this function is a hash of data conprising of the following Keys/Values
Undef will be returned on failure.
|
| Sitemap | Contact Me | © 2013 Steve Kemp |