-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathzenoss_client.py
More file actions
160 lines (133 loc) · 4.19 KB
/
zenoss_client.py
File metadata and controls
160 lines (133 loc) · 4.19 KB
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
# -*-coding: utf-8 -*-
"""
Author : Arijit Basu <sayanarijit@gmail.com>
Website : https://sayanarijit.github.io
"""
from __future__ import absolute_import, unicode_literals
import json
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
router_endpoints = {
'application_router',
'detailnav_router',
'devicedumpload_router',
'devicemanagement_router',
'device_router',
'evclasses_router',
'evconsole_router',
'host_router',
'introspection_router',
'jobs_router',
'manufacturers_router',
'messaging_router',
'mib_router',
'monitor_router',
'network_6_router',
'network_router',
'process_router',
'properties_router',
'report_router',
'search_router',
'service_router',
'settings_router',
'template_router',
'triggers_router',
'users_router',
'zenpack_router'
}
class InvalidRouterEndpointError(Exception):
def __init__(self, router):
Exception.__init__(self,
'"{}" is not listed as a valid router endpoint.\nTry one of: {}'.format(router, ', '.join(router_endpoints)))
class InvalidActionError(Exception):
def __init__(self, action):
Exception.__init__(self, '"{}" is not a valid action'.format(action))
class HTTPError(Exception):
def __init__(self, status):
Exception.__init__(self, 'Server returned HTTP status code: {}'.format(status))
class InvalidResponseError(Exception):
def __init__(self, content):
Exception.__init__(self, 'Server returned JSON incompatible response:\n{}'.format(content))
class ZenossClient(object):
"""
Zenoss API client for python
"""
def __init__(self, host, user, passwd, dmd='/zport/dmd', verify=False):
self.baseurl = 'https://' + host + dmd
self.session = requests.Session()
self.session.auth = (user, passwd)
self.session.verify = verify
self.session.headers.update({'content-type': 'application/json'})
self.session.tid = 0
def endpoint(self, endpoint):
"""
Return endpoint object
"""
if endpoint not in router_endpoints:
raise InvalidRouterEndpointError(endpoint)
return ZenossEndpoint(
endpoint=self.baseurl + '/' + endpoint,
session=self.session
)
def __getattr__(self, attr):
"""
Dynamically create endpoint
"""
return self.endpoint(attr)
class ZenossEndpoint(object):
"""
Zenoss endpoint
"""
def __init__(self, endpoint, session):
self.endpoint = endpoint
self.session = session
def action(self, action):
"""
Return action object
"""
if 'Router' not in action or action.replace('Router', '').lower() not in self.endpoint:
raise InvalidActionError(action)
return ZenossAction(
endpoint = self.endpoint,
action = action,
session = self.session
)
def __getattr__(self, attr):
"""
Dynamically create action object
"""
return self.action(attr)
class ZenossAction(object):
"""
Zenoss action
"""
def __init__(self, endpoint, action, session):
self.endpoint = endpoint
self.action = action
self.session = session
def method(self, method):
"""
Return callable method
"""
def wrapped(timeout=None, **kwargs):
payload = {'action': self.action, 'method': method,
'tid': self.session.tid, 'data': [kwargs]}
result = self.session.post(
self.endpoint,
data = json.dumps(payload),
timeout = timeout
)
self.session.tid += 1
if result.status_code != 200:
raise HTTPError(result.status_code)
try:
return result.json()
except:
raise InvalidResponseError(result.content)
return wrapped
def __getattr__(self, attr):
"""
Dynamically create method object
"""
return self.method(attr)