Skip to content

Commit 2bcc315

Browse files
committed
style: changed style formatting and removed mutable default agruments
1 parent 586beb6 commit 2bcc315

File tree

8 files changed

+58
-31
lines changed

8 files changed

+58
-31
lines changed

mamonsu/lib/config.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818

1919
class Config(DefaultConfig):
2020

21-
def __init__(self, cfg_file=None, plugin_directories=[]):
21+
def __init__(self, cfg_file=None, plugin_directories=None):
2222

23+
if plugin_directories is None:
24+
plugin_directories = []
2325
config = configparser.ConfigParser()
2426

2527
config.add_section('postgres')

mamonsu/lib/get_keys.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,19 @@
55
class GetKeys(object):
66
plg_type = 'all'
77

8-
def txt(self, type, plugins=[]):
8+
def txt(self, type, plugins=None):
99
# sort plugins!
10+
if plugins is None:
11+
plugins = []
1012
self.plg_type = type
1113
plugins.sort(key=lambda x: x.__class__.__name__)
1214
# create template
1315
template_data = self._get_all('keys_and_queries', plugins) # get data from all plugins
1416
return template_data
1517

16-
def _get_all(self, keys_and_queries, plugins=[]):
18+
def _get_all(self, keys_and_queries, plugins=None):
19+
if plugins is None:
20+
plugins = []
1721
result = ''
1822
# don't need keys for zabbix agent from these classes
1923
non_keys_classes = ('AgentApi', 'LogSender', 'ZbxSender')
@@ -28,7 +32,9 @@ def _get_all(self, keys_and_queries, plugins=[]):
2832
result += row
2933
return result
3034

31-
def key_and_query(self, args=[]):
35+
def key_and_query(self, args=None):
36+
if args is None:
37+
args = []
3238
result = ""
3339
for one in args:
3440
result += 'UserParameter={0}\n'.format(one)

mamonsu/lib/parser.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# -*- coding: utf-8 -*-
22
import sys
33
from mamonsu import __version__
4-
from optparse import OptionParser, BadOptionError, OptionGroup
4+
from optparse import OptionParser, BadOptionError
55
import mamonsu.lib.platform as platform
66

77
usage_msg = """
@@ -180,7 +180,7 @@ def print_total_help():
180180

181181
class MissOptsParser(OptionParser):
182182

183-
def print_help(self):
183+
def print_help(self, **kwargs):
184184
print("""
185185
186186
@@ -250,7 +250,7 @@ def _process_short_opts(self, rargs, values):
250250
def _add_help_option(self):
251251
self.add_option("--help",
252252
action="help",
253-
help=("show this help message and exit"))
253+
help="show this help message and exit")
254254

255255

256256
def parse_args():

mamonsu/lib/plugin.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class Plugin(object):
2424
AgentPluginType = 'all'
2525

2626
# PG version
27-
VersionPG = {'type': 'PG_V', 'number': '10.0'}
27+
VersionPG = '10'
2828

2929
# Macros for run as agent type or as mamonsu
3030
Macros = {"mamonsu": "", "agent": "{$PG_CONNINFO},{$PG_PATH}"}
@@ -33,7 +33,7 @@ class Plugin(object):
3333
Interval = 60
3434

3535
# plugin config
36-
DEFAULT_CONFIG = {} # type: Dict[str, str]
36+
DEFAULT_CONFIG = {} # type: dict[str, str]
3737

3838
_thread = None # type: Thread
3939
_sender = False
@@ -46,7 +46,7 @@ class Plugin(object):
4646
old_zabbix = False
4747

4848
# const
49-
PATH = "/etc/zabbix/zabbix_agent.d/scripts"
49+
PATH = "/etc/zabbix/zabbix_agentd.d/scripts"
5050
DELTA = Template.DELTA
5151
GRAPH_TYPE = Template.GRAPH_TYPE
5252
VALUE_TYPE = Template.VALUE_TYPE
@@ -156,7 +156,7 @@ def _log_exception(self, e, trace):
156156
self.log.debug(trace)
157157

158158
def _loop(self):
159-
while (True):
159+
while True:
160160
last_start = time.time()
161161
try:
162162
self.run(self.sender)

mamonsu/lib/sender.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import mamonsu.lib.platform as platform
77

88

9-
class Sender():
9+
class Sender:
1010

1111
def __init__(self):
1212
self._senders = []

mamonsu/lib/senders/zbx.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
from mamonsu.lib.queue import Queue
1515
from itertools import islice
1616

17-
class ZbxSender(Plugin):
1817

18+
class ZbxSender(Plugin):
1919
Interval = 10
2020
_sender = True
2121

@@ -64,14 +64,14 @@ def _flush(self):
6464
})
6565
self._send_data(data)
6666

67-
def send_file_to_zabbix (self,path):
67+
def send_file_to_zabbix(self, path):
6868
zabbix_client = self.config.fetch('zabbix', 'client')
6969
self.log.setLevel((self.config.fetch('log', 'level')).upper())
7070

