【问题标题】:Raid alert catch python script need to have Nagios logic addedRaid alert catch python脚本需要添加Nagios逻辑
【发布时间】:2021-05-27 21:37:50
【问题描述】:

谁能帮助我如何添加 Nagios 逻辑来捕捉警报到我下面的 python 脚本?

我尝试为所有 OK 和 CRITICAL 添加 sys.exit(0) 和 sys.exit(1),或者请让我知道我应该做什么,以便该脚本在运行 Nagios 时捕获 0、1、2并显示消息。

#!/usr/bin/python
import subprocess
import os, sys


#Check python present or not
#  dnf install python3.6-stack
# export PATH=/opt/python-3.6/bin:$PATH

def check_MegaRaid():
   # Next script
   failed=subprocess.run(["sudo /opt/MegaRAID/MegaCli/MegaCli64 -AdpAllInfo \ -aALL | grep -i 'Failed Disks' | awk -F':' '{print $2}'"], shell=True, stdout=subprocess.PIPE, universal_newlines=True)
   failed_status = failed.stdout
   print("failed_status is",failed_status)
   critical=subprocess.run(["sudo /opt/MegaRAID/MegaCli/MegaCli64 -AdpAllInfo \ -aALL | grep -i 'Critical Disks' | awk -F':' '{print $2}'"], shell=True, stdout=subprocess.PIPE, universal_newlines=True)
   critical_status = critical.stdout
   print("critical_status is",critical_status)

   if failed_status.strip() and critical_status.strip() == "0" :
       print("Raid check all OK" )
       sys.exit(0)
       #return 0

   else:
       print("CRITICAL")
       sys.exit(1)
       #return 1




def check_raid():
   process=subprocess.run(["sudo /sbin/mdadm --detail /dev/md127 | grep -i state | grep -w clean, | awk -F',' '{print $2}' |sed -e 's/^[ \t]*//' "], shell=True, stdout=subprocess.PIPE, universal_newlines=True)
   output = process.stdout
   check_process=subprocess.run(["sudo /sbin/mdadm --detail /dev/md127 | grep -i state | awk -F':' '{print $2}' |sed -e 's/^[ \t]*//' "], shell=True, stdout=subprocess.PIPE, universal_newlines=True)
   check = check_process.stdout
   if output.strip() == 'degraded':
       print("Raid disk state is CRITICAL ",output)
       #return 1
       sys.exit(1)

       
   elif check.strip() == 'clean':
       print("Raid check all OK")
       #return 0
       sys.exit(0)
   else:
       print("sudo /sbin/mdadm --detail /dev/md127 cmd not found : This  is an dataraid machine")
       check_MegaRaid()

#Check whether system configure raid
process=subprocess.run(["sudo cat /GEO_VERSION | grep -i raid | awk -F'Layout:' '{print $2}' | sed 's/[0-9]*//g' | sed -e 's/^[ \t]*//'"], shell=True, stdout=subprocess.PIPE, universal_newlines=True)
raid_value = process.stdout

if raid_value.strip() == 'raid':
   print("System configure Raid functions")
   check_raid()
else:
   print("There is no raid configured in this system")
   exit()

【问题讨论】:

    标签: python python-3.x nagios nagiosxi


    【解决方案1】:

    如果您有兴趣,请参考https://nagios-plugins.org/doc/guidelines.html

    0 没问题 1 是警告 2 是关键 3 未知

    因此,您需要做的第一件事是将您的 sys.exit(1) 替换为 sys.exit(2)

    我还将最后一个 exit() 替换为 sys.exit(3) 以表明它是一个未知的退出,这将帮助您识别 UI 中配置错误的服务。

    您还需要先指示状态,典型的单行插件输出如下所示:

    STATUS: message | perfdata
    

    但您似乎没有使用性能数据,因此请将您的关键出口更改为以字符 CRITICAL: 开头,而您的 OK 状态则以 OK: 开头。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 2016-04-08
      • 1970-01-01
      • 2017-07-16
      • 2023-01-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多