/usr/bin/schwartzmon is in libtheschwartz-perl 1.12-1.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 | #!/usr/bin/perl
use strict;
use DBI;
use Getopt::Long;
my $dbname = "schwartz";
my $user = "root";
my $pass = "";
my $job = "";
my $max_age = 0;
my $max_count = 0;
=head1 NAME
schwartzmon - monitor The Schwartz
=head1 USAGE
Type
schwartzmon --help
to get full usage.
=cut
sub usage {
die <<USAGE;
Usage: schwartzmon <command> [OPTS]
Possible commands:
queues View past-due job queue depths. (default cmd)
errors View errors.
Global options:
--job=<JOBNAME> Only look at one specific job name. Else all are considered.
--user=<user> Connect to the database as this user
--pass=<pass> Connect to the database with this password
--database=<db> Connect to this database
--dsn=<dsn> Connect to the database using this DSN
Options for 'queues' command:
--maxage=<n> Don't complain if age of overdue job queue is <= 'n'
--maxcount=<n> Don't complain if depth of overdue job queue is <= 'n'
Options for 'errors' command:
--follow | -f Like 'tail -f' for tracking the error log
--last=n Show last 'n' errors from log
--inlast=n Show errors in last 'n' seconds
Verbosity:
if no alerts, nothing is printed, and exit status is 0.
Exit status:
0 if no alerts,
non-zero if there are alerts, in which case the alerts are printed.
USAGE
}
my $opt_help = 0;
my ( $opt_follow, $opt_last, $opt_inlast, $opt_func, $dsn );
usage()
unless GetOptions(
"job=s" => \$job,
"maxage=i" => \$max_age,
"maxcount=i" => \$max_count,
"help" => \$opt_help,
"follow|f" => \$opt_follow,
"last=i" => \$opt_last,
"inlast=i" => \$opt_inlast,
"user=s" => \$user,
"pass=s" => \$pass,
"dsn=s" => \$dsn,
"database=s" => \$dbname,
"func=s" => \$opt_func,
);
usage() if $opt_help;
my $cmd = shift || "queues";
usage() unless $cmd =~ /^queues|errors$/;
my $dbset = DBSet->new;
$dsn ||= "DBI:mysql:$dbname";
$dbset->add( DBHandle->new( { dsn => $dsn, user => $user, pass => $pass } ) );
if ( $cmd eq "queues" ) { queues($dbset); }
if ( $cmd eq "errors" ) { errors($dbset); }
exit 0;
#################
sub queues {
my $dbs = shift;
my $some_alert = 0;
$dbs->foreach(
sub {
my $db = shift;
my $dbh = $db->dbh or next;
my $funcmap = $dbh->selectall_hashref(
"SELECT funcid, funcname FROM funcmap", "funcid" );
foreach my $funcid (
sort { $funcmap->{$a}{funcname} cmp $funcmap->{$b}{funcname} }
keys %$funcmap
)
{
my $funcname = $funcmap->{$funcid}{funcname};
next if $job && $funcname ne $job;
my $now = time();
my $inf = $dbh->selectrow_hashref(
"SELECT COUNT(*) as 'ct', MIN(run_after) 'oldest' FROM job WHERE funcid=? AND run_after <= $now",
undef, $funcid
);
my $behind = $inf->{ct} ? ( $now - $inf->{oldest} ) : 0;
# okay by default, then we apply rules:
my $okay = 1;
$okay = 0 if $behind > $max_age;
$okay = 0 if $inf->{ct} > $max_count;
next if $okay;
$some_alert = 1;
print "$funcname\n";
print " outstanding: $inf->{ct}\n";
print " behind_secs: $behind\n";
}
}
);
exit( $some_alert ? 1 : 0 );
}
sub errors {
my $dbs = shift;
if ($opt_follow) {
follow_errors($dbs);
}
$opt_last = 100 unless $opt_last || $opt_inlast;
my @rows;
$dbs->foreach(
sub {
my $db = shift;
my $dbh = $db->dbh
or next;
my $extra_where = '';
if ($opt_func) {
my $funcid = $db->funcid_of_func($opt_func) || 0;
$extra_where = "AND funcid=$funcid";
}
my $sql;
if ($opt_last) {
$sql
= "SELECT error_time, jobid, message FROM error WHERE 1=1 $extra_where "
. "ORDER BY error_time DESC LIMIT $opt_last";
}
elsif ($opt_inlast) {
my $since = time() - $opt_inlast;
$sql
= "SELECT error_time, jobid, message FROM error WHERE error_time >= $since $extra_where "
. "ORDER BY error_time LIMIT 50000";
}
my $sth = $dbh->prepare($sql);
$sth->execute;
push @rows, $_ while $_ = $sth->fetchrow_hashref;
}
);
@rows = sort { $a->{error_time} <=> $b->{error_time} } @rows;
if ( $opt_last && @rows > $opt_last ) {
shift @rows while @rows > $opt_last;
}
foreach my $r (@rows) {
print_error($r);
}
}
sub follow_errors {
my $dbs = shift;
while (1) {
$dbs->foreach(
sub {
my $db = shift;
my $dbh = $db->dbh
or next;
my $notes = $db->notes;
my $lastmax = $notes->{lastmax} || time();
my $seen = $notes->{seen} ||= {};
my $extra_where = '';
if ($opt_func) {
my $funcid = $db->funcid_of_func($opt_func) || 0;
$extra_where = "AND funcid=$funcid";
}
my $sth
= $dbh->prepare(
"SELECT error_time, jobid, message FROM error WHERE error_time >= ? $extra_where ORDER BY error_time"
);
$sth->execute($lastmax);
my @errors;
push @errors, $_ while $_ = $sth->fetchrow_hashref;
my $newmax = $lastmax;
foreach my $r (@errors) {
my $sig = join( ",", map { $_, $r->{$_} } sort keys %$r );
next if $seen->{$sig};
$seen->{$sig} = $r->{error_time};
print_error($r);
$newmax = $r->{error_time} if $r->{error_time} > $newmax;
}
$notes->{lastmax} = $newmax;
foreach my $sig ( keys %$seen ) {
my $time = $seen->{$sig};
delete $seen->{$sig} if $time < $newmax;
}
}
);
sleep 1;
}
}
sub print_error {
my $r = shift;
my $msg = $r->{message};
$msg =~ s/\s+$//g;
printf scalar( localtime( $r->{error_time} ) ) . " [$r->{jobid}]: $msg\n";
}
package DBSet;
sub new {
my ( $this, $args ) = @_;
my $class = ref($this) || $this;
return bless {}, $class;
}
sub add {
my ( $self, $db ) = @_;
push @$self, $db;
}
sub foreach {
my ( $self, $cb ) = @_;
foreach my $dbh (@$self) {
$cb->($dbh);
}
}
package DBHandle;
sub new {
my ( $class, $dbinf ) = @_;
return bless $dbinf, $class;
}
sub notes {
my $self = shift;
return $self->{notes} ||= {};
}
# returns DBI handle
sub dbh {
my $self = shift;
return $self->{_dbh}
||= DBI->connect( $self->{dsn}, $self->{user}, $self->{pass} )
}
sub funcid_of_func {
my ( $self, $func ) = @_;
my $notes = $self->notes;
return $notes->{"funcid_of_$func"} if exists $notes->{"funcid_of_$func"};
my $dbh = $self->dbh;
return $notes->{"funcid_of_$func"}
= $dbh->selectrow_array(
"SELECT funcid FROM funcmap WHERE funcname=?",
undef, $func );
}
=head1 COPYRIGHT, LICENSE & WARRANTY
This software is Copyright 2007, 2008 Six Apart Ltd, cpan@sixapart.com. All
rights reserved.
TheSchwartz is free software; you may redistribute it and/or modify it
under the same terms as Perl itself.
TheSchwartz comes with no warranty of any kind.
=cut
|