python分析nagios状态文件status.dat

回复 收藏
官网对于status.dat的解释
/usr/local/nagios/var/status.dat –
This is the bread and butter file of all of the “live” information on the monitoring environment. This file gets updated every 10-20 seconds (as specified in nagios.cfg) with all current status information.
通过分析status.dat可以取也当前的服务、HOST等状态。
status.dat的大概参数如下

  1. ########################################
  2. #          NAGIOS STATUS FILE
  3. #
  4. # THIS FILE IS AUTOMATICALLY GENERATED
  5. # BY NAGIOS.  DO NOT MODIFY THIS FILE!
  6. ########################################
  7. info {
  8.         created=1413524014
  9.         version=3.4.1
  10.         last_update_check=0
  11.         update_available=0
  12.         last_version=3.0.1
  13.         new_version=
  14.         }
  15. programstatus {
  16.         modified_host_attributes=0
  17.         modified_service_attributes=0
  18.         nagios_pid=5531
  19.         daemon_mode=1
  20.         ...
  21.         }
  22. hoststatus {
  23.         host_name=192.168.2.172
  24.         modified_attributes=0
  25.         check_command=check-host-alive
  26.         check_period=24x7
  27.          ...
  28.         }
  29. servicestatus {
  30.         host_name=192.168.2.172
  31.         service_description=Current Load
  32.         modified_attributes=0
  33.         check_command=check_nrpe!check_load
  34.         check_period=24x7
  35.         ...
  36.         }
  37. contactstatus {
  38.         contact_name=kk
  39.         modified_attributes=0
  40.         modified_host_attributes=0
  41.         modified_service_attributes=0
  42.         ...
  43.         }
  44. servicecomment {
  45.         host_name=192.168.2.146
  46.         service_description=Current Load
  47.         entry_type=3
  48.         comment_id=1785
  49.         ...
  50.         }
python脚本内容如下:
[code]#!/usr/bin/python
#######add by kuang at 201410  parse status.dat
################## host_status
import os
import re

filename='/usr/local/perl/status_test.dat'
filename='/usr/local/perl/status.dat'



file=open(filename,'r')
line=file.readline()
list=[]
all_line=[]
while line:
        oline=line
        line=file.readline()
        if re.search(r'^#',oline):
                continue
        if oline.strip() != '':
                all_line.append(oline)

file.close()


##########################
####
#### get  status.dat to  list
####
##########################
#print (all_line)
temp=''.join(all_line)

tmp_list=temp.split('}')

def replace_s(s):
        t=re.sub(r"\s?\{","",s)
        t=re.sub(r"(\t+)","    ",t)
        t=re.sub(r"(\n+)","",t)
        return t
tmp_list=map(replace_s,tmp_list)


for s in tmp_list:
        if len(s)>0:
                t=re.sub(r"\s{2,}","#",s)
#               print t
                tmp=t.split('#')

                d={}
                d1={}
                info=''
                for t in tmp:
                        if not re.search('=',t):
                                info=t
                        else:
                                d1[t.split('=')[0]]=t.split('=')[1]

                                d[info]=d1
                list.append(d)
##########################
####
#### show list   , list is too long
####
##########################

#print list

##########################
####
#### show status
####
##########################


#####get all ip and down status

def getpam(list,i,info,pam):
        info=list[info][pam]
        return info
ok=0
down=0
i=0
message=''
#dict_IP={}
list_down=[]
while i
2014-11-02 13:51 举报
已邀请:

回复帖子,请先登录注册

退出全屏模式 全屏模式 回复
评分
  • 评分区间
  • 学分
  • -30 ~ 30
可选评分理由: