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 @@
package WardenWatchdog;
#use Data::Dumper;
use WardenConf;
use strict;
use warnings;
#use Data::Dumper;
#use WardenConf;
use DBI;
use DBD::mysql;
use DateTime;
......@@ -19,7 +20,7 @@ use Email::Simple;
use Sys::Hostname;
#-------------------------------------------------------------------------------
# sendmail_wrapper
# sendmailWrapper
#
# Simple wrapper function for an mailserver.
#
......@@ -33,7 +34,8 @@ use Sys::Hostname;
# On Success (1)
# On Failure (0, 'Error message')
#-------------------------------------------------------------------------------
sub sendmail_wrapper{
sub sendmailWrapper
{
my $message = shift;
my $email_conf = shift;
......@@ -52,7 +54,7 @@ sub sendmail_wrapper{
#-------------------------------------------------------------------------------
# send_report
# sendReport
#
# Function for creating and sending of an Watchdog status report via email to
# administrators of an clients.
......@@ -71,7 +73,8 @@ sub sendmail_wrapper{
# On Success (1)
# On Failure (0, 'Error message')
#-------------------------------------------------------------------------------
sub send_report{
sub sendReport
{
my $input_data = shift;
my $contact = $$input_data{'contact'};
......@@ -104,7 +107,7 @@ sub send_report{
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) {
return (0, $err);
}
......@@ -112,7 +115,7 @@ sub send_report{
}
#-------------------------------------------------------------------------------
# connect_to_DB
# connectToDB
#
# Just simple database wrapper for Watchdog which creates db's handler.
#
......@@ -125,26 +128,27 @@ sub send_report{
# passwd => password
#
# Output:
# dbhRef = reference on a database handler
# dbh_ref = reference on a database handler
#
# Return:
# On Success (1)
# On Failure (0, 'Error message')
#-------------------------------------------------------------------------------
sub connect_to_DB {
sub connectToDB
{
my $dbConf = shift;
my $dbhRef = shift;
my $db_conf = shift;
my $dbh_ref = shift;
my $dbPlatform = $$dbConf{'platform'};
my $dbName = $$dbConf{'name'};
my $dbHostname = $$dbConf{'hostname'};
my $dbUser = $$dbConf{'user'};
my $dbPasswd = $$dbConf{'passwd'};
my $dn_platform = $$db_conf{'platform'};
my $db_name = $$db_conf{'name'};
my $db_hostname = $$db_conf{'hostname'};
my $db_user = $$db_conf{'user'};
my $db_passwd = $$db_conf{'passwd'};
my $dbh;
if($dbh = DBI->connect( "dbi:$dbPlatform:database=$dbName;host=$dbHostname", $dbUser, $dbPasswd, {mysql_auto_reconnect => 1})){
$$dbhRef = $dbh;
if($dbh = DBI->connect( "dbi:$dn_platform:database=$db_name;host=$db_hostname", $db_user, $db_passwd, {mysql_auto_reconnect => 1})) {
$$dbh_ref = $dbh;
return (1);
}
else{
......@@ -153,13 +157,13 @@ sub connect_to_DB {
}
#-------------------------------------------------------------------------------
# update_procedures
# updateProcedures
#
# Function takes DB handler and executes all database procedures in the array.
#
# Input:
# dbhRef = reference on a database handler
# procRef = reference on an array of database procedures
# dbh_ref = reference on a database handler
# proc_ref = reference on an array of database procedures
#
# Output: -
#
......@@ -167,18 +171,19 @@ sub connect_to_DB {
# On Success (1)
# On Failure (0, 'Error message')
#-------------------------------------------------------------------------------
sub update_procedures{
sub updateProcedures
{
my $dbhRef = shift;
my $procRef = shift;
my $dbh_ref = shift;
my $proc_ref = shift;
my $dbh = $$dbhRef;
my $dbh = $$dbh_ref;
if(!defined($dbh)) {
return (0, "update_procedures: no database handle defined")
return (0, "updateProcedures: no database handle defined")
}
my @sqlQueries = @{$procRef};
foreach my $proc (@sqlQueries) {
my @sql_queries = @{$proc_ref};
foreach my $proc (@sql_queries) {
$dbh->do($proc);
}
......@@ -186,13 +191,13 @@ sub update_procedures{
}
#-------------------------------------------------------------------------------
# send_query
# sendQuery
#
#
#
# Input:
# dbhRef = reference on a database handler
# configRef = Hash of parameters:
# dbh_ref = reference on a database handler
# config_ref = Hash of parameters:
# query => sql query of an action (check) on Warden database
# text => body of an email which is send to a admin of an client
# in case of nonempty check result
......@@ -200,7 +205,7 @@ sub update_procedures{
# in a database table.
#
# Output:
# eventsRef = Hash of parameters:
# events_ref = Hash of parameters:
# contact = email address of an client administrator
# 'contact' => predefined email text + information from database obtained
# by a query
......@@ -209,18 +214,19 @@ sub update_procedures{
# On Success (1)
# On Failure (0, 'Error message')
#-------------------------------------------------------------------------------
sub send_query{
sub sendQuery
{
my $dbhRef = shift;
my $configRef = shift;
my $eventsRef = shift;
my $dbh_ref = shift;
my $config_ref = shift;
my $events_ref = shift;
my $dbh = $$dbhRef;
my $dbh = $$dbh_ref;
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 ($rc,$err);
my $i = 0;
......@@ -266,7 +272,7 @@ sub send_query{
$i++;
}
%$eventsRef = %bad_events;
%$events_ref = %bad_events;
return (1);
}
......@@ -287,12 +293,13 @@ sub send_query{
# On Success (1)
# On Failure (0, 'Error message')
#-------------------------------------------------------------------------------
sub run{
sub run
{
my $conf_file = shift;
my $period = shift;
my $errMsg;
my $err_msg;
# server config
if(!defined($conf_file)) {
......@@ -303,27 +310,27 @@ sub run{
return (0,"No time period is defined");
}
our $server_conf = undef;
our $domain_name = undef;
our $email_subject = undef;
our $email_server_conf = undef;
our @sql_precondition = undef;
our @sql_queries = undef;
our @sql_postcondition = undef;
our $SERVER_CONF = undef;
our $DOMAIN_NAME = undef;
our $EMAIL_SUBJECT = undef;
our $EMAIL_SERVER_CONF = undef;
our @SQL_PRECONDITION = undef;
our @SQL_QUERIES = undef;
our @SQL_POSTCONDITION = undef;
# script config
if (!(do $conf_file)) {
if ($@) {
$errMsg = "Errors in config file '$conf_file': $@";
#syslog("err|$errMsg");
print $errMsg;
return (0,"Warden watchdog - $errMsg");
$err_msg = "Errors in config file '$conf_file': $@";
#syslog("err|$err_msg");
print $err_msg;
return (0,"Warden watchdog - $err_msg");
}
if (!(defined $_)) {
$errMsg = "Can't read config file '$conf_file': $!";
#syslog("err|$errMsg");
print $errMsg;
return (0,"Warden watchdog - $errMsg");
$err_msg = "Can't read config file '$conf_file': $!";
#syslog("err|$err_msg");
print $err_msg;
return (0,"Warden watchdog - $err_msg");
}
}
......@@ -336,18 +343,18 @@ sub run{
our $DB_HOST = undef;
# TODO replace with function call from Wardencommon
if (!(do $server_conf)) {
if (!(do $SERVER_CONF)) {
if ($@) {
$errMsg = "Errors in config file '$server_conf': $@";
#syslog("err|$errMsg");
print $errMsg;
return (0,"Warden watchdog - $errMsg");
$err_msg = "Errors in config file '$SERVER_CONF': $@";
#syslog("err|$err_msg");
print $err_msg;
return (0,"Warden watchdog - $err_msg");
}
if (!(defined $_)) {
$errMsg = "Can't read config file '$server_conf': $!";
#syslog("err|$errMsg");
print $errMsg;
return (0,"Warden watchdog - $errMsg");
$err_msg = "Can't read config file '$SERVER_CONF': $!";
#syslog("err|$err_msg");
print $err_msg;
return (0,"Warden watchdog - $err_msg");
}
}
......@@ -368,14 +375,14 @@ sub run{
my $dbh;
# connect to DB
my ($rc,$err) = connect_to_DB(\%db_conf,\$dbh);
my ($rc,$err) = connectToDB(\%db_conf,\$dbh);
if (!$rc) {
$errMsg = "Warden watchdog can\'t connect do DB: $err";
return (0,"Warden watchdog - $errMsg");
$err_msg = "Warden watchdog can\'t connect do DB: $err";
return (0,"Warden watchdog - $err_msg");
}
if(@sql_precondition){
($rc,$err) = update_procedures(\$dbh,\@sql_precondition);
if(@SQL_PRECONDITION) {
($rc,$err) = updateProcedures(\$dbh,\@SQL_PRECONDITION);
if (!$rc) {
#print "Warden watchdog - $err\n";
return (0,"Warden watchdog - $err\n");
......@@ -383,19 +390,19 @@ sub run{
}
my %bad_events;
if(@sql_queries){
foreach my $query (@sql_queries){
if(@SQL_QUERIES) {
foreach my $query (@SQL_QUERIES) {
$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) {
#print "Warden watchdog - $err\n";
return (0,"Warden watchdog - $err\n");
}
}
if(@sql_postcondition){
my ($rc,$err) = update_procedures(\$dbh,\@sql_postcondition);
if(@SQL_POSTCONDITION) {
my ($rc,$err) = updateProcedures(\$dbh,\@SQL_POSTCONDITION);
if (!$rc) {
#print "Warden watchdog - $err\n";
return (0,"Warden watchdog - $err\n");
......@@ -403,8 +410,8 @@ sub run{
}
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 ($rc,$err) = send_report(\%input);
my %input = (contact => $contact, domain => $DOMAIN_NAME, text => $text, subject => $EMAIL_SUBJECT, email_conf => $EMAIL_SERVER_CONF);
my ($rc,$err) = sendReport(\%input);
if (!$rc) {
#print $err;
return (0,"Warden client - networkReporter $err\n");
......
......@@ -9,7 +9,6 @@
use strict;
use warnings;
use Getopt::Long;
use FindBin;
FindBin::again();
......@@ -29,7 +28,8 @@ use WardenWatchdog;
# Return:
# On Success (1)
#-------------------------------------------------------------------------------
sub help {
sub help
{
my $help =" USAGE: ./wardenWatchdog.pl -c '/path/WardenWatchdog.conf' -i 7
OPTIONS
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment