-
Notifications
You must be signed in to change notification settings - Fork 9
/
storage_cim_print_search.py
executable file
·134 lines (110 loc) · 5.65 KB
/
storage_cim_print_search.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
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env python3
#
# IBM Storwize CIM print
#
# 2020 - 2021 Denis Pavlov
#
# Print CIM properties of Storwize classes with names set by search string to stdout and detected_properties_file
#
import os
import sys
from configread import configread
from json import load
from pynetdevices import WBEMDevice
from pywbem import _exceptions
def main():
# set project name as current directory name
project = os.path.abspath(__file__).split('/')[-2]
software = sys.argv[0]
if len(sys.argv) == 1:
print((f'Please add argument(s) to call of program for search Storage CIM classes: \n'
f'Example of syntax: {software} Array Volume'))
exit(1)
search_strings = sys.argv[1:]
# get config file name
conf_file = (
f'/etc/zabbix/externalscripts/{project}/conf.d/{project}.conf')
# read network device parameters from config and save it to dict
nd_parameters = configread(conf_file, 'NetworkDevice', 'device_file',
'login', 'name_space', 'password')
sd_parameters = configread(
conf_file, 'StorageDevice', 'detected_properties_file')
# get variables
login = nd_parameters['login']
password = nd_parameters['password']
# open config file with list of monitored storages
device_list_file = open(nd_parameters['device_file'])
# open properties file for writing
detected_properties_file = open(
sd_parameters['detected_properties_file'], 'w')
# unpack storage list to variables
for device_line in device_list_file:
device_type, device_name, device_ip = device_line.split(':')
device_ip = device_ip.rstrip('\n')
# connect to each storage via WBEM, get conn object
if device_type == 'storwize':
device = WBEMDevice(device_name, device_ip, login, password)
# get namespace from config, root/ibm by default
namespace = nd_parameters.get('name_space', 'root/ibm')
print(f'Connecting to {device_name} ...')
conn = device.Connect(namespace)
# try to get all cim classes from storage via WBEM
try:
sc_cim_classes = conn.EnumerateClassNames(
namespace=nd_parameters['name_space'], DeepInheritance=True)
except _exceptions.AuthError as error:
print((f'{project}_error: exception in {software}: can\'t exec query on {device_name}: {error} '
f'Check your username/password and permissions of user.'),
file=sys.stderr)
exit(1)
except _exceptions.ConnectionError as error:
print((f'{project}_error: exception in {software}: can\'t exec query on {device_name}: {error}. '
f'Check the connection to storage or try later.'),
file=sys.stderr)
exit(1)
except _exceptions.HTTPError as error:
print((f'{project}_error: exception in {software}: WBEM server return code 400 (Bad Request) on {device_name}: {error}. '
f'Check the your request.'),
file=sys.stderr)
exit(1)
except:
print(f'{project}_error: exception in {software}: {sys.exc_info()}',
file=sys.stderr)
exit(1)
# try to get all instances for each cim class from storage via WBEM
for sc_cim_class in sc_cim_classes:
for search_string in search_strings:
if sc_cim_class.find(search_string) > 0:
try:
instances = conn.EnumerateInstances(sc_cim_class,
namespace=nd_parameters['name_space'])
except _exceptions.AuthError as error:
print((f'{project}_error: exception in {software}: can\'t exec query on {device_name}: {error} '
f'Check your username/password and permissions of user.'),
file=sys.stderr)
exit(1)
except _exceptions.ConnectionError as error:
print((f'{project}_error: exception in {software}: can\'t exec query on {device_name}: {error}. '
f'Check the connection to storage or try later.'),
file=sys.stderr)
exit(1)
except _exceptions.HTTPError as error:
print((f'{project}_error: exception in {software}: WBEM server return code 400 (Bad Request) on {device_name}: {error}. '
f'Check the your request.'),
file=sys.stderr)
exit(1)
except:
print(f'{project}_error: exception in {software}: {sys.exc_info()}',
file=sys.stderr)
exit(1)
for instance in instances:
for prop_name, prop_value in instance.items():
output_string = (f'Device: {device_name}, CIM class: {sc_cim_class}\n'
f'Property: {prop_name}, Value: {prop_value}\n')
print(output_string)
detected_properties_file.write(
f'{output_string} \n')
device_list_file.close()
detected_properties_file.close()
if __name__ == "__main__":
main()