Skip to content
Snippets Groups Projects
Commit f85462ba authored by Jakub Cegan's avatar Jakub Cegan
Browse files

naprava kultury kodu a pridani readme

parent 21a4e9b9
No related branches found
No related tags found
No related merge requests found
+----------------------------+
| README - Warden Watchdog |
+----------------------------+
Content
A. Overall Information
B. Dependencies
C. Configuration file
D. Application run
--------------------------------------------------------------------------------
A. Overall Information
Warden Watchdog is a simple script for check of an Warden server DB. You can
create various SQL queries (checks) for an example for events from wrong IPs,
for events with incomplete description or for long quiet reporting clients.
Then you can run watchdog by hand or a repeatedly via Cron.
If one or more events are found by a check, than predefined information
email is sent to a person, who is responsible for a client. You can also set
a different recipient of a notification email for each check with a setting
'contact' field in a configuration file.
--------------------------------------------------------------------------------
B. Installation Dependencies
1. Applications:
Perl >= 5.10.1
MySQL >= 5.1.63
Apache >= 2.2.14
2. Perl modules:
DBI >= 1.612
DBD::mysql >= 4.016
DateTime >= 0.61
Getopt::Long >= 1.06
Email::Simple >= 2.100
Sys::Hostname >= 1.11
FindBin >= 1.50
--------------------------------------------------------------------------------
C. Configuration file
Each configuration file for a Warden Watchdog has four important groups of
settings. First group is clear and contains parameters such as path to Warden
server configuration file, notification email subject and a email server
configuration. Second group called SQL preconditions is an array containing
SQL queries which can be executed before Warden DB check. Last, fourth, group
called SQL postconditions is also an array which can contains SQL queries
useful for a Warden DB clean up after a DB check.
The second group in a configuration file is a different. It is an array of
hashes with a following structure and each one performs one check. In a
query is possible to use a '\$date' variable, which will be expanded by a
Watchdog on a today's date.
@sql_queries = (
{
query => '<SQL query (check) on Warden DB>';
text => 'Text of notification email for this DB check';
contact => '<email address>' # override contact from 'requestor' column
}
)
--------------------------------------------------------------------------------
D. Application run
You will need just a prepared configuration file and a count of days back
from now to the past. Warden database check from config will be then run in
this defined time interval.
USAGE: ./wardenWatchdog.pl -c '/path/WardenWatchdog.conf' -i 7
--------------------------------------------------------------------------------
Copyright (C) 2011-2013 Cesnet z.s.p.o
...@@ -8,10 +8,11 @@ ...@@ -8,10 +8,11 @@
package WardenWatchdog; package WardenWatchdog;
#use Data::Dumper;
use WardenConf;
use strict; use strict;
use warnings; use warnings;
#use Data::Dumper;
#use WardenConf;
use DBI; use DBI;
use DBD::mysql; use DBD::mysql;
use DateTime; use DateTime;
...@@ -19,7 +20,7 @@ use Email::Simple; ...@@ -19,7 +20,7 @@ use Email::Simple;
use Sys::Hostname; use Sys::Hostname;
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
# sendmail_wrapper # sendmailWrapper
# #
# Simple wrapper function for an mailserver. # Simple wrapper function for an mailserver.
# #
...@@ -33,12 +34,13 @@ use Sys::Hostname; ...@@ -33,12 +34,13 @@ use Sys::Hostname;
# On Success (1) # On Success (1)
# On Failure (0, 'Error message') # On Failure (0, 'Error message')
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
sub sendmail_wrapper{ sub sendmailWrapper
{
my $message = shift; my $message = shift;
my $email_conf = shift; my $email_conf = shift;
if(open(my $sendmail, $email_conf)){ if(open(my $sendmail, $email_conf)) {
print $sendmail $message; print $sendmail $message;
close $sendmail; close $sendmail;
return (1); return (1);
...@@ -52,7 +54,7 @@ sub sendmail_wrapper{ ...@@ -52,7 +54,7 @@ sub sendmail_wrapper{
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
# send_report # sendReport
# #
# Function for creating and sending of an Watchdog status report via email to # Function for creating and sending of an Watchdog status report via email to
# administrators of an clients. # administrators of an clients.
...@@ -71,7 +73,8 @@ sub sendmail_wrapper{ ...@@ -71,7 +73,8 @@ sub sendmail_wrapper{
# On Success (1) # On Success (1)
# On Failure (0, 'Error message') # On Failure (0, 'Error message')
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
sub send_report{ sub sendReport
{
my $input_data = shift; my $input_data = shift;
my $contact = $$input_data{'contact'}; my $contact = $$input_data{'contact'};
...@@ -81,38 +84,38 @@ sub send_report{ ...@@ -81,38 +84,38 @@ sub send_report{
my $email_conf = $$input_data{'email_conf'}; my $email_conf = $$input_data{'email_conf'};
my $message; my $message;
if(!($contact)){ if(!($contact)) {
return (0, "Empty 'To' email header!\n"); return (0, "Empty 'To' email header!\n");
} }
if(!($domain)){ if(!($domain)) {
return (0, "No sender's domain! Can't send email\n"); return (0, "No sender's domain! Can't send email\n");
} }
if(!($text)){ if(!($text)) {
return (0, "No text! Nothing to send\n"); return (0, "No text! Nothing to send\n");
} }
eval{ eval{
$message = Email::Simple->create( $message = Email::Simple->create(
header => [ header => [
To => $contact, To => $contact,
From => 'warden_watchdog@'.$domain, From => 'warden_watchdog@'.$domain,
Subject => $subject], Subject => $subject],
body => $text); body => $text);
} or do { } or do {
return (0, "Can't create email message\n"); return (0, "Can't create email message\n");
}; };
my ($rc, $err) = sendmail_wrapper($message->as_string,$email_conf); my ($rc, $err) = sendmailWrapper($message->as_string,$email_conf);
if(!$rc){ if(!$rc) {
return (0, $err); return (0, $err);
} }
return (1); return (1);
} }
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
# connect_to_DB # connectToDB
# #
# Just simple database wrapper for Watchdog which creates db's handler. # Just simple database wrapper for Watchdog which creates db's handler.
# #
...@@ -125,26 +128,27 @@ sub send_report{ ...@@ -125,26 +128,27 @@ sub send_report{
# passwd => password # passwd => password
# #
# Output: # Output:
# dbhRef = reference on a database handler # dbh_ref = reference on a database handler
# #
# Return: # Return:
# On Success (1) # On Success (1)
# On Failure (0, 'Error message') # On Failure (0, 'Error message')
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
sub connect_to_DB { sub connectToDB
{
my $dbConf = shift; my $db_conf = shift;
my $dbhRef = shift; my $dbh_ref = shift;
my $dbPlatform = $$dbConf{'platform'}; my $dn_platform = $$db_conf{'platform'};
my $dbName = $$dbConf{'name'}; my $db_name = $$db_conf{'name'};
my $dbHostname = $$dbConf{'hostname'}; my $db_hostname = $$db_conf{'hostname'};
my $dbUser = $$dbConf{'user'}; my $db_user = $$db_conf{'user'};
my $dbPasswd = $$dbConf{'passwd'}; my $db_passwd = $$db_conf{'passwd'};
my $dbh; my $dbh;
if($dbh = DBI->connect( "dbi:$dbPlatform:database=$dbName;host=$dbHostname", $dbUser, $dbPasswd, {mysql_auto_reconnect => 1})){ if($dbh = DBI->connect( "dbi:$dn_platform:database=$db_name;host=$db_hostname", $db_user, $db_passwd, {mysql_auto_reconnect => 1})) {
$$dbhRef = $dbh; $$dbh_ref = $dbh;
return (1); return (1);
} }
else{ else{
...@@ -153,13 +157,13 @@ sub connect_to_DB { ...@@ -153,13 +157,13 @@ sub connect_to_DB {
} }
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
# update_procedures # updateProcedures
# #
# Function takes DB handler and executes all database procedures in the array. # Function takes DB handler and executes all database procedures in the array.
# #
# Input: # Input:
# dbhRef = reference on a database handler # dbh_ref = reference on a database handler
# procRef = reference on an array of database procedures # proc_ref = reference on an array of database procedures
# #
# Output: - # Output: -
# #
...@@ -167,18 +171,19 @@ sub connect_to_DB { ...@@ -167,18 +171,19 @@ sub connect_to_DB {
# On Success (1) # On Success (1)
# On Failure (0, 'Error message') # On Failure (0, 'Error message')
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
sub update_procedures{ sub updateProcedures
{
my $dbhRef = shift; my $dbh_ref = shift;
my $procRef = shift; my $proc_ref = shift;
my $dbh = $$dbhRef; my $dbh = $$dbh_ref;
if(!defined($dbh)){ if(!defined($dbh)) {
return (0, "update_procedures: no database handle defined") return (0, "updateProcedures: no database handle defined")
} }
my @sqlQueries = @{$procRef}; my @sql_queries = @{$proc_ref};
foreach my $proc (@sqlQueries) { foreach my $proc (@sql_queries) {
$dbh->do($proc); $dbh->do($proc);
} }
...@@ -186,13 +191,13 @@ sub update_procedures{ ...@@ -186,13 +191,13 @@ sub update_procedures{
} }
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
# send_query # sendQuery
# #
# #
# #
# Input: # Input:
# dbhRef = reference on a database handler # dbh_ref = reference on a database handler
# configRef = Hash of parameters: # config_ref = Hash of parameters:
# query => sql query of an action (check) on Warden database # query => sql query of an action (check) on Warden database
# text => body of an email which is send to a admin of an client # text => body of an email which is send to a admin of an client
# in case of nonempty check result # in case of nonempty check result
...@@ -200,7 +205,7 @@ sub update_procedures{ ...@@ -200,7 +205,7 @@ sub update_procedures{
# in a database table. # in a database table.
# #
# Output: # Output:
# eventsRef = Hash of parameters: # events_ref = Hash of parameters:
# contact = email address of an client administrator # contact = email address of an client administrator
# 'contact' => predefined email text + information from database obtained # 'contact' => predefined email text + information from database obtained
# by a query # by a query
...@@ -209,18 +214,19 @@ sub update_procedures{ ...@@ -209,18 +214,19 @@ sub update_procedures{
# On Success (1) # On Success (1)
# On Failure (0, 'Error message') # On Failure (0, 'Error message')
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
sub send_query{ sub sendQuery
{
my $dbhRef = shift; my $dbh_ref = shift;
my $configRef = shift; my $config_ref = shift;
my $eventsRef = shift; my $events_ref = shift;
my $dbh = $$dbhRef; my $dbh = $$dbh_ref;
if(!defined($dbh)){ if(!defined($dbh)) {
return (0, "send_query: no database handle defined") return (0, "sendQuery: no database handle defined")
} }
my @config = @{$configRef}; my @config = @{$config_ref};
my %bad_events; my %bad_events;
my ($rc,$err); my ($rc,$err);
my $i = 0; my $i = 0;
...@@ -228,22 +234,22 @@ sub send_query{ ...@@ -228,22 +234,22 @@ sub send_query{
while ($i < scalar(@config)) { while ($i < scalar(@config)) {
# run DB query -> requestor, client name # run DB query -> requestor, client name
my $sth; my $sth;
if (defined($config[$i]{query})){ if (defined($config[$i]{query})) {
$sth = $dbh->prepare($config[$i]{query}); $sth = $dbh->prepare($config[$i]{query});
} }
else{ else{
return (0, "No query available\n"); return (0, "No query available\n");
} }
if (!($sth->execute)){ if (!($sth->execute)) {
return (0, "Couldn't get data from my database: $sth->errstr\n"); return (0, "Couldn't get data from my database: $sth->errstr\n");
}; };
my $contact; my $contact;
my $msg_text = 1; my $msg_text = 1;
while(my $result = $sth->fetchrow_hashref()){ while(my $result = $sth->fetchrow_hashref()) {
if (defined($config[$i]{contact})){ if (defined($config[$i]{contact})) {
# override contact from 'requestor' collumn # override contact from 'requestor' collumn
$contact = $config[$i]{contact}; $contact = $config[$i]{contact};
} }
...@@ -251,14 +257,14 @@ sub send_query{ ...@@ -251,14 +257,14 @@ sub send_query{
$contact = $result->{'requestor'}; $contact = $result->{'requestor'};
} }
# information header # information header
if($msg_text){ if($msg_text) {
$bad_events{$contact} .= $config[$i]{text} . "\n\n"; $bad_events{$contact} .= $config[$i]{text} . "\n\n";
$bad_events{$contact} .= join(" | ", map {$_ // "UNKNOWN" } keys %$result) . "\n"; $bad_events{$contact} .= join(" | ", map {$_ // "UNKNOWN" } keys %$result) . "\n";
$msg_text = 0; $msg_text = 0;
} }
$bad_events{$contact} .= join(" | ", map {$_ // "NULL" } values %$result) . "\n"; $bad_events{$contact} .= join(" | ", map {$_ // "NULL" } values %$result) . "\n";
} }
foreach my $key (keys %bad_events){ foreach my $key (keys %bad_events) {
$bad_events{$key} .= "\n\n"; $bad_events{$key} .= "\n\n";
} }
...@@ -266,7 +272,7 @@ sub send_query{ ...@@ -266,7 +272,7 @@ sub send_query{
$i++; $i++;
} }
%$eventsRef = %bad_events; %$events_ref = %bad_events;
return (1); return (1);
} }
...@@ -287,43 +293,44 @@ sub send_query{ ...@@ -287,43 +293,44 @@ sub send_query{
# On Success (1) # On Success (1)
# On Failure (0, 'Error message') # On Failure (0, 'Error message')
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
sub run{ sub run
{
my $conf_file = shift; my $conf_file = shift;
my $period = shift; my $period = shift;
my $errMsg; my $err_msg;
# server config # server config
if(!defined($conf_file)){ if(!defined($conf_file)) {
return (0,"No conf file is available"); return (0,"No conf file is available");
} }
if(!defined($period)){ if(!defined($period)) {
return (0,"No time period is defined"); return (0,"No time period is defined");
} }
our $server_conf = undef; our $SERVER_CONF = undef;
our $domain_name = undef; our $DOMAIN_NAME = undef;
our $email_subject = undef; our $EMAIL_SUBJECT = undef;
our $email_server_conf = undef; our $EMAIL_SERVER_CONF = undef;
our @sql_precondition = undef; our @SQL_PRECONDITION = undef;
our @sql_queries = undef; our @SQL_QUERIES = undef;
our @sql_postcondition = undef; our @SQL_POSTCONDITION = undef;
# script config # script config
if (!(do $conf_file)) { if (!(do $conf_file)) {
if ($@){ if ($@) {
$errMsg = "Errors in config file '$conf_file': $@"; $err_msg = "Errors in config file '$conf_file': $@";
#syslog("err|$errMsg"); #syslog("err|$err_msg");
print $errMsg; print $err_msg;
return (0,"Warden watchdog - $errMsg"); return (0,"Warden watchdog - $err_msg");
} }
if (!(defined $_)){ if (!(defined $_)) {
$errMsg = "Can't read config file '$conf_file': $!"; $err_msg = "Can't read config file '$conf_file': $!";
#syslog("err|$errMsg"); #syslog("err|$err_msg");
print $errMsg; print $err_msg;
return (0,"Warden watchdog - $errMsg"); return (0,"Warden watchdog - $err_msg");
} }
} }
...@@ -336,18 +343,18 @@ sub run{ ...@@ -336,18 +343,18 @@ sub run{
our $DB_HOST = undef; our $DB_HOST = undef;
# TODO replace with function call from Wardencommon # TODO replace with function call from Wardencommon
if (!(do $server_conf)) { if (!(do $SERVER_CONF)) {
if ($@){ if ($@) {
$errMsg = "Errors in config file '$server_conf': $@"; $err_msg = "Errors in config file '$SERVER_CONF': $@";
#syslog("err|$errMsg"); #syslog("err|$err_msg");
print $errMsg; print $err_msg;
return (0,"Warden watchdog - $errMsg"); return (0,"Warden watchdog - $err_msg");
} }
if (!(defined $_)){ if (!(defined $_)) {
$errMsg = "Can't read config file '$server_conf': $!"; $err_msg = "Can't read config file '$SERVER_CONF': $!";
#syslog("err|$errMsg"); #syslog("err|$err_msg");
print $errMsg; print $err_msg;
return (0,"Warden watchdog - $errMsg"); return (0,"Warden watchdog - $err_msg");
} }
} }
...@@ -368,44 +375,44 @@ sub run{ ...@@ -368,44 +375,44 @@ sub run{
my $dbh; my $dbh;
# connect to DB # connect to DB
my ($rc,$err) = connect_to_DB(\%db_conf,\$dbh); my ($rc,$err) = connectToDB(\%db_conf,\$dbh);
if (!$rc){ if (!$rc) {
$errMsg = "Warden watchdog can\'t connect do DB: $err"; $err_msg = "Warden watchdog can\'t connect do DB: $err";
return (0,"Warden watchdog - $errMsg"); return (0,"Warden watchdog - $err_msg");
} }
if(@sql_precondition){ if(@SQL_PRECONDITION) {
($rc,$err) = update_procedures(\$dbh,\@sql_precondition); ($rc,$err) = updateProcedures(\$dbh,\@SQL_PRECONDITION);
if (!$rc){ if (!$rc) {
#print "Warden watchdog - $err\n"; #print "Warden watchdog - $err\n";
return (0,"Warden watchdog - $err\n"); return (0,"Warden watchdog - $err\n");
} }
} }
my %bad_events; my %bad_events;
if(@sql_queries){ if(@SQL_QUERIES) {
foreach my $query (@sql_queries){ foreach my $query (@SQL_QUERIES) {
$query->{query} =~ s/\$date/$date/; $query->{query} =~ s/\$date/$date/;
} }
my ($rc,$err) = send_query(\$dbh,\@sql_queries,\%bad_events); my ($rc,$err) = sendQuery(\$dbh,\@SQL_QUERIES,\%bad_events);
if (!$rc){ if (!$rc) {
#print "Warden watchdog - $err\n"; #print "Warden watchdog - $err\n";
return (0,"Warden watchdog - $err\n"); return (0,"Warden watchdog - $err\n");
} }
} }
if(@sql_postcondition){ if(@SQL_POSTCONDITION) {
my ($rc,$err) = update_procedures(\$dbh,\@sql_postcondition); my ($rc,$err) = updateProcedures(\$dbh,\@SQL_POSTCONDITION);
if (!$rc){ if (!$rc) {
#print "Warden watchdog - $err\n"; #print "Warden watchdog - $err\n";
return (0,"Warden watchdog - $err\n"); return (0,"Warden watchdog - $err\n");
} }
} }
while (my ($contact, $text) = each(%bad_events)){ while (my ($contact, $text) = each(%bad_events)) {
my %input = (contact => $contact, domain => $domain_name, text => $text, subject => $email_subject, email_conf => $email_server_conf); my %input = (contact => $contact, domain => $DOMAIN_NAME, text => $text, subject => $EMAIL_SUBJECT, email_conf => $EMAIL_SERVER_CONF);
my ($rc,$err) = send_report(\%input); my ($rc,$err) = sendReport(\%input);
if (!$rc){ if (!$rc) {
#print $err; #print $err;
return (0,"Warden client - networkReporter $err\n"); return (0,"Warden client - networkReporter $err\n");
} }
......
...@@ -9,7 +9,6 @@ ...@@ -9,7 +9,6 @@
use strict; use strict;
use warnings; use warnings;
use Getopt::Long; use Getopt::Long;
use FindBin; use FindBin;
FindBin::again(); FindBin::again();
...@@ -29,7 +28,8 @@ use WardenWatchdog; ...@@ -29,7 +28,8 @@ use WardenWatchdog;
# Return: # Return:
# On Success (1) # On Success (1)
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
sub help { sub help
{
my $help =" USAGE: ./wardenWatchdog.pl -c '/path/WardenWatchdog.conf' -i 7 my $help =" USAGE: ./wardenWatchdog.pl -c '/path/WardenWatchdog.conf' -i 7
OPTIONS OPTIONS
...@@ -41,12 +41,12 @@ sub help { ...@@ -41,12 +41,12 @@ sub help {
} }
my ($help, $config, $interval); my ($help, $config, $interval);
if (@ARGV < 3 || defined($help) || !GetOptions('help|?|h' => \$help, 'c|conf=s' => \$config, 'i|interval=i' => \$interval)){ if (@ARGV < 3 || defined($help) || !GetOptions('help|?|h' => \$help, 'c|conf=s' => \$config, 'i|interval=i' => \$interval)) {
help(); help();
} }
else{ else {
my ($rc,$err) = WardenWatchdog::run($config,$interval); my ($rc,$err) = WardenWatchdog::run($config,$interval);
if(!$rc){ if(!$rc) {
print "WardenWatchdog error: $err"; print "WardenWatchdog error: $err";
} }
} }
......
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