免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1596 | 回复: 4
打印 上一主题 下一主题

[求解] 如何在mysql cluster使用awk&sed格式化状态列表 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-03-01 15:48 |只看该作者 |倒序浏览
[root@spocproxy ~]# ndb_mgm -e show
Connected to Management Server at: 192.168.2.4:1186
Cluster Configuration
---------------------
[ndbd(NDB)] 2 node(s)
id=2    @192.168.2.4  (mysql-5.1.56 ndb-7.1.17, Nodegroup: 0, Master)
id=3    @192.168.2.5  (mysql-5.1.56 ndb-7.1.17, starting, Nodegroup: 0)

[ndb_mgmd(MGM)] 1 node(s)
id=1    @192.168.2.4  (mysql-5.1.56 ndb-7.1.17)

[mysqld(API)]   4 node(s)
id=100  @192.168.2.4  (mysql-5.1.56 ndb-7.1.17)
id=101  @192.168.2.5  (mysql-5.1.56 ndb-7.1.17)
id=102 (not connected, accepting connect from any host)
id=103 (not connected, accepting connect from any host)

将以上输出格式化成下面格式
[
{"type":"ndbd","id":"2","hostname":"192.168.2.4"},
{"type":"ndbd","id":"3","hostname":"192.168.2.5"},
{"type":"ndb_mgmd","id":"1","hostname":"192.168.2.4"},
{"type":"mysqld","id":"100","hostname":"192.168.2.4"},
{"type":"mysqld","id":"101","hostname":"192.168.2.5"},
]

我现在的方法是:

ndb_mgm -c $connectstring -e show |sed -e '/mysqld(API)/,$!d' -e '/@[0-9].*/!d' -e 's/id=//;s/@//' |awk  'BEGIN {print "["};{printf ("{\"type\":\"mysqld\",\"id\":\"%s\",\"hostname\":\"%s\"},\n",$1,$2)};END {print "]"}'

ndb_mgm -c $connectstring -e show |sed -e '/ndbd(NDB)/,/ndb_mgmd/!d' -e '/@[0-9].*/!d' -e 's/id=//;s/@//'|awk  'BEGIN {print "["};{printf ("{\"type\":\"ndbd\",\"id\":\"%s\",\"hostname\":\"%s\"},\n",$1,$2)};END {print "]"}'

ndb_mgm -c $connectstring -e show |sed -e '/ndb_mgmd/,/mysqld(API)/!d' -e '/@[0-9].*/!d' -e 's/id=//;s/@//' |awk  'BEGIN {print "["};{printf ("{\"type\":\"ndb_mgmd\",\"id\":\"%s\",\"hostname\":\"%s\"},\n",$1,$2)};END {print "]"}'
分成三次去格式化,得到的结果是:
[
{"type":"ndbd","id":"2","hostname":"192.168.2.4"},
{"type":"ndbd","id":"3","hostname":"192.168.2.5"},
]
[
{"type":"ndb_mgmd","id":"1","hostname":"192.168.2.4"},
]
[
{"type":"mysqld","id":"100","hostname":"192.168.2.4"},
{"type":"mysqld","id":"101","hostname":"192.168.2.5"},
]

问题是如何通过一行命令得到我想要的结果
[
{"type":"ndbd","id":"2","hostname":"192.168.2.4"},
{"type":"ndbd","id":"3","hostname":"192.168.2.5"},
{"type":"ndb_mgmd","id":"1","hostname":"192.168.2.4"},
{"type":"mysqld","id":"100","hostname":"192.168.2.4"},
{"type":"mysqld","id":"101","hostname":"192.168.2.5"},
]

不使用shell脚本. 有兴趣的朋友可以研究下.

论坛徽章:
0
2 [报告]
发表于 2012-03-01 16:45 |只看该作者
  1. awk '/^\[/{type=$1;next}/\@/{split(type,a,"[\(|\[]");gsub(/id=/,"",$1);gsub(/@/,"",$2);print a[2],$1,$2}' data

  2. 输出格式你自己再整一下吧
复制代码

论坛徽章:
0
3 [报告]
发表于 2012-03-01 17:24 |只看该作者
佩服佩服,能帮忙解释下么

论坛徽章:
145
技术图书徽章
日期:2013-10-01 15:32:13戌狗
日期:2013-10-25 13:31:35金牛座
日期:2013-11-04 16:22:07子鼠
日期:2013-11-18 18:48:57白羊座
日期:2013-11-29 10:09:11狮子座
日期:2013-12-12 09:57:42白羊座
日期:2013-12-24 16:24:46辰龙
日期:2014-01-08 15:26:12技术图书徽章
日期:2014-01-17 13:24:40巳蛇
日期:2014-02-18 14:32:59未羊
日期:2014-02-20 14:12:13白羊座
日期:2014-02-26 12:06:59
4 [报告]
发表于 2012-03-01 18:09 |只看该作者
回复 1# kevinbin

t for type

$ awk -F'[]() @=[]+' 'BEGIN{print "["}/^\[/{t=$2}/^id=/&&$3~/[0-9]/{print "{\"type\":\""t"\",\"id\":\""$2"\",\"hostname\":\""$3"\"},"}END{print "]"}' ndb_mgm.txt
[
{"type":"ndbd","id":"2","hostname":"192.168.2.4"},
{"type":"ndbd","id":"3","hostname":"192.168.2.5"},
{"type":"ndb_mgmd","id":"1","hostname":"192.168.2.4"},
{"type":"mysqld","id":"100","hostname":"192.168.2.4"},
{"type":"mysqld","id":"101","hostname":"192.168.2.5"},
]

   

论坛徽章:
0
5 [报告]
发表于 2012-03-01 19:51 |只看该作者
  1. sed -rn '/\[([^(]+).+$/{s//{"type:","\1",/;h;:a;g;N;s/^([^\n]+)\nid=([0-9]+) +@([^ ]+).+$/\1"id:","\2","hostname:","\3"},/p;ta}'
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP