免费注册 查看新帖 |

Chinaunix

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

[文本处理] 如何将nagios监控项配置文件中注释掉特定的监控项,脚本批量执行 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-04-23 15:35 |只看该作者 |倒序浏览
因nagios中有很的的server,所以就有很多的配置文件,有的配置文件中有下面的监控项:

define service{
use aaa-service
service_description Database Usage Projection
normal_check_interval 120
check_command check_db_projection!60
notifications_enabled 1
}

有的配置文件中没有,现在因为这个监控项没有意义了,想把含有此监控项的配置文件注释掉。

结果应为下:

#define service{
# use aaa-service
# service_description Database Usage Projection
# normal_check_interval 120
# check_command check_db_projection!60
# notifications_enabled 1
# }
其他的监控均保持不变。
目前的方法是

comment=`awk 'BEGIN{RS=""} /db_projection/{print}' 100.cfg|sed 's/^/#/g'`
keep=`awk 'BEGIN{RS=""} ! /db_projection/{print}' 100.cfg`
echo -e "$keep\n\n$comment" >t.txt

非常麻烦,这还是处理的一个文件。

看是否有比较简单的方法

论坛徽章:
780
金牛座
日期:2014-02-26 17:49:58水瓶座
日期:2014-02-26 18:10:15白羊座
日期:2014-04-15 19:29:52寅虎
日期:2014-04-17 19:43:21酉鸡
日期:2014-04-19 21:24:10子鼠
日期:2014-04-22 13:55:24卯兔
日期:2014-04-22 14:20:58亥猪
日期:2014-04-22 16:13:09狮子座
日期:2014-05-05 22:31:17摩羯座
日期:2014-05-06 10:32:53处女座
日期:2014-05-12 09:23:11子鼠
日期:2014-05-21 18:21:27
2 [报告]
发表于 2014-04-23 15:42 |只看该作者
看你的代码,只要配置文件中含有 db_projection,
所有行都会被注释掉,而不仅仅是define service
这个块。

论坛徽章:
16
IT运维版块每日发帖之星
日期:2015-08-24 06:20:00综合交流区版块每日发帖之星
日期:2015-10-14 06:20:00IT运维版块每日发帖之星
日期:2015-10-25 06:20:00IT运维版块每日发帖之星
日期:2015-11-06 06:20:00IT运维版块每日发帖之星
日期:2015-12-10 06:20:00平安夜徽章
日期:2015-12-26 00:06:302016猴年福章徽章
日期:2016-02-18 15:30:34IT运维版块每日发帖之星
日期:2016-04-15 06:20:00IT运维版块每日发帖之星
日期:2016-05-21 06:20:00综合交流区版块每日发帖之星
日期:2016-08-16 06:20:002015七夕节徽章
日期:2015-08-21 11:06:17IT运维版块每日发帖之星
日期:2015-08-14 06:20:00
3 [报告]
发表于 2014-04-23 15:46 |只看该作者
回复 1# linuxandunix


    sed 的标签可以搞掂,匹配xx 追加,匹配}不追加,然后s/^/#/替换,如此循环。

论坛徽章:
780
金牛座
日期:2014-02-26 17:49:58水瓶座
日期:2014-02-26 18:10:15白羊座
日期:2014-04-15 19:29:52寅虎
日期:2014-04-17 19:43:21酉鸡
日期:2014-04-19 21:24:10子鼠
日期:2014-04-22 13:55:24卯兔
日期:2014-04-22 14:20:58亥猪
日期:2014-04-22 16:13:09狮子座
日期:2014-05-05 22:31:17摩羯座
日期:2014-05-06 10:32:53处女座
日期:2014-05-12 09:23:11子鼠
日期:2014-05-21 18:21:27
4 [报告]
发表于 2014-04-23 15:47 |只看该作者
回复 1# linuxandunix
个人觉得这样和你的做法等价
  1. grep -q "db_projection" i;[ $? -eq 0 ] && sed 's/^/#/g' i

  2. #define service{
  3. #use aaa-service
  4. #service_description Database Usage Projection
  5. #normal_check_interval 120
  6. #check_command check_db_projection!60
  7. #notifications_enabled 1
  8. #}