7171
metrics = []
7272
with open(path, 'r') as f:
7373
while True:
74-
lines=list(islice(f, 100))
74+
lines = list(islice(f, 100))
7575
for line in lines:
7676
try:
7777
split_line = line.rstrip('\n').split('\t')
@@ -83,9 +83,11 @@ def send_file_to_zabbix (self,path):
8383
'clock': int(split_line[0])}
8484
metrics.append(metric)
8585
else:
86-
self.log.error('Can\'t load metric in line: "{0}". The line must have the format: time <tab> value <tab> metric\'s name.'.format(line.rstrip('\n')))
86+
self.log.error(
87+
'Can\'t load metric in line: "{0}". The line must have the format: time <tab> value <tab> metric\'s name.'.format(
88+
line.rstrip('\n')))
8789
except Exception as e:
88-
self.log.error('Can\'t load metric in line: "{0}". Error : {1} '.format(line.rstrip('\n'),e,))
90+
self.log.error('Can\'t load metric in line: "{0}". Error : {1} '.format(line.rstrip('\n'), e, ))
8991

9092
data = json.dumps({
9193
'request': 'sender data',

mamonsu/lib/supervisor.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111

1212
class Supervisor(object):
13-
1413
Running = True
1514

1615
def __init__(self, config):
@@ -42,7 +41,7 @@ def _start_plugins(self):
4241
plugin.start()
4342
else:
4443
plugin.log.info('plugin {0} was disabled due to the statement in '
45-
'the config file '. format(plugin.__class__.__name__.lower()))
44+
'the config file '.format(plugin.__class__.__name__.lower()))
4645

4746
def _loop(self):
4847
plugin_errors, plugin_probes, last_error = 0, 0, ''

mamonsu/lib/zbx_template.py

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# -*- coding: utf-8 -*-
2-
from xml.dom import minidom
32
from mamonsu.lib.const import Template
43
from mamonsu.lib.plugin import Plugin
54
import re
@@ -118,14 +117,14 @@ def turn_agent_type(self, xml):
118117
xml = re.sub(r"<type>2", "<type>0", xml)
119118
return xml
120119

121-
def xml(self, type, plugins=[]):
120+
def xml(self, type, plugins=None):
122121
# sort plugins!
122+
if plugins is None:
123+
plugins = []
123124
plugins.sort(key=lambda x: x.__class__.__name__)
124125
self.plg_type = type
125126
# create template
126-
template_data = {}
127-
template_data['template'] = self.Template
128-
template_data['application'] = self.Application
127+
template_data = {'template': self.Template, 'application': self.Application}
129128
if Plugin.Type == 'agent':
130129
template_data['macros'] = self._macro()
131130
else:
@@ -139,7 +138,9 @@ def xml(self, type, plugins=[]):
139138
output_xml = ZbxTemplate.turn_agent_type(self, output_xml)
140139
return output_xml
141140

142-
def _get_all(self, items='items', plugins=[]):
141+
def _get_all(self, items='items', plugins=None):
142+
if plugins is None:
143+
plugins = []
143144
result = ''
144145
for plugin in plugins:
145146
if plugin.AgentPluginType == self.plg_type or self.plg_type == 'all':
@@ -157,12 +158,16 @@ def _macro(self, xml_key='macro'):
157158
result += '<{1}>{0}</{1}>'.format(self._format_args(self.macro_defaults, value), xml_key)
158159
return result
159160

160-
def item(self, args={}, xml_key='item'):
161+
def item(self, args=None, xml_key='item'):
162+
if args is None:
163+
args = {}
161164
return '<{2}>{0}{1}</{2}>'.format(
162165
self._format_args(self.item_defaults, args),
163166
self._application(), xml_key)
164167

165-
def trigger(self, args={}, xml_key='trigger', defaults=None):
168+
def trigger(self, args=None, xml_key='trigger', defaults=None):
169+
if args is None:
170+
args = {}
166171
if defaults is None:
167172
defaults = self.trigger_defaults
168173
try:
@@ -175,7 +180,9 @@ def trigger(self, args={}, xml_key='trigger', defaults=None):
175180
self._format_args(defaults, args),
176181
xml_key)
177182

178-
def graph(self, args={}, xml_key='graph'):
183+
def graph(self, args=None, xml_key='graph'):
184+
if args is None:
185+
args = {}
179186
try:
180187
items = args['items']
181188
except KeyError:
@@ -201,7 +208,9 @@ def graph(self, args={}, xml_key='graph'):
201208
graph_items, xml_key)
202209

203210
# condition for template creation for zabbix version 4.4
204-
def condition(self, args={}, xml_key='condition'):
211+
def condition(self, args=None, xml_key='condition'):
212+
if args is None:
213+
args = {}
205214
try:
206215
conditions = args['condition']
207216
except KeyError:
@@ -216,8 +225,18 @@ def condition(self, args={}, xml_key='condition'):
216225

217226
return res
218227

219-
def discovery_rule(self, rule={},conditions=[], items=[], triggers=[], graphs=[]):
228+
def discovery_rule(self, rule=None, conditions=None, items=None, triggers=None, graphs=None):
220229

230+
if rule is None:
231+
rule = {}
232+
if conditions is None:
233+
conditions = []
234+
if items is None:
235+
items = []
236+
if triggers is None:
237+
triggers = []
238+
if graphs is None:
239+
graphs = []
221240
result_items = '<item_prototypes>'
222241
for item in items:
223242
result_items += self.item(item, xml_key='item_prototype')
@@ -237,7 +256,6 @@ def discovery_rule(self, rule={},conditions=[], items=[], triggers=[], graphs=[]
237256
result_graphs += '</graph_prototypes>'
238257

239258
if len(conditions) > 0:
240-
241259
result_conditions = '<filter>'
242260
for condition in conditions:
243261
result_conditions += self.condition(

0 commit comments

Comments
 (0)