Skip to content

Commit

Permalink
vhosts handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Lacrymology committed Jan 5, 2015
1 parent ef936c7 commit 1ca67ef
Showing 1 changed file with 34 additions and 15 deletions.
49 changes: 34 additions & 15 deletions src/collectors/mail/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@
#### Example configuration
```
spool_path = /var/mail/vhosts/example.com
mailbox_prefix = example_com
[MailCollector]
spool_path = /var/mail/vhosts
[[prefixes]]
[[[example_com]]]
spool_path = example.com
[[[example_net]]]
spool_path = example.net
```
This will publish metrics with the following pattern:
This will analyze, e.g: /var/mail/vhosts/example.com and publish metrics
with the following pattern:
<hostname>.mail.example_com.<user>.count
"""
Expand All @@ -29,8 +35,6 @@ def get_default_config_help(self):
config_help.update({
'spool_path': ('Path to mail spool that contains files to be '
'analyzed.'),
'mailbox_prefix': ('Prefix to add to the metric name. Use in case '
'you are monitoring more than one domain.'),
'path': 'mail',
})
return config_help
Expand All @@ -42,27 +46,42 @@ def get_default_config(self):
config = super(MailCollector, self).get_default_config()
config.update({
'spool_path': '/var/mail/',
'mailbox_prefix': '',
'prefixes': {}
})
return config

def collect(self):
metrics = {}
metrics['total'] = 0

for uname in os.listdir(self.config['spool_path']):
fpath = os.path.join(self.config['spool_path'], uname)
if os.path.isfile(fpath):
mbox = mailbox.mbox(fpath)
metrics[uname] = len(mbox)
metrics['total'] += metrics[uname]
base_path = self.config['spool_path']
paths = {
'': base_path,
}
for prefix, conf in self.config['prefixes'].iteritems():
# if conf['spool_path'] is an absolute path, os.path.join will
# remove base_path automatically
path = os.path.join(base_path, conf['spool_path'])
paths[prefix] = path

for prefix, path in paths.iteritems():
metricparts = []
if prefix:
metricparts.append(prefix)

for uname in os.listdir(path):
metricparts.append(uname)
fpath = os.path.join(self.config['spool_path'], uname)
if os.path.isfile(fpath):
mbox = mailbox.mbox(fpath)
mname = ".".join(metricparts + ['count'])
metrics[mname] = len(mbox)
metrics['total'] += metrics[mname]

self.publish_metrics(metrics)

return True

def publish_metrics(self, metrics):
prefix = self.config['mailbox_prefix']
metric_prefix = ("{0}." if prefix else '').format(prefix)
for name, value in metrics.iteritems():
self.publish('{0}{1}.count'.format(metric_prefix, name), value)
self.publish(name, value)

0 comments on commit 1ca67ef

Please sign in to comment.