summaryrefslogtreecommitdiffstats
path: root/hostinfo
diff options
context:
space:
mode:
Diffstat (limited to 'hostinfo')
-rw-r--r--hostinfo/printer.py26
-rw-r--r--hostinfo/utils.py20
2 files changed, 23 insertions, 23 deletions
diff --git a/hostinfo/printer.py b/hostinfo/printer.py
index 5edc76f..4302e04 100644
--- a/hostinfo/printer.py
+++ b/hostinfo/printer.py
@@ -2,6 +2,7 @@
from __future__ import absolute_import
from hostinfo import prefix
+from hostinfo import utils
def _get_full_key(prev_key, key):
if prev_key == '':
@@ -24,15 +25,6 @@ def _space(filter_key, full_key, printer, force=False):
if filter_key is None and full_key == '':
printer.space(force)
-def _group_by(value, group, display_check, print_value):
- groups = {}
- for elem in value:
- key = group(elem)
- if key is not None and display_check(key):
- groups.setdefault(key, []).append(print_value(elem))
-
- return groups
-
class Printer:
labels = {
@@ -55,25 +47,13 @@ class Printer:
prefix.Printer.flags = flags
def cb_print_addresses(self, value, full_key, filter_key):
- def _group_ip(address):
- if 'vserver' not in address:
- return address['interface']
- else:
- return None
-
def _print_ip(address):
return '%s/%s' % (address['address'], address['netmask'])
display_check = self._is_group_displayed(full_key, filter_key)
- return _group_by(value, _group_ip, display_check, _print_ip)
+ return utils.group_by(value, 'interface', None, display_check, _print_ip)
def cb_print_ports(self, value, full_key, filter_key):
- def _group_port(port):
- if 'process' in port:
- return port['process']
- else:
- return 'UNKNOWN'
-
def _print_port(port):
if port['proto'] in ['tcp6', 'udp6']:
return '(%s) [%s]:%s' % (port['proto'].replace('6', ''),
@@ -81,7 +61,7 @@ class Printer:
return '(%s) %s:%s' % (port['proto'], port['ip'], port['port'])
display_check = self._is_group_displayed(full_key, filter_key)
- return (_group_by(value, _group_port, display_check, _print_port),
+ return (utils.group_by(value, 'process', 'UNKNOWN', display_check, _print_port),
['sshd', 'nrpe', 'munin-node'])
def cb_print_vserver(self, value, full_key, filter_key):
diff --git a/hostinfo/utils.py b/hostinfo/utils.py
new file mode 100644
index 0000000..cfd8de3
--- /dev/null
+++ b/hostinfo/utils.py
@@ -0,0 +1,20 @@
+# -*- coding: utf-8 -*-
+
+def group_by(value, group, default=None, display_check=None, print_value=None):
+ groups = {}
+ for elem in value:
+ key = None
+ if group in elem:
+ key = elem[group]
+ else:
+ key = default
+
+ if display_check is None or display_check(key):
+ output = elem
+ if print_value is not None:
+ output = print_value(elem)
+
+ groups.setdefault(key, []).append(output)
+
+ return groups
+