Gearman::XS client implementation
[cloudstore.git] / gearman / zfs.pl
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4
5 use Data::Dump qw(dump);
6
7 sub home_dir {
8         my $login = shift;
9         my ( undef, undef, $uid, $gid, undef, undef, $email, $dir, $shell ) = getpwnam $login;
10         return $dir;
11 }
12
13 use Gearman::Worker;
14 my $worker = Gearman::Worker->new;
15 $worker->job_servers('127.0.0.1:4730');
16
17 die "missing ZSLICE from env" unless $ENV{ZSLICE};
18
19 sub zfs_get {
20         my ($command,$job) = @_;
21         my $work = $job->arg;
22         chomp $work;
23
24
25         my $zfs = sprintf 'zfs get -H -p -o value %s@%s %s', $command, $work, $ENV{ZSLICE};
26            $zfs = sprintf 'zfs userspace -H -p -o name,used,quota %s', $ENV{ZSLICE} if $command eq 'userspace';
27
28         warn "# zfs_get $command [$work] -> $zfs\n";
29
30         my $v = `$zfs`;
31
32         warn "# $command $work = $v\n";
33
34         return $v;
35 }
36
37 $worker->register_function( zfs_userused  => sub { zfs_get( 'userused'  => $_[0] ) } );
38
39 $worker->register_function( zfs_userquota => sub { zfs_get( 'userquota' => $_[0] ) } );
40
41 $worker->register_function( zfs_userspace => sub { zfs_get( 'userspace' => $_[0] ) } );
42
43 warn "$0 pid $$ waitng for jobs\n";
44 $worker->work while 1;
45