Mackerelで俺を監視
TIME rest time current/total
TopicsPlaceHolder

Mackerelで俺を監視

Mackerel meetup #3

Jan 22th, 2015

Profile

songmu

Q. 自分自身の監視にMackerelは使えるか?

 A. 使えます

成果

仕組み

アプリケーションの切り替えをSlateで検知

% cat ~/.slate.js
S.on('appActivated', function (event, app) {
    S.log('appActivated:' + app.name());
});

/var/log/system.log にアプリ切り替えログが出る

Jan 21 23:23:41 songmu-mac Slate[14580]: appActivated:Slack

キーの押下をKarabinerで検知

% sudo sysctl -w karabiner.debug=1

/var/log/system.log にキーダウンのログが出る

Jan 21 23:26:41 songmu-mac kernel[0]: org.pqrs.Karabiner --Debug-- KeyboardEventCallback [ caught]: eventType 10...

tail -F /var/log/system.log してPerlでparse

#!/usr/bin/env perl
use 5.020;
use warnings;
$| = 1;
while (my $line = <>) {
    state $current_app = 'Terminal';
    if (my ($app) = $line =~ /Slate\[.*appActivated:(.*)/) {
        if ($app =~ /ターミナル/) {
            $app = 'Terminal';
        }
        $current_app = $app; next;
    }
    my $press_key = quotemeta 'org.pqrs.Karabiner --Debug-- KeyboardEventCallback [ caught]: eventType 10';
    if ($line =~ /$press_key/) {
        printf qq[{"app": "%s"}\n], $current_app if $current_app;
    }
}

fluent-catで結果を投げ込み

#!/bin/sh
tail -F /var/log/system.log | ./keylogger.pl | fluent-cat keylogger

fluent-plugin-mackerelでサービスメトリックの投稿

<source>
  type forward
  port 24224
</source>

<match keylogger>
    type datacounter
    count_interval 1m
    count_key app
    tag keycounter
    aggregate all
    pattern1 Terminal     ^Terminal$
    pattern2 MacVim       ^MacVim$
    pattern3 LimeChat     ^LimeChat$
    pattern4 Slack        ^Slack$
    pattern5 GoogleChrome ^Google\s*Chrome$
</match>

<match keycounter>
    type         mackerel
    service      songmu
    api_key      [YOU API KEY]
    metrics_name keylogger.${out_key}
    out_keys     Terminal_count,MacVim_count,LimeChat_count,Slack_count,GoogleChrome_count
</match>

完成

今後の展望

以上

We are Hiring

hatena