2017-07-02 40 views
1

我想檢索使用Ruby腳本Redis的狀態,我想下面的命令:獲得Redis的狀態通過紅寶石

redis_status_command, stdeerr, status = Open3.capture3(`redis-cli -h 127.0.0.1 -p 6379 info|grep status`) 

OR

redis_status_command, stdeerr, status = Open3.capture3(`systemctl status redis`) 

然後,當我嘗試打印變量redis_status_command它返回一個空格,但我知道Open3.capture3部分內的命令在命令行中工作。我如何使用Ruby檢索Redis狀態?謝謝

+0

是否redis- rb gem包含你在找什麼? - https://github.com/redis/redis-rb – arjabbar

回答

0

隨着「redis的」寶石,你可以得到所有的信息屬性如下:

require 'redis' 
client = Redis.new(
    url: "redis://your-host.your-domain.com", 
    port: 6379 
) 
client.info 

的輸出將是信息的哈希值:

{ 
    "redis_version" => "3.2.4", 
    "redis_git_sha1" => "0", 
    "redis_git_dirty" => "0", 
    "redis_build_id" => "0", 
    "redis_mode" => "standalone", 
    "os" => "Amazon ElastiCache", 
    "arch_bits" => "64", 
    "multiplexing_api" => "epoll", 
    "gcc_version" => "0.0.0", 
    "process_id" => "1", 
    "run_id" => "73f5f76133b7bfd66eb89850a2f9df43e838f567", 
    "tcp_port" => "6379", 
    "uptime_in_seconds" => "1754115", 
    "uptime_in_days" => "20", 
    "hz" => "10", 
    "lru_clock" => "5884700", 
    "executable" => "-", 
    "config_file" => "-", 
    "connected_clients" => "10", 
    "client_longest_output_list" => "0", 
    "client_biggest_input_buf" => "0", 
    "blocked_clients" => "0", 
    "used_memory" => "16110168", 
    "used_memory_human" => "15.36M", 
    "used_memory_rss" => "19808256", 
    "used_memory_rss_human" => "18.89M", 
    "used_memory_peak" => "19915496", 
    "used_memory_peak_human" => "18.99M", 
    "used_memory_lua" => "37888", 
    "used_memory_lua_human" => "37.00K", 
    "maxmemory" => "6501171200", 
    "maxmemory_human" => "6.05G", 
    "maxmemory_policy" => "volatile-lru", 
    "mem_fragmentation_ratio" => "1.23", 
    "mem_allocator" => "jemalloc-4.0.3", 
    "loading" => "0", 
    "rdb_changes_since_last_save" => "30264", 
    "rdb_bgsave_in_progress" => "0", 
    "rdb_last_save_time" => "1497302809", 
    "rdb_last_bgsave_status" => "ok", 
    "rdb_last_bgsave_time_sec" => "-1", 
    "rdb_current_bgsave_time_sec" => "-1", 
    "aof_enabled" => "0", 
    "aof_rewrite_in_progress" => "0", 
    "aof_rewrite_scheduled" => "0", 
    "aof_last_rewrite_time_sec" => "-1", 
    "aof_current_rewrite_time_sec" => "-1", 
    "aof_last_bgrewrite_status" => "ok", 
    "aof_last_write_status" => "ok", 
    "total_connections_received" => "29746", 
    "total_commands_processed" => "3809776", 
    "instantaneous_ops_per_sec" => "1", 
    "total_net_input_bytes" => "382270539", 
    "total_net_output_bytes" => "3134102675", 
    "instantaneous_input_kbps" => "0.05", 
    "instantaneous_output_kbps" => "0.03", 
    "rejected_connections" => "0", 
    "sync_full" => "1", 
    "sync_partial_ok" => "0", 
    "sync_partial_err" => "0", 
    "expired_keys" => "0", 
    "evicted_keys" => "0", 
    "keyspace_hits" => "674", 
    "keyspace_misses" => "318", 
    "pubsub_channels" => "0", 
    "pubsub_patterns" => "0", 
    "latest_fork_usec" => "162", 
    "migrate_cached_sockets" => "0", 
    "role" => "master", 
    "connected_slaves" => "1", 
    "slave0" => "ip=192.168.1.2,port=6379,state=online,offset=341359205,lag=0", 
    "master_repl_offset" => "341359205", 
    "repl_backlog_active" => "1", 
    "repl_backlog_size" => "1048576", 
    "repl_backlog_first_byte_offset" => "340310630", 
    "repl_backlog_histlen" => "1048576", 
    "used_cpu_sys" => "494.00", 
    "used_cpu_user" => "912.16", 
    "used_cpu_sys_children" => "0.00", 
    "used_cpu_user_children" => "0.00", 
    "cluster_enabled" => "0", 
    "db0" => "keys=3479,expires=0,avg_ttl=0" 
}