|
前阵子,遇到需要把nagios监控的服务统计到表格的需求,想着如果每一次改动,都要去维护表格的话,难免会有疏漏或者问题,于是观察了nagios的host.cfg,services.cfg等配置文件写了一个脚本,包含对多个主机关联到同一个服务的处理等,脚本也有局限性,就是需要把每个主机,都归类到某个组,比如Linux主机归类到Linux组,实际环境,一般也是有这样做归类的,脚本运行过程中会有文件产生,但都很小,以下是相应模块安装和脚本文件,建议在测试机上面跑完再到实际环境跑。 一,安装xlsxwriter
一个excel操作模块,可以操作多个工作表的文字、数字、公式、图表等。
pip安装方法:
1
| #pip install XlsxWriter
|
源码安装方法(需下载)
1
| #tar -zxvf XlsxWriter-master.tar.gz
#cd XlsxWriter-master
#python setup.py install
|
二,脚本文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
| import xlsxwriter
import sys
reload(sys)
sys.setdefaultencoding('utf8')
#注意路径
os.system("cat /usr/local/nagios/etc/hosts.cfg | grep -E 'host_name|address|hostgroups' | \
sed s/[[:space:]]// | awk '{print $1,$2,$3}' | sed -e 's/[[:space:]]$//g' > /root/autocount/hostip")
#####################处理一行多个host_name情况########################
os.system("cat /usr/local/nagios/etc/services.cfg | grep -E 'host_name|service_description|contacts'|sed s/[[:space:]]// | \
awk '{print $1,$2,$3,$4,$5,$6}' | sed -e 's/[[:space:]]*$//g' | sed -e 's/,[[:space:]]/,/g' > /root/autocount/serviceip2")
#含有完整行的文件,及host_name都有的
os.system("cat /usr/local/nagios/etc/services.cfg | grep 'host_name' | sed s/[[:space:]]// | awk '{print $2,$3,$4,$5,$6}' | sed s/[[:space:]]//g | awk '/,/{print $0}' > /root/autocount/serviceip1")
os.system("sort -u /root/autocount/serviceip1 > /root/autocount/serviceip3")
#去除重复行
with open("/root/autocount/serviceip3") as f3:
#a中保存有一行多个host_name的主机
a=f3.readlines()
with open("/root/autocount/serviceip2") as f4:
b=f4.readlines()
j=0
for i in a:
k = i.strip('\n').split(',')
l=3
while j < len(k):
print k[j]
for m in b:
if k[j] in m :
#b中的某一行含有一行多主机中的一个主机
print k[j]
if i in m :
b.insert(len(b),'host_name '+k[j]+'\n')
l=0
#插入到最后,然后进入下一次的for m in b,即往后面继续for了,就到下面的那个service_description,设置l是把最近的service_description同contacts加入到b的后面,而不是所有。
if 'service_description' in m and l <= 2:
print m
#此时的len(b)是加入了一个host_name之后的,所以会继续往后insert
b.insert(len(b),m)
if 'contacts' in m and l <= 2:
print m
b.insert(len(b),m)
l+=1
j+=1
l=3
j=0
testfile = open("/root/autocount/testfile",'w')
for bb in b:
print >> testfile,bb,
testfile.close()
#此时testfile中还是含有长主机名的,即多个主机在配置文件中显示为同一行的(关联服务)
jiji=0
for i in a:
try:
while True:
ind = b.index("host_name "+i)
b.pop(ind)
b.pop(ind)
b.pop(ind)
print "###############"
jiji+=1
except:
continue
#利用a中先前存的类似host1,host2,host3这样关联的主机,从b中剔除。
print "jiji is %s " % jiji
finall = open("/root/autocount/serviceip",'w')
for kk in b:
print >> finall,kk,
finall.close()
#关闭保存文件,现在就是不存在类似多个主机名的行了。
file1="/root/autocount/hostip"
file2="/root/autocount/serviceip"
f = open("/root/autocount/joinout",'w')
final = open("/root/autocount/final",'w')
f1=file(file1,"r")
j=0
totalhost=0
for i in f1.readlines():
totalhost+=1
print >> f,i,
f2=file(file2,"r")
m = 2
for k in f2.readlines():
if i == k : #先判断是否主机名相等,要不如果serviceip文件服务无联系人,就会出现错乱
print "testtesttest"
m = 0
continue
elif m < 2 and ('host_name' not in k):
print >> f,k,
m+=1
else:
m = 2
continue
f1.close()
f2.close()
f.close()
totalhost=totalhost/3
with open("/root/autocount/joinout") as f:
a=f.readlines()
linen=0
con=[]
totalservice=0
for i in a:
if 'service_description' in i:
totalservice+=1
con.append(linen)
linen+=1
else:
linen+=1
print con
m=1
#处理一些没有contacts的情况
for j in con:
if 'contacts' not in a[j+m]:
a.insert(j+m,'contacts none\n')
m+=1
for x in a:
print >> final,x,
title = [u'序列号',u'host',u'ip',u'service',u'联系人',u'所属部门']
workbook = xlsxwriter.Workbook('Nagios监控信息统计.xlsx')
worksheet = workbook.add_worksheet()
worksheet.set_column('A:C',30)
worksheet.set_column('D:F',30)
worksheet.set_column('H:I',10)
#worksheet.set_row(3,30)
#worksheet.set_row(6,30)
#worksheet.set_row(7,30)
merge_format = workbook.add_format({
'bold': 1,
'border': 1,
'align': 'center',
'valign': 'vcenter',
'fg_color': '#CC0033'})
merge_format1 = workbook.add_format({
'bold': 1,
'border': 1,
'align': 'center',
'valign': 'vcenter',
'fg_color': '#D7E4BC'})
bold = workbook.add_format({
'bold': 1,
'border': 1,
'align': 'center',
'valign': 'vcenter'})
merge_format2 = workbook.add_format({
# 'bold': 1,
'border': 1,
'align': 'center',
'valign': 'vcenter'})
format_title=workbook.add_format()
format_title.set_border(1)
format_title.set_bg_color('#cccccc')
format_title.set_align('center')
worksheet.merge_range('A1:F1','Nagios监控信息',merge_format)
worksheet.merge_range('H1:I1','主机总计 服务总计',merge_format1)
worksheet.write('H2',totalhost,bold)
worksheet.write('I2',totalservice,bold)
worksheet.write_row('A2',title,format_title)
#######################################
service_list=[]
count = 0
slist_end = 0
initline=3
def writetoexecl(numb,host,ip,group,service_list):
global initline
global slist_end
slist_len=str(len(service_list))
initl=str(initline)
slist_end = int(initl) + int(slist_len) - 1
slist_end1=str(slist_end)
#一行的就调用write方法
if initl == slist_end1:
worksheet.write('A'+initl,numb,merge_format2)
worksheet.write('B'+initl,host,merge_format2)
worksheet.write('C'+initl,ip,merge_format2)
worksheet.write('F'+initl,group,merge_format2)
else:
worksheet.merge_range('A'+initl+':'+'A'+slist_end1,numb,merge_format2)
worksheet.merge_range('B'+initl+':'+'B'+slist_end1,host,merge_format2)
worksheet.merge_range('C'+initl+':'+'C'+slist_end1,ip,merge_format2)
worksheet.merge_range('F'+initl+':'+'F'+slist_end1,group,merge_format2)
servicell = initl
# 输出服务和联系人
for element in service_list:
print '++++++++'
service1=''.join(element.keys())
contacts1=''.join(element.values())
worksheet.write('D'+servicell,service1,merge_format2)
worksheet.write('E'+servicell,contacts1,merge_format2)
servicell = str(int(servicell) + 1)
print '++++++++'
#worksheet.merge_range('A50:A60',num,merge_format)
#print initl
#print slist_end1
initline = int(initl) + int(slist_len)
for i in a:
service_dict={}
#定义字典存放服务和联系人
if 'host_name' in i:
host=i.split()[1]
print host
if 'service_description' in i:
key=i.split()[1]
service_dict[key]=''
if 'contacts' in i:
service_dict[key]=i.split()[1]
#print service_dict
service_list.append(service_dict)
#print service_list
if 'address' in i:
ip = i.split()[1]
print ip
if 'hostgroups' in i:
groups = i.split()[1]
print service_list
print groups
count += 1
writetoexecl(count,host,ip,groups,service_list)
service_list=[]
continue
workbook.close()
|
统计的效果如下:
|
|