-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathibm_unused
executable file
·118 lines (107 loc) · 4.47 KB
/
ibm_unused
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
#!/usr/bin/python2.7
#
# List all unused objects
from __future__ import print_function
import sys
from subprocess import check_output, Popen, PIPE, CalledProcessError, STDOUT
import re
# Parsing arguments
import argparse
parser = argparse.ArgumentParser(description='List all unused objects')
parser.add_argument('device', metavar='DEVICE',
help='Hostname of the storage cluster')
parser.add_argument('--version', action='version', version='%(prog)s 1.0')
parser.add_argument('-v', '--verbose', default=False, action='store_true',
help='print verbose output')
parser.add_argument('--type', metavar='TYPE', default='all',
help="select TYPE (vdisk, host, group) of unused objects")
parser.add_argument('--pattern', metavar='PATTERN', default='.*',
help="select objects corresponding to PATTERN (regex defaulting to .*)")
parser.add_argument('--remove', action='store_true',
help="remove all or selected unused objects (see --type and --pattern)")
parser.add_argument('--action', action='store_true',
help='execute changes')
args = parser.parse_args()
def open_ssh():
controlpath = "-oControlPath=~/.ssh/ssh-%r-%h-%p"
if args.verbose:
print("INFO: opening SSH control session")
return Popen(['ssh', '-T', '-oControlMaster=yes', controlpath, args.device], stdin=PIPE, stdout=None, stderr=None)
def close_ssh(sshproc):
controlpath = "-oControlPath=~/.ssh/ssh-%r-%h-%p"
if args.verbose:
print("INFO: closing SSH control session")
return sshproc.communicate("exit\n")
def cmd_ssh(cmd):
controlpath = "-oControlPath=~/.ssh/ssh-%r-%h-%p"
command = ["ssh", '-oControlMaster=no' , controlpath, args.device]
command.extend(cmd)
if args.verbose:
print("INFO: running command '{}'".format(' '.join(cmd)))
return check_output(command, stderr=STDOUT)
# Open SSH connection
sshproc = open_ssh()
output = cmd_ssh(["lshostvdiskmap", "-nohdr", "-delim", ":"])
mappedvdisks = []
mappedhosts = []
for line in output.split():
mappedvdisks.append(line.split(':')[3])
mappedhosts.append(line.split(':')[0])
if args.type == "vdisk" or args.type == "all":
print("#### Unused vdisks:")
output = cmd_ssh(["lsvdisk", "-nohdr", "-delim", ":"])
for line in output.split():
vdisk = line.split(':')
if vdisk[0] not in mappedvdisks and re.match('^' + args.pattern + '$', vdisk[1]):
print(vdisk[0] + ':' + vdisk[1], end='')
if args.remove and args.action:
print(" => ", end="")
try:
output = cmd_ssh(["rmvdisk", vdisk[0]])
print("removed")
except CalledProcessError, e:
print("ERROR: " + e.output)
elif args.remove:
print(" => would be removed (use --action)")
else:
print()
print()
if args.type == "group" or args.type == "all":
print("#### Unused consistency groups:")
output = cmd_ssh(["lsfcconsistgrp", "-nohdr", "-delim", ":"])
#fields = ['id', 'name', 'status', 'start_time']
for line in output.split():
fcgrp = line.split(':')
if fcgrp[2] == "empty" and re.match('^' + args.pattern + '$', fcgrp[1]):
print(fcgrp[0] + ':' + fcgrp[1], end="")
if args.remove and args.action:
print(" => ", end="")
try:
output = cmd_ssh(["rmfcconsistgrp", fcgrp[0]])
print("removed")
except CalledProcessError, e:
print("ERROR: " + e.output)
elif args.remove:
print(" => would be removed (use --action)")
else:
print()
print()
if args.type == "host" or args.type == "all":
print("#### Unused hosts:")
output = cmd_ssh(["lshost", "-nohdr", "-delim", ":"])
for line in output.split():
host = line.split(':')
if host[0] not in mappedhosts and re.match('^' + args.pattern + '$', host[1]):
print(host[0] + ':' + host[1], end="")
if args.remove and args.action:
print(" => ", end="")
try:
output = cmd_ssh(["rmhost", host[0]])
print("removed")
except CalledProcessError, e:
print("ERROR: " + e.output)
elif args.remove:
print(" => would be removed (use --action)")
else:
print()
close_ssh(sshproc)