Skip to content
Snippets Groups Projects
Commit f7fde0e2 authored by Tomáš Plesník's avatar Tomáš Plesník
Browse files

napojeno na novou verzi sendMsg v WardenCommon; presunuto nacitani...

napojeno na novou verzi sendMsg v WardenCommon; presunuto nacitani konfiguracniho souboru; zmena formatovani if podminek
parent cc3da0eb
No related branches found
No related tags found
No related merge requests found
...@@ -27,6 +27,21 @@ use WardenCommon; ...@@ -27,6 +27,21 @@ use WardenCommon;
our $VERSION = "2.2"; our $VERSION = "2.2";
my $etc = "$FindBin::RealBin/../etc"; my $etc = "$FindBin::RealBin/../etc";
# read config file
my $conf_file = "$etc/warden-server.conf";
our $SYSLOG = undef;
our $SYSLOG_VERBOSE = undef;
our $SYSLOG_FACILITY = undef;
our $DB_NAME = undef;
our $DB_USER = undef;
our $DB_PASS = undef;
our $DB_HOST = undef;
unless (do $conf_file) {
die("Errors in config file '$conf_file': $@") if $@;
die("Can't read config file '$conf_file': $!") unless defined $_;
# if $_ defined, it's retvalue of last statement of conf, for which we don't care
}
################################################################################ ################################################################################
...@@ -50,13 +65,28 @@ sub help { ...@@ -50,13 +65,28 @@ sub help {
} }
#-------------------------------------------------------------------------------
# sendMsg - wrapper for more complex WardenCommon::sendMsg function
#-------------------------------------------------------------------------------
sub sendMsg
{
my $severity = shift;
my $syslog_msg = shift;
WardenCommon::sendMsg($SYSLOG, $SYSLOG_VERBOSE, $SYSLOG_FACILITY, $severity,
$syslog_msg, undef, $RealScript);
}
################################################################################ ################################################################################
# MAIN # MAIN
################################################################################ ################################################################################
our ($opt_n, $opt_r, $opt_t, $opt_o, $opt_i, $opt_h); our ($opt_n, $opt_r, $opt_t, $opt_o, $opt_i, $opt_h);
if ($#ARGV == -1) {usage} if ($#ARGV == -1) {
usage;
}
die usage unless getopts("n:r:t:i:ho"); die usage unless getopts("n:r:t:i:ho");
my $hostname = $opt_n; my $hostname = $opt_n;
...@@ -68,7 +98,9 @@ my $receive_own_events = "f"; ...@@ -68,7 +98,9 @@ my $receive_own_events = "f";
if ($opt_o) {$receive_own_events = "t"} if ($opt_o) {$receive_own_events = "t"}
# catch help param # catch help param
if ($help) {help} if ($help) {
help;
}
# check validity of IP address (CIDR format) # check validity of IP address (CIDR format)
if ($ip_net_client !~ /^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\/(\d|[1-2]\d|3[0-2]))$/) { if ($ip_net_client !~ /^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\/(\d|[1-2]\d|3[0-2]))$/) {
...@@ -77,15 +109,17 @@ if ($ip_net_client !~ /^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}( ...@@ -77,15 +109,17 @@ if ($ip_net_client !~ /^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}(
# superuser controle # superuser controle
my $UID = $<; my $UID = $<;
if ($UID != 0) {WardenCommon::errMsg("You must be root for running this script!")} if ($UID != 0) {
WardenCommon::errMsg("You must be root for running this script!");
}
# check parameters definition # check parameters definition
switch () { switch () {
case {!defined $hostname} {WardenCommon::errMsg("Parameter 'hostname' is not defined!")} case {!defined $hostname} {WardenCommon::errMsg("Parameter 'hostname' is not defined!")}
case {!defined $requestor} {WardenCommon::errMsg("Parameter 'requestor' is not defined!")} case {!defined $requestor} {WardenCommon::errMsg("Parameter 'requestor' is not defined!")}
case {!defined $type} {WardenCommon::errMsg("Parameter 'type' is not defined!")} case {!defined $type} {WardenCommon::errMsg("Parameter 'type' is not defined!")}
case {!defined $receive_own_events} {WardenCommon::errMsg("Parameter 'receive_own_events' is not defined!")} case {!defined $receive_own_events} {WardenCommon::errMsg("Parameter 'receive_own_events' is not defined!")}
case {!defined $ip_net_client} {WardenCommon::errMsg("Parameter 'ip_net_client' is not defined!")} case {!defined $ip_net_client} {WardenCommon::errMsg("Parameter 'ip_net_client' is not defined!")}
} }
# defined variables by script # defined variables by script
...@@ -95,21 +129,6 @@ my $registered = DateTime->now; ...@@ -95,21 +129,6 @@ my $registered = DateTime->now;
my $service = undef; my $service = undef;
my $description_tags = undef; my $description_tags = undef;
# read config file
my $conf_file = "$etc/warden-server.conf";
our $SYSLOG = undef;
our $SYSLOG_VERBOSE = undef;
our $SYSLOG_FACILITY = undef;
our $DB_NAME = undef;
our $DB_USER = undef;
our $DB_PASS = undef;
our $DB_HOST = undef;
unless (do $conf_file) {
die("Errors in config file '$conf_file': $@") if $@;
die("Can't read config file '$conf_file': $!") unless defined $_;
# if $_ defined, it's retvalue of last statement of conf, for which we don't care
}
# connect to DB # connect to DB
our $DBH = DBI->connect("DBI:mysql:database=$DB_NAME;host=$DB_HOST", $DB_USER, $DB_PASS, {RaiseError => 1, mysql_auto_reconnect => 1}) our $DBH = DBI->connect("DBI:mysql:database=$DB_NAME;host=$DB_HOST", $DB_USER, $DB_PASS, {RaiseError => 1, mysql_auto_reconnect => 1})
|| die "Could not connect to database: $DBH->errstr"; || die "Could not connect to database: $DBH->errstr";
...@@ -120,15 +139,15 @@ $sth->execute($hostname, $client_type, $type, $ip_net_client) or die "Cannot exe ...@@ -120,15 +139,15 @@ $sth->execute($hostname, $client_type, $type, $ip_net_client) or die "Cannot exe
my $result = $sth->fetchrow(); my $result = $sth->fetchrow();
my $warden_server = WardenCommon::trim(`hostname -f`); my $warden_server = WardenCommon::trim(`hostname -f`);
if (defined $result) { if (defined $result) {
WardenCommon::sendMsg($SYSLOG, $SYSLOG_VERBOSE, $SYSLOG_FACILITY, "err", "Attempt to re-register the receiver [hostname: '$hostname', type: '$type', cidr: '$ip_net_client']"); sendMsg("err", "Attempt to re-register receiver [Hostname: '$hostname', Type: '$type', CIDR: '$ip_net_client'] at Warden server '$warden_server'");
WardenCommon::errMsg("Receiver has already been registered at $warden_server in '$result'"); WardenCommon::errMsg("Receiver has already been registered at Warden server '$warden_server' in '$result'");
} else { } else {
$sth = $DBH->prepare("INSERT INTO clients VALUES (?,?,?,?,?,?,?,?,?,?,?);") or die "Cannot prepare statement: " . $DBH->errstr; $sth = $DBH->prepare("INSERT INTO clients VALUES (?,?,?,?,?,?,?,?,?,?,?);") or die "Cannot prepare statement: " . $DBH->errstr;
my $ret_val = $sth->execute(undef, $hostname, $registered, $requestor, $service, $client_type, $type, $receive_own_events, $description_tags, $ip_net_client, $valid) or die "Cannot execute statement: " . $sth->errstr; my $ret_val = $sth->execute(undef, $hostname, $registered, $requestor, $service, $client_type, $type, $receive_own_events, $description_tags, $ip_net_client, $valid) or die "Cannot execute statement: " . $sth->errstr;
if ($ret_val == 1) { if ($ret_val == 1) {
WardenCommon::sendMsg($SYSLOG, $SYSLOG_VERBOSE, $SYSLOG_FACILITY, "info", "New receiver [hostname: '$hostname', type: '$type', cidr: '$ip_net_client'] was registered"); sendMsg("info", "New receiver [Hostname: '$hostname', Type: '$type', CIDR: '$ip_net_client'] has been registered");
WardenCommon::succMsg("Registration of receiver from $hostname was SUCCESSFUL!!!"); WardenCommon::succMsg("Registration of receiver $hostname ($type) was SUCCESSFUL!!!");
} else { } else {
WardenCommon::errMsg("Registration of receiver from $hostname FAILED!!!"); WardenCommon::errMsg("Registration of receiver $hostname ($type) FAILED!!!");
} }
} }
...@@ -27,6 +27,21 @@ use WardenCommon; ...@@ -27,6 +27,21 @@ use WardenCommon;
our $VERSION = "2.2"; our $VERSION = "2.2";
my $etc = "$FindBin::RealBin/../etc"; my $etc = "$FindBin::RealBin/../etc";
# read config file
my $conf_file = "$etc/warden-server.conf";
our $SYSLOG = undef;
our $SYSLOG_VERBOSE = undef;
our $SYSLOG_FACILITY = undef;
our $DB_NAME = undef;
our $DB_USER = undef;
our $DB_PASS = undef;
our $DB_HOST = undef;
unless (do $conf_file) {
die("Errors in config file '$conf_file': $@") if $@;
die("Can't read config file '$conf_file': $!") unless defined $_;
# if $_ defined, it's retvalue of last statement of conf, for which we don't care
}
################################################################################ ################################################################################
...@@ -50,13 +65,28 @@ sub help { ...@@ -50,13 +65,28 @@ sub help {
} }
#-------------------------------------------------------------------------------
# sendMsg - wrapper for more complex WardenCommon::sendMsg function
#-------------------------------------------------------------------------------
sub sendMsg
{
my $severity = shift;
my $syslog_msg = shift;
WardenCommon::sendMsg($SYSLOG, $SYSLOG_VERBOSE, $SYSLOG_FACILITY, $severity,
$syslog_msg, undef, $RealScript);
}
################################################################################ ################################################################################
# MAIN # MAIN
################################################################################ ################################################################################
our ($opt_n, $opt_r, $opt_s, $opt_d, $opt_i, $opt_h); our ($opt_n, $opt_r, $opt_s, $opt_d, $opt_i, $opt_h);
if ($#ARGV == -1) {usage} if ($#ARGV == -1) {
usage;
}
die usage unless getopts("n:r:s:d:i:h"); die usage unless getopts("n:r:s:d:i:h");
my $hostname = $opt_n; my $hostname = $opt_n;
...@@ -67,7 +97,9 @@ my $ip_net_client = $opt_i; ...@@ -67,7 +97,9 @@ my $ip_net_client = $opt_i;
my $help = $opt_h; my $help = $opt_h;
# catch help param # catch help param
if ($help) {help} if ($help) {
help;
}
# check validity of IP address (CIDR format) # check validity of IP address (CIDR format)
if ($ip_net_client !~ /^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\/(\d|[1-2]\d|3[0-2]))$/) { if ($ip_net_client !~ /^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\/(\d|[1-2]\d|3[0-2]))$/) {
...@@ -76,7 +108,9 @@ if ($ip_net_client !~ /^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}( ...@@ -76,7 +108,9 @@ if ($ip_net_client !~ /^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}(
# superuser controle # superuser controle
my $UID = $<; my $UID = $<;
if ($UID != 0) {WardenCommon::errMsg("You must be root for running this script!")} if ($UID != 0) {
WardenCommon::errMsg("You must be root for running this script!");
}
# check parameters definition # check parameters definition
switch () { switch () {
...@@ -94,21 +128,6 @@ my $registered = DateTime->now; ...@@ -94,21 +128,6 @@ my $registered = DateTime->now;
my $type = undef; my $type = undef;
my $receive_own_events = undef; my $receive_own_events = undef;
# read config file
my $conf_file = "$etc/warden-server.conf";
our $SYSLOG = undef;
our $SYSLOG_VERBOSE = undef;
our $SYSLOG_FACILITY = undef;
our $DB_NAME = undef;
our $DB_USER = undef;
our $DB_PASS = undef;
our $DB_HOST = undef;
unless (do $conf_file) {
die("Errors in config file '$conf_file': $@") if $@;
die("Can't read config file '$conf_file': $!") unless defined $_;
# if $_ defined, it's retvalue of last statement of conf, for which we don't care
}
# connect to DB # connect to DB
our $DBH = DBI->connect("DBI:mysql:database=$DB_NAME;host=$DB_HOST", $DB_USER, $DB_PASS, {RaiseError => 1, mysql_auto_reconnect => 1}) our $DBH = DBI->connect("DBI:mysql:database=$DB_NAME;host=$DB_HOST", $DB_USER, $DB_PASS, {RaiseError => 1, mysql_auto_reconnect => 1})
|| die "Could not connect to database: $DBH->errstr"; || die "Could not connect to database: $DBH->errstr";
...@@ -119,15 +138,15 @@ $sth->execute($hostname, $service, $client_type, $ip_net_client) or die "Cannot ...@@ -119,15 +138,15 @@ $sth->execute($hostname, $service, $client_type, $ip_net_client) or die "Cannot
my $result = $sth->fetchrow(); my $result = $sth->fetchrow();
my $warden_server = WardenCommon::trim(`hostname -f`); my $warden_server = WardenCommon::trim(`hostname -f`);
if (defined $result) { if (defined $result) {
WardenCommon::sendMsg($SYSLOG, $SYSLOG_VERBOSE, $SYSLOG_FACILITY, "err", "Attempt to re-register the sender [hostname: '$hostname', service: '$service', cidr: '$ip_net_client']"); sendMsg("err", "Attempt to re-register sender [Hostname: '$hostname', Service: '$service', CIDR: '$ip_net_client'] at Warden server '$warden_server'");
WardenCommon::errMsg("Sender has already been registered at $warden_server in '$result'"); WardenCommon::errMsg("Sender has already been registered at Warden server '$warden_server' in '$result'");
} else { } else {
$sth = $DBH->prepare("INSERT INTO clients VALUES (?,?,?,?,?,?,?,?,?,?,?);") or die "Cannot prepare statement: " . $DBH->errstr; $sth = $DBH->prepare("INSERT INTO clients VALUES (?,?,?,?,?,?,?,?,?,?,?);") or die "Cannot prepare statement: " . $DBH->errstr;
my $ret_val = $sth->execute(undef, $hostname, $registered, $requestor, $service, $client_type, $type, $receive_own_events, $description_tags, $ip_net_client, $valid) or die "Cannot execute statement: " . $sth->errstr; my $ret_val = $sth->execute(undef, $hostname, $registered, $requestor, $service, $client_type, $type, $receive_own_events, $description_tags, $ip_net_client, $valid) or die "Cannot execute statement: " . $sth->errstr;
if ($ret_val == 1) { if ($ret_val == 1) {
WardenCommon::sendMsg($SYSLOG, $SYSLOG_VERBOSE, $SYSLOG_FACILITY, "info", "New sender [hostname: '$hostname', service: '$service', cidr: '$ip_net_client'] was registered"); sendMsg("info", "New sender [Hostname: '$hostname', Service: '$service', CIDR: '$ip_net_client'] has been registered");
WardenCommon::succMsg("Registration of sender from $hostname was SUCCESSFUL!!!"); WardenCommon::succMsg("Registration of sender $hostname ($service) was SUCCESSFUL!!!");
} else { } else {
WardenCommon::errMsg("Registration of sender from $hostname FAILED!!!"); WardenCommon::errMsg("Registration of sender $hostname ($service) FAILED!!!");
} }
} }
...@@ -27,6 +27,21 @@ use WardenCommon; ...@@ -27,6 +27,21 @@ use WardenCommon;
our $VERSION = "2.2"; our $VERSION = "2.2";
my $etc = "$FindBin::RealBin/../etc"; my $etc = "$FindBin::RealBin/../etc";
# read config file
my $conf_file = "$etc/warden-server.conf";
our $SYSLOG = undef;
our $SYSLOG_VERBOSE = undef;
our $SYSLOG_FACILITY = undef;
our $DB_NAME = undef;
our $DB_USER = undef;
our $DB_PASS = undef;
our $DB_HOST = undef;
unless (do $conf_file) {
die("Errors in config file '$conf_file': $@") if $@;
die("Can't read config file '$conf_file': $!") unless defined $_;
# if $_ defined, it's retvalue of last statement of conf, for which we don't care
}
################################################################################ ################################################################################
...@@ -46,41 +61,47 @@ sub help { ...@@ -46,41 +61,47 @@ sub help {
} }
#-------------------------------------------------------------------------------
# sendMsg - wrapper for more complex WardenCommon::sendMsg function
#-------------------------------------------------------------------------------
sub sendMsg
{
my $severity = shift;
my $syslog_msg = shift;
WardenCommon::sendMsg($SYSLOG, $SYSLOG_VERBOSE, $SYSLOG_FACILITY, $severity,
$syslog_msg, undef, $RealScript);
}
################################################################################ ################################################################################
# MAIN # MAIN
################################################################################ ################################################################################
our ($opt_h, $opt_i); our ($opt_h, $opt_i);
if ($#ARGV == -1) {usage} if ($#ARGV == -1) {
usage;
}
die usage unless getopts("i:h"); die usage unless getopts("i:h");
my $client_id = $opt_i; my $client_id = $opt_i;
my $help = $opt_h; my $help = $opt_h;
# catch help param # catch help param
if ($help) {help} if ($help) {
help;
}
# superuser controle # superuser controle
my $UID = $<; my $UID = $<;
if ($UID != 0) {WardenCommon::errMsg("You must be root for running this script!")} if ($UID != 0) {
WardenCommon::errMsg("You must be root for running this script!");
}
# check parameters definition # check parameters definition
if (!defined $client_id) {WardenCommon::errMsg("Parameter 'client_id' is not defined!")} if (!defined $client_id) {
WardenCommon::errMsg("Parameter 'client_id' is not defined!");
# read config file
my $conf_file = "$etc/warden-server.conf";
our $SYSLOG = undef;
our $SYSLOG_VERBOSE = undef;
our $SYSLOG_FACILITY = undef;
our $DB_NAME = undef;
our $DB_USER = undef;
our $DB_PASS = undef;
our $DB_HOST = undef;
unless (do $conf_file) {
die("Errors in config file '$conf_file': $@") if $@;
die("Can't read config file '$conf_file': $!") unless defined $_;
# if $_ defined, it's retvalue of last statement of conf, for which we don't care
} }
# connect to DB # connect to DB
...@@ -95,8 +116,8 @@ my $warden_server = WardenCommon::trim(`hostname -f`); ...@@ -95,8 +116,8 @@ my $warden_server = WardenCommon::trim(`hostname -f`);
# delete registered client # delete registered client
if (!defined $id) { if (!defined $id) {
WardenCommon::sendMsg($SYSLOG, $SYSLOG_VERBOSE, $SYSLOG_FACILITY, "err", "Attempt to delete unregister client (client_id: #$client_id)"); sendMsg("err", "Attempt to delete unregister client [Client_id: '$client_id', Hostname: '$hostname', Client_type: '$client_type', Service: '$service', Type: '$type'] from Warden server '$warden_server'");
WardenCommon::errMsg("Client (#$client_id) is not registered at $warden_server"); WardenCommon::errMsg("Client (#$client_id) is not registered at Warden server '$warden_server'");
} else { } else {
if ($client_type eq 's') { if ($client_type eq 's') {
$sth = $DBH->prepare("UPDATE clients SET valid = 'f' WHERE client_id = ?;") or die "Cannot prepare statement: " . $DBH->errstr; $sth = $DBH->prepare("UPDATE clients SET valid = 'f' WHERE client_id = ?;") or die "Cannot prepare statement: " . $DBH->errstr;
...@@ -104,19 +125,19 @@ if (!defined $id) { ...@@ -104,19 +125,19 @@ if (!defined $id) {
if ($ret_val == 1) { if ($ret_val == 1) {
$sth = $DBH->prepare("UPDATE events SET valid = 'f' where hostname = ? AND service = ?;") or die "Cannot prepare statement: " . $DBH->errstr; $sth = $DBH->prepare("UPDATE events SET valid = 'f' where hostname = ? AND service = ?;") or die "Cannot prepare statement: " . $DBH->errstr;
$sth->execute($hostname, $service) or die "Cannot execute statement: " . $sth->errstr; $sth->execute($hostname, $service) or die "Cannot execute statement: " . $sth->errstr;
WardenCommon::sendMsg($SYSLOG, $SYSLOG_VERBOSE, $SYSLOG_FACILITY, "info", "Sender '$hostname' [client_id: '$client_id', service: '$service'] was deleted and its data were invalidated from $warden_server"); sendMsg("info", "Sender [Client_id: '$client_id', Hostname: '$hostname', Service: '$service'] has been deleted and its data were invalidated from Warden server '$warden_server'");
WardenCommon::succMsg("Unregistration of sender client (client_id: #$client_id) was SUCCESSFUL!!!"); WardenCommon::succMsg("Unregistration of sender (#$client_id) was SUCCESSFUL!!!");
} else { } else {
WardenCommon::errMsg("Unregistration of sender client (client_id: #$client_id) FAILED!!!"); WardenCommon::errMsg("Unregistration of sender (#$client_id) FAILED!!!");
} }
} else { } else {
$sth = $DBH->prepare("UPDATE clients SET valid = 'f' WHERE client_id = ?;") or die "Cannot prepare statement: " . $DBH->errstr; $sth = $DBH->prepare("UPDATE clients SET valid = 'f' WHERE client_id = ?;") or die "Cannot prepare statement: " . $DBH->errstr;
my $ret_val = $sth->execute($client_id) or die "Cannot execute statement: " . $sth->errstr; my $ret_val = $sth->execute($client_id) or die "Cannot execute statement: " . $sth->errstr;
if ($ret_val == 1) { if ($ret_val == 1) {
WardenCommon::sendMsg($SYSLOG, $SYSLOG_VERBOSE, $SYSLOG_FACILITY, "info", "Receiver '$hostname' [client_id: '$client_id', type: '$type'] was deleted from $warden_server"); sendMsg("info", "Receiver [Client_id: '$client_id', Hostname: '$hostname', Type: '$type'] has been deleted from Warden server '$warden_server'");
WardenCommon::succMsg("Unregistration of receiver client (client_id: #$client_id) was SUCCESSFUL!!!"); WardenCommon::succMsg("Unregistration of receiver (#$client_id) was SUCCESSFUL!!!");
} else { } else {
WardenCommon::errMsg("Unregistration of receiver client (client_id: #$client_id) FAILED!!!"); WardenCommon::errMsg("Unregistration of receiver (#$client_id) FAILED!!!");
} }
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment