Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
R
resmon
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
mirrors
resmon
Commits
8161c39e
Commit
8161c39e
authored
12 years ago
by
Mark Harrison
Browse files
Options
Downloads
Patches
Plain Diff
Core::Log module for log file error monitoring
parent
00b76490
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
lib/Core/Log.pm
+108
-0
108 additions, 0 deletions
lib/Core/Log.pm
with
108 additions
and
0 deletions
lib/Core/Log.pm
0 → 100644
+
108
−
0
View file @
8161c39e
package
Core::
Log
;
use
strict
;
use
warnings
;
use
base
'
Resmon::Module
';
use
Resmon::
ExtComm
qw(run_command cache_command)
;
=pod
=head1 NAME
Core::Log - Monitor a log file for errors matching a certain pattern
=head1 SYNOPSIS
Core::Log {
/var/log/foo: match => ^ERROR:
}
=head1 DESCRIPTION
This module will tail a log file and count the number of errors it finds. An
error is determined by the match parameter to the check, which is a regular
expression.
When a log file rotates or is moved aside, the error count is reset. This
can be used to clear an alert for the error count being greater than 0.
=head1 CONFIGURATION
=over
=item check_name
The name of the file to monitor
=item match
Regular expression for matching error lines
=back
=head1 METRICS
=over
=item errs
The number of errors
=item errstring
A string showing the errors
=back
=cut
sub
handler
{
my
$self
=
shift
;
my
$config
=
$self
->
{
config
};
# All configuration is in here
my
$file
=
$self
->
{
check_name
};
# The check name is in here
my
@statinfo
=
stat
(
$file
);
if
(
!
exists
(
$self
->
{
file_dev
})
||
$self
->
{
file_dev
}
!=
$statinfo
[
0
]
||
$self
->
{
file_ino
}
!=
$statinfo
[
1
])
{
# New file, reset stats
$self
->
{
lastsize
}
=
0
;
$self
->
{
errs
}
=
0
;
$self
->
{
errstring
}
=
'';
}
if
(
$self
->
{
lastsize
}
==
0
||
$self
->
{
lastsize
}
!=
$statinfo
[
7
])
{
# If the logfile has grown
my
$log
;
if
(
!
open
(
$log
,
"
<
$file
"))
{
die
("
Unable to open log file
$file
");
}
seek
(
$log
,
$self
->
{
lastsize
},
0
);
while
(
<
$log
>
)
{
chomp
;
if
(
/$config->{match}/
)
{
$self
->
{
errstring
}
.=
"
"
if
(
length
(
$self
->
{
errstring
}));
$self
->
{
errstring
}
.=
$_
;
$self
->
{
errs
}
++
;
}
}
close
(
$log
);
}
# Remember where we were
$self
->
{
file_dev
}
=
$statinfo
[
0
];
$self
->
{
file_ino
}
=
$statinfo
[
1
];
$self
->
{
lastsize
}
=
$statinfo
[
7
];
return
{
"
errs
"
=>
[
$self
->
{
errs
},
"
i
"],
"
errstring
"
=>
[
$self
->
{
errstring
},
"
s
"],
};
};
1
;
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment