Forked mumble-django project from https://bitbucket.org/Svedrin/mumble-django
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

176 lines
5.3 KiB

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # kate: space-indent on; indent-width 4; replace-tabs on;
  4. # This is PerlDoc documentation (POD) to be viewed with munindoc (or perldoc).
  5. """
  6. =head1 NAME
  7. mumble-django - graph Mumble user counts for server instances
  8. =head1 DESCRIPTION
  9. This plugin monitors the number of users connected to the Mumble server
  10. instances configured in Mumble-Django. It automatically adapts to servers
  11. being offline, new servers being added and servers being deleted, and
  12. therefore should not require too much maintenance.
  13. =head1 APPLICABLE SYSTEMS
  14. Mumble servers that have Mumble-Django installed.
  15. =head1 SYNOPSIS
  16. B<munin-run mumble-django> [config|autoconf]
  17. =head1 OPTIONS
  18. =over 4
  19. =item B<config> - emit graph configuration options for Munin to use.
  20. =item B<autoconf> - check if the plugin can be safely installed.
  21. =back
  22. If neither are given, the plugin will emit the current user counts for each
  23. known server instance.
  24. =head1 CONFIGURATION
  25. The plugin is configured in the I<settings.py> file along with your
  26. Mumble-Django installation. The plugin allows self-testing to see if it has
  27. everything it needs in order to run; just run it with the parameter "autoconf"
  28. and the plugin will tell you if it can be safely installed.
  29. The following variables are relevant in I<settings.py>:
  30. =over 4
  31. =item B<MUNIN_WARNING> - the "warning" level factor, defaults to 0.80.
  32. =item B<MUNIN_CRITICAL> - the "critical" level factor, defaults to 0.95.
  33. =item B<MUNIN_TITLE> - the title of the graph, defaults to "Mumble Users".
  34. =item B<MUNIN_CATEGORY> - the category the graphs appear in, defaults to "network".
  35. =back
  36. All of these settings can be overridden in I<settings.py> simply by defining
  37. them there. If a variable is omitted, the defaults are used as specified.
  38. The WARNING and CRITICAL level factors are multiplied with the server's slot
  39. count to form the real thresholds.
  40. =head1 MAGIC MARKERS
  41. #%# family=auto
  42. #%# capabilities=autoconf
  43. =head1 BUGS
  44. Bugs are tracked along with Mumble-Django bugs in the issue tracker:
  45. http://bitbucket.org/Svedrin/mumble-django/issue/
  46. If you find a bug, please report it.
  47. =head1 AUTHOR
  48. Copyright (C) 2009 - 2010, Michael "Svedrin" Ziegler
  49. =head1 LICENSE
  50. Mumble-Django is free software; you can redistribute it and/or modify
  51. it under the terms of the GNU General Public License as published by
  52. the Free Software Foundation; either version 2 of the License, or
  53. (at your option) any later version.
  54. This package is distributed in the hope that it will be useful,
  55. but WITHOUT ANY WARRANTY; without even the implied warranty of
  56. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  57. GNU General Public License for more details.
  58. =cut
  59. """
  60. # Set this to the same path you used in settings.py, or None for auto-detection.
  61. MUMBLE_DJANGO_ROOT = None
  62. ### DO NOT CHANGE ANYTHING BELOW THIS LINE ###
  63. import os, sys
  64. from os.path import join, dirname, abspath, exists
  65. # Path auto-detection
  66. if not MUMBLE_DJANGO_ROOT or not exists( MUMBLE_DJANGO_ROOT ):
  67. MUMBLE_DJANGO_ROOT = dirname(abspath(__file__))
  68. # environment variables
  69. sys.path.append( MUMBLE_DJANGO_ROOT )
  70. sys.path.append( join( MUMBLE_DJANGO_ROOT, 'pyweb' ) )
  71. os.environ['DJANGO_SETTINGS_MODULE'] = 'pyweb.settings'
  72. # If you get an error about Python not being able to write to the Python
  73. # egg cache, the egg cache path might be set awkwardly. This should not
  74. # happen under normal circumstances, but every now and then, it does.
  75. # Uncomment this line to point the egg cache to /tmp.
  76. #os.environ['PYTHON_EGG_CACHE'] = '/tmp/pyeggs'
  77. import locale
  78. from django.conf import settings
  79. from mumble.models import MumbleServer, Mumble
  80. warn = getattr( settings, "MUNIN_WARNING", 0.80 )
  81. crit = getattr( settings, "MUNIN_CRITICAL", 0.95 )
  82. title = getattr( settings, "MUNIN_TITLE", "Mumble Users" )
  83. categ = getattr( settings, "MUNIN_CATEGORY", "network" )
  84. def get_running_instances():
  85. for server in MumbleServer.objects.all():
  86. if not server.online:
  87. continue
  88. runinst = server.ctl.getBootedServers()
  89. for inst in server.mumble_set.order_by("srvid").filter( srvid__in=runinst ):
  90. yield inst
  91. if sys.argv[-1] == 'config':
  92. prefenc = locale.getpreferredencoding()
  93. print "graph_vlabel Users"
  94. print "graph_args --base 1000"
  95. print "graph_title", title
  96. print "graph_category", categ
  97. for mumble in get_running_instances():
  98. print "srv%d.label %s" % ( mumble.id, mumble.name.replace('#', '').encode(prefenc, "replace") )
  99. if mumble.connecturl:
  100. print "srv%d.info %s" % ( mumble.id, mumble.connecturl )
  101. if mumble.users:
  102. print "srv%d.warning %d" % ( mumble.id, int( mumble.users * warn ) )
  103. print "srv%d.critical %d" % ( mumble.id, int( mumble.users * crit ) )
  104. elif sys.argv[-1] == 'autoconf':
  105. if Mumble.objects.count() == 0:
  106. print "no (no servers configured)"
  107. else:
  108. # check if connecting works
  109. try:
  110. for mumble in get_running_instances():
  111. mumble.ctl
  112. except Exception, instance:
  113. print "no (can't connect to server %s: %s)" % ( mumble.name, instance )
  114. else:
  115. print "yes"
  116. else:
  117. for mumble in get_running_instances():
  118. print "srv%d.value %d" % ( mumble.id, len( mumble.ctl.getPlayers( mumble.srvid ) ) )