redis-cli infoの結果をCSV形式で出力してみる


社内のプロジェクトで、Redisの統計情報(=redis info)を定期的に取得してグラフ化や解析を行う必要があり、これをCSV形式で出力できないかちょっと試してみたら、ワンライナーとしてはちょっと長いですが(ほとんどはCSVのヘッダレコードの出力・・・f^_^; )、意外と簡単にできました。

参考までにRedisの統計情報出力サンプルはこのとおり。

[user@localhost ~]$ redis-cli info
# Server
redis_version:3.2.10
redis_git_sha1:00000000
redis_git_dirty:0

/* 中略 */

repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

# CPU
used_cpu_sys:733.45
used_cpu_user:360.33
used_cpu_sys_children:0.00
used_cpu_user_children:0.00

# Cluster
cluster_enabled:0

# Keyspace
db0:keys=62,expires=31,avg_ttl=287947
[user@localhost ~]$

縦に並んでいたりコメント行や空行があったりで、これをこのままExcelとかで集計しようとするとちょっと大変ですね。
このワンライナーでは計測日時とホスト名を同時に出力しているので、これをExcelに取り込んでピポッドテーブルでごにょごにょするなど、後で比較解析も容易かと思います。

echo "time,server,redis_version,redis_git_sha1,redis_git_dirty,redis_build_id,redis_mode,os,arch_bits,multiplexing_api,gcc_version,process_id,run_id,tcp_port,uptime_in_seconds,uptime_in_days,hz,lru_clock,executable,config_file,connected_clients,client_longest_output_list,client_biggest_input_buf,blocked_clients,used_memory,used_memory_human,used_memory_rss,used_memory_rss_human,used_memory_peak,used_memory_peak_human,total_system_memory,total_system_memory_human,used_memory_lua,used_memory_lua_human,maxmemory,maxmemory_human,maxmemory_policy,mem_fragmentation_ratio,mem_allocator,loading,rdb_changes_since_last_save,rdb_bgsave_in_progress,rdb_last_save_time,rdb_last_bgsave_status,rdb_last_bgsave_time_sec,rdb_current_bgsave_time_sec,aof_enabled,aof_rewrite_in_progress,aof_rewrite_scheduled,aof_last_rewrite_time_sec,aof_current_rewrite_time_sec,aof_last_bgrewrite_status,aof_last_write_status,total_connections_received,total_commands_processed,instantaneous_ops_per_sec,total_net_input_bytes,total_net_output_bytes,instantaneous_input_kbps,instantaneous_output_kbps,rejected_connections,sync_full,sync_partial_ok,sync_partial_err,expired_keys,evicted_keys,keyspace_hits,keyspace_misses,pubsub_channels,pubsub_patterns,latest_fork_usec,migrate_cached_sockets,role,connected_slaves,master_repl_offset,repl_backlog_active,repl_backlog_size,repl_backlog_first_byte_offset,repl_backlog_histlen,used_cpu_sys,used_cpu_user,used_cpu_sys_children,used_cpu_user_children,cluster_enabled,db0.keys,db0.expires,db0.avg_ttl" > /tmp/redis-cli_info.log;while true;do date "+%Y/%m/%d %H:%M:%S" | tr '\n' ',' >> /tmp/redis-cli_info.log;hostname | tr '\n' ',' >> /tmp/redis-cli_info.log;redis-cli info | sed '/^#/d' | sed '/^\r$/d' | sed -e 's/^.*://g' | sed -e 's/,/\r\n/g' | sed -e 's/^.*=//g' | perl -pe 's/\r\n/,/g' | sed -e 's/,$/\n/' >> /tmp/redis-cli_info.csv;sleep 60;done

出力イメージはこんな感じです。

