Created
November 29, 2011 19:31
-
-
Save riywo/1406087 to your computer and use it in GitHub Desktop.
rrdtoolプロセスをTCPサーバにしてみる
This file contains hidden or 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
use strict; | |
use warnings; | |
use IO::Socket::INET; | |
use Parallel::Prefork; | |
use IPC::Open3; | |
sub MaxRequestsPerChild () { 100 } | |
my $listen_sock = IO::Socket::INET->new( | |
Listen => 5, | |
LocalAddr => '0.0.0.0:10000', | |
Proto => 'tcp', | |
) or die $!; | |
my $pm = Parallel::Prefork->new({ | |
max_workers => 10, | |
trap_signals => { | |
TERM => 'TERM', | |
HUP => 'TERM', | |
}, | |
}); | |
while ($pm->signal_received ne 'TERM') { | |
$pm->start and next; | |
my $reqs_before_exit = MaxRequestsPerChild; | |
$SIG{TERM} = sub { $reqs_before_exit = 0 }; | |
my ($wtr, $rdr); | |
my $rrdtool_pid = open3($wtr, $rdr, 0, qw(rrdtool - /tmp)); | |
while ($reqs_before_exit-- > 0) { | |
my $s = $listen_sock->accept(); | |
while (defined $s and my $q = $s->getline) { | |
if ($q =~ /^[\s|\t]*cd/) { | |
$s->print("ERROR: don't use cd command!!\n"); | |
next; | |
} | |
print $wtr $q; | |
while (my $stdout = <$rdr>) { | |
$s->print($stdout); | |
last if($stdout =~ /^(OK|ERROR:) /); | |
} | |
} | |
} | |
kill 'TERM', $rrdtool_pid; | |
waitpid($rrdtool_pid, 0); | |
$pm->finish; | |
} | |
$pm->wait_all_children; | |
__END__ | |
$ perl rrd-server.pl & | |
[1] 67379 | |
$ telnel localhost 10000 | |
Trying 127.0.0.1... | |
Connected to localhost. | |
Escape character is '^]'. | |
help | |
RRDtool 1.4.5 Copyright 1997-2010 by Tobias Oetiker <[email protected]> | |
Compiled Nov 29 2011 11:22:47 | |
Usage: rrdtool [options] command command_options | |
Valid commands: create, update, updatev, graph, graphv, dump, restore, | |
last, lastupdate, first, info, fetch, tune, | |
resize, xport, flushcached | |
Valid remote commands: quit, ls, cd, mkdir, pwd | |
RRDtool is distributed under the Terms of the GNU General | |
Public License Version 2. (www.gnu.org/copyleft/gpl.html) | |
For more information read the RRD manpages | |
OK u:0.01 s:0.01 r:20.79 | |
ls | |
d . | |
d .. | |
- temperature.rrd | |
OK u:0.01 s:0.01 r:30.63 | |
info temperature.rrd | |
filename = "temperature.rrd" | |
rrd_version = "0003" | |
(snip) | |
rra[3].cdp_prep[0].unknown_datapoints = 0 | |
OK u:0.01 s:0.01 r:39.75 | |
^] | |
telnet> q | |
Connection closed. |
acceptの度にforkするのでいいか。。。
cdしたらずれちゃうしw
cdできないようにして、rrdtool自体はprefork時点で作ったものを使い回す。
シグナル受信時はacceptをスルーするみたいなのでdefined $sすればok。
外部プロセスをfork & execだったらopen3で十分だったorz
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
kill -TERM 67379
ってすると
Can't call method "getline" on an undefined value at rrd-server.pl line 34.
って出てしまう。
forgroundでCtrl+Cだったら出ない。