#!/usr/bin/env python
# -*- coding: utf-8 -*-
# date:2019-11-04
# author:liuyuqiang
# function:getting disk info from server

getbasedata = """
hostname -I|awk '{print $1}';
df -h|grep data|wc -l;
ls /data*|grep -v "lost+found"|wc -l;
df -h|grep data|awk '{print $2}'|grep T|sed "s/T//"|xargs|sed 's/\s/+/g'|bc;
df -h|grep data|awk '{print $5}'|sed "s/%//"|xargs|sed 's/\s/+/g'|bc;
df -h|grep data|sed "s/%//"|awk '{if ($5>=90) {print $5}}'|wc -l;
df -h|grep data|sed "s/%//"|awk '{if (($5>=80)&&($5<90)) {print $5}}'|wc -l;
df -h|grep data|sed "s/%//"|awk '{if (($5>=70)&&($5<80)) {print $5}}'|wc -l;
df -h|grep data|sed "s/%//"|awk '{if (($5>=60)&&($5<70)) {print $5}}'|wc -l;
df -h|grep data|sed "s/%//"|awk '{if ($5<60) {print $5}}'|wc -l
"""

try:
    # python2
    import commands

    with open("/tmp/lyq/.getdata.sh", "w") as f:
        print >> f, getbasedata
    (status, getdata) = commands.getstatusoutput(
        "ansible -i ds1iplist all -o -m script -a '/tmp/lyq/.getdata.sh'|grep -w 'SUCCESS' |sed 's/}//'|awk -F: '{print $NF}'")
except IOError as err:
    print("Error", str(err))
    # python3
    import subprocess

    # with open("/tmp/lyq/.getdata.sh", "w") as f:
    ##   print(getbasedata, file=f)
    (status, getdata) = subprocess.getstatusoutput(
        "ansible -i ds1iplist all -o -m script -a '/tmp/lyq/.getdata.sh'|grep -w 'SUCCESS'|sed 's/}//'|awk -F: '{print $NF}'")
finally:
    print("Getting Server base data \n")
    with open("/tmp/lyq/.getdata.sh", "w") as f:
        print >> f, ""

ds1basedata = []

def format_basedata():
    for each_item in getdata.split("\n"):
        ds1basedata.append(eval(each_item))
    for newitem in ds1basedata:
        for i in range(2, 5):
            newitem.append(str(float('%.2f' % (eval(newitem[i]) / eval(newitem[1])))))
        #print(newitem)
        newitem.append(str(int(eval(newitem[11])*1024/30-eval(newitem[10]))))
        #print(newitem)

base_iplist=[]
ok_iplist=[]
fail_iplist=[]
def get_data_fail_iplist():
    try:
        with open("ds1iplist","r") as f:
            data=f.readlines()
            for i in data:
                base_iplist.append(i.strip())
    except IOError as err:
        print("IOError :"+str(err))

    for i in ds1basedata:
        ok_iplist.append(i[0])
    fail_iplist = list(set(base_iplist) - set(ok_iplist))
    if len(fail_iplist)>0:
        print("Get data Fail Iplist: ")
    for f in fail_iplist:
        print(f)

def get_disk_num():
    global type12, type36
    type12list = []
    type36list = []
    type12 = type36 = 0
    for newitem in ds1basedata:
        if newitem[1] == '12':
            type12 += 1
            type12list.append(newitem[0])
        elif newitem[1] == '36':
            type36 += 1
            type36list.append(newitem[0])
        else:
            print("New Disk_num!!!", newitem[1])

    if len(type12list) > 0:
        print("12_disk_server_total_num: {} ".format(type12))
        print("12_disk_server_IPlist: ")
        for i in type12list:
            print(i)
        print("\n")

    if len(type36list) > 0:
        print("36_disk_server_total_num: {} ".format(type36))
        print("36_disk_server_IPlist: ")
        for i in type36list:
            print(i)
        print("\n")

def get_single_disk_use_per():
    global single_diskgt90, single_diskgt80, single_diskgt70, single_diskgt60, single_disklt60, total_disk_num, total_disk_TB, total_volum_3copies, total_diskuse_avg_per, total_diskuse_avg_per_sum,can_create_volum
    single_diskgt90 = single_diskgt80 = single_diskgt70 = single_diskgt60 = single_disklt60 = total_disk_num = total_disk_TB = total_volum_3copies = total_diskuse_avg_per = total_diskuse_avg_per_sum =can_create_volum= 0
    for newitem in ds1basedata:
        total_disk_num += int(eval(newitem[1]))
        total_disk_TB += int(eval(newitem[3]))
        total_volum_3copies += int(eval(newitem[2]))
        can_create_volum += int(eval(newitem[13]))
        total_diskuse_avg_per_sum += int(eval(newitem[12]))
        total_diskuse_avg_per = total_diskuse_avg_per_sum / len(ds1basedata)
        single_diskgt90 += int(eval(newitem[5]))
        single_diskgt80 += int(eval(newitem[6]))
        single_diskgt70 += int(eval(newitem[7]))
        single_diskgt60 += int(eval(newitem[8]))
        single_disklt60 += int(eval(newitem[9]))

    print(
        "total_disk_num   : {} \ntotal_disk_TB    : {} TB\ntotal_volum      : {} \ncan_create_volum : {} "
        "\ntotal_diskuse_avg: {} % \nsingle_disk_gt_90: {} \nsingle_disk_gt_80: {} \nsingle_disk_gt_70: {} "
        "\nsingle_disk_gt_60: {} \nsingle_disk_lt_60: {} \n".format(
            total_disk_num, total_disk_TB, int(total_volum_3copies/3),int(can_create_volum/3), float('%.2f' % total_diskuse_avg_per), single_diskgt90,
            single_diskgt80, single_diskgt70, single_diskgt60, single_disklt60))

if __name__ == '__main__':
    format_basedata()
    get_disk_num()
    get_single_disk_use_per()
    get_data_fail_iplist()
---------------------------------------------------------
输出结果展示
---------------------------------------------------------
Getting Server base data
---------------------------------------------------------
12_disk_server_total_num: 3
12_disk_server_IPlist:
192.36.93.40
192.36.93.47
192.37.41.155

36_disk_server_total_num: 6
36_disk_server_IPlist:
192.36.36.194
192.36.36.131
192.36.36.130
192.36.36.132
192.38.145.192
192.38.145.193

total_disk_num   : 252
total_disk_TB    : 2289 TB
total_volum      : 7519
can_create_volum : 720
total_diskuse_avg: 21.0 %
single_disk_gt_90: 71
single_disk_gt_80: 0
single_disk_gt_70: 0
single_disk_gt_60: 0
single_disk_lt_60: 181

Get data Fail Iplist:
192.27.12.78