网站新手,希望我的问题不会遗漏许多细节或太模糊。基本上我正在运行netbackup脚本,以创建一个新文件:
subprocess.call('/usr/openv/netbackup/bin/goodies/available_media > /var/log/nbu/available_media' + adate, shell=True)
我想将其格式化并使用out.write将其输入到另一个文件中,该文件指定池名称,例如“Auth Pool”,“Web Pool”等。使用count来查找有多少磁带处于状态每个游泳池都有“可用”“全”“冷冻”。
#Pool #Full #Avail #Frozen
AUTH 100 5 23
到目前为止我所管理的所有内容如下:
from datetime import datetime
import subprocess
import os
import sys
date = datetime.now()
adate = '%s%s%s' % (date.year, date.month, date.day)
subprocess.call('/usr/openv/netbackup/bin/goodies/available_media > /var/log/nbu/available_media' + adate, shell=True)
log = open('/var/log/nbu/available_media' + adate, 'r')
text = log.read()
auth = text.split('Auth_Offsite', 1)[0]
notapes = auth.count('AVAILABLE')
目前我的知识限制我只使用拆分计算第一个池的可用磁带。我是python的新手所以请温柔:P 非常感谢所有帮助。
答案 0 :(得分:0)
假设您的日志文件是制表符分隔的,那么这是一个快速而又脏的应该粗略地做你想要的。如果你是python的新手,有些功能对你来说可能不熟悉,但它应该会给你一些关于你可能想要谷歌的想法。
log = open('/var/log/nbu/available_media' + adate, 'r')
# Splits on newline chars, skips the titles and ----- lines
lines = log.readlines()[2:]
# Break up each line by field
lines = [line.strip().split('\t') for line in lines]
# Going to form a dictionary mapping pool names to dictionaries of tape state counts
states_dict = {}
all_states = set()
for line in lines:
if len(line) == 1:
current_pool = line[0]
states_dict[current_pool] = {}
elif len(line) > 1:
state = line[-1]
states_dict[current_pool][state] = states_dict[current_pool].setdefault(state, 0) + 1
all_states.add(state)
all_states = sorted(list(all_states))
# write output
with open('output_file_name.txt','wb') as outfile:
outfile.write('Pool\t' + '\t'.join(all_states) + '\n')
for pool in states_dict:
outfile.write(pool)
for state in all_states:
outfile.write('\t' + states_dict[pool].setdefault(state, 0))
outfile.write('\n')
答案 1 :(得分:0)
使用PYTHON
#!/bin/python
from __future__ import print_function
import re
header_type=None
my_file=open("/usr/openv/netbackup/bin/goodies/available_media","r")
results={"Auth pool":[],"Web pool":[],"Support pool":[]}
for line in my_file:
if re.search("^ *Auth pool *$",line):
header_type=re.search("^ *Auth pool *$",line).group()
if re.search("^ *Web pool *$",line):
header_type=re.search("^ *Web pool *$",line).group()
if re.search("^ *Support pool *$",line):
header_type=re.search("^ *Support pool *$",line).group()
if header_type in results:
tmp=line.split()
if len(tmp)>=8 and tmp[8] in ["FULL","FROZEN","AVAILABLE"]:
results[header_type].append(tmp[8])
new_file=open("/var/log/nbu/available_media","w")
new_file.write("#pool\t\t#full\t#Avail#Frozen\n")
for i in results:
new_file.write(i+"\t")
for j in ["FULL","FROZEN","AVAILABLE"]:
counts=len([state_type for state_type in results[i] if state_type==j])
new_file.write(str(counts)+"\t")
new_file.write("\n")
new_file.close()
因为这个问题是bash下的标签(我正在使用你的linux操作系统上有awk脚本语言)
使用AWK
创建文件名awkscript
并放置以下代码
#!/bin/awk
BEGIN{OFS="\t\t";print("pool","full","frozen","avail")}{
if($0~/Auth pool/){
full=frozen=avail=0;
while(!($0~/Web pool|Support pool/) && getline){
if($9=="FULL"){full++;};if($9=="FROZEN"){frozen++};
if($9=="AVAILABLE"){avail++}
};
print("Auth pool",full,frozen,avail)
}
if($0~/Web pool/){
full=frozen=avail=0;
while(!($0~/Auth pool|Support pool/) && getline){
if($9=="FULL"){full++;};if($9=="FROZEN"){frozen++};
if($9=="AVAILABLE"){avail++}
};
print("Web pool",full,frozen,avail)
}
if($0~/Support pool/){
full=frozen=avail=0;
while(!($0~/Auth pool|Web pool/) && getline){
if($9=="FULL"){full++;};if($9=="FROZEN"){frozen++};
if($9=="AVAILABLE"){avail++}
};
print("Support pool",full,frozen,avail)
}
}
在bash shell中运行脚本
awk -f <path to awkscript> '/usr/openv/netbackup/bin/goodies/available_media'
注意这将在壳中打印结果
您只需将输出重定向到文件
即可awk -f 'path to awkscript' '/usr/openv/netbackup/bin/goodies/available_media' > new_file