写了一个 memcachedbctl 的 perl脚本

首先需要明确一下几个目录:
1. memcachedb database 的文件为: /var/memcachedb/data/memcachedb.db
2. memcachedb 的Home为: /var/memcachedb/
3. memcachedb 的配置文件为 /etc/memcachedb/memcachedb.conf(这个文件在下一篇文章中贴出来。
4. pid 文件在 /var/run/memcachedb/memcachedb.pid
如果这几个目录和这个不一致的话,就修改一下这个脚本。

至于使用方法,直接 运行一下就可以知道了。
#!/usr/local/bin/perl -w

use strict;
use Getopt::Long;
use Data::Dumper;


checkUser();
my $len = @ARGV;
my $configFile = '/etc/memcachedb/memcachedb.conf';
my $cmd = '';
usage() if($len < 1);
my %options = getOptions();

if($ARGV[0] eq 'start') {
start();
} elsif( $ARGV[0] eq 'stop' ) {
stop();
} elsif( $ARGV[0] eq 'restart') {
restart();
} elsif( $ARGV[0] eq 'status' ) {
status();
} elsif( $ARGV[0] eq 'dbstat' ) {
dbstat();
} else {
usage();
}

sub start
{
check();
my $cmd = '/usr/local/bin/memcachedb';
my %relations = getOptionRelation();
my ($key, $value);
my ($opt, $type, $defaultValue);
while(($key, $value) = each(%options))
{
next if(! exists $relations{$key});
$opt = $relations{$key}[0];
$type = $relations{$key}[1];
$defaultValue = $relations{$key}[2];
if($type eq 'number' || $type eq 'string') {
$cmd .= ' ' . $opt . ' ' . ($value eq '' ? $defaultValue : $value);
} elsif ($type eq 'boolean' ) {
$cmd .= ' ' . ($value ? $opt : '');
}
}
$cmd .= " &";
print " memcachedb starting... n";
my $ret = system($cmd);
print " memcachedb start ";
print "success!n" if($ret == 0);
print "failed!n" if($ret != 0);
}
sub stop
{
my $pidFile = getValue("pid-file");
if( -e $pidFile)
{
my $pid = `cat $pidFile`;
chomp($pid);
system("kill $pid") == 0 or do {
print "kill memcachedb failed!n";
exit(0);
};
print "kill success!n";
`sleep 3`;
unlink($pidFile);
}
}
sub restart
{
stop();
start();
}
sub status
{
my $pidFile = getValue("pid-file");
if( -e $pidFile ) {
print "Memcachedb is running. ", "Pid is ", `cat $pidFile`;
} else {
print "Memcachedb is not running.";
}
}
sub dbstat
{
my $bin = '/usr/local/bin/db_stat';
my $cmd = $bin . " -d " . getValue('database-data-file');
print $cmd;
my $output = `$cmd`;
print $output;
}
sub getOptions
{
my %options;
my ($key, $value);

if( ! -f $configFile ) {
print "$configFile not exists!n";
usage();
}
open F, "<$configFile" or do {
usage();
};
while(<F>)
{
next if /^s*#|^s*$|^[/;
chomp;
($key,$value) = split(/s*=s*/);
$options{"$key"} = $value;
}
close F;

=cut
while(($key, $value) = each(%options)){
print $key, $value;
print "n";
}
=cut
return %options;
}


sub usage
{
print "Usage: memcachedbctl (status|dbstat|start|stop|restart)n";
exit(1);
}


sub getOptionRelation
{
my %options = (
'port' => ['-p', 'number', 21201],
'udp-port' => ['-U', 'number', 0],
'socket-file' => ['-s', 'string', '/tmp/memcachedb.sock'],
'perm' => ['-a', 'string', '0700'],
'listen' => ['-l', 'string', 'INDRR_ANY'],
'daemon' => ['-d', 'boolean', 1],
'max-core-file-size' => ['-r', 'boolean', 1],
'user' => ['-u', 'string', 'root'],
'max-connections' => ['-c', 'number', 1024],
'max-item-buffer-size' => ['-b', 'string', 1024], ## 1024bytes
'pid-file' => ['-P', 'string', '/var/run/memcachedb/memcachedb.pid'],
'max-cache-size' => ['-m', 'number', 64], ## 64MB
'page-size' => ['-A', 'number', 4096], ## 4096 bytes
'database-data-file' => ['-f', 'string', '/var/memcachedb/data/memcachedb.db'],
'database-data-home' => ['-H', 'string', '/var/memcachedb'],
'log-buffer-size' => ['-L', 'number', 32], ## 32KB
'checkpoint-cycle' => ['-C', 'number', 60], ## 60seconds,
'deadlock-check-cycle' => ['-D', 'number', 100], ## 100 ms
'db-txn-nosync' => ['-N', 'boolean', 0],
'server-name-port' => ['-R', 'string', '127.0.0.1:31201'],
'remote-server-name-port' => ['-O','string','127.0.0.1:31202'],
'master' => ['-M', 'boolean', 0],
'slave' => ['-S', 'boolean', 0]
);
return %options;
}

sub check
{
my $pidFile = getValue("pid-file");
if(-e $pidFile)
{
print "There already have a memcachedb deamon running.";
print "pid is ", `cat $pidFile`, "n";
exit 1;
}
}


sub getValue
{
my ($key) = @_;
if(exists($options{$key}))
{
return $options{$key};
}
my %relations = getOptionRelation();
return $relations{'pid-file'}[2];
}

sub checkUser
{
my $user = `whoami`;
chomp($user);
if($user ne 'root') {
print "You should run this script as root!n";
usage();
}
}
This article is posted by on , link is .

Leave a reply