整个博客均为博主原创,转载请先联系~
#以下均为博主学习笔记#
redis简单介绍
Redis是一个高性能的key-value数据库
Redis支持主从同步。数据可以从主服务器向任意数量的从服务器上同步
Redis根本是使用内存存储
redis的存储分为内存存储、磁盘存储和log文件三部分
安装模块
perl -MCPAN -e 'install Redis'
用法:
连接redis
my $r = Redis->new(server => '127.0.0.1:6379');
my $r = Redis->new(
server => '127.0.0.1:6379', #服务器地址端口
name => 'connect_name', #连接名
reconnect => 2, every => 100_000 #自动连接
);
设置set/mset
$r->set( key => 'value' ); #设置key value
$r->mset(key => 'value' [key => 'value' ...])
获取get/mget
my $value = $r->get( 'key' ); #获得key对应的值
my ( $value1, $value2, ...) = $r->mget('key1','key2',...);
删除del
$r->del( 'key' ); #删除key
info
my $info_hash = $r->info; #返回哈希引用
while ( my ($key, $value) = each(%$info_hash) ) {
print "$key => $value, ";
}
exists
$r->exists(key); #测试是否存在
退出quit
$r->quit();
# 更多语法请查看 perldoc Redis
例子: #以下例子为转载
Redis连接测试脚本
#!/usr/bin/perl
use strict;
use redis;
my ($key, $value) = ('list1', 'dbserver_01');
my $r = Redis->new(server => '127.0.0.1:6379');
$r->lpush($key, $value);
my $listlen = $r -> llen($key);
print "List insert data length: $listlen \n";
my $reval = $r->rpop($key);
my $listlen = $r -> llen($key);
print "$key:$reval\n";
print "List pop data length: $listlen \n";
#!/usr/bin/perl
# nagios: -epn
################################################################################
# check_redis - Nagios Plugin for Redis checks.
#
# @author farmer.luo at gmail.com
# @date 2010-05-12
# @license GPL v2
# check_nagios.pl -h
-p -w -c
#
# Run the script need:
#
# perl -MCPAN -e shell
# install Redis
#
################################################################################
use strict;
use warnings;
use Redis;
use File::Basename;
use utils qw($TIMEOUT %ERRORS &print_revision &support);
use Time::Local;
use vars qw($opt_h); # Redis 主机
use vars qw($opt_p); # Redis 端口
use vars qw($opt_w); # 超过这个时间发出警告
use vars qw($opt_c); # 超过这个时间发出严重警告
use Getopt::Std;
$opt_h = "";
$opt_p = "6379";
$opt_w = 5;
$opt_c = 10;
my $r = "";
getopt('hpwcd');
if ( $opt_h eq "" ) {
help();
exit(1);
}
my $start = time();
redis_connect();
# print $@;
if ( $@ ) {
print "UNKNOWN - cann't connect to redis server:" . $opt_h . ".";
exit $ERRORS{"UNKNOWN"};
}
if ( redis_set() ) {
print "WARNING - redis server:" . $opt_h . ",set key error.";
exit $ERRORS{"WARNING"};
}
if ( redis_get() ) {
print "WARNING - redis server:" . $opt_h . ",get key error.";
exit $ERRORS{"WARNING"};
}
if ( redis_del() ) {
print "WARNING - redis server:" . $opt_h . ",del key error.";
exit $ERRORS{"WARNING"};
}
#sleep(3);
my $stop = time();
my $run = $stop - $start;
if ( $run > $opt_c ) {
print "CRITICAL - redis server(" . $opt_h . ") run for " . $run . " seconds!";
exit $ERRORS{"CRITICAL"};
} elsif ( $run > $opt_w ) {
print "WARNING - redis server(" . $opt_h . ") run for " . $run . " seconds!";
exit $ERRORS{"WARNING"};
} else {
redis_info();
redis_quit();
exit $ERRORS{"OK"};
}
sub help{
die "Usage:\n" , basename( $0 ) , " -h hostname -p port -w warning time -c critical time -d down time\n"
}
sub redis_connect{
my $redis_hp = $opt_h . ":" . $opt_p;
eval{ $r = Redis->new( server => $redis_hp ); };
}
sub redis_set{
$r->set( redis_nagios_key => 'test' ) || return 1;
return 0;
}
sub redis_get{
my $value = $r->get( 'redis_nagios_key' ) || return 1;
return 0;
}
sub redis_del{
$r->del( 'redis_nagios_key' ) || return 1;
return 0;
}
sub redis_info{
my $info_hash = $r->info;
print "OK - redis server(" . $opt_h . ") info:";
while ( my ($key, $value) = each(%$info_hash) ) {
print "$key => $value, ";
}
}
sub redis_quit{
$r->quit();
}
阅读(1802) | 评论(0) | 转发(0) |