Created
October 2, 2015 06:27
-
-
Save hiboma/7d3d3727323a0da524a3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
use IO::Socket; | |
my %to_be_aggregated = ( | |
'unicorn' => 'unicorn_worker', | |
'perl-fcgi' => 'perl-fcgi', | |
'mogilefsd' => 'mogilefsd', | |
'mogilefsd [monitor]' => 'mogilefsd_monitor', | |
'mogilefsd [replicate]' => 'mogilefsd_replicate', | |
'mogilefsd [delete]' => 'mogilefsd_delete', | |
'mogilefsd [queryworker]'=> 'mogilefsd_queryworker', | |
'mogilefsd [reaper]' => 'mogilefsd_reaper', | |
'mogilefsd [job_master]'=> 'mogilefsd_job_master', | |
'mogilefsd [fsck]' => 'mogilefsd_fsck', | |
'lsof' => 'lsof', | |
'others' => 'others', | |
'/usr/local/bin/serf agent -config-dir=/etc/serf/conf.d' => 'serf', | |
'/usr/sbin/lighttpd -f /etc/lighttpd/lighttpd.conf' => 'lighttpd', | |
); | |
if ( @ARGV and $ARGV[0] eq 'config' ) { | |
print_config(); | |
} | |
else { | |
print_stat(); | |
} | |
exit; | |
sub print_config { | |
print <<"..."; | |
graph_args --base 1000 -r --lower-limit 0 | |
graph_title CPU usage, by process | |
graph_category system | |
graph_info This graph shows CPU usage, for monitored processes. | |
graph_vlabel % | |
graph_scale no | |
graph_period second | |
... | |
my $is_first_proc = 1; | |
foreach my $cmd (reverse sort values %to_be_aggregated) { | |
print "${cmd}.label ${cmd}\n"; | |
print "${cmd}.info CPU used by process ${cmd}\n"; | |
print "${cmd}.type GAUGE\n"; | |
if ($is_first_proc) { | |
print "${cmd}.draw AREA\n"; | |
$is_first_proc = 0; | |
} else { | |
print "${cmd}.draw STACK\n"; | |
} | |
} | |
} | |
sub aggregate_cpu_usage { | |
my %usage = (); | |
# コマンドライン (cmdline) ごとに、CPU 使用率 (pcpu) を加算するよ | |
map { | |
my @chunks = split /\s+/, $_, 12; | |
my $usage = $chunks[8]; | |
my $cmdline = $chunks[11]; | |
if ( my $munin_key = $to_be_aggregated{$cmdline} ){ | |
$usage{$munin_key} ||= 0; | |
$usage{$munin_key} += $usage; | |
} | |
elsif ($cmdline =~ /^unicorn worker/) { | |
$usage{'unicorn_worker'} ||= 0; | |
$usage{'unicorn_worker'} += $usage; | |
} | |
elsif ($cmdline =~ /^lsof/) { | |
$usage{'lsof'} ||= 0; | |
$usage{'lsof'} += $usage; | |
} | |
else { | |
$usage{'others'} += $usage; | |
} | |
} | |
# 先頭と末尾の空白を trim するよ | |
map { | |
s/^\s+//; | |
s/\s+$//; | |
$_; | |
} | |
# ps の結果を \n で分割して配列にするよ | |
split /\n/, `top -n1 -b -c | tail -n +8`, ; | |
%usage; | |
} | |
sub print_stat { | |
my %cpu_usage = aggregate_cpu_usage; | |
foreach my $munin_key (values %to_be_aggregated) { | |
if (my $pcpu = $cpu_usage{$munin_key}) { | |
printf "${munin_key}.value %0.1f\n", $pcpu; | |
} else { | |
print "${munin_key}.value 0\n"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment