/
pullenuminstances.py
executable file
·167 lines (131 loc) · 5.23 KB
/
pullenuminstances.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Example of using Pull Operations to retrieve instances from a
WBEM Server. This example allows either simplistic command line
input with a fixed number of parameters or defaults to internal
definitions if incorrect number of cmd line arguments are supplied.
It:
Creates a connection
Opens an enumeration session with OpenEnumerateInstances
Executes a pull loop until the result.eos =True
Displays overall statistics on the returns
It also displays the results of the open and each pull in detail
"""
from __future__ import print_function
import sys
import datetime
from pywbem import WBEMConnection, CIMError, Error
# Default connection attributes. Used if not all arguments are
# supplied on the command line.
USERNAME = 'blah'
PASSWORD = 'blah'
TEST_CLASS = 'CIM_ComputerSystem'
TEST_NAMESPACE = 'root/cimv2'
SERVER_URL = 'http://localhost'
class ElapsedTimer(object):
"""
Set up elapsed time timer. Calculates time between initiation
and access.
"""
def __init__(self):
""" Initiate the object with current time"""
self.start_time = datetime.datetime.now()
def reset(self):
""" Reset the start time for the timer"""
self.start_time = datetime.datetime.now()
def elapsed_ms(self):
""" Get the elapsed time in milliseconds. returns floating
point representation of elapsed time in seconds.
"""
dt = datetime.datetime.now() - self.start_time
return ((dt.days * 24 * 3600) + dt.seconds) * 1000 \
+ dt.microseconds / 1000.0
def elapsed_sec(self):
""" get the elapsed time in seconds. Returns floating
point representation of time in seconds
"""
return self.elapsed_ms() / 1000
def execute_request(conn, classname, max_open, max_pull):
"""
Enumerate instances defined by the function's
classname argument using the OpenEnumerateInstances and
PullInstancesWithPath.
* classname - Classname for the enumeration.
* max_open - defines the maximum number of instances
for the server to return for the open
*max_pull defines the maximum number of instances for the
WBEM server to return for each pull operation.
Displays results of each open or pull operation including
size, return parameters, and time to execute.
Any exception exits the function.
"""
start = ElapsedTimer()
result = conn.OpenEnumerateInstances(classname,
MaxObjectCount=max_open)
print('open rtn eos=%s context=%s, count=%s time=%s ms' %
(result.eos, result.context, len(result.instances),
start.elapsed_ms()))
# save instances since we reuse result
insts = result.instances
# loop to make pull requests until end_of_sequence received.
pull_count = 0
while not result.eos:
pull_count += 1
op_start = ElapsedTimer()
result = conn.PullInstancesWithPath(result.context,
MaxObjectCount=max_pull)
insts.extend(result.instances)
print('pull rtn eos=%s context=%s, insts=%s time=%s ms' %
(result.eos, result.context, len(result.instances),
op_start.elapsed_ms()))
print('Result instance count=%s pull count=%s time=%.2f sec' % \
(len(insts), pull_count, start.elapsed_sec()))
return insts
def main():
"""
Get arguments and call the execution function
"""
# if less than required arguments, use the defaults
if len(sys.argv) < 8:
print("Usage: %s server_url username password namespace classname "
"max_open, max_pull" % sys.argv[0])
server_url = SERVER_URL
username = USERNAME
password = PASSWORD
namespace = TEST_NAMESPACE
classname = TEST_CLASS
max_open = 0
max_pull = 100
else:
server_url = sys.argv[1]
username = sys.argv[2]
password = sys.argv[3]
namespace = sys.argv[4]
classname = sys.argv[5]
max_open = sys.argv[6]
max_pull = sys.argv[7]
print('Parameters: server_url=%s\n username=%s\n namespace=%s\n' \
' classname=%s\n max_open=%s,\n max_pull=%s' % \
(server_url, username, namespace, classname, max_open, max_pull))
# connect to the server
conn = WBEMConnection(server_url, (username, password),
default_namespace=namespace,
no_verification=True)
#Call method to execute the enumeration sequence and return instances
try:
instances = execute_request(conn, classname, max_open, max_pull)
# print the resulting instances
for instance in instances:
print('\npath=%s\n%s' % (instance.path, instance.tomof()))
# handle exceptions
except CIMError as ce:
print('Operation Failed: CIMError: code=%s, Description=%s' % \
(ce.status_code_name, ce.status_description))
sys.exit(1)
except Error as err:
print ("Operation failed: %s" % err)
sys.exit(1)
return 0
if __name__ == '__main__':
sys.exit(main())