-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlib_rac.py
152 lines (132 loc) · 5.06 KB
/
lib_rac.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
# -*- coding: utf-8 -*-
import logging, json, subprocess
from typing import Dict, List, Union, Callable, TypeVar, Any
logger = logging.getLogger(__name__)
logger.addHandler(logging.NullHandler())
ListRac = List[Dict[str, str]]
class UserDecorators:
@classmethod
def to_json(cls, func):
def wrapper(*args, **kwargs):
result = func(*args, **kwargs)
result = json.dumps(result)
return result
return wrapper
class Client1C:
def __init__(
self,
hostname: str,
cls_user: Union[str, None] = None,
cls_pwd: Union[str, None] = None,
) -> None:
self.hostname = hostname
self.cls_user = cls_user
self.cls_pwd = cls_pwd
self.cluster_id = self.get_cluster_id()
def get_cluster_id(self) -> str:
command = "rac cluster list {}".format(self.hostname)
result = self._exec_rac(command)
return result[0]["cluster"]
def get_db_list(self) -> ListRac:
command = "rac infobase --cluster={} summary list {}"
if self.cls_user and self.cls_pwd:
command += " --cluster-user={} --cluster-pwd={}".format(
self.cls_user, self.cls_pwd
)
result = self._exec_rac(command.format(self.cluster_id, self.hostname))
return result
def get_session_list(self, db_id: str) -> ListRac:
command = "rac session --cluster={} list --infobase={} {}"
if self.cls_user and self.cls_pwd:
command += " --cluster-user={} --cluster-pwd={}".format(
self.cls_user, self.cls_pwd
)
command = command.format(self.cluster_id, db_id, self.hostname)
result = self._exec_rac(command)
return result
def get_lock_list(self, db_id: str) -> ListRac:
command = "rac lock --cluster={} list --infobase={} {}"
if self.cls_user and self.cls_pwd:
command += " --cluster-user={} --cluster-pwd={}".format(
self.cls_user, self.cls_pwd
)
command = command.format(self.cluster_id, db_id, self.hostname)
result = self._exec_rac(command)
return result
def get_license_list(self, db_id: str) -> ListRac:
command = "rac session --cluster={} list --infobase={} {} --licenses"
if self.cls_user and self.cls_pwd:
command += " --cluster-user={} --cluster-pwd={}".format(
self.cls_user, self.cls_pwd
)
command = command.format(self.cluster_id, db_id, self.hostname)
result = self._exec_rac(command)
return result
def get_db_info(
self,
db_id: str,
user_name: Union[str, None] = None,
user_pwd: Union[str, None] = None,
) -> ListRac:
command = "rac infobase --cluster={} info --infobase={} {}"
if self.cls_user and self.cls_pwd:
command += " --cluster-user={} --cluster-pwd={}".format(
self.cls_user, self.cls_pwd
)
if user_name and user_pwd:
command += " --infobase-user={} --infobase-pwd={}".format(
user_name, user_pwd
)
command = command.format(self.cluster_id, db_id, self.hostname)
result = self._exec_rac(command)
return result
def get_process_list(self) -> ListRac:
command = "rac process --cluster={} list {}"
if self.cls_user and self.cls_pwd:
command += " --cluster-user={} --cluster-pwd={}".format(
self.cls_user, self.cls_pwd
)
command = command.format(self.cluster_id, self.hostname)
result = self._exec_rac(command)
return result
@staticmethod
def _exec_rac(command: str) -> ListRac:
result = subprocess.run(
command.split(),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
)
if result.stderr:
err = "Error subprocess.run, arg: {}, Error:{}".format(
result.args, result.stderr
)
logger.error(err)
raise IOError(err)
else:
return Client1C._row_output_to_dict(result.stdout)
@staticmethod
def _row_output_to_dict(output: str) -> ListRac:
result = []
for block in output.split("\n\n"):
if block:
dict_block = {}
for line in block.split("\n"):
k, v = line.split(":", maxsplit=1)
k, v = k.strip(), v.strip("\"' ")
dict_block[k] = v
result.append(dict_block)
return result
@staticmethod
@UserDecorators.to_json
def get_zabbix_lld(output: ListRac) -> ListRac:
result = []
for item in output:
new_item = {}
for x, y in item.items():
new_item["{{#{}}}".format(x.upper())] = y
result.append(new_item)
return result
@staticmethod
def counter_session(session_list: ListRac, d_key: str, v_filter: str) -> int:
return len([x for x in session_list if x[d_key] == v_filter])