复制代码

论坛徽章:
0
5 [报告]
发表于 2014-04-23 15:47 |只看该作者
下面这个一整个模块。
define service{
use aaa-service
service_description Database Usage Projection
normal_check_interval 120
check_command check_db_projection!60
notifications_enabled 1
}

需要把整个这些都注释掉。 如何包含Database Usage Projection 或者 check_db_projection

论坛徽章:
0
6 [报告]
发表于 2014-04-23 15:49 |只看该作者
回复 4# Herowinter

感觉看起来 ,你这个更简单些哦。

   

论坛徽章:
0
7
发表于 2014-04-23 15:50
回复 3# expert1


怎么搞啊,求方法啊

   

论坛徽章:
0
8 [报告]
发表于 2014-04-23 15:54 |只看该作者
回复 3# expert1


    大神,求你的具体命令啊。。。。

论坛徽章:
16
IT运维版块每日发帖之星
日期:2015-08-24 06:20:00综合交流区版块每日发帖之星
日期:2015-10-14 06:20:00IT运维版块每日发帖之星
日期:2015-10-25 06:20:00IT运维版块每日发帖之星
日期:2015-11-06 06:20:00IT运维版块每日发帖之星
日期:2015-12-10 06:20:00平安夜徽章
日期:2015-12-26 00:06:302016猴年福章徽章
日期:2016-02-18 15:30:34IT运维版块每日发帖之星
日期:2016-04-15 06:20:00IT运维版块每日发帖之星
日期:2016-05-21 06:20:00综合交流区版块每日发帖之星
日期:2016-08-16 06:20:002015七夕节徽章
日期:2015-08-21 11:06:17IT运维版块每日发帖之星
日期:2015-08-14 06:20:00
9 [报告]
发表于 2014-04-23 16:49 |只看该作者
本帖最后由 expert1 于 2014-04-23 16:51 编辑

回复 8# linuxandunix


    还没人回答??? 那我来一发


  1. cat a

  2. define service{
  3. xxxxxxxxxxxxxxxx
  4. xxxxxxxxxxxxxxxxxxxxx
  5. xxxxxxxxxxxxx

  6. }

  7. define service{
  8. use aaa-service
  9. service_description Database Usage Projection
  10. normal_check_interval 120
  11. check_command check_db_projection!60
  12. notifications_enabled 1
  13. }

  14.   sed '/{/{:a;N;/}/!ba;/check_db_project/s/^\|\n/&#/g}' a
  15. define service{
  16. xxxxxxxxxxxxxxxx
  17. xxxxxxxxxxxxxxxxxxxxx
  18. xxxxxxxxxxxxx

  19. }

  20. #define service{
  21. # use aaa-service
  22. # service_description Database Usage Projection
  23. # normal_check_interval 120
  24. # check_command check_db_projection!60
  25. # notifications_enabled 1
  26. # }


  27. 或者

  28. awk -vRS='define service{[^}]+}' '{print RT~/check_db_projection/?"#"gensub(/\n/,"&#","g",RT):RT}'

复制代码

论坛徽章:
780
金牛座
日期:2014-02-26 17:49:58水瓶座
日期:2014-02-26 18:10:15白羊座
日期:2014-04-15 19:29:52寅虎
日期:2014-04-17 19:43:21酉鸡
日期:2014-04-19 21:24:10子鼠
日期:2014-04-22 13:55:24卯兔
日期:2014-04-22 14:20:58亥猪
日期:2014-04-22 16:13:09狮子座
日期:2014-05-05 22:31:17摩羯座
日期:2014-05-06 10:32:53处女座
日期:2014-05-12 09:23:11子鼠
日期:2014-05-21 18:21:27
10 [报告]
发表于 2014-04-23 16:51 |只看该作者
回复 9# expert1
前辈宝刀未老,风采依旧,佩服佩服。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP