Skip to content

Commit

Permalink
Enable Zabbix low level discovery of WSGI process groups
Browse files Browse the repository at this point in the history
  • Loading branch information
xolox committed May 27, 2016
1 parent 4ca6758 commit b62968e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
2 changes: 1 addition & 1 deletion apache_manager/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from apache_manager.compat import HTTPError, urlopen

# Semi-standard module versioning.
__version__ = '0.4'
__version__ = '0.5'

# Hide internal identifiers from API documentation.
__all__ = (
Expand Down
45 changes: 32 additions & 13 deletions apache_manager/cli.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Monitor and control Apache web server workers from Python.
#
# Author: Peter Odding <peter@peterodding.com>
# Last Change: June 29, 2015
# Last Change: May 27, 2016
# URL: https://apache-manager.readthedocs.org

"""
Expand Down Expand Up @@ -42,6 +42,14 @@
Change the pathname of the file where the Apache manager stores monitoring
metrics after every run. Defaults to `/tmp/apache-manager.txt'.
-z, --zabbix-discovery
Generate a JSON fragment that's compatible with the low-level discovery
support in the Zabbix monitoring system. With the right template in place
this enables the Zabbix server to discover the names of the WSGI process
groups that are active on any given server. This makes it possible to
collect and analyze the memory usage of specific WSGI process groups.
-n, --dry-run, --simulate
Don't actually kill any Apache workers.
Expand All @@ -61,6 +69,7 @@

# Standard library modules.
import getopt
import json
import logging
import sys

Expand All @@ -81,7 +90,7 @@
)

# Modules included in our package.
from apache_manager import ApacheManager
from apache_manager import ApacheManager, NATIVE_WORKERS_LABEL
from apache_manager.interactive import watch_metrics

# Initialize a logger for this program.
Expand All @@ -99,12 +108,13 @@ def main():
max_memory_idle = None
max_ss = None
watch = False
zabbix_discovery = False
# Parse the command line options.
try:
options, arguments = getopt.getopt(sys.argv[1:], 'wa:i:t:f:nvqh', [
options, arguments = getopt.getopt(sys.argv[1:], 'wa:i:t:f:znvqh', [
'watch', 'max-memory-active=', 'max-memory-idle=', 'max-ss=',
'max-time=', 'data-file=', 'dry-run', 'simulate', 'verbose',
'quiet', 'help'
'max-time=', 'data-file=', 'zabbix-discovery', 'dry-run',
'simulate', 'verbose', 'quiet', 'help',
])
for option, value in options:
if option in ('-w', '--watch'):
Expand All @@ -117,6 +127,8 @@ def main():
max_ss = parse_timespan(value)
elif option in ('-f', '--data-file'):
data_file = value
elif option in ('-z', '--zabbix-discovery'):
zabbix_discovery = True
elif option in ('-n', '--dry-run', '--simulate'):
logger.info("Performing a dry run ..")
dry_run = True
Expand All @@ -140,14 +152,15 @@ def main():
timeout=max_ss,
dry_run=dry_run,
)
if connected_to_terminal(sys.stdout):
if watch:
watch_metrics(manager)
elif data_file != '-':
for line in report_metrics(manager):
if line_is_heading(line):
line = ansi_wrap(line, color=HIGHLIGHT_COLOR)
print(line)
if watch and connected_to_terminal(sys.stdout):
watch_metrics(manager)
elif zabbix_discovery:
report_zabbix_discovery(manager)
elif data_file != '-':
for line in report_metrics(manager):
if line_is_heading(line):
line = ansi_wrap(line, color=HIGHLIGHT_COLOR)
print(line)
finally:
if (not watch) and (data_file == '-' or not dry_run):
manager.save_metrics(data_file)
Expand Down Expand Up @@ -183,6 +196,12 @@ def report_memory_usage(lines, label, memory_usage):
lines.append(" - Maximum: %s" % format_size(memory_usage.max))


def report_zabbix_discovery(manager):
"""Enable Zabbix low-level discovery of WSGI application groups."""
worker_groups = [NATIVE_WORKERS_LABEL] + sorted(manager.wsgi_process_groups.keys())
print(json.dumps({'data': [{'{#NAME}': name} for name in worker_groups]}))


def line_is_heading(line):
"""Check whether a line of output generated by :func:`report_metrics()` should be highlighted as a heading."""
return line.endswith(':')

0 comments on commit b62968e

Please sign in to comment.