* Moved most of 'scancore-database' into the new Database->configure_pgsql() method. I had renamed that script, but now it will be removed soon.

* Created System->reload_daemon().

Signed-off-by: Digimer <digimer@alteeve.ca>
main
Digimer 8 years ago
parent ae58dd6f12
commit 13352bad6d
  1. 378
      AN/Tools/Database.pm
  2. 67
      AN/Tools/System.pm
  3. 33
      AN/an-tools.xml
  4. 22
      cgi-bin/words.xml
  5. 5
      tools/an-prep-database

@ -5,12 +5,14 @@ package AN::Tools::Database;
use strict;
use warnings;
use DBI;
use Data::Dumper;
our $VERSION = "3.0.0";
my $THIS_FILE = "Database.pm";
### Methods;
# configure_pgsql
# connect
# disconnect
# get_local_id
@ -80,6 +82,354 @@ sub parent
# Public methods #
#############################################################################################################
=head2 configure_pgsql
This configures the local database server. Specifically, it checks to make sure the daemon is running and starts it if not. It also checks the 'pg_hba.conf' configuration to make sure it is set properly to listen on this machine's IP addresses and interfaces.
If the system is already configured, this method will do nothing, so it is safe to call it at any time.
If there is a problem, C<< undef >> is returned.
Parameters;
=head3 id (required)
This is the ID of the local database in the local configuration file that will be used to configure the local system.
=cut
sub configure_pgsql
{
my $self = shift;
my $parameter = shift;
my $an = $self->parent;
my $id = defined $parameter->{id} ? $parameter->{id} : "";
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { id => $id }});
if (not $id)
{
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0020", variables => { method => "Database->configure_pgsql()", parameter => "id" }});
return(undef);
}
# If we're not running with root access, return.
if (($< != 0) && ($> != 0))
{
# This is a minor error as it will be hit by every unpriviledged program that connects to the
# database(s).
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 2, priority => "alert", key => "log_0113"});
return(undef);
}
# First, is it running?
my $running = $an->System->check_daemon({daemon => "postgresql"});
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { running => $running }});
if (not $running)
{
# Do we need to initialize the databae?
if (not -e $an->data->{path}{configs}{'pg_hba.conf'})
{
# Initialize.
my $output = $an->System->call({shell_call => $an->data->{path}{exe}{'postgresql-setup'}." initdb", source => $THIS_FILE, line => __LINE__});
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { output => $output }});
# Did it succeed?
if (not -e $an->data->{path}{configs}{'pg_hba.conf'})
{
# Failed...
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0050"});
return(undef);
}
else
{
# Initialized!
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 1, key => "log_0055"});
}
}
}
# Setup postgresql.conf, if needed
my $postgresql_conf = $an->Storage->read_file({file => $an->data->{path}{configs}{'postgresql.conf'}});
my $update_postgresql_file = 1;
my $new_postgresql_conf = "";
foreach my $line (split/\n/, $postgresql_conf)
{
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { line => $line }});
if ($line =~ /^listen_addresses = '\*'/)
{
# No need to update.
$update_postgresql_file = 0;
last;
}
elsif ($line =~ /^#listen_addresses = 'localhost'/)
{
# Inject the new listen_addresses
$new_postgresql_conf .= "# This has been changed by AN::Tools::Database->configure_pgsql() to enable\n";
$new_postgresql_conf .= "# listening on all interfaces.\n";
$new_postgresql_conf .= "#listen_addresses = 'localhost'\n";
$new_postgresql_conf .= "listen_addresses = '*'\n";
}
$new_postgresql_conf .= $line."\n";
}
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { update_postgresql_file => $update_postgresql_file }});
if ($update_postgresql_file)
{
# Back up the existing one, if needed.
my $postgresql_backup = $an->data->{path}{directories}{backups}."/pgsql/postgresql.conf";
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { postgresql_backup => $postgresql_backup }});
if (not -e $postgresql_backup)
{
$an->Storage->copy_file({source => $an->data->{path}{configs}{'postgresql.conf'}, target => $postgresql_backup});
}
# Write the updated one.
$an->Storage->write_file({
file => $an->data->{path}{configs}{'postgresql.conf'},
body => $new_postgresql_conf,
user => "postgres",
group => "postgres",
mode => "0600",
overwrite => 1,
});
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 1, key => "log_0056", variables => { file => $an->data->{path}{configs}{'postgresql.conf'} }});
}
# Setup pg_hba.conf now, if needed.
my $pg_hba_conf = $an->Storage->read_file({file => $an->data->{path}{configs}{'pg_hba.conf'}});
my $update_pg_hba_file = 1;
my $new_pg_hba_conf = "";
foreach my $line (split/\n/, $pg_hba_conf)
{
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { line => $line }});
if ($line =~ /^host\s+all\s+all\s+\all\s+md5$/)
{
# No need to update.
$update_pg_hba_file = 0;
last;
}
elsif ($line =~ /^# TYPE\s+DATABASE/)
{
# Inject the new listen_addresses
$new_pg_hba_conf .= $line."\n";
$new_pg_hba_conf .= "host\tall\t\tall\t\tall\t\t\tmd5\n";
}
else
{
$new_pg_hba_conf .= $line."\n";
}
}
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { update_pg_hba_file => $update_pg_hba_file }});
if ($update_pg_hba_file)
{
# Back up the existing one, if needed.
my $pg_hba_backup = $an->data->{path}{directories}{backups}."/pgsql/pg_hba.conf";
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { pg_hba_backup => $pg_hba_backup }});
if (not -e $pg_hba_backup)
{
$an->Storage->copy_file({source => $an->data->{path}{configs}{'pg_hba.conf'}, target => $pg_hba_backup});
}
# Write the new one.
$an->Storage->write_file({
file => $an->data->{path}{configs}{'pg_hba.conf'},
body => $new_pg_hba_conf,
user => "postgres",
group => "postgres",
mode => "0600",
overwrite => 1,
});
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 1, key => "log_0057", variables => { file => $an->data->{path}{configs}{'postgresql.conf'} }});
}
# Start or restart the daemon?
if (not $running)
{
# Start the daemon.
my $return_code = $an->System->start_daemon({daemon => "postgresql"});
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { return_code => $return_code }});
if ($return_code eq "0")
{
# Started the daemon.
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 1, key => "log_0059"});
}
else
{
# Failed to start
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0094"});
return(undef);
}
}
elsif (($update_postgresql_file) or ($update_pg_hba_file))
{
# Reload
my $return_code = $an->System->start_daemon({daemon => "postgresql"});
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { return_code => $return_code }});
if ($return_code eq "0")
{
# Reloaded the daemon.
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 1, key => "log_0112"});
}
else
{
# Failed to reload
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0111"});
}
}
### TODO: This might be a security issue... We create the file owned as root with 600 permissions,
### but if we're not doing something write, we might still be exposing the password for a
### moment...
# Create the .pgpass file, if needed.
my $created_pgpass = 0;
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, secure => 1, list => {
'path::secure::postgres_pgpass' => $an->data->{path}{secure}{postgres_pgpass},
"database::${id}::password" => $an->data->{database}{$id}{password},
}});
if ((not -e $an->data->{path}{secure}{postgres_pgpass}) && ($an->data->{database}{$id}{password}))
{
my $body = "*:*:*:postgres:".$an->data->{database}{$id}{password}."\n";
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, secure => 1, list => { body => $body }});
$an->Storage->write_file({
file => $an->data->{path}{secure}{postgres_pgpass},
body => $body,
user => "postgres",
group => "postgres",
mode => "0600",
overwrite => 1,
secure => 1,
});
if (-e $an->data->{path}{secure}{postgres_pgpass})
{
$created_pgpass = 1;
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { created_pgpass => $created_pgpass }});
}
}
# Does the database user exist?
my $create_user = 1;
my $scancore_user = $an->data->{database}{$id}{user};
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { scancore_user => $scancore_user }});
if (not $scancore_user)
{
# No database user defined
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0099", variables => { id => $id }});
return(undef);
}
my $user_list = $an->System->call({shell_call => $an->data->{path}{exe}{su}." - postgres -c \"".$an->data->{path}{exe}{psql}." template1 -c 'SELECT usename, usesysid FROM pg_catalog.pg_user;'\"", source => $THIS_FILE, line => __LINE__});
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { user_list => $user_list }});
foreach my $line (split/\n/, $user_list)
{
if ($line =~ /^ $scancore_user\s+\|\s+(\d+)/)
{
# User exists already
my $id = $1;
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 2, key => "log_0060", variables => { user => $scancore_user, id => $id }});
$create_user = 0;
last;
}
}
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { create_user => $create_user }});
if ($create_user)
{
# Create the user
my $create_output = $an->System->call({shell_call => $an->data->{path}{exe}{su}." - postgres -c \"".$an->data->{path}{exe}{createuser}." --no-superuser --createdb --no-createrole $scancore_user\"", source => $THIS_FILE, line => __LINE__});
my $user_list = $an->System->call({shell_call => $an->data->{path}{exe}{su}." - postgres -c \"".$an->data->{path}{exe}{psql}." template1 -c 'SELECT usename, usesysid FROM pg_catalog.pg_user;'\"", source => $THIS_FILE, line => __LINE__});
my $user_exists = 0;
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { create_output => $create_output, user_list => $user_list }});
foreach my $line (split/\n/, $user_list)
{
if ($line =~ /^ $scancore_user\s+\|\s+(\d+)/)
{
# Success!
my $id = $1;
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 2, key => "log_0095", variables => { user => $scancore_user, id => $id }});
$user_exists = 1;
last;
}
}
if (not $user_exists)
{
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0096", variables => { user => $scancore_user }});
return(undef);
}
# Update/set the passwords.
if ($an->data->{database}{$id}{password})
{
foreach my $user ("postgres", $scancore_user)
{
my $update_output = $an->System->call({secure => 1, shell_call => $an->data->{path}{exe}{su}." - postgres -c \"".$an->data->{path}{exe}{psql}." template1 -c \\\"ALTER ROLE $user WITH PASSWORD '".$an->data->{database}{$id}{password}."';\\\"\"", source => $THIS_FILE, line => __LINE__});
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, secure => 1, list => { update_output => $update_output }});
foreach my $line (split/\n/, $user_list)
{
if ($line =~ /ALTER ROLE/)
{
# Password set
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 2, key => "log_0100", variables => { user => $user }});
}
}
}
}
}
# Create the database, if needed.
my $create_database = 1;
my $scancore_database = $an->data->{database}{$id}{name};
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { "database::${id}::name" => $an->data->{database}{$id}{name} }});
my $database_list = $an->System->call({shell_call => $an->data->{path}{exe}{su}." - postgres -c \"".$an->data->{path}{exe}{psql}." template1 -c 'SELECT datname FROM pg_catalog.pg_database;'\"", source => $THIS_FILE, line => __LINE__});
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { database_list => $database_list }});
foreach my $line (split/\n/, $database_list)
{
if ($line =~ /^ $scancore_database$/)
{
# Database already exists.
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 2, key => "log_0105", variables => { database => $scancore_database }});
$create_database = 0;
last;
}
}
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { create_database => $create_database }});
if ($create_database)
{
my $create_output = $an->System->call({shell_call => $an->data->{path}{exe}{su}." - postgres -c \"".$an->data->{path}{exe}{createdb}." --owner $scancore_user $scancore_database\"", source => $THIS_FILE, line => __LINE__});
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { create_output => $create_output }});
my $database_exists = 0;
my $database_list = $an->System->call({shell_call => $an->data->{path}{exe}{su}." - postgres -c \"".$an->data->{path}{exe}{psql}." template1 -c 'SELECT datname FROM pg_catalog.pg_database;'\"", source => $THIS_FILE, line => __LINE__});
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { database_list => $database_list }});
foreach my $line (split/\n/, $database_list)
{
if ($line =~ /^ $scancore_database$/)
{
# Database created
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 2, key => "log_0110", variables => { database => $scancore_database }});
$database_exists = 1;
last;
}
}
if (not $database_exists)
{
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0109", variables => { database => $scancore_database }});
return(undef);
}
}
# Remove the temporary password file.
if (($created_pgpass) && (-e $an->data->{path}{secure}{postgres_pgpass}))
{
unlink $an->data->{path}{secure}{postgres_pgpass};
if (-e $an->data->{path}{secure}{postgres_pgpass})
{
# Failed to unlink the file.
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "alert", key => "log_0107"});
}
}
return(0);
}
=head2 connect_to_databases
This method tries to connect to all databases it knows of. To define databases for a machine to connect to, load a configuration file with the following parameters;
@ -256,11 +606,29 @@ sub connect
}
}
# Before we try to connect, see if this is a local database and, if so, make sure it's setup.
if (($host eq $an->_hostname) or
($host eq $an->_short_hostname) or
($host eq "localhost") or
($host eq "127.0.0.1") or
(not $an->data->{sys}{read_db_id}))
{
$an->data->{sys}{read_db_id} = $id;
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { "sys::read_db_id" => $an->data->{sys}{read_db_id} }});
# Set it up (or update it) if needed. This method just returns if nothing is needed.
$an->Database->configure_pgsql({id => $id});
}
# Connect!
my $dbh = "";
### NOTE: The Database->write() method, when passed an array, will automatically disable
### autocommit, do the bulk write, then commit when done.
# We connect with fatal errors, autocommit and UTF8 enabled.
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
db_connect_string => $db_connect_string,
user => $user,
}});
eval { $dbh = DBI->connect($db_connect_string, $user, $password, {
RaiseError => 1,
AutoCommit => 1,
@ -321,7 +689,7 @@ sub connect
port => $port,
};
}
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 1, priority => "alert", key => $message_key, variables => { $variables }});
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 1, priority => "alert", key => $message_key, variables => $variables });
}
elsif ($dbh =~ /^DBI::db=HASH/)
{
@ -337,6 +705,12 @@ sub connect
id => $id,
}});
if (not $an->data->{sys}{use_db_fh})
{
$an->data->{sys}{use_db_fh} = $dbh;
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { 'sys::use_db_fh' => $an->data->{sys}{use_db_fh} }});
}
# Now that I have connected, see if my 'hosts' table exists.
my $query = "SELECT COUNT(*) FROM pg_catalog.pg_tables WHERE tablename=".$an->data->{sys}{use_db_fh}->quote($test_table)." AND schemaname='public';";
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { query => $query }});
@ -480,7 +854,7 @@ sub connect
alert_level => "warning",
alert_agent_name => "ScanCore",
alert_title_key => "an_alert_title_0006",
alert_message_key => "cleared_message_0001",
alert_message_key => "cleared_log_0055",
alert_message_variables => {
name => $an->data->{database}{$id}{name},
host => $an->data->{database}{$id}{host},

@ -18,6 +18,7 @@ my $THIS_FILE = "System.pm";
# ping
# read_ssh_config
# remote_call
# reload_daemon
# start_daemon
# stop_daemon
@ -293,7 +294,7 @@ sub ping
my $ping = $parameter->{ping} ? $parameter->{ping} : "";
my $port = $parameter->{port} ? $parameter->{port} : "";
my $target = $parameter->{target} ? $parameter->{target} : "";
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => {
count => $count,
fragment => $fragment,
payload => $payload,
@ -307,28 +308,28 @@ sub ping
if ($payload)
{
$payload -= 28;
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { payload => $payload }});
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { payload => $payload }});
}
# Build the call
my $shell_call = $an->data->{path}{exe}{'ping'}." -W 1 -n $ping -c 1";
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { shell_call => $shell_call }});
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { shell_call => $shell_call }});
if (not $fragment)
{
$shell_call .= " -M do";
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { shell_call => $shell_call }});
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { shell_call => $shell_call }});
}
if ($payload)
{
$shell_call .= " -s $payload";
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { shell_call => $shell_call }});
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { shell_call => $shell_call }});
}
my $pinged = 0;
my $average_ping_time = 0;
foreach my $try (1..$count)
{
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { count => $count, try => $try }});
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { count => $count, try => $try }});
last if $pinged;
my $output = "";
@ -343,24 +344,24 @@ sub ping
port => $port,
password => $password,
});
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { output => $output }});
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { output => $output }});
}
else
{
### Local calls
$output = $an->System->call({shell_call => $shell_call});
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { output => $output }});
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { output => $output }});
}
foreach my $line (split/\n/, $output)
{
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { line => $line }});
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { line => $line }});
if ($line =~ /(\d+) packets transmitted, (\d+) received/)
{
# This isn't really needed, but might help folks watching the logs.
my $pings_sent = $1;
my $pings_received = $2;
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => {
pings_sent => $pings_sent,
pings_received => $pings_received,
}});
@ -369,7 +370,7 @@ sub ping
{
# Contact!
$pinged = 1;
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { pinged => $pinged }});
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { pinged => $pinged }});
}
else
{
@ -381,14 +382,14 @@ sub ping
if ($line =~ /min\/avg\/max\/mdev = .*?\/(.*?)\//)
{
$average_ping_time = $1;
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { average_ping_time => $average_ping_time }});
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { average_ping_time => $average_ping_time }});
}
}
}
# 0 == Ping failed
# 1 == Ping success
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => {
pinged => $pinged,
average_ping_time => $average_ping_time,
}});
@ -851,6 +852,45 @@ sub remote_call
return($error, $output);
};
=head2 reload_daemon
This method reloads a daemon (typically to pick up a change in configuration). The return code from the start request will be returned.
If the return code for the reload command wasn't read, C<< undef >> is returned. If it did reload, C<< 0 >> is returned. If the reload failed, a non-0 return code will be returned.
Parameters;
=head3 daemon (required)
This is the name of the daemon to reload.
=cut
sub reload_daemon
{
my $self = shift;
my $parameter = shift;
my $an = $self->parent;
my $return = undef;
my $daemon = defined $parameter->{daemon} ? $parameter->{daemon} : "";
my $say_daemon = $daemon =~ /\.service$/ ? $daemon : $daemon.".service";
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { daemon => $daemon, say_daemon => $say_daemon }});
my $output = $an->System->call({shell_call => $an->data->{path}{exe}{systemctl}." reload ".$say_daemon."; ".$an->data->{path}{exe}{'echo'}." return_code:\$?"});
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { output => $output }});
foreach my $line (split/\n/, $output)
{
if ($line =~ /return_code:(\d+)/)
{
$return = $1;
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { 'return' => $return }});
}
}
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { 'return' => $return }});
return($return);
}
=head2 start_daemon
This method starts a daemon. The return code from the start request will be returned.
@ -879,6 +919,7 @@ sub start_daemon
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { output => $output }});
foreach my $line (split/\n/, $output)
{
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { line => $line }});
if ($line =~ /return_code:(\d+)/)
{
$return = $1;

@ -87,11 +87,11 @@ It also has replacement variables: [#!variable!first!#] and [#!variable!second!#
<key name="log_0043"><![CDATA[[ Error ] - The method System->call() was called but 'shell_call' was not passed or was empty.]]></key>
<key name="log_0044">The host: [#!variable!host!#] has renewed its database lock.</key>
<key name="log_0045">The host: [#!variable!host!#] is requesting a database lock.</key>
<key name="log_0046"><![CDATA[[ Error ] - The method Storage->copy_file() was asked to copy: [#!variable!source!#] to: [#!variable!target!#], but the target already exists and 'overwrite' wasn't specified, so aborting.]]></key>
<key name="log_0046"><![CDATA[[ Note ] - The method Storage->copy_file() was asked to copy: [#!variable!source!#] to: [#!variable!target!#], but the target already exists and 'overwrite' wasn't specified, skipping.]]></key>
<key name="log_0047"><![CDATA[[ Error ] - The method Log->level() was passed an invalid log level: [#!variable!set!#]. Only '0', '1', '2', '3' or '4' are valid.]]></key>
<key name="log_0048"><![CDATA[[ Warning ] - Testing of AN::Tools is beginning. This will generate warnings and alerts and are not a concern.]]></key>
<key name="log_0049"><![CDATA[[ Warning ] - Testing of AN::Tools is complete.]]></key>
<key name="log_0050">#!free!#</key>
<key name="log_0050">[ Error ] - There is a local database defined, but it does not appear to exist and we could not initialize the database server. Is 'postgresql-server' installed?</key>
<key name="log_0051"><![CDATA[[ Error ] - The method Storage->change_owner() was asked to change the ownership of: [#!variable!target!#] which doesn't exist.]]></key>
<key name="log_0052"><![CDATA[[ Error ] - The method Storage->copy_file() was called but the source file: [#!variable!source!#] doesn't exist.]]></key>
<key name="log_0053"><![CDATA[[ Error ] - The 'Database->connect()' method tried to connect to the same database twice: [#!variable!target!#].]]></key>
@ -105,12 +105,12 @@ Connecting to Database with configuration ID: [#!variable!id!#]
- user: ............ [#!variable!user!#]
- password: ........ [#!variable!password!#]
</key>
<key name="log_0055">#!free!#</key>
<key name="log_0056">#!free!#</key>
<key name="log_0057">#!free!#</key>
<key name="log_0055">Initialized PostgreSQL.</key>
<key name="log_0056">Updated: [#!variable!file!#] to listen on all interfaces.</key>
<key name="log_0057">Updated: [#!variable!file!#] to require passwords for access.</key>
<key name="log_0058"><![CDATA[[ Error ] - The method System->remote_call() was called but the port: [#!variable!port!#] is invalid. It must be a digit between '1' and '65535'.]]></key>
<key name="log_0059">#!free!#</key>
<key name="log_0060">#!free!#</key>
<key name="log_0059">Started the PostgreSQL database server.</key>
<key name="log_0060">Database user: [#!variable!user!#] already exists with ID: [#!variable!id!#].</key>
<key name="log_0061"><![CDATA[[ Error ] - The method Get->users_home() was asked to find the home directory for the user: [#!variable!user!#], but was unable to do so.]]></key>
<key name="log_0062">SSH session opened without a password to: [#!variable!target!#].</key>
<key name="log_0063">The database with the ID: [#!variable!id!#] did not respond to pings and 'database::#!variable!id!#::ping_before_connect' is not set to '0' in '#!data!path::configs::striker.conf!#', skipping it.</key>
@ -155,9 +155,9 @@ The database connection error was:
<key name="log_0091">Failed to connect to any database.</key>
<key name="log_0092"><![CDATA[[ Error ] - Unable to connect to the database: [#!variable!server!#] (id: [#!variable!id!#]_.]]></key>
<key name="log_0093"><![CDATA[[ Error ] - The method Alert->check_alert_sent() was called but the 'modified_date' parameter was not passed and/or 'sys::db_timestamp' is not set. Did the program fail to connect to any databases?]]></key>
<key name="log_0094">#!free!#</key>
<key name="log_0095">#!free!#</key>
<key name="log_0096">#!free!#</key>
<key name="log_0094">[ Error ] - Failed to start the Postgres server. Please check the system logs for details.</key>
<key name="log_0095">The database user: [#!variable!user!#] was created with ID: [#!variable!id!#].</key>
<key name="log_0096">[ Error ] - Failed to add the database user: [#!variable!user!#]! Unable to proceed.</key>
<key name="log_0097"><![CDATA[[ Error ] - The method Alert->check_alert_sent() was called but the 'set' parameter was not passed or it is empty. It should be 'set' or 'clear'.]]></key>
<key name="log_0098">
[ Warning ] - Failed to set an alert because this host is not yet in the database. This can happen if the alert was set before this host was added to the database.
@ -168,16 +168,21 @@ The database connection error was:
- Name: [#!variable!alert_name!#]
- Timestamp: [#!variable!modified_date!#]
</key>
<key name="log_0099">#!free!#</key>
<key name="log_0100">#!free!#</key>
<key name="log_0099">[ Error ] - There is no ScanCore database user set for the local machine. Please check: [#!data!path::config::striker.conf!#]'s DB entry: [#!variable!id!#].</key>
<key name="log_0100">Database user: [#!variable!user!#] password has been set/updated.</key>
<key name="log_0101"><![CDATA[[ Error ] - The method Alert->register_alert() was called but the 'title_key' parameter was not passed or it is empty and 'header' is enable (default).]]></key>
<key name="log_0102">I am not recording the alert with message_key: [#!variable!message_key!#] to the database because its log level was lower than any recipients.</key>
<key name="log_0103">The local machine's UUID was not read properly. It should be stored in: [#!data!sys::host_uuid!#] and contain hexadecimal characters in the format: '012345-6789-abcd-ef01-23456789abcd' and usually matches the output of 'dmidecode --string system-uuid'. If this file exists and if there is a string in the file, please verify that it is structured correctly.</key>
<key name="log_0104">The database with ID: [#!variable!id!#] for: [#!variable!file!#] is behind.</key>
<key name="log_0105">#!free!#</key>
<key name="log_0105">ScanCore database: [#!variable!database!#] already exists.</key>
<key name="log_0106">The database with ID: [#!variable!id!#] for: [#!variable!file!#] and table: [#!variable!table!#] is behind.</key>
<key name="log_0107">#!free!#</key>
<key name="log_0107">[ Warning ] - Failed to delete the temporary postgres password.</key>
<key name="log_0108"><![CDATA[[ Error ] - The method Database->insert_or_update_states() was called but the 'state_host_uuid' parameter was not passed or it is empty. Normally this is set to 'sys::data_uuid'.]]></key>
<key name="log_0109">[ Error ] - Failed to create the ScanCore database: [#!variable!database!#]</key>
<key name="log_0110">ScanCore database: [#!variable!database!#] created.</key>
<key name="log_0111">[ Warning ] - Failed to reload the Postgres server. Please check the system logs for details. The updated configuration is probably not active yet.</key>
<key name="log_0112">Reloaded the PostgreSQL database server.</key>
<key name="log_0113"><![CDATA[[ Note ] - The 'Database->configure_pgsql() method was called but the parent program is not running with root priviledges. Returning without doing anything.]]>.</key>
<!-- Test words. Do NOT change unless you update 't/Words.t' or tests will needlessly fail. -->
<key name="t_0000">Test</key>

@ -30,28 +30,6 @@ This is the AN::Tools master 'words' file.
<key name="js_0002">Down</key>
<key name="js_0003">Mbps</key>
<!-- Errors -->
<key name="error_0001">[ Error ] - There is a local database defined, but it does not appear to exist and we could not initialize the database server. Is 'postgresql-server' installed?</key>
<key name="error_0002">[ Error ] - Failed to start the Postgres server. Please check the system logs for details.</key>
<key name="error_0003">[ Error ] - There is no ScanCore database user set for the local machine. Please check: [#!data!path::config::striker.conf!#]'s DB entry: [#!variable!id!#].</key>
<key name="error_0004">[ Error ] - Failed to add the database user: [#!variable!user!#]! Unable to proceed.</key>
<key name="error_0005">[ Error ] - Failed to create the ScanCore database: [#!variable!database!#]</key>
<!-- Messages -->
<key name="message_0001">Initialized PostgreSQL.</key>
<key name="message_0002">Updated: [#!variable!file!#] to listen on all interfaces.</key>
<key name="message_0003">Updated: [#!variable!file!#] to require passwords for access.</key>
<key name="message_0004">Started the PostgreSQL database server.</key>
<key name="message_0005">Database user: [#!variable!user!#] already exists with ID: [#!variable!id!#].</key>
<key name="message_0006">Database user: [#!variable!user!#] was created with ID: [#!variable!id!#].</key>
<key name="message_0007">Database user: [#!variable!user!#] password has been set/updated.</key>
<key name="message_0008">ScanCore database: [#!variable!database!#] already exists.</key>
<key name="message_0009">ScanCore database: [#!variable!database!#] created.</key>
<key name="message_0010">Failed to find a local ID, no databases are stored on this machine.</key>
<!-- Warnings -->
<key name="warning_0001">[ Warning ] - Failed to delete the temporary postgres password.</key>
</language>
<!-- 日本語 -->
<language name="jp" long_name="日本語" description="Striker/ScanCore language file.">

@ -1,6 +1,7 @@
#!/usr/bin/perl
#
# This checks the state of the database server and, if necessary, sets up the database.
# This checks the state of the postgresql database server and, if necessary, configures it for external
# access, initializes it and gets it running.
#
# Exit codes;
# 0 = Normal exit.
@ -30,7 +31,7 @@ $an->Log->level({set => 2});
$an->Log->secure({set => 1});
# Paths
$an->data->{path}{tools}{'scancore-database'} = "/usr/sbin/striker/scancore-database";
$an->data->{path}{tools}{'an-prep-database'} = "/usr/sbin/striker/scancore-database";
$an->data->{path}{tools}{'scancore-update-states'} = "/usr/sbin/striker/scancore-update-states";
$an->data->{path}{config}{'striker.conf'} = "/etc/striker/striker.conf";
Loading…
Cancel
Save