From f85462ba28cc7be6a96ac5923dd22bbe4b536fea Mon Sep 17 00:00:00 2001
From: Jakub Cegan <cegan@ics.muni.cz>
Date: Thu, 12 Sep 2013 22:43:06 +0200
Subject: [PATCH] naprava kultury kodu a pridani readme

---
 .../wardenWatchdog/README.wardenWatchdog      |  82 +++++++
 src/contrib/wardenWatchdog/WardenWatchdog.pm  | 221 +++++++++---------
 src/contrib/wardenWatchdog/wardenWatchdog.pl  |  10 +-
 3 files changed, 201 insertions(+), 112 deletions(-)
 create mode 100644 src/contrib/wardenWatchdog/README.wardenWatchdog

diff --git a/src/contrib/wardenWatchdog/README.wardenWatchdog b/src/contrib/wardenWatchdog/README.wardenWatchdog
new file mode 100644
index 0000000..f54e354
--- /dev/null
+++ b/src/contrib/wardenWatchdog/README.wardenWatchdog
@@ -0,0 +1,82 @@
++----------------------------+
+| 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
diff --git a/src/contrib/wardenWatchdog/WardenWatchdog.pm b/src/contrib/wardenWatchdog/WardenWatchdog.pm
index 41316c2..5b92657 100644
--- a/src/contrib/wardenWatchdog/WardenWatchdog.pm
+++ b/src/contrib/wardenWatchdog/WardenWatchdog.pm
@@ -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,12 +34,13 @@ use Sys::Hostname;
 #   On Success (1)
 #   On Failure (0, 'Error message')
 #-------------------------------------------------------------------------------
