Skip to content

Instantly share code, notes, and snippets.

@sugar84
Created July 20, 2012 10:56
Show Gist options
  • Save sugar84/3150157 to your computer and use it in GitHub Desktop.
Save sugar84/3150157 to your computer and use it in GitHub Desktop.
simple memory watcher for ubic
#!/usr/bin/env perl
use Modern::Perl;
# launch: bin.pl <service_name> [memory_limit in %]
my $service_name = shift @ARGV;
my $mem_limit = @ARGV ? shift @ARGV : 20;
my $pid = get_pid($service_name);
my $mem_size = get_memory($pid);
warn "$service_name:$pid has $mem_size";
if ($mem_size > $mem_limit) {
system("ubic restart $service_name");
}
sub get_pid {
my $service_name = shift;
return unless $service_name;
my $res = qx/ubic status '$service_name'/;
my ($pid) = $res =~ /pid\s+(\d+)/;
unless ($pid) {
die "$service_name: incorrect service name or process isn't running";
}
return $pid;
}
sub get_memory {
my $pid = shift;
return unless $pid;
my $re = qr/^\S+\s+$pid\s+\d+\.\d+\s+(\d+\.\d+)/o;
my $mem_size = 0;
my @ps_ret = qx/ps aux/;
for my $record (@ps_ret) {
if ($record =~ $re) {
$mem_size = $1;
last;
}
}
return $mem_size;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment