一、自动报表前期工作:

  需要安装XlsxWriter模块,可以从github上下载以后解压、setup.py install。

二、程序:

#!/usr/bin/env python
# -*- coding=utf8 -*- import os
import re
import xlsxwriter
import sys
import collections # salt 172.18.144.69 cmd.run "salt-call hdfs_capacity.hdfs_cap decare"
def check_capacity():
value_dicts=collections.OrderedDict()
ns_list=str(os.popen("cat /software/servers/hadoop-2.7.1/etc/hadoop/hdfs-site.xml|grep -A 1 dfs.nameservices|grep -v dfs.nameservices| awk -F '>|<' '{print $3}'").read().strip().split("\n")).strip("['']").split(",")
for ns in ns_list:
result=os.popen("su - hadp -c 'hadoop dfs -count -q hdfs://%s/user/*'"%ns).read()
lists=[]
for line in result.strip().split("\n"):
temp_list = [i for i in re.split('\|+',line.strip().replace(" ","|")) if i !=' ']
temp_value = str(int(temp_list[6])/1024/1024/1024)
temp_list[6] = temp_value
lists.append(temp_list[4:])
value_dicts[ns]=sorted(lists, key=lambda temp:int(temp[2]),reverse=True)
return value_dicts def xml(value_dicts,cluster_name):
workbook = xlsxwriter.Workbook ('/tmp/%s_hdfs.xlsx'%cluster_name)
worksheet = workbook.add_worksheet ()
worksheet.set_column ('E:E', 40)
worksheet.set_column ('B:D', 15)
worksheet.set_row(0,20)
#设置格式
header_format = workbook.add_format ({'bold': True,'bg_color':'#7CCD7C','border':1,'align': 'center','valign': 'vcenter'})
value_format = workbook.add_format ({'border':1})
menu_format = workbook.add_format ({'bold': True,'border':1,'bg_color':'red','align': 'center','valign': 'vcenter'})
worksheet.write ('A1', u'ns',header_format)
worksheet.write ('B1', u'目录个数',header_format)
worksheet.write ('C1', u'文件个数',header_format)
worksheet.write ('D1', u'容量(单位GB)',header_format)
worksheet.write ('E1', u'用户目录',header_format)
ind_num=2
num=0
for index,keys in enumerate(value_dicts.keys()):
ind_num+=int(num)
num=0
for indexs,value in enumerate(value_dicts[keys]):
worksheet.write ('B%s'%str(int(indexs)+ind_num), value[0],value_format)
worksheet.write ('C%s'%str(int(indexs)+ind_num), value[1],value_format)
worksheet.write ('D%s'%str(int(indexs)+ind_num), value[2],value_format)
worksheet.write ('E%s'%str(int(indexs)+ind_num), value[3],value_format)
num=int(indexs)+1
if num-1 == 0:
worksheet.write ('A%s'%str(ind_num), keys,menu_format)
else:
#合并单元格
worksheet.merge_range(ind_num-1,0, ind_num+num-2,0,keys,menu_format)
workbook.close() def hdfs_cap(cluster_name):
try:
xml(check_capacity(),cluster_name)
except Exception,e:
return False
else:
return True if __name__ == "__main__":
cluster_name=sys.argv[1]
xml(check_capacity(),cluster_name)

三、后续:

  考虑到本次程序是单一的,以后可以考虑到放到django中,通过salt-api来调用,并从页面出口。

最新文章

  1. 探索ASP.NET MVC5系列之~~~2.视图篇(上)---包含XSS防御和异步分部视图的处理
  2. Oracle 在线重定义表分区
  3. div隐藏
  4. Testlink中分析结果的图表显示乱码
  5. web服务器、应用服务器、http服务器区别
  6. 0ffice365 Calendar API
  7. Razor 语法快速参考
  8. RESTful API -备
  9. UVa 232 Crossword Answers
  10. HDU 3571 N-dimensional Sphere
  11. 关于Hbase的cache配置
  12. 第二次冲刺spring会议(第三次会议)
  13. Java并发基础——Thread
  14. AngularJS 拦截器实现全局$http请求loading效果
  15. exit(0)与exit(1),return三者区别(详解)
  16. 【luogu2161】【SHOI2009】Booking会场预约
  17. HDU-1398 Square Coins(生成函数)
  18. ADB——模拟手机按键输入
  19. 论文总结(negFIN: An efficient algorithm for fast mining frequent itemsets)
  20. javascript OOP实例—探测器

热门文章

  1. (四)G1 garbage collector
  2. json字符串转泛型集合对象
  3. Windows下使用WSRM限制MongoDB内存
  4. RFID电子标签天线的印刷
  5. 使用jspatch进行热修复的实战总结
  6. scala中的数组的转换操作
  7. HTML中添加背景音乐
  8. 如何安装并使用hibernate tools
  9. OC中的字典
  10. JSONModel对架构的影响及解决方案