-sub sendmail_wrapper{
+sub sendmailWrapper
+{
 
   my $message    = shift;
   my $email_conf = shift;
 
-  if(open(my $sendmail, $email_conf)){
+  if(open(my $sendmail, $email_conf)) {
     print $sendmail $message;
     close $sendmail;
     return (1);
@@ -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'};
@@ -81,38 +84,38 @@ sub send_report{
   my $email_conf = $$input_data{'email_conf'};
 
   my $message;
-  if(!($contact)){
+  if(!($contact)) {
     return (0, "Empty 'To' email header!\n");
   }
 
-  if(!($domain)){
+  if(!($domain)) {
     return (0, "No sender's domain! Can't send email\n");
   }
 
-  if(!($text)){
+  if(!($text)) {
     return (0, "No text! Nothing to send\n");
   }
 
   eval{
     $message = Email::Simple->create(
     header => [
-      To => $contact,
-      From => 'warden_watchdog@'.$domain,
+      To      => $contact,
+      From    => 'warden_watchdog@'.$domain,
       Subject => $subject],
-    body => $text);
+    body   => $text);
   } or do {
     return (0, "Can't create email message\n");
   };
 
-  my ($rc, $err) = sendmail_wrapper($message->as_string,$email_conf);
-  if(!$rc){
+  my ($rc, $err) = sendmailWrapper($message->as_string,$email_conf);
+  if(!$rc) {
     return (0, $err);
   }
   return (1);
 }
 
 #-------------------------------------------------------------------------------
-# 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;
-  if(!defined($dbh)){
-    return (0, "update_procedures: no database handle defined")
+  my $dbh = $$dbh_ref;
+  if(!defined($dbh)) {
+    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;
-  if(!defined($dbh)){
-    return (0, "send_query: no database handle defined")
+  my $dbh = $$dbh_ref;
+  if(!defined($dbh)) {
+    return (0, "sendQuery: no database handle defined")
   }
 
-  my @config = @{$configRef};
+  my @config = @{$config_ref};
   my %bad_events;
   my ($rc,$err);
   my $i = 0;
@@ -228,22 +234,22 @@ sub send_query{
   while ($i < scalar(@config)) {
     # run DB query -> requestor, client name
     my $sth;
-    if (defined($config[$i]{query})){
+    if (defined($config[$i]{query})) {
       $sth = $dbh->prepare($config[$i]{query});
     }
     else{
       return (0, "No query available\n");
     }
 
-    if (!($sth->execute)){
+    if (!($sth->execute)) {
       return (0, "Couldn't get data from my database: $sth->errstr\n");
     };
 
     my $contact;
     my $msg_text = 1;
 
-    while(my $result = $sth->fetchrow_hashref()){
-      if (defined($config[$i]{contact})){
+    while(my $result = $sth->fetchrow_hashref()) {
+      if (defined($config[$i]{contact})) {
         # override contact from 'requestor' collumn
         $contact = $config[$i]{contact};
       }
@@ -251,14 +257,14 @@ sub send_query{
         $contact = $result->{'requestor'};
       }
       # information header
-      if($msg_text){
+      if($msg_text) {
         $bad_events{$contact} .= $config[$i]{text} . "\n\n";
         $bad_events{$contact} .= join(" | ", map {$_ // "UNKNOWN" } keys %$result) . "\n";
         $msg_text = 0;
       }
       $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";
     }
 
@@ -266,7 +272,7 @@ sub send_query{
     $i++;
   }
 
-  %$eventsRef = %bad_events;
+  %$events_ref = %bad_events;
   return (1);
 }
 
@@ -287,43 +293,44 @@ 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)){
+  if(!defined($conf_file)) {
     return (0,"No conf file is available");
   }
 
-  if(!defined($period)){
+  if(!defined($period)) {
     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");
+    if ($@) {
+      $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");
+    if (!(defined $_)) {
+      $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 ($@){
-      $errMsg = "Errors in config file '$server_conf': $@";
-      #syslog("err|$errMsg");
-      print $errMsg;
-      return (0,"Warden watchdog - $errMsg");
+  if (!(do $SERVER_CONF)) {
+    if ($@) {
+      $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");
+    if (!(defined $_)) {
+      $err_msg = "Can't read config file '$SERVER_CONF': $!";
+      #syslog("err|$err_msg");
+      print $err_msg;
+      return (0,"Warden watchdog - $err_msg");
     }
   }
 
@@ -368,44 +375,44 @@ sub run{
 
   my $dbh;
   # connect to DB
-  my ($rc,$err) = connect_to_DB(\%db_conf,\$dbh);
-  if (!$rc){
-    $errMsg = "Warden watchdog can\'t connect do DB: $err";
-    return (0,"Warden watchdog - $errMsg");
+  my ($rc,$err) = connectToDB(\%db_conf,\$dbh);
+  if (!$rc) {
+    $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 (!$rc){
+  if(@SQL_PRECONDITION) {
+    ($rc,$err) = updateProcedures(\$dbh,\@SQL_PRECONDITION);
+    if (!$rc) {
       #print "Warden watchdog - $err\n";
       return (0,"Warden watchdog - $err\n");
     }
   }
 
   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);
-    if (!$rc){
+    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 (!$rc){
+  if(@SQL_POSTCONDITION) {
+    my ($rc,$err) = updateProcedures(\$dbh,\@SQL_POSTCONDITION);
+    if (!$rc) {
       #print "Warden watchdog - $err\n";
       return (0,"Warden watchdog - $err\n");
     }
   }
 
-  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);
-    if (!$rc){
+  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) = sendReport(\%input);
+    if (!$rc) {
       #print $err;
       return (0,"Warden client - networkReporter $err\n");
     }
diff --git a/src/contrib/wardenWatchdog/wardenWatchdog.pl b/src/contrib/wardenWatchdog/wardenWatchdog.pl
index 2e396b0..c653534 100755
--- a/src/contrib/wardenWatchdog/wardenWatchdog.pl
+++ b/src/contrib/wardenWatchdog/wardenWatchdog.pl
@@ -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
@@ -41,12 +41,12 @@ sub help {
 }
 
 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();
 }
-else{
+else {
   my ($rc,$err) = WardenWatchdog::run($config,$interval);
-  if(!$rc){
+  if(!$rc) {
     print "WardenWatchdog error: $err";
   }
 }
-- 
GitLab