如何格式化node_redis信息函数输出?

时间:2013-11-04 09:23:20

标签: node.js redis format

我想查看带有节点的电脑上的Redis信息,因此我使用node_redis并运行info功能:

var redis = require("redis"),
    client = redis.createClient();

client.on("connect", function () {
    client.info(function (err, replay) {
        console.log(replay);
    })
})

但响应是非格式化的:

`#Server\r\nredis_version:2.6.16\r\nredis_git_sha1:00000000\r\nredis_git_dirty:0\r\nredis_mode:standalone\r\nos:Linux 3.8.0-29-generic x86_64\r\narch_bits:64\r\nmultiplexing_api:epoll\r\ngcc_version:4.6.3\r\nprocess_id:2941\r\nrun_id:e60f261a6f4f6f081563a47961315eff6b1c005d\r\ntcp_port:6379\r\nuptime_in_seconds:1777\r\nuptime_in_days:0\r\nhz:10\r\nlru_clock:2040689\r\n\r\n# Clients\r\nconnected_clients:2\r\nclient_longest_output_list:0\r\nclient_biggest_input_buf:0\r\nblocked_clients:0\r\n\r\n# Memory\r\nused_memory:562584\r\nused_memory_human:549.40K\r\nused_memory_rss:2031616\r\nused_memory_peak:561784\r\nused_memory_peak_human:548.62K\r\nused_memory_lua:31744\r\nmem_fragmentation_ratio:3.61\r\nmem_allocator:jemalloc-3.2.0\r\n\r\n# Persistence\r\nloading:0\r\nrdb_changes_since_last_save:0\r\nrdb_bgsave_in_progress:0\r\nrdb_last_save_time:1383553917\r\nrdb_last_bgsave_status:ok\r\nrdb_last_bgsave_time_sec:-1\r\nrdb_current_bgsave_time_sec:-1\r\naof_enabled:0\r\naof_rewrite_in_progress:0\r\naof_rewrite_scheduled:0\r\naof_last_rewrite_time_sec:-1\r\naof_current_rewrite_time_sec:-1\r\naof_last_bgrewrite_status:ok\r\n\r\n# Stats\r\ntotal_connections_received:3\r\ntotal_commands_processed:5\r\ninstantaneous_ops_per_sec:0\r\nrejected_connections:0\r\nexpired_keys:0\r\nevicted_keys:0\r\nkeyspace_hits:0\r\nkeyspace_misses:0\r\npubsub_channels:0\r\npubsub_patterns:0\r\nlatest_fork_usec:0\r\n\r\n# Replication\r\nrole:master\r\nconnected_slaves:0\r\n\r\n# CPU\r\nused_cpu_sys:0.13\r\nused_cpu_user:0.19\r\nused_cpu_sys_children:0.00\r\nused_cpu_user_children:0.00\r\n\r\n# Keyspace\r\n'

如何将其转换为对象?像:

{
    redis_version:2.6.16,
    redis_git_sha1:00000000,
    redis_git_dirty:0,
    ......
}

这样我就可以阅读每个属性的值,获取我需要的信息

2 个答案:

答案 0 :(得分:1)

检查this问题。基本上,您需要编写一个简单的解析器,如第一个答案中提供的解析器。

答案 1 :(得分:0)

您可以使用node-redis-info

npm install redis-info

用法:

> var parser = require('redis-info');
undefined
> var info = parser.parse(redis_info_str);
undefined
> info.fields.redis_version
2.6.1
> info.startWith('pubsub')
[ [ 'pubsub_channels', '2' ],
  [ 'pubsub_patterns', '0' ] ]
> info.contains('memory')
[ [ 'used_memory', '15080416' ],
  [ 'used_memory_human', '14.38M' ],
  [ 'used_memory_rss', '21258240' ],
  [ 'used_memory_peak', '18985904' ],
  [ 'used_memory_peak_human', '18.11M' ] ]