-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdg_log_monitor.pl
135 lines (116 loc) · 4.68 KB
/
dg_log_monitor.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/perl -w
#============================================================#
# Script to monitor DataGuard log file (like drcnvcsea3.log).#
# 02/25/2013 Created from ggserr_log_monitoring.pl. #
#============================================================#
use strict;
use warnings;
use File::Basename;
use File::ReadBackwards;
use FileHandle;
use Mail::Sender;
use lib $ENV{WORKING_DIR};
require $ENV{MY_LIBRARY};
#--------------------------------------------------------------#
# DB name and server name should be UPPER case. This is needed #
# to read corresponding section from configuration file #
#--------------------------------------------------------------#
my $db_name = uc $ENV{ORACLE_SID};
my $server_name = uc $ENV{ORACLE_HOST_NAME};
my $config_db_name = $server_name.'_'.$db_name;
#----------------------------------------------------#
# Call procedure from my_library.pl #
# Get file names, check for double execution #
# and return reference hash with config parameters #
# It does not check for double execution on Windows. #
#----------------------------------------------------#
my $config_params_ref = GetConfig();
#------------------------------------------#
# Read configuration file and check format #
#------------------------------------------#
my $log_file = $config_params_ref->{$config_db_name}{'log_file'};
my $errors_include = $config_params_ref->{$config_db_name}{'errors_include'};
my $errors_exclude = $config_params_ref->{$config_db_name}{'errors_exclude'};
my $oldest_timestamp = $config_params_ref->{$config_db_name}{'timestamp'};
if ( ( !defined $log_file )
or ( !defined $errors_include )
or ( !defined $errors_exclude )
)
{
print "Check configuration file. Some parameter was not defined.\n";
exit 1;
}
# In case if there is no new timestamps in log file.
# Timestamp to write in configuration file.
my $timestamp2remember = $oldest_timestamp;
# Flag for first timestamp
my $the_first_time = 1;
# DataGuard timestamp: 2013-02-26 03:53:35.169
my $timestamp_pattern = "^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}.\\d{3} ";
#--------------------------------#
# Open log file to read backward #
#--------------------------------#
tie *DGERR, 'File::ReadBackwards', $log_file
or die "Can't read $log_file $!" ;
#--------------------------------------------------#
# Read the file line by line starting from the end #
#--------------------------------------------------#
my $message = '';
while( <DGERR> )
{
my $logfile_line = $_;
#------------------------------------------------------#
# This is the last loop if we reached oldest timestamp #
#------------------------------------------------------#
last if ($logfile_line =~ m/$oldest_timestamp/);
# Find and remember the first timestamp we read
if ($the_first_time)
{
# Write it into configuration file at the end of the script.
# Extract timestamp from the log line: first 19 chars
if ($logfile_line =~ m/$timestamp_pattern/)
{
$timestamp2remember = substr $logfile_line, 0, 23;
$the_first_time = 0;
}
}
#==========================================#
# This is not a timestamp. #
# Check for errors to include and exclude. #
#==========================================#
if ( ($logfile_line =~ m/$errors_include/i)
and ($logfile_line !~ m/$errors_exclude/i))
{
$message = $logfile_line . $message;
}
} # while( <DGERR> )
#----------------------------------------------------#
# Send e-mail if there are errors #
#----------------------------------------------------#
if ( ( $message ne '' ) and ( $oldest_timestamp ne $timestamp2remember ))
{
my $subject = "Errors in DataGuard Log $db_name on $server_name.";
# Print to output file and error message
$message = "Errors found in time range\n$oldest_timestamp\n$timestamp2remember\n"
. $message;
SendAlert ( $server_name, $db_name, $subject, $message );
}
else
{
# If there are no alerts in alert log file, this should be the only
# output line in the script log:
if ($oldest_timestamp eq $timestamp2remember)
{
print "No errors found since time\n$oldest_timestamp\n";
}
else
{
print "No errors found in time range\n$oldest_timestamp\n$timestamp2remember\n";
}
}
#-------------------------------------------------#
# All is done. Now we can overwrite old timestamp #
# in configuration file #
#-------------------------------------------------#
RewriteConfigFile ($config_db_name, $config_params_ref, 'timestamp', $timestamp2remember);
exit;