Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1730937
  • 博文数量: 150
  • 博客积分: 660
  • 博客等级: 上士
  • 技术积分: 2480
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-08 11:39
文章分类

全部博文(150)

文章存档

2019年(4)

2018年(36)

2017年(53)

2016年(7)

2015年(3)

2014年(3)

2013年(27)

2012年(2)

2011年(1)

2006年(1)

2005年(13)

分类: 系统运维

2018-11-05 13:41:27

利用zabbix开展自动化运维的一些探索之---zabbix remote command应用自动删除过期日志

摘要

  • 目的

    降低运维工作中的重复性、简单性的手工操作,降低人为的失误,提升运维的自动化水平。 日常运维工作中经常遇到磁盘空间已满的问题,每次都手工操作的话比较费时和耗力,且没有太多的技术含量。因此决定将该工作通过zabbix系统的触发器和remote command功能相结合进行处理,实现自动化的工作流程。

  • 方法

    通过zabbix提供的tigger和remote command功能组合来执行,首先指定一个阈值,作为触发器触发的依据,然后zabbix server在监测到磁盘可用空间低于80%时触发该remote command动作的执行,remotecommand调用自定义的清理脚本执行清理磁盘空间的工作。

  • 结果

    通过在某服务器上进行测试和验证,该服务器上磁盘达可用空间低于80%时,触发remote command,自动执行了清理脚本,清理完成后可用磁盘空间大于80%。

  • 结论

    利用remote command功能,可以处理很多类似场景,例如服务意外宕机,如果超过一定时间无人处理,则自动调用remote command进行重启。这样就能将很多需要手工处理的工作转换为自动化处理。

实施说明

客户端

  • zabbix_agentd.conf 配置 必须有

EnableRemoteCommands=1

修改完配置需要重启agent
  • 自定义的清理脚本 示例

脚本存放位置 /etc/zabbix/scripts

cat rm_logs.sh
#!/bin/sh
find /xxx/tomcat-*/logs/ -type f  -name "*.log*" -mtime +30 |xargs rm -f
find /xxx/tomcat-*/logs/ -type f  -name "*.out*" -mtime +30 |xargs rm -f
find /xxx/tomcat-*/logs/ -type f  -name "*.log"  -mtime +3 -size +10000M |xargs rm -f

服务端

  • 创建一个action

    Action页面

    Name:rm logs  # 名称
    
    Type of calculation:A and B and C   #描述下面条件的关系
    
    Conditions
    
    Label   Name    Action
    A   Maintenance status not in maintenance   # 非维护状态 
    B   Trigger name like Free disk space is less than 20% on volume    #被监测的触发器名字
    C   Host = web02                                  # 主机筛选条件
    
    Enabled

    Opertions页面

    Default operation step duration:1h      #默认操作步骤持续时间
    
    Default subject:Problem: {TRIGGER.NAME} auto rm logs command will execute in 2 minutes                    #默认主题,需要自定义
    
    Default message:                        #可以自定义
    
    Problem started at {EVENT.TIME} on {EVENT.DATE}
    Problem name: {TRIGGER.NAME}
    Host: {HOST.NAME}
    Severity: {TRIGGER.SEVERITY}
    
    Original problem ID: {EVENT.ID}
    {TRIGGER.URL}
    
    Pause operations while in maintenance    #维护期间是否暂停执行
    
    Operations:                                  #自定义两步操作                   
    Steps   Details Start in    Duration    Action
    1   Send message to users: Li Hui (Hui Li) via Goluk opsbot
    Immediately 120         #第一步,立即发送邮件通知某磁盘低于80%可用空间,即将执行自动清理脚本
    
    2   Run remote commands on current host 
    00:02:00    120            #第二步,执行清理脚本

    第一步Operation details

    Steps : 1 - 1                    
    
    Step duration : 120     
    
    Operation type : Send message
    
    Send to User groups : ops
    Send to Users       : ops
    Send only to        : email
    
    Default message : default 

    第二步Operation details

    Steps :2 - 2
    
    Step duration : 120
    
    Operation type : Remote command
    
    Target list : Current host  
    
    Type : Custom script
    
    Execute on : Zabbix agent
    
    Commands : /bin/sh /etc/zabbix/scripts/rm_logs.sh
  • 忽略的细节

    • 上述示例中的trigger name 

      一般的linux主机都会加上Temmplate OS Linux模板,该模板会有 "Free disk space is less than 20% on volume {#FSNAME}" 这样的trigger

    • 上述的zabbix agent 以及 server 默认是已经配置完成,正常运行状态

结果及验证

可以通过手工增加某个磁盘上的日志达到80%阈值进行测试,测试结果可以看在problem里面会有:“ x年x月x日x时x分 remote command executed”的字样。

阅读(2200) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~