time,server,redis_version,redis_git_sha1,redis_git_dirty,redis_build_id,redis_mode,os,arch_bits,multiplexing_api,gcc_version,process_id,run_id,tcp_port,uptime_in_seconds,uptime_in_days,hz,lru_clock,executable,config_file,connected_clients,client_longest_output_list,client_biggest_input_buf,blocked_clients,used_memory,used_memory_human,used_memory_rss,used_memory_rss_human,used_memory_peak,used_memory_peak_human,total_system_memory,total_system_memory_human,used_memory_lua,used_memory_lua_human,maxmemory,maxmemory_human,maxmemory_policy,mem_fragmentation_ratio,mem_allocator,loading,rdb_changes_since_last_save,rdb_bgsave_in_progress,rdb_last_save_time,rdb_last_bgsave_status,rdb_last_bgsave_time_sec,rdb_current_bgsave_time_sec,aof_enabled,aof_rewrite_in_progress,aof_rewrite_scheduled,aof_last_rewrite_time_sec,aof_current_rewrite_time_sec,aof_last_bgrewrite_status,aof_last_write_status,total_connections_received,total_commands_processed,instantaneous_ops_per_sec,total_net_input_bytes,total_net_output_bytes,instantaneous_input_kbps,instantaneous_output_kbps,rejected_connections,sync_full,sync_partial_ok,sync_partial_err,expired_keys,evicted_keys,keyspace_hits,keyspace_misses,pubsub_channels,pubsub_patterns,latest_fork_usec,migrate_cached_sockets,role,connected_slaves,master_repl_offset,repl_backlog_active,repl_backlog_size,repl_backlog_first_byte_offset,repl_backlog_histlen,used_cpu_sys,used_cpu_user,used_cpu_sys_children,used_cpu_user_children,cluster_enabled,db0.keys,db0.expires,db0.avg_ttl
2018/11/16 09:40:53,myhost,3.2.10,00000000,0,f83a0de63af68303,standalone,Linux 2.6.32-696.10.1.el6.x86_64 x86_64,64,epoll,4.4.7,2444,0316e81b3eb8a32010cdbb49064c273d7a988006,6379,664275,7,10,15602197,/usr/bin/redis-server,/etc/redis.conf,1,0,0,0,819832,800.62K,4718592,4.50M,1178048,1.12M,1968099328,1.83G,37888,37.00K,0,0B,noeviction,5.76,jemalloc-3.6.0,0,164225,0,1541664578,ok,-1,-1,0,0,0,-1,-1,ok,ok,159222,466116,0,34130604,6744173,0.01,0.00,0,0,0,0,30526,0,36572,30591,0,0,0,0,master,0,0,0,1048576,0,0,449.98,220.89,0.00,0.00,0,31,0,0
2018/11/16 09:41:53,myhost,3.2.10,00000000,0,f83a0de63af68303,standalone,Linux 2.6.32-696.10.1.el6.x86_64 x86_64,64,epoll,4.4.7,2444,0316e81b3eb8a32010cdbb49064c273d7a988006,6379,664335,7,10,15602257,/usr/bin/redis-server,/etc/redis.conf,1,0,0,0,819832,800.62K,4718592,4.50M,1178048,1.12M,1968099328,1.83G,37888,37.00K,0,0B,noeviction,5.76,jemalloc-3.6.0,0,164225,0,1541664578,ok,-1,-1,0,0,0,-1,-1,ok,ok,159237,466129,0,34130786,6746472,0.01,0.00,0,0,0,0,30526,0,36572,30591,0,0,0,0,master,0,0,0,1048576,0,0,450.01,220.91,0.00,0.00,0,31,0,0
2018/11/16 09:42:53,myhost,3.2.10,00000000,0,f83a0de63af68303,standalone,Linux 2.6.32-696.10.1.el6.x86_64 x86_64,64,epoll,4.4.7,2444,0316e81b3eb8a32010cdbb49064c273d7a988006,6379,664395,7,10,15602317,/usr/bin/redis-server,/etc/redis.conf,1,0,0,0,819832,800.62K,4718592,4.50M,1178048,1.12M,1968099328,1.83G,37888,37.00K,0,0B,noeviction,5.76,jemalloc-3.6.0,0,164225,0,1541664578,ok,-1,-1,0,0,0,-1,-1,ok,ok,159252,466142,0,34130968,6748771,0.01,0.00,0,0,0,0,30526,0,36572,30591,0,0,0,0,master,0,0,0,1048576,0,0,450.05,220.92,0.00,0.00,0,31,0,0