forked from adamjshook/hadoop-metrics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FsUtil.py
executable file
·121 lines (92 loc) · 4.38 KB
/
FsUtil.py
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
#!/usr/bin/python
import ConfigParser
from collections import namedtuple
from funcs import *
from Config import *
from PostgresUtil import *
class FsUtil:
def __init__(self, conf):
self.conf = conf
self.pgUtil = PostgresUtil(conf)
def printReport(self):
printInfo("Grabbing and printing drive metrics")
self.printDriveUsage()
self.printDriveWriteTest()
# This function prints usage statistics for drives that are available
# It runs an initial test to make sure the drive exists on the host
# If the drive does not exist, it is not reported in this report
def printDriveUsage(self):
hostfile = self.conf.get(Config.HOST_FILE)
dataDrives = self.conf.get(Config.DATA_DRIVES)
driveMetrics = []
for dataDrive in dataDrives.split(" "):
# First, test if the drive exists on all host, getting the list of where it worked
output = getCommandOutput("massh %s worked test -e %s" % (hostfile, dataDrive))
if len(output) > 0:
# We have some hosts that have this drive
tmpHostfile = self.writeHostFile(output.split("\n"))
cmd = "massh %s verbose \"df %s | grep -v Filesystem\" | awk '{print $1,$8,$7,$4,$5,$6}'" % (tmpHostfile, dataDrive)
output = getCommandOutput(cmd).split("\n")
for line in output:
(host, drive, perc, size, used, avail) = line.split(" ")
driveMetrics.append((host.replace("[","").replace("]",""), drive, perc.replace("%", ""), size, used, avail))
driveMetrics.sort()
self.__printDriveUsageInserts(driveMetrics)
row = namedtuple('Row', ['Host', 'Drive', 'PercentUsed', 'Size', 'Used', 'Avail'])
toPrint = []
for (host, drive, perc, size, used, avail) in driveMetrics:
toPrint.append(row(host, drive, perc, size, used, avail))
pprinttable(toPrint)
def __printDriveUsageInserts(self, driveMetrics):
for (host, drive, perc, size, used, avail) in driveMetrics:
row = DriveUsageRow()
row.host = host
row.drive = drive
row.perc = perc
row.size = size
row.used = used
row.avail = avail
self.pgUtil.writeInsert(row)
def printDriveWriteTest(self):
printInfo("Getting non-writeable drives")
hostfile = self.conf.get(Config.HOST_FILE)
dataDrives = self.conf.get(Config.DATA_DRIVES)
failedDrives = []
for drive in dataDrives.split(" "):
# Check if the drives exist
output = getCommandOutput("massh %s bombed sudo test -e %s" % (hostfile, drive))
if len(output) > 0:
for host in output.split("\n"):
failedDrives.append((host, drive, 'dne'))
output = getCommandOutput("massh %s worked sudo test -e %s" % (hostfile, drive))
if len(output) > 0:
tmpHostFile = self.writeHostFile(output.split("\n"))
output = getCommandOutput("massh %s bombed sudo test -w %s" % (tmpHostFile, drive))
if len(output) > 0:
for host in output.split("\n"):
failedDrives.append((host, drive, 'ro'))
if len(failedDrives) == 0:
printInfo("No non-writeable drives to report")
else:
row = namedtuple('Row', ['Host', 'Drive', 'Reason'])
failedDrives.sort()
self.__printDriveWriteTest(failedDrives)
toPrint = []
for (host, drive, reason) in failedDrives:
toPrint.append(row(host, drive, reason))
pprinttable(toPrint)
def __printDriveWriteTest(self, failedDrives):
for (host, drive, reason) in failedDrives:
row = DriveWriteTestRow()
row.host = host
row.drive = drive
row.reason = reason
self.pgUtil.writeInsert(row)
def writeHostFile(self, hosts):
fName = self.conf.get(Config.TMP_DIR) + "/fsutil.txt"
f = open(fName, 'w')
for item in hosts:
f.write(item + "\n")
f.flush()
f.close()
return fName