Skip to content

Instantly share code, notes, and snippets.

@mamemomonga
Created May 14, 2020 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mamemomonga/4e646283ea837edf1d799f3ad728099f to your computer and use it in GitHub Desktop.
Save mamemomonga/4e646283ea837edf1d799f3ad728099f to your computer and use it in GitHub Desktop.
コンテナの/app/log/production.logに特定文字列がでてきたらSlackCatをつかって通知する
#!/usr/bin/env perl
# -----------------
# コンテナの/app/log/production.logに特定文字列がでてきたらSlackCatをつかって通知する
# 実行 nohup ./log-monitor.pl &
# -----------------
use feature 'say';
use strict;
use warnings;
$|=1;
my $container='コンテナ名';
my $channel='チャンネル名';
my $username='ユーザ名';
my @targets=(
"Completed 404 Not Found",
"Completed 500 Internal Server Error",
);
my $kid=open(my $log,"docker exec $container tail -f /app/log/production.log | ") || die $!;
while (my $log=<$log>) {
foreach my $search (@targets) {
if($log=~m#\Q$search\E#sg) {
open(my $ofh, "| slackcat --channel $channel --username $username -s") || die $!;
print $ofh "エラー「$search」を検出しました\n";
print $ofh $log;
print $log;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment